import { Block } from '../block'
import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants'
import { bytes, dec, hex } from '../convert'
-import { queue } from './queue'
export async function _cache (transport: LedgerTransport, index: number = 0, block: Block): Promise<LedgerResponse> {
- return queue<LedgerResponse>(async () => {
- try {
- if (typeof index !== 'number' || index < 0 || index >= HARDENED_OFFSET) {
- throw new TypeError('Invalid account index')
- }
- if (!(block instanceof Block)) {
- throw new TypeError('Invalid block format')
- }
- if (!(block.link instanceof Uint8Array)) {
- throw new TypeError('Invalid block link')
- }
- if (!(block.representative instanceof Account)) {
- throw new TypeError('Invalid block link')
- }
- if (!block.signature) {
- throw new ReferenceError('Cannot cache unsigned block')
- }
+ try {
+ if (typeof index !== 'number' || index < 0 || index >= HARDENED_OFFSET) {
+ throw new TypeError('Invalid account index')
+ }
+ if (!(block instanceof Block)) {
+ throw new TypeError('Invalid block format')
+ }
+ if (!(block.link instanceof Uint8Array)) {
+ throw new TypeError('Invalid block link')
+ }
+ if (!(block.representative instanceof Account)) {
+ throw new TypeError('Invalid block link')
+ }
+ if (!block.signature) {
+ throw new ReferenceError('Cannot cache unsigned block')
+ }
- 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 previous = block.previous
- const link = block.link
- const representative = hex.toBytes(block.representative.publicKey, 32)
- const balance = hex.toBytes(block.balance.toString(16), 16)
- const signature = hex.toBytes(block.signature, 64)
- const data = new Uint8Array([APDU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature])
+ 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 previous = block.previous
+ const link = block.link
+ const representative = hex.toBytes(block.representative.publicKey, 32)
+ const balance = hex.toBytes(block.balance.toString(16), 16)
+ const signature = hex.toBytes(block.signature, 64)
+ const data = new Uint8Array([APDU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature])
- const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
- const response = await t
- .send(APDU_CODES.class, APDU_CODES.cacheBlock, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data as Buffer)
- .then((res: Buffer) => bytes.toDec(res))
- .catch((err: any) => err.statusCode)
- .finally(async () => await t.close()) as number
+ const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
+ const response = await t
+ .send(APDU_CODES.class, APDU_CODES.cacheBlock, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data as Buffer)
+ .then((res: Buffer) => bytes.toDec(res))
+ .catch((err: any) => err.statusCode)
+ .finally(async () => await t.close()) as number
- return { status: STATUS_CODES[response] }
- } catch (err: any) {
- console.error('Ledger.#cacheBlock()', err)
- return { status: err.message }
+ const status = STATUS_CODES[response]
+ if (status !== 'OK') {
+ throw new Error('failed to cache frontier block in ledger', { cause: status })
}
- })
+ return { status }
+ } catch (err: any) {
+ console.error('Ledger cache()', err)
+ return { status: err.message }
+ }
}