import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './wallet.js'
import { SendBlock } from './block.js'
+/**
+* Hashes data using BLAKE2b.
+*
+* @param {string|string[]} data - Hexadecimal-formatted data to hash
+* @returns {Promise<Uint8Array>} Array of bytes representing hashed input data
+*/
+export async function blake2b (data: string | string[]): Promise<Uint8Array> {
+ if (!Array.isArray(data)) data = [data]
+ const ctx = blake2bInit(32)
+ data.forEach(str => blake2bUpdate(ctx, hex.toBytes(str)))
+ return blake2bFinal(ctx)
+}
+
/**
* Converts a decimal amount from one unit divider to another.
*
return `${i}${f ? '.' : ''}${f}`
}
-/**
-* Hashes data using BLAKE2b.
-*
-* @param {string|string[]} data - Hexadecimal-formatted data to hash
-* @returns {Promise<Uint8Array>} Array of bytes representing hashed input data
-*/
-export async function blake2b (data: string | string[]): Promise<Uint8Array> {
- if (!Array.isArray(data)) data = [data]
- const ctx = blake2bInit(32)
- data.forEach(str => blake2bUpdate(ctx, hex.toBytes(str)))
- return blake2bFinal(ctx)
-}
-
/**
* Converts one or more strings to hexadecimal and hashes them with BLAKE2b.
*
return bytes.toHex(hash)
}
+
+/**
+* Checks the endianness of the current machine.
+*
+* @returns {Promise<boolean>} True if little-endian, else false
+*/
+export async function littleEndian () {
+ const buffer = new ArrayBuffer(2)
+ new DataView(buffer).setUint16(0, 256, true)
+ return new Uint16Array(buffer)[0] === 256
+}
+
/**
* Signs arbitrary strings with a private key using the Ed25519 signature scheme.
*
hex.toBytes(signature))
}
-export default { blake2b, convert, hash, sign, sweep, verify }
+export default { blake2b, convert, hash, littleEndian, sign, sweep, verify }
* @returns {Promise<Account>}\r
*/\r
async ckd (index: number): Promise<Account> {\r
- const hash = await Tools.blake2b([this.seed, dec.toHex(index, 4)])\r
+ const indexBytes = dec.toBytes(index, 4)\r
+ if (await Tools.littleEndian()) {\r
+ indexBytes.reverse()\r
+ }\r
+ const hash = await Tools.blake2b([this.seed, bytes.toHex(indexBytes)])\r
const key = bytes.toHex(hash)\r
return await Account.fromPrivateKey(key, index)\r
}\r