this.#validateKey(privateKey)\r
let publicKey: string\r
try {\r
- const result = await this.#poolNanoNaCl.assign({ method: 'convert' }, privateKey)\r
+ const result = await this.#poolNanoNaCl.assign({ method: 'convert' }, privateKey.buffer)\r
publicKey = result.publicKey[0]\r
} catch (err) {\r
throw new Error(`Failed to derive public key from private key`, { cause: err })\r
* @param {(string|Uint8Array)} password Used to lock the account\r
* @returns True if successfully locked\r
*/\r
- async lock (password: string | Uint8Array): Promise<boolean> {\r
+ async lock (password: string | Uint8Array<ArrayBuffer>): Promise<boolean> {\r
if (typeof password === 'string') password = utf8.toBytes(password)\r
if (password == null || !(password instanceof Uint8Array)) {\r
throw new Error('Failed to lock account')\r
name: this.#pub,\r
id: this.#pub\r
}\r
- const response = await Account.#poolSafe.assign(headers, this.#prv, password)\r
+ const response = await Account.#poolSafe.assign(headers, this.#prv.buffer, password.buffer)\r
const success = response?.result[0]\r
if (!success) {\r
throw null\r
* @param {(string|Uint8Array)} password Used previously to lock the account\r
* @returns True if successfully unlocked\r
*/\r
- async unlock (password: string | Uint8Array): Promise<boolean> {\r
+ async unlock (password: string | Uint8Array<ArrayBuffer>): Promise<boolean> {\r
if (typeof password === 'string') password = utf8.toBytes(password)\r
if (password == null || !(password instanceof Uint8Array)) {\r
throw new Error('Failed to unlock account')\r
method: 'get',\r
name: this.#pub\r
}\r
- const response = await Account.#poolSafe.assign(headers, password)\r
+ const response = await Account.#poolSafe.assign(headers, password.buffer)\r
const { id, privateKey } = response?.result[0]\r
if (id == null || id !== this.#pub) {\r
throw null\r
*/\r
export class Bip39Mnemonic {\r
static #isInternal: boolean = false\r
- #bip44Seed: Uint8Array | null = null\r
- #blake2bSeed: Uint8Array | null = null\r
+ #bip44Seed: Uint8Array<ArrayBuffer> | null = null\r
+ #blake2bSeed: Uint8Array<ArrayBuffer> | null = null\r
#phrase: string = ''\r
get phrase (): string { return this.#phrase.normalize('NFKD') }\r
\r
: this.#bip44Seed\r
}\r
\r
- async toBlake2bSeed (): Promise<Uint8Array>\r
+ async toBlake2bSeed (): Promise<Uint8Array<ArrayBuffer>>\r
/**\r
* Converts the mnemonic phrase to a BLAKE2b seed.\r
*\r
* @returns {string} Hexadecimal seed\r
*/\r
async toBlake2bSeed (format: 'hex'): Promise<string>\r
- async toBlake2bSeed (format?: 'hex'): Promise<string | Uint8Array> {\r
+ async toBlake2bSeed (format?: 'hex'): Promise<string | Uint8Array<ArrayBuffer>> {\r
if (this.#blake2bSeed == null) {\r
const wordArray = this.phrase.split(' ')\r
const bits = wordArray.map((w: string) => {\r
method: 'detached',
msg: this.hash
}
- const result = await Block.#poolNanoNaCl.assign(headers, hex.toBytes(account.privateKey))
+ const result = await Block.#poolNanoNaCl.assign(headers, hex.toBytes(account.privateKey).buffer)
this.signature = result.signature[0]
} catch (err) {
throw new Error(`Failed to sign block`, { cause: err })
* @param {object} obj - Object to convert\r
* @returns {Uint8Array} Byte array representation of the input object\r
*/\r
- static toBytes (obj: { [key: number]: number }): Uint8Array {\r
+ static toBytes (obj: { [key: number]: number }): Uint8Array<ArrayBuffer> {\r
const values = Object.keys(obj)\r
.map(key => +key)\r
.sort((a, b) => a - b)\r
* @param {string} utf8 - String to convert\r
* @returns {Uint8Array} Byte array representation of the input string\r
*/\r
- static toBytes (utf8: string): Uint8Array {\r
- return new TextEncoder().encode(utf8)\r
+ static toBytes (utf8: string): Uint8Array<ArrayBuffer> {\r
+ return new TextEncoder().encode(utf8) as Uint8Array<ArrayBuffer>\r
}\r
\r
/**\r
// SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
// SPDX-License-Identifier: GPL-3.0-or-later
+import { isTypedArray } from "util/types"
+
export type Header = {
[key: string]: number | string
}
reject: (value: any) => void
resolve: (value: any) => void
headers: Header
- data: (ArrayBuffer | Uint8Array)[]
+ data: ArrayBuffer[]
results: any[]
}
return n
}
- async assign (headers: Header, ...data: (ArrayBuffer | Uint8Array)[]): Promise<any> {
+ async assign (headers: Header, ...data: ArrayBuffer[]): Promise<any> {
return new Promise((resolve, reject) => {
const job: Job = {
id: performance.now(),
import { KeyPair, Wallet } from './wallet'\r
import { Bip39Mnemonic } from '#src/lib/bip39-mnemonic.js'\r
import { SEED_LENGTH_BIP44 } from '#src/lib/constants.js'\r
-import { hex } from '#src/lib/convert.js'\r
+import { hex, utf8 } from '#src/lib/convert.js'\r
import { Entropy } from '#src/lib/entropy.js'\r
import { Pool } from '#src/lib/pool.js'\r
import { Bip44CkdWorker } from '#workers'\r
* @param {string} [salt=''] - Used when generating the final seed\r
* @returns {Bip44Wallet} A newly instantiated Bip44Wallet\r
*/\r
- static async fromEntropy (key: Uint8Array, entropy: string, salt?: string): Promise<Bip44Wallet>\r
- static async fromEntropy (passkey: string | Uint8Array, entropy: string, salt: string = ''): Promise<Bip44Wallet> {\r
+ static async fromEntropy (key: Uint8Array<ArrayBuffer>, entropy: string, salt?: string): Promise<Bip44Wallet>\r
+ static async fromEntropy (passkey: string | Uint8Array<ArrayBuffer>, entropy: string, salt: string = ''): Promise<Bip44Wallet> {\r
+ if (typeof passkey === 'string') passkey = utf8.toBytes(passkey)\r
let wallet: Bip44Wallet\r
try {\r
const id = await Entropy.create()\r
* @param {string} [salt=''] - Used when generating the final seed\r
* @returns {Bip44Wallet} A newly instantiated Bip44Wallet\r
*/\r
- static async fromMnemonic (key: Uint8Array, mnemonic: string, salt?: string): Promise<Bip44Wallet>\r
- static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string, salt: string = ''): Promise<Bip44Wallet> {\r
+ static async fromMnemonic (key: Uint8Array<ArrayBuffer>, mnemonic: string, salt?: string): Promise<Bip44Wallet>\r
+ static async fromMnemonic (passkey: string | Uint8Array<ArrayBuffer>, mnemonic: string, salt: string = ''): Promise<Bip44Wallet> {\r
+ if (typeof passkey === 'string') passkey = utf8.toBytes(passkey)\r
let wallet: Bip44Wallet\r
try {\r
const id = await Entropy.create()\r
* @param {string} seed - Hexadecimal 128-character string used to derive private-public key pairs\r
* @returns {Bip44Wallet} A newly instantiated Bip44Wallet\r
*/\r
- static async fromSeed (key: Uint8Array, seed: string): Promise<Bip44Wallet>\r
- static async fromSeed (passkey: string | Uint8Array, seed: string): Promise<Bip44Wallet> {\r
+ static async fromSeed (key: Uint8Array<ArrayBuffer>, seed: string): Promise<Bip44Wallet>\r
+ static async fromSeed (passkey: string | Uint8Array<ArrayBuffer>, seed: string): Promise<Bip44Wallet> {\r
+ if (typeof passkey === 'string') passkey = utf8.toBytes(passkey)\r
if (seed.length !== SEED_LENGTH_BIP44) {\r
throw new Error(`Expected a ${SEED_LENGTH_BIP44}-character seed, but received ${seed.length}-character string.`)\r
}\r
async ckd (indexes: number[]): Promise<KeyPair[]> {\r
const data: any = []\r
indexes.forEach(i => data.push({ index: i }))\r
- const privateKeys: KeyPair[] = await Bip44Wallet.#poolBip44Ckd.assign(data, this.seed)\r
+ const privateKeys: KeyPair[] = await Bip44Wallet.#poolBip44Ckd.assign(data, this.seed.buffer)\r
for (let i = 0; i < privateKeys.length; i++) {\r
if (privateKeys[i].privateKey == null) {\r
throw new Error('Failed to derive private keys')\r
import { Bip39Mnemonic } from '#src/lib/bip39-mnemonic.js'\r
import { Blake2b } from '#src/lib/blake2b.js'\r
import { SEED_LENGTH_BLAKE2B } from '#src/lib/constants.js'\r
-import { bytes, hex } from '#src/lib/convert.js'\r
+import { bytes, hex, utf8 } from '#src/lib/convert.js'\r
import { Entropy } from '#src/lib/entropy.js'\r
\r
/**\r
export class Blake2bWallet extends Wallet {\r
static #isInternal: boolean = false\r
\r
- constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) {\r
+ constructor (id: Entropy, seed?: Uint8Array<ArrayBuffer>, mnemonic?: Bip39Mnemonic) {\r
if (!Blake2bWallet.#isInternal) {\r
throw new Error(`Blake2bWallet cannot be instantiated directly. Use 'await Blake2bWallet.create()' instead.`)\r
}\r
* @param {string} seed - Hexadecimal 64-character string used to derive private-public key pairs\r
* @returns {Blake2bWallet} A newly instantiated Blake2bWallet\r
*/\r
- static async fromSeed (key: Uint8Array, seed: string): Promise<Blake2bWallet>\r
- static async fromSeed (passkey: string | Uint8Array, seed: string): Promise<Blake2bWallet> {\r
+ static async fromSeed (key: Uint8Array<ArrayBuffer>, seed: string): Promise<Blake2bWallet>\r
+ static async fromSeed (passkey: string | Uint8Array<ArrayBuffer>, seed: string): Promise<Blake2bWallet> {\r
+ if (typeof passkey === 'string') passkey = utf8.toBytes(passkey)\r
if (seed.length !== SEED_LENGTH_BLAKE2B) {\r
throw new Error(`Expected a ${SEED_LENGTH_BLAKE2B}-character seed, but received ${seed.length}-character string.`)\r
}\r
* @param {string} mnemonic - Used when generating the final seed\r
* @returns {Blake2bWallet} A newly instantiated Blake2bWallet\r
*/\r
- static async fromMnemonic (key: Uint8Array, mnemonic: string): Promise<Blake2bWallet>\r
- static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string): Promise<Blake2bWallet> {\r
+ static async fromMnemonic (key: Uint8Array<ArrayBuffer>, mnemonic: string): Promise<Blake2bWallet>\r
+ static async fromMnemonic (passkey: string | Uint8Array<ArrayBuffer>, mnemonic: string): Promise<Blake2bWallet> {\r
+ if (typeof passkey === 'string') passkey = utf8.toBytes(passkey)\r
let wallet: Blake2bWallet\r
try {\r
const id = await Entropy.create()\r
#id: Entropy\r
#locked: boolean = true\r
#m: Bip39Mnemonic | null\r
- #s: Uint8Array\r
+ #s: Uint8Array<ArrayBuffer>\r
\r
get id () { return this.#id.hex }\r
get isLocked () { return this.#locked }\r
get mnemonic () { return this.#m instanceof Bip39Mnemonic ? this.#m.phrase : '' }\r
get seed () { return this.#s }\r
\r
- constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) {\r
+ constructor (id: Entropy, seed?: Uint8Array<ArrayBuffer>, mnemonic?: Bip39Mnemonic) {\r
if (this.constructor === Wallet) {\r
throw new Error('Wallet is an abstract class and cannot be instantiated directly.')\r
}\r
* @param {(string|Uint8Array)} password Used to lock the wallet\r
* @returns True if successfully locked\r
*/\r
- async lock (password: string | Uint8Array): Promise<boolean> {\r
+ async lock (password: string | Uint8Array<ArrayBuffer>): Promise<boolean> {\r
if (typeof password === 'string') {\r
password = utf8.toBytes(password)\r
}\r
name: this.id,\r
id: this.id,\r
}\r
- const response = await Wallet.#poolSafe.assign(headers, password, utf8.toBytes(this.#m?.phrase ?? ''), this.#s)\r
+ const response = await Wallet.#poolSafe.assign(headers, password.buffer, utf8.toBytes(this.#m?.phrase ?? '').buffer, this.#s.buffer)\r
const success = response?.result[0]\r
if (!success) {\r
throw null\r
* @param {(string|Uint8Array)} password Used previously to lock the wallet\r
* @returns True if successfully unlocked\r
*/\r
- async unlock (password: string | Uint8Array): Promise<boolean> {\r
+ async unlock (password: string | Uint8Array<ArrayBuffer>): Promise<boolean> {\r
if (typeof password === 'string') {\r
password = utf8.toBytes(password)\r
}\r
method: 'get',\r
name: this.id\r
}\r
- const response = await Wallet.#poolSafe.assign(headers,\r
- password)\r
+ const response = await Wallet.#poolSafe.assign(headers, password.buffer)\r
let { id, mnemonic, seed } = response?.result[0]\r
if (id == null || id !== this.id) {\r
throw null\r