UNPKG

@hazae41/ledger

Version:

Private and supply-chain hardened Ledger controller for TypeScript

102 lines (98 loc) 3.4 kB
'use strict'; var binary = require('@hazae41/binary'); var cursor = require('@hazae41/cursor'); class InvalidHidTagError extends Error { tag; #class = InvalidHidTagError; name = this.#class.name; constructor(tag) { super(`Invalid HID tag ${tag}`); this.tag = tag; } } class HidFrame { channel; fragment; index; #class = HidFrame; static tag = 0x05; constructor(channel, fragment, index) { this.channel = channel; this.fragment = fragment; this.index = index; } sizeOrThrow() { return 2 + 1 + 2 + this.fragment.sizeOrThrow(); } writeOrThrow(cursor) { cursor.writeUint16OrThrow(this.channel); cursor.writeUint8OrThrow(this.#class.tag); cursor.writeUint16OrThrow(this.index); this.fragment.writeOrThrow(cursor); } static readOrThrow(cursor) { const channel = cursor.readUint16OrThrow(); const tag = cursor.readUint8OrThrow(); if (tag !== this.tag) throw new InvalidHidTagError(tag); const index = cursor.readUint16OrThrow(); const bytes = cursor.readAndCopyOrThrow(cursor.remaining); const fragment = new binary.Opaque(bytes); return new HidFrame(channel, fragment, index); } static *splitOrThrow(channel, bytes) { const chunks = new cursor.Cursor(bytes).splitOrThrow(59); let chunk = chunks.next(); for (let i = 0; !chunk.done; chunk = chunks.next(), i++) yield new HidFrame(channel, new binary.Opaque(chunk.value), i); return chunk.value; } static async unsplitOrThrow(channel, generator) { const first = await generator.next(); if (first.done) return first.value; const frames = binary.Readable.readFromBytesOrThrow(HidContainer, first.value.fragment.bytes); const bytes = new Uint8Array(frames.length); const cursor$1 = new cursor.Cursor(bytes); cursor$1.writeOrThrow(frames.fragment.bytes.slice(0, cursor$1.remaining)); if (!cursor$1.remaining) return cursor$1.bytes; let frame = await generator.next(); for (; !frame.done; frame = await generator.next()) { cursor$1.writeOrThrow(frame.value.fragment.bytes.slice(0, cursor$1.remaining)); if (!cursor$1.remaining) return cursor$1.bytes; continue; } return frame.value; } } class HidContainer { length; fragment; constructor(length, fragment) { this.length = length; this.fragment = fragment; } static newOrThrow(fragment) { return new HidContainer(fragment.sizeOrThrow(), fragment); } sizeOrThrow() { return Math.ceil((2 + this.length) / 59) * 59; } writeOrThrow(cursor) { cursor.writeUint16OrThrow(this.length); this.fragment.writeOrThrow(cursor); cursor.fillOrThrow(0, cursor.remaining); } static readOrThrow(cursor) { const length = cursor.readUint16OrThrow(); const bytes = cursor.readAndCopyOrThrow(cursor.remaining); const fragment = new binary.Opaque(bytes); return new HidContainer(length, fragment); } } exports.HidContainer = HidContainer; exports.HidFrame = HidFrame; exports.InvalidHidTagError = InvalidHidTagError; //# sourceMappingURL=index.cjs.map