From: Chris Duncan Date: Sun, 27 Jul 2025 21:25:47 +0000 (-0700) Subject: Accept password only for secure account methods. Remove private key export. X-Git-Tag: v0.10.5~49^2~1 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=f6661a1861b8fdd78b1ec90c5f46b8a08416cd7a;p=libnemo.git Accept password only for secure account methods. Remove private key export. --- diff --git a/src/lib/account.ts b/src/lib/account.ts index c0e24bc..176e065 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -167,45 +167,6 @@ export class Account { } } - /** - * USING THIS METHOD IS DISCOURAGED. This library works in its entirety without - * exposing the private keys of accounts. - * - * Retrieves and decrypts the private key of the Account. The same password - * used to lock it must be used to unlock it. If derived from a wallet, the - * password for the account is the wallet seed. - * - * @param {Key} password Used previously to lock the Account - * @returns Private key bytes as a Uint8Array - */ - async export (password: Key): Promise> - /** - * USING THIS METHOD IS DISCOURAGED. This library works in its entirety without - * exposing the private keys of accounts. - * - * Retrieves and decrypts the private key of the Account. The same password - * used to lock it must be used to unlock it. If derived from a wallet, the - * password for the account is the wallet seed. - * - * @param {Key} password Used previously to lock the Account - * @returns Private key bytes as a hexadecimal string - */ - async export (password: Key, format: 'hex'): Promise - async export (password: Key, format?: 'hex'): Promise { - if (typeof password === 'string') password = utf8.toBytes(password) - try { - const privateKey = new Uint8Array(await this.#export(password)) - return format === 'hex' - ? bytes.toHex(privateKey) - : privateKey - } catch (err) { - console.log(err) - throw new Error('Failed to export Account private key') - } finally { - bytes.erase(password) - } - } - /** * Refreshes the account from its current state on the network. * @@ -246,20 +207,17 @@ export class Account { * @param {Key} password - Required to decrypt the private key for signing * @returns {Promise} Hexadecimal-formatted 64-byte signature */ - async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: Key): Promise { - if (typeof password === 'string') password = utf8.toBytes(password) + async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: string): Promise { try { const { signature } = await NanoNaClWorker.request({ method: 'detached', - privateKey: await this.#export(password), + privateKey: await this.#getPrivateKey(password), msg: hex.toBuffer(block.hash) }) block.signature = bytes.toHex(new Uint8Array(signature)) return block.signature } catch (err) { throw new Error(`Failed to sign block`, { cause: err }) - } finally { - bytes.erase(password) } } @@ -314,26 +272,20 @@ export class Account { * Retrieves and decrypts the private key of the Account. The same password * used to lock it must be used to unlock it. * - * @param {Key} password Used previously to lock the Account - * @returns Private key bytes as a Uint8Array + * @param {string} password Used previously to lock the Account + * @returns {Promise} Promise for buffer of private key */ - async #export (password: Key): Promise { - if (typeof password === 'string') password = utf8.toBytes(password) - if (password == null || !(password instanceof Uint8Array)) { - throw new Error('Password must be string or bytes') - } + async #getPrivateKey (password: string): Promise { try { const response = await SafeWorker.request({ method: 'fetch', names: this.publicKey, store: 'Account', - password: password.buffer + password: utf8.toBuffer(password) }) return response[this.publicKey] } catch (err) { throw new Error(`Failed to export private key for Account ${this.address}`, { cause: err }) - } finally { - bytes.erase(password) } }