import { HARDENED_OFFSET } from '../constants'
import { bytes, dec, hex, utf8 } from '../convert'
import { _cache } from './cache'
-import { queue } from './queue'
export async function _sign (transport: LedgerTransport, index: number, data: string | Block, frontier?: Block): Promise<string> {
try {
}
}
-/**
- * Sign a block with the Ledger device.
- *
- * @param {number} index - Account number
- * @param {object} block - Block data to sign
- * @returns {Promise} Status, signature, and block hash
- */
async function signBlock (transport: LedgerTransport, index: number, block: Block): Promise<string> {
if (block.signature !== undefined) {
throw new TypeError('Block signature already exists', { cause: block.signature })
const previous = block.previous
const link = block.link
const representative = hex.toBytes(block.representative.publicKey, 32)
- const balance = hex.toBytes(BigInt(block.balance).toString(16), 16)
+ const balance = hex.toBytes(block.balance.toString(16), 16)
const data = new Uint8Array([...DERIVATION_PATH, ...account, ...previous, ...link, ...representative, ...balance])
- const res = await queue(async () => req(transport, APDU_CODES.signBlock, data as Buffer))
+ const res = await req(transport, APDU_CODES.signBlock, data as Buffer)
if (res.hash !== block.hash) {
throw new Error('Hash from Ledger does not match hash from block', { cause: `${res.hash} | ${block.hash}` })
}
return res.signature
}
-/**
- * Sign a nonce with the Ledger device.
- *
- * @param {number} index - Account number
- * @param {Uint8Array} nonce - 128-bit value to sign
- * @returns {Promise} Status and signature
- */
async function signNonce (transport: LedgerTransport, index: number, nonce: Uint8Array<ArrayBuffer>): Promise<string> {
if (nonce.byteLength !== 16) {
throw new RangeError('Nonce must be 16-byte string')
}
-
const derivationAccount = dec.toBytes(index + HARDENED_OFFSET, 4)
const data = new Uint8Array([...DERIVATION_PATH, ...derivationAccount, ...nonce])
- const res = await queue(async () => req(transport, APDU_CODES.signNonce, data as Buffer))
+ const res = await req(transport, APDU_CODES.signNonce, data as Buffer)
return res.signature
}
.send(APDU_CODES.class, command, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data)
.catch((err: any) => dec.toBytes(err.statusCode))
.finally(async () => await t.close()) as Uint8Array
-
const statusCode = bytes.toDec(response.slice(-2)) as number
const status = STATUS_CODES[statusCode] ?? 'UNKNOWN_ERROR'
-
if (status !== 'OK') {
throw new Error('Signing with ledger failed', { cause: status })
}