]> git.codecow.com Git - libnemo.git/commitdiff
Refactor Ledger tests to work with agnostic wallet.
authorChris Duncan <chris@zoso.dev>
Wed, 20 Aug 2025 20:54:31 +0000 (13:54 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 20 Aug 2025 20:54:31 +0000 (13:54 -0700)
test/test.ledger.mjs

index cb043ea3a4d1f1bf70245288e2ff1832c58998e0..8a8804486b3c78e88f43341bbe7bf7056bec65c7 100644 (file)
@@ -15,18 +15,18 @@ let Account
 */
 let Block
 /**
-* @type {typeof import('../dist/types.d.ts').Ledger}
-*/
-let Ledger
-/**
 * @type {typeof import('../dist/types.d.ts').Rpc}
 */
 let Rpc
+/**
+* @type {typeof import('../dist/types.d.ts').Wallet}
+*/
+let Wallet
 
 if (isNode) {
-       ({ Account, Block, Ledger, Rpc } = await import('../dist/nodejs.min.js'))
+       ({ Account, Block, Rpc, Wallet } = await import('../dist/nodejs.min.js'))
 } else {
-       ({ Account, Block, Ledger, Rpc } = await import('../dist/browser.min.js'))
+       ({ Account, Block, Rpc, Wallet } = await import('../dist/browser.min.js'))
 }
 
 const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME)
@@ -37,46 +37,39 @@ const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME)
 */
 await Promise.all([
        /* node:coverage disable */
-       suite('Ledger hardware wallet', { skip: false || isNode || Ledger.isUnsupported }, async () => {
+       suite('Ledger hardware wallet', { skip: false || isNode }, async () => {
 
                const { OPEN_BLOCK, RECEIVE_BLOCK, SEND_BLOCK } = NANO_TEST_VECTORS
-
                let wallet, account, openBlock, sendBlock, receiveBlock
+               try {
+                       wallet = await Wallet.create('Ledger')
+               } catch {
+                       return
+               }
 
                await test('request permissions', async () => {
-                       wallet = await Ledger.create()
-                       let status = wallet.status
-                       assert.equal(status, 'DISCONNECTED')
-                       assert.equal(status, wallet.status)
-
-                       status = await click(
-                               'Reset permissions, unlock device, quit Nano app, then click to continue',
-                               async () => wallet.connect()
-                       )
-                       assert.equal(status, 'BUSY')
-                       assert.equal(status, wallet.status)
-
-                       status = await click(
-                               'Open Nano app on device, allow device to auto-lock, then click to continue',
-                               async () => wallet.connect()
-                       )
-                       assert.equal(status, 'LOCKED')
-                       assert.equal(status, wallet.status)
-
-                       status = await click(
-                               'Unlock device, verify Nano app is open, then click to continue',
-                               async () => wallet.connect()
-                       )
-                       assert.equal(status, 'CONNECTED')
-                       assert.equal(status, wallet.status)
-               })
-
-               await test('get version', async () => {
-                       const { status, name, version } = await wallet.version()
-
-                       assert.equal(status, 'OK')
-                       assert.equal(name, 'Nano')
-                       assert.equal(version, '1.2.6')
+                       await assert.rejects(wallet.unlock(), 'expect DISCONNECTED')
+
+                       await assert.rejects(async () => {
+                               await click(
+                                       'Reset permissions, unlock device, quit Nano app, then click to continue',
+                                       async () => wallet.unlock()
+                               )
+                       }, 'expect BUSY')
+
+                       await assert.rejects(async () => {
+                               await click(
+                                       'Open Nano app on device, allow device to auto-lock, then click to continue',
+                                       async () => wallet.unlock()
+                               )
+                       }, 'expect LOCKED')
+
+                       await assert.resolves(async () => {
+                               await click(
+                                       'Unlock device, verify Nano app is open, then click to continue',
+                                       async () => wallet.unlock()
+                               )
+                       }, 'expect CONNECTED')
                })
 
                await test('get first account', async () => {
@@ -109,7 +102,7 @@ await Promise.all([
                                assert.exists(account.address)
                                assert.exists(account.publicKey)
                                assert.exists(account.balance)
-                               assert.ok(account.balance >= 0)
+                               assert.ok(account.balance ?? -1 >= 0)
                        }
                })
 
@@ -121,21 +114,14 @@ await Promise.all([
                        assert.nullish(openBlock.signature)
                        assert.equal(openBlock.account.publicKey, account.publicKey)
 
-                       const signature = await wallet.sign(0, openBlock)
+                       await wallet.sign(0, openBlock)
 
-                       assert.ok(/^[A-F0-9]{128}$/i.test(signature))
+                       assert.ok(/^[A-F0-9]{128}$/i.test(openBlock.signature ?? ''))
 
-                       await openBlock.sign(0)
+                       await openBlock.sign(wallet, 0)
 
                        assert.exists(openBlock.signature)
                        assert.ok(/^[A-F0-9]{128}$/i.test(openBlock.signature ?? ''))
-                       assert.equal(signature, openBlock.signature)
-               })
-
-               await test('cache open block', async () => {
-                       const { status } = await wallet.updateCache(0, openBlock)
-
-                       assert.equal(status, 'OK')
                })
 
                await test('sign send block from wallet which requires cache to be up-to-date', async () => {
@@ -146,10 +132,9 @@ await Promise.all([
                        assert.nullish(sendBlock.signature)
                        assert.equal(sendBlock.account.publicKey, account.publicKey)
 
-                       const signature = await wallet.sign(0, sendBlock, 'hex')
+                       await wallet.sign(0, sendBlock, openBlock)
 
-                       assert.ok(/^[A-F0-9]{128}$/i.test(signature))
-                       assert.equal(signature, sendBlock.signature)
+                       assert.ok(/^[A-F0-9]{128}$/i.test(sendBlock.signature ?? ''))
                })
 
                await test('sign a receive block from block object which can accept previous block for cache', async () => {
@@ -160,7 +145,7 @@ await Promise.all([
                        assert.nullish(receiveBlock.signature)
                        assert.equal(receiveBlock.account.publicKey, account.publicKey)
 
-                       await receiveBlock.sign(0, sendBlock)
+                       await receiveBlock.sign(wallet, 0, sendBlock)
 
                        assert.exists(receiveBlock.signature)
                        assert.ok(/^[A-F0-9]{128}$/i.test(receiveBlock.signature ?? ''))
@@ -174,7 +159,7 @@ await Promise.all([
                        assert.nullish(sendBlock.signature)
                        assert.equal(sendBlock.account.publicKey, account.publicKey)
 
-                       await assert.rejects(wallet.sign(0, sendBlock, 'hex'))
+                       await assert.rejects(wallet.sign(0, sendBlock))
                })
 
                await test('sign a send block from wallet including frontier block for cache', async () => {
@@ -185,16 +170,15 @@ await Promise.all([
                        assert.nullish(sendBlock.signature)
                        assert.equal(sendBlock.account.publicKey, account.publicKey)
 
-                       const signature = await wallet.sign(0, sendBlock, 'hex', receiveBlock)
+                       await wallet.sign(0, sendBlock, receiveBlock)
 
                        assert.exists(sendBlock.signature)
                        assert.ok(/^[A-F0-9]{128}$/i.test(sendBlock.signature ?? ''))
-                       assert.equal(signature, sendBlock.signature)
                })
 
                await test('destroy wallet', async () => {
                        await wallet.destroy()
-                       await assert.rejects(wallet.version())
+                       await assert.rejects(wallet.unlock())
                })
 
                await test('fail when using new', async () => {