]> git.codecow.com Git - libnemo.git/commitdiff
Move ADPU codes into constants file.
authorChris Duncan <chris@zoso.dev>
Fri, 4 Jul 2025 07:53:53 +0000 (00:53 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 4 Jul 2025 07:53:53 +0000 (00:53 -0700)
src/lib/constants.ts
src/lib/ledger.ts

index 734292f78d2d8d9307572b457048e5e6c04ac64d..edcae0c90725dfbe167ffe13453d6d98e169352c 100644 (file)
@@ -34,6 +34,18 @@ export const LEDGER_STATUS_CODES: { [key: number]: string } = Object.freeze({
        0x9000: 'OK'
 })
 
+export const LEDGER_ADPU_CODES: { [key: string]: number } = Object.freeze({
+       class: 0xa1,
+       bip32DerivationLevel: 0x03,
+       version: 0x01,
+       account: 0x02,
+       cacheBlock: 0x03,
+       signBlock: 0x04,
+       signNonce: 0x05,
+       paramUnused: 0x00
+
+})
+
 export const UNITS: { [key: string]: number } = Object.freeze({
        RAW: 0,
        RAI: 24,
index 629fe1c075dd4e34c76e769508c961b0b98d8b52..0e9dc04ae73dc3b70748f13d8ce7ce034562b1a3 100644 (file)
@@ -8,7 +8,7 @@ import { default as TransportBLE } from '@ledgerhq/hw-transport-web-ble'
 import { default as TransportUSB } from '@ledgerhq/hw-transport-webusb'
 import { default as TransportHID } from '@ledgerhq/hw-transport-webhid'
 import { ChangeBlock, ReceiveBlock, SendBlock } from './block'
-import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET, LEDGER_STATUS_CODES } from './constants'
+import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET, LEDGER_ADPU_CODES, LEDGER_STATUS_CODES } from './constants'
 import { bytes, dec, hex, utf8 } from './convert'
 import { Rpc } from './rpc'
 
@@ -33,16 +33,6 @@ interface LedgerSignResponse extends LedgerResponse {
 
 export class Ledger {
        static #isInternal: boolean = false
-       static #adpu = {
-               class: 0xa1,
-               bip32DerivationLevel: 0x03,
-               version: 0x01,
-               account: 0x02,
-               cacheBlock: 0x03,
-               signBlock: 0x04,
-               signNonce: 0x05,
-               paramUnused: 0x00
-       }
        #status: 'DISCONNECTED' | 'LOCKED' | 'BUSY' | 'CONNECTED' = 'DISCONNECTED'
        get status () { return this.#status }
        openTimeout = 3000
@@ -151,7 +141,7 @@ export class Ledger {
        async open (): Promise<LedgerResponse> {
                const name = new TextEncoder().encode('Nano')
                const transport = await this.DynamicTransport.create(this.openTimeout, this.listenTimeout)
-               const response = await transport.send(0xe0, 0xd8, Ledger.#adpu.paramUnused, Ledger.#adpu.paramUnused, name as Buffer)
+               const response = await transport.send(0xe0, 0xd8, LEDGER_ADPU_CODES.paramUnused, LEDGER_ADPU_CODES.paramUnused, name as Buffer)
                        .then(res => bytes.toDec(res))
                        .catch(err => err.statusCode) as number
                return new Promise(resolve => setTimeout(resolve, 1000, { status: LEDGER_STATUS_CODES[response] }))
@@ -172,7 +162,7 @@ export class Ledger {
        */
        async close (): Promise<LedgerResponse> {
                const transport = await this.DynamicTransport.create(this.openTimeout, this.listenTimeout)
-               const response = await transport.send(0xb0, 0xa7, Ledger.#adpu.paramUnused, Ledger.#adpu.paramUnused)
+               const response = await transport.send(0xb0, 0xa7, LEDGER_ADPU_CODES.paramUnused, LEDGER_ADPU_CODES.paramUnused)
                        .then(res => bytes.toDec(res))
                        .catch(err => err.statusCode) as number
                return new Promise(resolve => setTimeout(resolve, 1000, { status: LEDGER_STATUS_CODES[response] }))
@@ -188,7 +178,7 @@ export class Ledger {
        */
        async version (): Promise<LedgerVersionResponse> {
                const transport = await this.DynamicTransport.create(this.openTimeout, this.listenTimeout)
-               const response = await transport.send(0xb0, Ledger.#adpu.version, Ledger.#adpu.paramUnused, Ledger.#adpu.paramUnused)
+               const response = await transport.send(0xb0, LEDGER_ADPU_CODES.version, LEDGER_ADPU_CODES.paramUnused, LEDGER_ADPU_CODES.paramUnused)
                        .catch(err => dec.toBytes(err.statusCode)) as Uint8Array
                await transport.close()
 
@@ -220,10 +210,10 @@ export class Ledger {
                const purpose = dec.toBytes(BIP44_PURPOSE + HARDENED_OFFSET, 4)
                const coin = dec.toBytes(BIP44_COIN_NANO + HARDENED_OFFSET, 4)
                const account = dec.toBytes(index + HARDENED_OFFSET, 4)
-               const data = new Uint8Array([Ledger.#adpu.bip32DerivationLevel, ...purpose, ...coin, ...account])
+               const data = new Uint8Array([LEDGER_ADPU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account])
 
                const transport = await this.DynamicTransport.create(this.openTimeout, this.listenTimeout)
-               const response = await transport.send(Ledger.#adpu.class, Ledger.#adpu.account, show ? 0x01 : 0x00, Ledger.#adpu.paramUnused, data as Buffer)
+               const response = await transport.send(LEDGER_ADPU_CODES.class, LEDGER_ADPU_CODES.account, show ? 0x01 : 0x00, LEDGER_ADPU_CODES.paramUnused, data as Buffer)
                        .catch(err => dec.toBytes(err.statusCode)) as Uint8Array
                await transport.close()
 
@@ -271,10 +261,10 @@ export class Ledger {
                const representative = hex.toBytes(block.representative.publicKey)
                const balance = hex.toBytes(BigInt(block.balance).toString(16), 16)
                const signature = hex.toBytes(block.signature)
-               const data = new Uint8Array([Ledger.#adpu.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature])
+               const data = new Uint8Array([LEDGER_ADPU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature])
 
                const transport = await this.DynamicTransport.create(this.openTimeout, this.listenTimeout)
-               const response = await transport.send(Ledger.#adpu.class, Ledger.#adpu.cacheBlock, Ledger.#adpu.paramUnused, Ledger.#adpu.paramUnused, data as Buffer)
+               const response = await transport.send(LEDGER_ADPU_CODES.class, LEDGER_ADPU_CODES.cacheBlock, LEDGER_ADPU_CODES.paramUnused, LEDGER_ADPU_CODES.paramUnused, data as Buffer)
                        .then(res => bytes.toDec(res))
                        .catch(err => err.statusCode) as number
                await transport.close()
@@ -313,8 +303,8 @@ export class Ledger {
                        if (nonce.length !== 16) {
                                throw new RangeError('Nonce must be 16-byte string')
                        }
-                       const data = new Uint8Array([Ledger.#adpu.bip32DerivationLevel, ...purpose, ...coin, ...account, ...nonce])
-                       const response = await transport.send(Ledger.#adpu.class, Ledger.#adpu.signNonce, Ledger.#adpu.paramUnused, Ledger.#adpu.paramUnused, data as Buffer)
+                       const data = new Uint8Array([LEDGER_ADPU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...nonce])
+                       const response = await transport.send(LEDGER_ADPU_CODES.class, LEDGER_ADPU_CODES.signNonce, LEDGER_ADPU_CODES.paramUnused, LEDGER_ADPU_CODES.paramUnused, data as Buffer)
                                .catch(err => dec.toBytes(err.statusCode)) as Uint8Array
                        await transport.close()
 
@@ -334,8 +324,8 @@ export class Ledger {
                        const link = hex.toBytes(input.link)
                        const representative = hex.toBytes(input.representative.publicKey)
                        const balance = hex.toBytes(BigInt(input.balance).toString(16), 16)
-                       const data = new Uint8Array([Ledger.#adpu.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance])
-                       const response = await transport.send(Ledger.#adpu.class, Ledger.#adpu.signBlock, Ledger.#adpu.paramUnused, Ledger.#adpu.paramUnused, data as Buffer)
+                       const data = new Uint8Array([LEDGER_ADPU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance])
+                       const response = await transport.send(LEDGER_ADPU_CODES.class, LEDGER_ADPU_CODES.signBlock, LEDGER_ADPU_CODES.paramUnused, LEDGER_ADPU_CODES.paramUnused, data as Buffer)
                                .catch(err => dec.toBytes(err.statusCode)) as Uint8Array
                        await transport.close()