From: Chris Duncan Date: Fri, 8 Aug 2025 18:38:29 +0000 (-0700) Subject: Fix safe sign method ignoring wallet type when deriving account and add test scenario. X-Git-Tag: v0.10.5~43^2~16 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=e3ead7a180793110dc024022907274401cc21eea;p=libnemo.git Fix safe sign method ignoring wallet type when deriving account and add test scenario. --- diff --git a/src/lib/safe.ts b/src/lib/safe.ts index c6dd767..a1ef851 100644 --- a/src/lib/safe.ts +++ b/src/lib/safe.ts @@ -196,7 +196,9 @@ export class Safe { if (data == null) { throw new Error('Data to sign not found') } - const prv = await Bip44.ckd(this.#seed, BIP44_COIN_NANO, index) + const prv = this.#type === 'BIP-44' + ? await Bip44.ckd(this.#seed, BIP44_COIN_NANO, index) + : await this.#deriveBlake2bPrivateKey(this.#seed, index) const sig = await NanoNaCl.detached(new Uint8Array(data), new Uint8Array(prv)) return { signature: sig.buffer } } catch (err) { diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index 47a0a62..8c7b331 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -95,9 +95,22 @@ await Promise.all([ }), suite('Block signing using official test vectors', async () => { - const { ADDRESS_0, BIP39_SEED, OPEN_BLOCK, PASSWORD, PRIVATE_0, RECEIVE_BLOCK, SEND_BLOCK } = NANO_TEST_VECTORS + const { ADDRESS_0, BIP39_SEED, BLAKE2B_ADDRESS_1, BLAKE2B_PUBLIC_1, BLAKE2B_SEED, OPEN_BLOCK, PASSWORD, PRIVATE_0, RECEIVE_BLOCK, SEND_BLOCK } = NANO_TEST_VECTORS - await test('sign open block with wallet', async () => { + await test('sign open block with BLAKE2b wallet', async () => { + const wallet = await Wallet.import('BLAKE2b', PASSWORD, BLAKE2B_SEED) + await assert.resolves(wallet.unlock(PASSWORD)) + + const block = new Block(BLAKE2B_ADDRESS_1, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) + .receive(OPEN_BLOCK.link, OPEN_BLOCK.balance) + const signature = await wallet.sign(1, block, 'hex') + assert.equal(block.signature, signature) + assert.ok(await block.verify(BLAKE2B_PUBLIC_1)) + + await wallet.destroy() + }) + + await test('sign open block with BIP-44 wallet', async () => { const wallet = await Wallet.import('BIP-44', PASSWORD, BIP39_SEED) await assert.resolves(wallet.unlock(PASSWORD))