From 3c14879d0111001854dd1b33824473da4ae377b7 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 23 Jul 2025 16:44:26 -0700 Subject: [PATCH] Fix account import from public key. Remove tests for private key on Ledger devices. --- src/lib/account.ts | 29 +++++++++++++++++------------ test/test.ledger.mjs | 11 +++-------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index 6e2dce4..36b5498 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -395,36 +395,41 @@ export class Account { * @param {Key[]} input - Public keys or addresses of the accounts * @returns {Account[]} The instantiated Account objects */ - static #fromPublic (input: Key[] | KeyPair[] | unknown): Account[] { - if (!this.#isKeys(input) && !this.#isKeyPairs(input)) { + static #fromPublic (input: (Key | KeyPair)[] | unknown): Account[] { + const keypairs = this.#isKeyPairs(input) + ? input + : this.#isKeys(input) + ? input.map(i => { return { publicKey: i } as KeyPair }) + : [] + if (keypairs.length === 0) { throw new TypeError('Invalid public input for Account') } + const accounts: Account[] = [] let address: string let publicKey: Uint8Array let index - for (let i of input) { + for (let keypair of keypairs) { let keyError, addressError + const key = keypair.publicKey try { - this.#validateKey(i) - publicKey = (typeof i === 'string') - ? hex.toBytes(i) - : i + this.#validateKey(key) + publicKey = (typeof key === 'string') + ? hex.toBytes(key) + : key address = this.#keyToAddress(publicKey) } catch (err) { keyError = err try { - this.validate(i) - address = i + this.validate(key) + address = key publicKey = this.#addressToKey(address) } catch (err) { addressError = err throw new TypeError('Failed to import Account from public data', { cause: { keyError, addressError } }) } } - if (this.#isKeyPair(i)) { - index = i.index ?? undefined - } + index = keypair.index this.#isInternal = true accounts.push(new this(address, publicKey, index)) } diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index b01c1d5..3016200 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -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') } }) -- 2.47.3