UNPKG

3.55 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Transport as LedgerTransport } from "@ledgerhq/hw-transport";
3/**
4 * APDU Header Flags
5 *
6 * Describes the APDU Class, Instruction-Type, Parameter 1, and Parameter 2.
7 *
8 * APDU Header: ({ CLA + INS + P1 + P2 })
9 * - CLA: Apdu Class
10 * - INS: Instruction Type
11 * - P1: Instruction Parameter 1
12 * - P2: Instruction Parameter 2
13 *
14 * Instruction Types:
15 * - INS_GET_PUBLIC_KEY: Get a PublicKey from a Ledger Device
16 * - INS_GET_VERSION: Get the SXP Application Version from a Ledger Device
17 * - INS_SIGN_TRANSACTION: Sign a Transaction using a Ledger Device
18 * - INS_SIGN_MESSAGE: Sign a Message using a Ledger Device
19 *
20 * App / PublicKey Context:
21 * P1: User Approval
22 * - P1_NON_CONFIRM: Do NOT request user approval
23 * - P1_CONFIRM: Request user approval
24 *
25 * P2: ChainCode
26 * - P2_NO_CHAINCODE: Don't use a ChainCode
27 * - P2_CHAINCODE: Use a Chaincode
28 *
29 * Signing Context:
30 * P1: Payload Segment
31 * - P1_SINGLE: N(1) where N === 1
32 * - P1_FIRST: N(1) where N > 1
33 * - P1_MORE: N(2)..N-1 where N > 2
34 * - P1_LAST: Nth where N > 1
35 *
36 * P2:
37 * - P2_SCHNORR_LEG: Use Schnorr (bcrypto-v4.1.0) Signatures
38 *
39 */
40export declare enum ApduFlag {
41 /** APDU Class */
42 CLA = 224,
43 /** App / PublicKey Context */
44 INS_GET_PUBLIC_KEY = 2,
45 INS_GET_VERSION = 6,
46 P1_NON_CONFIRM = 0,
47 P1_CONFIRM = 1,
48 P2_NO_CHAINCODE = 0,
49 P2_CHAINCODE = 1,
50 /** Signing Context */
51 INS_SIGN_TRANSACTION = 4,
52 INS_SIGN_MESSAGE = 8,
53 P1_SINGLE = 128,
54 P1_FIRST = 0,
55 P1_MORE = 1,
56 P1_LAST = 129,
57 P2_SCHNORR_LEG = 80
58}
59/**
60 * Create and manage an Apdu payload instance for sending to a Ledger device.
61 *
62 * @example const response = await new Apdu(CLA, INS, P1, P2, Payload).send(this.transport);
63 */
64export declare class Apdu {
65 readonly cla: number;
66 readonly ins: number;
67 readonly p1: number;
68 readonly p2: number;
69 private readonly _payload;
70 private readonly CHUNK_MAX;
71 private readonly CHUNK_SIZE;
72 private readonly PAYLOAD_MAX;
73 /**
74 * Construct an Apdu instance.
75 *
76 * @param {number} cla a class byte
77 * @param {number} ins an instruction byte
78 * @param {number} p1 a parameter-1 byte
79 * @param {number} p2 a parameter-2 byte
80 * @param {Buffer} payload an optional payload
81 * @throws {PayloadLengthError} if the payload is too big to be processed
82 */
83 constructor(cla: number, ins: number, p1: number, p2: number, payload?: Buffer);
84 /**
85 * Send a large Apdu payload in chunks for handling by a Ledger device.
86 *
87 * @param {LedgerTransport} transport the transport instance over which the apdu call is sent
88 * @returns {Promise<Buffer>} the apdu response, e.g. from a Ledger device
89 */
90 send(transport: LedgerTransport): Promise<Buffer>;
91 /**
92 * Split the Apdu Payload into a Chunked Array.
93 *
94 * @param {Buffer} payload the bytes to be chunked
95 * @param {number} chunkSize the element size by which to split the payload
96 * @returns {Buffer[]} the chunked payload of an Apdu instance
97 */
98 protected getChunks(payload: Buffer, chunkSize: number): Buffer[];
99 /**
100 * Get the Segment Flag (P1) of a given chunk.
101 *
102 * @param {Buffer} index the index of the current chunk
103 * @param {Buffer} length total length of the payload
104 * @returns {ApduFlag} which segment of a payload is being sent
105 */
106 private getChunkSegmentFlag;
107}
108export { ApduFlag as Flag };
109export { Apdu as Builder };