]> git.codecow.com Git - libnemo.git/commitdiff
Fix buffer typings.
authorChris Duncan <chris@zoso.dev>
Sat, 12 Jul 2025 07:58:05 +0000 (00:58 -0700)
committerChris Duncan <chris@zoso.dev>
Sat, 12 Jul 2025 07:58:05 +0000 (00:58 -0700)
src/lib/account.ts
src/lib/bip39-mnemonic.ts
src/lib/block.ts
src/lib/convert.ts
src/lib/pool.ts
src/lib/wallets/bip44-wallet.ts
src/lib/wallets/blake2b-wallet.ts
src/lib/wallets/wallet.ts

index fa6a429176a22398e1e6f01c62d5b50d78bfd35b..d84ac69db483a8a06ef2681f4d86fee211abc4f2 100644 (file)
@@ -136,7 +136,7 @@ export class Account {
                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
@@ -152,7 +152,7 @@ export class Account {
        * @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
@@ -163,7 +163,7 @@ export class Account {
                                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
@@ -217,7 +217,7 @@ export class Account {
        * @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
@@ -227,7 +227,7 @@ export class Account {
                                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
index d51c7a8efd1cb419ea3978b8b60076d8954362b2..82645987c9037bab75b15bbe58cfb8b67f6e7836 100644 (file)
@@ -13,8 +13,8 @@ const { subtle } = globalThis.crypto
 */\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
@@ -169,14 +169,14 @@ export class Bip39Mnemonic {
                        : 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
index 7b7e4d32cff9f5fc45484818c701c221f742bb23..dec40b0afcb0bc57f4ba485553092f8e88fff4ca 100644 (file)
@@ -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 })
index abea692690039f8cda5989dae4ed4aee825e051d..f75264e1f0eca75c43173276ad6a80e94490c025 100644 (file)
@@ -297,7 +297,7 @@ export class obj {
        * @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
@@ -313,8 +313,8 @@ export class utf8 {
        * @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
index d72fb64e7740699fa0519bdee1f0bd49f3170783..d714755b3eec771679ab32e319b93b97adb3202c 100644 (file)
@@ -1,6 +1,8 @@
 // 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
 }
@@ -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<any> {
+       async assign (headers: Header, ...data: ArrayBuffer[]): Promise<any> {
                return new Promise((resolve, reject) => {
                        const job: Job = {
                                id: performance.now(),
index ce9b84f00e9619f61a897a8f8d294071782cf87e..fd713d0ebc972c9de2c00f0122d2d861f1e998ea 100644 (file)
@@ -4,7 +4,7 @@
 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
@@ -90,8 +90,9 @@ export class Bip44Wallet extends Wallet {
        * @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
@@ -129,8 +130,9 @@ export class Bip44Wallet extends Wallet {
        * @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
@@ -169,8 +171,9 @@ export class Bip44Wallet extends Wallet {
        * @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
@@ -212,7 +215,7 @@ export class Bip44Wallet extends Wallet {
        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
index ba5ef98e6976887eb8fa86045244724dd4a711b2..ab0dff33604fa61d34d08c8f5b42082581429fef 100644 (file)
@@ -5,7 +5,7 @@ import { KeyPair, Wallet } from './wallet'
 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
@@ -27,7 +27,7 @@ import { Entropy } from '#src/lib/entropy.js'
 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
@@ -77,8 +77,9 @@ export class Blake2bWallet extends Wallet {
        * @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
@@ -115,8 +116,9 @@ export class Blake2bWallet extends Wallet {
        * @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
index 781362722c29238f28032953b942b8597b1b224c..cd2ab03df5ef7ee428ba1b4b1e4a13f738bd0680 100644 (file)
@@ -32,7 +32,7 @@ export abstract class Wallet {
        #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
@@ -40,7 +40,7 @@ export abstract class Wallet {
        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
@@ -152,7 +152,7 @@ export abstract class Wallet {
        * @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
@@ -165,7 +165,7 @@ export abstract class Wallet {
                                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
@@ -219,7 +219,7 @@ export abstract class Wallet {
        * @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
@@ -231,8 +231,7 @@ export abstract class Wallet {
                                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