]> git.codecow.com Git - libnemo.git/commitdiff
Fix account import from public key. Remove tests for private key on Ledger devices.
authorChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 23:44:26 +0000 (16:44 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 23:44:26 +0000 (16:44 -0700)
src/lib/account.ts
test/test.ledger.mjs

index 6e2dce40ac4b17d3f18a9ed0eef468863676a877..36b54989238e37a9b2a6aa48577a485cdb648c32 100644 (file)
@@ -395,36 +395,41 @@ export class Account {
        * @param {Key[]} input - Public keys or addresses of the accounts\r
        * @returns {Account[]} The instantiated Account objects\r
        */\r
-       static #fromPublic (input: Key[] | KeyPair[] | unknown): Account[] {\r
-               if (!this.#isKeys(input) && !this.#isKeyPairs(input)) {\r
+       static #fromPublic (input: (Key | KeyPair)[] | unknown): Account[] {\r
+               const keypairs = this.#isKeyPairs(input)\r
+                       ? input\r
+                       : this.#isKeys(input)\r
+                               ? input.map(i => { return { publicKey: i } as KeyPair })\r
+                               : []\r
+               if (keypairs.length === 0) {\r
                        throw new TypeError('Invalid public input for Account')\r
                }\r
+\r
                const accounts: Account[] = []\r
                let address: string\r
                let publicKey: Uint8Array<ArrayBuffer>\r
                let index\r
-               for (let i of input) {\r
+               for (let keypair of keypairs) {\r
                        let keyError, addressError\r
+                       const key = keypair.publicKey\r
                        try {\r
-                               this.#validateKey(i)\r
-                               publicKey = (typeof i === 'string')\r
-                                       ? hex.toBytes(i)\r
-                                       : i\r
+                               this.#validateKey(key)\r
+                               publicKey = (typeof key === 'string')\r
+                                       ? hex.toBytes(key)\r
+                                       : key\r
                                address = this.#keyToAddress(publicKey)\r
                        } catch (err) {\r
                                keyError = err\r
                                try {\r
-                                       this.validate(i)\r
-                                       address = i\r
+                                       this.validate(key)\r
+                                       address = key\r
                                        publicKey = this.#addressToKey(address)\r
                                } catch (err) {\r
                                        addressError = err\r
                                        throw new TypeError('Failed to import Account from public data', { cause: { keyError, addressError } })\r
                                }\r
                        }\r
-                       if (this.#isKeyPair(i)) {\r
-                               index = i.index ?? undefined\r
-                       }\r
+                       index = keypair.index\r
                        this.#isInternal = true\r
                        accounts.push(new this(address, publicKey, index))\r
                }\r
index b01c1d574ba687e84fa9cf41b28fceccdcb78eb1..3016200bfff58f9f30b8005e17b82b27730987f8 100644 (file)
@@ -88,8 +88,7 @@ await Promise.all([
                        assert.ok(account instanceof Account)
                        assert.exists(account.address)
                        assert.exists(account.publicKey)
-                       assert.exists(account.privateKey)
-                       assert.equal(account.privateKey, '0000000000000000000000000000000000000000000000000000000000000000')
+                       await assert.rejects(account.export(''))
                })
 
                await test('get second and third accounts', async () => {
@@ -101,23 +100,19 @@ await Promise.all([
                                assert.ok(account instanceof Account)
                                assert.exists(account.address)
                                assert.exists(account.publicKey)
-                               assert.exists(account.privateKey)
-                               assert.equal(account.privateKey, '0000000000000000000000000000000000000000000000000000000000000000')
                        }
                })
 
-               await test('refresh first three accounts (must already be opened to be refreshed)', async () => {
+               await test('refresh first three accounts (must already be opened to be refreshed)', { skip: true }, async () => {
                        const accounts = await wallet.refresh(rpc, 0, 2)
 
                        assert.exists(accounts)
                        for (const account of accounts) {
                                assert.ok(account instanceof Account)
                                assert.exists(account.address)
+                               assert.exists(account.publicKey)
                                assert.exists(account.balance)
                                assert.ok(account.balance >= 0)
-                               assert.exists(account.publicKey)
-                               assert.exists(account.privateKey)
-                               assert.equal(account.privateKey, '0000000000000000000000000000000000000000000000000000000000000000')
                        }
                })