From: Chris Duncan Date: Sat, 12 Jul 2025 07:58:05 +0000 (-0700) Subject: Fix buffer typings. X-Git-Tag: v0.10.5~57^2~23 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=200c21e9c7525c21c44688430968b964d00f508c;p=libnemo.git Fix buffer typings. --- diff --git a/src/lib/account.ts b/src/lib/account.ts index fa6a429..d84ac69 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -136,7 +136,7 @@ export class Account { this.#validateKey(privateKey) let publicKey: string try { - const result = await this.#poolNanoNaCl.assign({ method: 'convert' }, privateKey) + const result = await this.#poolNanoNaCl.assign({ method: 'convert' }, privateKey.buffer) publicKey = result.publicKey[0] } catch (err) { throw new Error(`Failed to derive public key from private key`, { cause: err }) @@ -152,7 +152,7 @@ export class Account { * @param {(string|Uint8Array)} password Used to lock the account * @returns True if successfully locked */ - async lock (password: string | Uint8Array): Promise { + async lock (password: string | Uint8Array): Promise { if (typeof password === 'string') password = utf8.toBytes(password) if (password == null || !(password instanceof Uint8Array)) { throw new Error('Failed to lock account') @@ -163,7 +163,7 @@ export class Account { name: this.#pub, id: this.#pub } - const response = await Account.#poolSafe.assign(headers, this.#prv, password) + const response = await Account.#poolSafe.assign(headers, this.#prv.buffer, password.buffer) const success = response?.result[0] if (!success) { throw null @@ -217,7 +217,7 @@ export class Account { * @param {(string|Uint8Array)} password Used previously to lock the account * @returns True if successfully unlocked */ - async unlock (password: string | Uint8Array): Promise { + async unlock (password: string | Uint8Array): Promise { if (typeof password === 'string') password = utf8.toBytes(password) if (password == null || !(password instanceof Uint8Array)) { throw new Error('Failed to unlock account') @@ -227,7 +227,7 @@ export class Account { method: 'get', name: this.#pub } - const response = await Account.#poolSafe.assign(headers, password) + const response = await Account.#poolSafe.assign(headers, password.buffer) const { id, privateKey } = response?.result[0] if (id == null || id !== this.#pub) { throw null diff --git a/src/lib/bip39-mnemonic.ts b/src/lib/bip39-mnemonic.ts index d51c7a8..8264598 100644 --- a/src/lib/bip39-mnemonic.ts +++ b/src/lib/bip39-mnemonic.ts @@ -13,8 +13,8 @@ const { subtle } = globalThis.crypto */ export class Bip39Mnemonic { static #isInternal: boolean = false - #bip44Seed: Uint8Array | null = null - #blake2bSeed: Uint8Array | null = null + #bip44Seed: Uint8Array | null = null + #blake2bSeed: Uint8Array | null = null #phrase: string = '' get phrase (): string { return this.#phrase.normalize('NFKD') } @@ -169,14 +169,14 @@ export class Bip39Mnemonic { : this.#bip44Seed } - async toBlake2bSeed (): Promise + async toBlake2bSeed (): Promise> /** * Converts the mnemonic phrase to a BLAKE2b seed. * * @returns {string} Hexadecimal seed */ async toBlake2bSeed (format: 'hex'): Promise - async toBlake2bSeed (format?: 'hex'): Promise { + async toBlake2bSeed (format?: 'hex'): Promise> { if (this.#blake2bSeed == null) { const wordArray = this.phrase.split(' ') const bits = wordArray.map((w: string) => { diff --git a/src/lib/block.ts b/src/lib/block.ts index 7b7e4d3..dec40b0 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -152,7 +152,7 @@ abstract class Block { 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 }) diff --git a/src/lib/convert.ts b/src/lib/convert.ts index abea692..f75264e 100644 --- a/src/lib/convert.ts +++ b/src/lib/convert.ts @@ -297,7 +297,7 @@ export class obj { * @param {object} obj - Object to convert * @returns {Uint8Array} Byte array representation of the input object */ - static toBytes (obj: { [key: number]: number }): Uint8Array { + static toBytes (obj: { [key: number]: number }): Uint8Array { const values = Object.keys(obj) .map(key => +key) .sort((a, b) => a - b) @@ -313,8 +313,8 @@ export class utf8 { * @param {string} utf8 - String to convert * @returns {Uint8Array} Byte array representation of the input string */ - static toBytes (utf8: string): Uint8Array { - return new TextEncoder().encode(utf8) + static toBytes (utf8: string): Uint8Array { + return new TextEncoder().encode(utf8) as Uint8Array } /** diff --git a/src/lib/pool.ts b/src/lib/pool.ts index d72fb64..d714755 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: 2025 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later +import { isTypedArray } from "util/types" + export type Header = { [key: string]: number | string } @@ -10,7 +12,7 @@ type Job = { reject: (value: any) => void resolve: (value: any) => void headers: Header - data: (ArrayBuffer | Uint8Array)[] + data: ArrayBuffer[] results: any[] } @@ -43,7 +45,7 @@ export class Pool { return n } - async assign (headers: Header, ...data: (ArrayBuffer | Uint8Array)[]): Promise { + async assign (headers: Header, ...data: ArrayBuffer[]): Promise { return new Promise((resolve, reject) => { const job: Job = { id: performance.now(), diff --git a/src/lib/wallets/bip44-wallet.ts b/src/lib/wallets/bip44-wallet.ts index ce9b84f..fd713d0 100644 --- a/src/lib/wallets/bip44-wallet.ts +++ b/src/lib/wallets/bip44-wallet.ts @@ -4,7 +4,7 @@ import { KeyPair, Wallet } from './wallet' import { Bip39Mnemonic } from '#src/lib/bip39-mnemonic.js' import { SEED_LENGTH_BIP44 } from '#src/lib/constants.js' -import { hex } from '#src/lib/convert.js' +import { hex, utf8 } from '#src/lib/convert.js' import { Entropy } from '#src/lib/entropy.js' import { Pool } from '#src/lib/pool.js' import { Bip44CkdWorker } from '#workers' @@ -90,8 +90,9 @@ export class Bip44Wallet extends Wallet { * @param {string} [salt=''] - Used when generating the final seed * @returns {Bip44Wallet} A newly instantiated Bip44Wallet */ - static async fromEntropy (key: Uint8Array, entropy: string, salt?: string): Promise - static async fromEntropy (passkey: string | Uint8Array, entropy: string, salt: string = ''): Promise { + static async fromEntropy (key: Uint8Array, entropy: string, salt?: string): Promise + static async fromEntropy (passkey: string | Uint8Array, entropy: string, salt: string = ''): Promise { + if (typeof passkey === 'string') passkey = utf8.toBytes(passkey) let wallet: Bip44Wallet try { const id = await Entropy.create() @@ -129,8 +130,9 @@ export class Bip44Wallet extends Wallet { * @param {string} [salt=''] - Used when generating the final seed * @returns {Bip44Wallet} A newly instantiated Bip44Wallet */ - static async fromMnemonic (key: Uint8Array, mnemonic: string, salt?: string): Promise - static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string, salt: string = ''): Promise { + static async fromMnemonic (key: Uint8Array, mnemonic: string, salt?: string): Promise + static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string, salt: string = ''): Promise { + if (typeof passkey === 'string') passkey = utf8.toBytes(passkey) let wallet: Bip44Wallet try { const id = await Entropy.create() @@ -169,8 +171,9 @@ export class Bip44Wallet extends Wallet { * @param {string} seed - Hexadecimal 128-character string used to derive private-public key pairs * @returns {Bip44Wallet} A newly instantiated Bip44Wallet */ - static async fromSeed (key: Uint8Array, seed: string): Promise - static async fromSeed (passkey: string | Uint8Array, seed: string): Promise { + static async fromSeed (key: Uint8Array, seed: string): Promise + static async fromSeed (passkey: string | Uint8Array, seed: string): Promise { + if (typeof passkey === 'string') passkey = utf8.toBytes(passkey) if (seed.length !== SEED_LENGTH_BIP44) { throw new Error(`Expected a ${SEED_LENGTH_BIP44}-character seed, but received ${seed.length}-character string.`) } @@ -212,7 +215,7 @@ export class Bip44Wallet extends Wallet { async ckd (indexes: number[]): Promise { const data: any = [] indexes.forEach(i => data.push({ index: i })) - const privateKeys: KeyPair[] = await Bip44Wallet.#poolBip44Ckd.assign(data, this.seed) + const privateKeys: KeyPair[] = await Bip44Wallet.#poolBip44Ckd.assign(data, this.seed.buffer) for (let i = 0; i < privateKeys.length; i++) { if (privateKeys[i].privateKey == null) { throw new Error('Failed to derive private keys') diff --git a/src/lib/wallets/blake2b-wallet.ts b/src/lib/wallets/blake2b-wallet.ts index ba5ef98..ab0dff3 100644 --- a/src/lib/wallets/blake2b-wallet.ts +++ b/src/lib/wallets/blake2b-wallet.ts @@ -5,7 +5,7 @@ import { KeyPair, Wallet } from './wallet' import { Bip39Mnemonic } from '#src/lib/bip39-mnemonic.js' import { Blake2b } from '#src/lib/blake2b.js' import { SEED_LENGTH_BLAKE2B } from '#src/lib/constants.js' -import { bytes, hex } from '#src/lib/convert.js' +import { bytes, hex, utf8 } from '#src/lib/convert.js' import { Entropy } from '#src/lib/entropy.js' /** @@ -27,7 +27,7 @@ import { Entropy } from '#src/lib/entropy.js' export class Blake2bWallet extends Wallet { static #isInternal: boolean = false - constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) { + constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) { if (!Blake2bWallet.#isInternal) { throw new Error(`Blake2bWallet cannot be instantiated directly. Use 'await Blake2bWallet.create()' instead.`) } @@ -77,8 +77,9 @@ export class Blake2bWallet extends Wallet { * @param {string} seed - Hexadecimal 64-character string used to derive private-public key pairs * @returns {Blake2bWallet} A newly instantiated Blake2bWallet */ - static async fromSeed (key: Uint8Array, seed: string): Promise - static async fromSeed (passkey: string | Uint8Array, seed: string): Promise { + static async fromSeed (key: Uint8Array, seed: string): Promise + static async fromSeed (passkey: string | Uint8Array, seed: string): Promise { + if (typeof passkey === 'string') passkey = utf8.toBytes(passkey) if (seed.length !== SEED_LENGTH_BLAKE2B) { throw new Error(`Expected a ${SEED_LENGTH_BLAKE2B}-character seed, but received ${seed.length}-character string.`) } @@ -115,8 +116,9 @@ export class Blake2bWallet extends Wallet { * @param {string} mnemonic - Used when generating the final seed * @returns {Blake2bWallet} A newly instantiated Blake2bWallet */ - static async fromMnemonic (key: Uint8Array, mnemonic: string): Promise - static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string): Promise { + static async fromMnemonic (key: Uint8Array, mnemonic: string): Promise + static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string): Promise { + if (typeof passkey === 'string') passkey = utf8.toBytes(passkey) let wallet: Blake2bWallet try { const id = await Entropy.create() diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index 7813627..cd2ab03 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -32,7 +32,7 @@ export abstract class Wallet { #id: Entropy #locked: boolean = true #m: Bip39Mnemonic | null - #s: Uint8Array + #s: Uint8Array get id () { return this.#id.hex } get isLocked () { return this.#locked } @@ -40,7 +40,7 @@ export abstract class Wallet { get mnemonic () { return this.#m instanceof Bip39Mnemonic ? this.#m.phrase : '' } get seed () { return this.#s } - constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) { + constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) { if (this.constructor === Wallet) { throw new Error('Wallet is an abstract class and cannot be instantiated directly.') } @@ -152,7 +152,7 @@ export abstract class Wallet { * @param {(string|Uint8Array)} password Used to lock the wallet * @returns True if successfully locked */ - async lock (password: string | Uint8Array): Promise { + async lock (password: string | Uint8Array): Promise { if (typeof password === 'string') { password = utf8.toBytes(password) } @@ -165,7 +165,7 @@ export abstract class Wallet { name: this.id, id: this.id, } - const response = await Wallet.#poolSafe.assign(headers, password, utf8.toBytes(this.#m?.phrase ?? ''), this.#s) + const response = await Wallet.#poolSafe.assign(headers, password.buffer, utf8.toBytes(this.#m?.phrase ?? '').buffer, this.#s.buffer) const success = response?.result[0] if (!success) { throw null @@ -219,7 +219,7 @@ export abstract class Wallet { * @param {(string|Uint8Array)} password Used previously to lock the wallet * @returns True if successfully unlocked */ - async unlock (password: string | Uint8Array): Promise { + async unlock (password: string | Uint8Array): Promise { if (typeof password === 'string') { password = utf8.toBytes(password) } @@ -231,8 +231,7 @@ export abstract class Wallet { method: 'get', name: this.id } - const response = await Wallet.#poolSafe.assign(headers, - password) + const response = await Wallet.#poolSafe.assign(headers, password.buffer) let { id, mnemonic, seed } = response?.result[0] if (id == null || id !== this.id) { throw null