]> git.codecow.com Git - libnemo.git/commitdiff
Accept password only for secure account methods. Remove private key export.
authorChris Duncan <chris@zoso.dev>
Sun, 27 Jul 2025 21:25:47 +0000 (14:25 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 27 Jul 2025 21:25:47 +0000 (14:25 -0700)
src/lib/account.ts

index c0e24bc01c1d799094077d5b43edd21d05e5d1ec..176e065adfd248d99ce42ba66e327be46f0764e4 100644 (file)
@@ -167,45 +167,6 @@ export class Account {
                }\r
        }\r
 \r
-       /**\r
-       * USING THIS METHOD IS DISCOURAGED. This library works in its entirety without\r
-       * exposing the private keys of accounts.\r
-       *\r
-       * Retrieves and decrypts the private key of the Account. The same password\r
-       * used to lock it must be used to unlock it. If derived from a wallet, the\r
-       * password for the account is the wallet seed.\r
-       *\r
-       * @param {Key} password Used previously to lock the Account\r
-       * @returns Private key bytes as a Uint8Array\r
-       */\r
-       async export (password: Key): Promise<Uint8Array<ArrayBuffer>>\r
-       /**\r
-       * USING THIS METHOD IS DISCOURAGED. This library works in its entirety without\r
-       * exposing the private keys of accounts.\r
-       *\r
-       * Retrieves and decrypts the private key of the Account. The same password\r
-       * used to lock it must be used to unlock it. If derived from a wallet, the\r
-       * password for the account is the wallet seed.\r
-       *\r
-       * @param {Key} password Used previously to lock the Account\r
-       * @returns Private key bytes as a hexadecimal string\r
-       */\r
-       async export (password: Key, format: 'hex'): Promise<string>\r
-       async export (password: Key, format?: 'hex'): Promise<Key> {\r
-               if (typeof password === 'string') password = utf8.toBytes(password)\r
-               try {\r
-                       const privateKey = new Uint8Array(await this.#export(password))\r
-                       return format === 'hex'\r
-                               ? bytes.toHex(privateKey)\r
-                               : privateKey\r
-               } catch (err) {\r
-                       console.log(err)\r
-                       throw new Error('Failed to export Account private key')\r
-               } finally {\r
-                       bytes.erase(password)\r
-               }\r
-       }\r
-\r
        /**\r
        * Refreshes the account from its current state on the network.\r
        *\r
@@ -246,20 +207,17 @@ export class Account {
        * @param {Key} password - Required to decrypt the private key for signing\r
        * @returns {Promise<string>} Hexadecimal-formatted 64-byte signature\r
        */\r
-       async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: Key): Promise<string> {\r
-               if (typeof password === 'string') password = utf8.toBytes(password)\r
+       async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: string): Promise<string> {\r
                try {\r
                        const { signature } = await NanoNaClWorker.request<ArrayBuffer>({\r
                                method: 'detached',\r
-                               privateKey: await this.#export(password),\r
+                               privateKey: await this.#getPrivateKey(password),\r
                                msg: hex.toBuffer(block.hash)\r
                        })\r
                        block.signature = bytes.toHex(new Uint8Array(signature))\r
                        return block.signature\r
                } catch (err) {\r
                        throw new Error(`Failed to sign block`, { cause: err })\r
-               } finally {\r
-                       bytes.erase(password)\r
                }\r
        }\r
 \r
@@ -314,26 +272,20 @@ export class Account {
        * Retrieves and decrypts the private key of the Account. The same password\r
        * used to lock it must be used to unlock it.\r
        *\r
-       * @param {Key} password Used previously to lock the Account\r
-       * @returns Private key bytes as a Uint8Array\r
+       * @param {string} password Used previously to lock the Account\r
+       * @returns {Promise<ArrayBuffer>} Promise for buffer of private key\r
        */\r
-       async #export (password: Key): Promise<ArrayBuffer> {\r
-               if (typeof password === 'string') password = utf8.toBytes(password)\r
-               if (password == null || !(password instanceof Uint8Array)) {\r
-                       throw new Error('Password must be string or bytes')\r
-               }\r
+       async #getPrivateKey (password: string): Promise<ArrayBuffer> {\r
                try {\r
                        const response = await SafeWorker.request<ArrayBuffer>({\r
                                method: 'fetch',\r
                                names: this.publicKey,\r
                                store: 'Account',\r
-                               password: password.buffer\r
+                               password: utf8.toBuffer(password)\r
                        })\r
                        return response[this.publicKey]\r
                } catch (err) {\r
                        throw new Error(`Failed to export private key for Account ${this.address}`, { cause: err })\r
-               } finally {\r
-                       bytes.erase(password)\r
                }\r
        }\r
 \r