1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.Builder = exports.Flag = exports.Apdu = exports.ApduFlag = void 0;
|
23 | const TransportErrors = __importStar(require("./errors"));
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | var ApduFlag;
|
62 | (function (ApduFlag) {
|
63 |
|
64 | ApduFlag[ApduFlag["CLA"] = 224] = "CLA";
|
65 |
|
66 | ApduFlag[ApduFlag["INS_GET_PUBLIC_KEY"] = 2] = "INS_GET_PUBLIC_KEY";
|
67 | ApduFlag[ApduFlag["INS_GET_VERSION"] = 6] = "INS_GET_VERSION";
|
68 | ApduFlag[ApduFlag["P1_NON_CONFIRM"] = 0] = "P1_NON_CONFIRM";
|
69 | ApduFlag[ApduFlag["P1_CONFIRM"] = 1] = "P1_CONFIRM";
|
70 | ApduFlag[ApduFlag["P2_NO_CHAINCODE"] = 0] = "P2_NO_CHAINCODE";
|
71 | ApduFlag[ApduFlag["P2_CHAINCODE"] = 1] = "P2_CHAINCODE";
|
72 |
|
73 | ApduFlag[ApduFlag["INS_SIGN_TRANSACTION"] = 4] = "INS_SIGN_TRANSACTION";
|
74 | ApduFlag[ApduFlag["INS_SIGN_MESSAGE"] = 8] = "INS_SIGN_MESSAGE";
|
75 | ApduFlag[ApduFlag["P1_SINGLE"] = 128] = "P1_SINGLE";
|
76 | ApduFlag[ApduFlag["P1_FIRST"] = 0] = "P1_FIRST";
|
77 | ApduFlag[ApduFlag["P1_MORE"] = 1] = "P1_MORE";
|
78 | ApduFlag[ApduFlag["P1_LAST"] = 129] = "P1_LAST";
|
79 | ApduFlag[ApduFlag["P2_SCHNORR_LEG"] = 80] = "P2_SCHNORR_LEG";
|
80 | })(ApduFlag = exports.ApduFlag || (exports.ApduFlag = {}));
|
81 | exports.Flag = ApduFlag;
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | class Apdu {
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | constructor(cla, ins, p1, p2, payload = Buffer.alloc(0)) {
|
99 | Object.defineProperty(this, "cla", {
|
100 | enumerable: true,
|
101 | configurable: true,
|
102 | writable: true,
|
103 | value: void 0
|
104 | });
|
105 | Object.defineProperty(this, "ins", {
|
106 | enumerable: true,
|
107 | configurable: true,
|
108 | writable: true,
|
109 | value: void 0
|
110 | });
|
111 | Object.defineProperty(this, "p1", {
|
112 | enumerable: true,
|
113 | configurable: true,
|
114 | writable: true,
|
115 | value: void 0
|
116 | });
|
117 | Object.defineProperty(this, "p2", {
|
118 | enumerable: true,
|
119 | configurable: true,
|
120 | writable: true,
|
121 | value: void 0
|
122 | });
|
123 | Object.defineProperty(this, "_payload", {
|
124 | enumerable: true,
|
125 | configurable: true,
|
126 | writable: true,
|
127 | value: void 0
|
128 | });
|
129 | Object.defineProperty(this, "CHUNK_MAX", {
|
130 | enumerable: true,
|
131 | configurable: true,
|
132 | writable: true,
|
133 | value: 10
|
134 | });
|
135 | Object.defineProperty(this, "CHUNK_SIZE", {
|
136 | enumerable: true,
|
137 | configurable: true,
|
138 | writable: true,
|
139 | value: 255
|
140 | });
|
141 | Object.defineProperty(this, "PAYLOAD_MAX", {
|
142 | enumerable: true,
|
143 | configurable: true,
|
144 | writable: true,
|
145 | value: this.CHUNK_MAX * this.CHUNK_SIZE
|
146 | });
|
147 | if (payload && payload.length > this.PAYLOAD_MAX) {
|
148 | throw new TransportErrors.PayloadLengthError(payload.length, this.PAYLOAD_MAX);
|
149 | }
|
150 | this.cla = cla;
|
151 | this.ins = ins;
|
152 | this.p1 = p1;
|
153 | this.p2 = p2;
|
154 | this._payload = payload;
|
155 | }
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | async send(transport) {
|
163 | const chunks = this.getChunks(this._payload, this.CHUNK_SIZE);
|
164 | const promises = [];
|
165 | let index = 0;
|
166 | for (const chunk of chunks) {
|
167 | promises.push(await transport.send(this.cla, this.ins, this.getChunkSegmentFlag(index, chunks.length), this.p2, chunk));
|
168 | index += 1;
|
169 | }
|
170 | return Buffer.concat(promises.map((r) => r.slice(0, r.length - 2)));
|
171 | }
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | getChunks(payload, chunkSize) {
|
180 | return this._payload.length <= this.CHUNK_SIZE
|
181 | ? [this._payload]
|
182 | : Array.from({ length: Math.ceil(payload.length / chunkSize) }, (v, i) => payload.slice(i * chunkSize, i * chunkSize + chunkSize));
|
183 | }
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | getChunkSegmentFlag(index, length) {
|
192 |
|
193 | if (index > 0 && index < length - 1) {
|
194 |
|
195 | return ApduFlag.P1_MORE;
|
196 | }
|
197 | else if (index === length - 1 && length > 1) {
|
198 |
|
199 | return ApduFlag.P1_LAST;
|
200 | }
|
201 | else if (index === 0 && length > 1) {
|
202 |
|
203 | return ApduFlag.P1_FIRST;
|
204 | }
|
205 | else {
|
206 | return this.p1;
|
207 | }
|
208 | }
|
209 | }
|
210 | exports.Apdu = Apdu;
|
211 | exports.Builder = Apdu;
|
212 |
|
\ | No newline at end of file |