@ledgerhq/hw-app-exchange 0.1.0 → 0.1.1-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/lib/Exchange.js +97 -191
- package/lib/Exchange.js.map +1 -1
- package/lib/Exchange.test.js +54 -111
- package/lib/Exchange.test.js.map +1 -1
- package/lib-es/Exchange.js +92 -187
- package/lib-es/Exchange.js.map +1 -1
- package/lib-es/Exchange.test.js +50 -107
- package/lib-es/Exchange.test.js.map +1 -1
- package/package.json +4 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @ledgerhq/hw-app-exchange
|
|
2
|
+
|
|
3
|
+
## 0.1.1-next.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`87d2349fd8`](https://github.com/LedgerHQ/ledger-live/commit/87d2349fd835e5deb39d1ee8dfcf3539f4195472)]:
|
|
8
|
+
- @ledgerhq/errors@6.12.5-next.0
|
|
9
|
+
- @ledgerhq/hw-transport@6.28.3-next.0
|
package/lib/Exchange.js
CHANGED
|
@@ -8,57 +8,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
13
|
};
|
|
41
|
-
exports
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
(0, invariant_1
|
|
55
|
-
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const errors_1 = require("@ledgerhq/errors");
|
|
16
|
+
const invariant_1 = __importDefault(require("invariant"));
|
|
17
|
+
const START_NEW_TRANSACTION_COMMAND = 0x03;
|
|
18
|
+
const SET_PARTNER_KEY_COMMAND = 0x04;
|
|
19
|
+
const CHECK_PARTNER_COMMAND = 0x05;
|
|
20
|
+
const PROCESS_TRANSACTION_RESPONSE = 0x06;
|
|
21
|
+
const CHECK_TRANSACTION_SIGNATURE = 0x07;
|
|
22
|
+
const CHECK_PAYOUT_ADDRESS = 0x08;
|
|
23
|
+
const CHECK_ASSET_IN = 0x08;
|
|
24
|
+
const CHECK_REFUND_ADDRESS = 0x09;
|
|
25
|
+
const SIGN_COIN_TRANSACTION = 0x0a;
|
|
26
|
+
const maybeThrowProtocolError = (result) => {
|
|
27
|
+
(0, invariant_1.default)(result.length >= 2, "ExchangeTransport: Unexpected result length");
|
|
28
|
+
const resultCode = result.readUInt16BE(result.length - 2);
|
|
56
29
|
if (resultCode !== 0x9000) {
|
|
57
30
|
throw new errors_1.TransportStatusError(resultCode);
|
|
58
31
|
}
|
|
59
32
|
};
|
|
60
|
-
|
|
61
|
-
|
|
33
|
+
class Exchange {
|
|
34
|
+
constructor(transport, transactionType, transactionRate) {
|
|
62
35
|
this.allowedStatuses = [
|
|
63
36
|
0x9000, 0x6a80, 0x6a81, 0x6a82, 0x6a83, 0x6a84, 0x6a85, 0x6e00, 0x6d00,
|
|
64
37
|
0x9d1a,
|
|
@@ -67,159 +40,92 @@ var Exchange = /** @class */ (function () {
|
|
|
67
40
|
this.transactionRate = transactionRate || 0 /* RateTypes.Fixed */;
|
|
68
41
|
this.transport = transport;
|
|
69
42
|
}
|
|
70
|
-
|
|
71
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (this.transactionType === 1 /* ExchangeTypes.Sell */ ||
|
|
80
|
-
this.transactionType === 2 /* ExchangeTypes.Fund */) {
|
|
81
|
-
return [2 /*return*/, result.slice(0, 32).toString("base64")];
|
|
82
|
-
}
|
|
83
|
-
return [2 /*return*/, result.toString("ascii", 0, 10)];
|
|
84
|
-
}
|
|
85
|
-
});
|
|
43
|
+
startNewTransaction() {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
const result = yield this.transport.send(0xe0, START_NEW_TRANSACTION_COMMAND, this.transactionRate, this.transactionType, Buffer.alloc(0), this.allowedStatuses);
|
|
46
|
+
maybeThrowProtocolError(result);
|
|
47
|
+
if (this.transactionType === 1 /* ExchangeTypes.Sell */ ||
|
|
48
|
+
this.transactionType === 2 /* ExchangeTypes.Fund */) {
|
|
49
|
+
return result.slice(0, 32).toString("base64");
|
|
50
|
+
}
|
|
51
|
+
return result.toString("ascii", 0, 10);
|
|
86
52
|
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
switch (_a.label) {
|
|
93
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, SET_PARTNER_KEY_COMMAND, this.transactionRate, this.transactionType, partnerNameAndPublicKey, this.allowedStatuses)];
|
|
94
|
-
case 1:
|
|
95
|
-
result = _a.sent();
|
|
96
|
-
maybeThrowProtocolError(result);
|
|
97
|
-
return [2 /*return*/];
|
|
98
|
-
}
|
|
99
|
-
});
|
|
53
|
+
}
|
|
54
|
+
setPartnerKey(partnerNameAndPublicKey) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const result = yield this.transport.send(0xe0, SET_PARTNER_KEY_COMMAND, this.transactionRate, this.transactionType, partnerNameAndPublicKey, this.allowedStatuses);
|
|
57
|
+
maybeThrowProtocolError(result);
|
|
100
58
|
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
switch (_a.label) {
|
|
107
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, CHECK_PARTNER_COMMAND, this.transactionRate, this.transactionType, signatureOfPartnerData, this.allowedStatuses)];
|
|
108
|
-
case 1:
|
|
109
|
-
result = _a.sent();
|
|
110
|
-
maybeThrowProtocolError(result);
|
|
111
|
-
return [2 /*return*/];
|
|
112
|
-
}
|
|
113
|
-
});
|
|
59
|
+
}
|
|
60
|
+
checkPartner(signatureOfPartnerData) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
const result = yield this.transport.send(0xe0, CHECK_PARTNER_COMMAND, this.transactionRate, this.transactionType, signatureOfPartnerData, this.allowedStatuses);
|
|
63
|
+
maybeThrowProtocolError(result);
|
|
114
64
|
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
feeHex,
|
|
130
|
-
]);
|
|
131
|
-
return [4 /*yield*/, this.transport.send(0xe0, PROCESS_TRANSACTION_RESPONSE, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses)];
|
|
132
|
-
case 1:
|
|
133
|
-
result = _a.sent();
|
|
134
|
-
maybeThrowProtocolError(result);
|
|
135
|
-
return [2 /*return*/];
|
|
136
|
-
}
|
|
137
|
-
});
|
|
65
|
+
}
|
|
66
|
+
processTransaction(transaction, fee) {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
let hex = fee.toString(16);
|
|
69
|
+
hex = hex.padStart(hex.length + (hex.length % 2), "0");
|
|
70
|
+
const feeHex = Buffer.from(hex, "hex");
|
|
71
|
+
const bufferToSend = Buffer.concat([
|
|
72
|
+
Buffer.from([transaction.length]),
|
|
73
|
+
transaction,
|
|
74
|
+
Buffer.from([feeHex.length]),
|
|
75
|
+
feeHex,
|
|
76
|
+
]);
|
|
77
|
+
const result = yield this.transport.send(0xe0, PROCESS_TRANSACTION_RESPONSE, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses);
|
|
78
|
+
maybeThrowProtocolError(result);
|
|
138
79
|
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
switch (_a.label) {
|
|
145
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, CHECK_TRANSACTION_SIGNATURE, this.transactionRate, this.transactionType, transactionSignature, this.allowedStatuses)];
|
|
146
|
-
case 1:
|
|
147
|
-
result = _a.sent();
|
|
148
|
-
maybeThrowProtocolError(result);
|
|
149
|
-
return [2 /*return*/];
|
|
150
|
-
}
|
|
151
|
-
});
|
|
80
|
+
}
|
|
81
|
+
checkTransactionSignature(transactionSignature) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
const result = yield this.transport.send(0xe0, CHECK_TRANSACTION_SIGNATURE, this.transactionRate, this.transactionType, transactionSignature, this.allowedStatuses);
|
|
84
|
+
maybeThrowProtocolError(result);
|
|
152
85
|
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return [4 /*yield*/, this.transport.send(0xe0, this.transactionType === 0 /* ExchangeTypes.Swap */
|
|
172
|
-
? CHECK_PAYOUT_ADDRESS
|
|
173
|
-
: CHECK_ASSET_IN, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses)];
|
|
174
|
-
case 1:
|
|
175
|
-
result = _a.sent();
|
|
176
|
-
maybeThrowProtocolError(result);
|
|
177
|
-
return [2 /*return*/];
|
|
178
|
-
}
|
|
179
|
-
});
|
|
86
|
+
}
|
|
87
|
+
checkPayoutAddress(payoutCurrencyConfig, currencyConfigSignature, addressParameters) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
(0, invariant_1.default)(payoutCurrencyConfig.length <= 255, "Currency config is too big");
|
|
90
|
+
(0, invariant_1.default)(addressParameters.length <= 255, "Address parameter is too big.");
|
|
91
|
+
(0, invariant_1.default)(currencyConfigSignature.length >= 67 &&
|
|
92
|
+
currencyConfigSignature.length <= 73, "Signature should be DER serialized and have length in [67, 73] bytes.");
|
|
93
|
+
const bufferToSend = Buffer.concat([
|
|
94
|
+
Buffer.from([payoutCurrencyConfig.length]),
|
|
95
|
+
payoutCurrencyConfig,
|
|
96
|
+
currencyConfigSignature,
|
|
97
|
+
Buffer.from([addressParameters.length]),
|
|
98
|
+
addressParameters,
|
|
99
|
+
]);
|
|
100
|
+
const result = yield this.transport.send(0xe0, this.transactionType === 0 /* ExchangeTypes.Swap */
|
|
101
|
+
? CHECK_PAYOUT_ADDRESS
|
|
102
|
+
: CHECK_ASSET_IN, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses);
|
|
103
|
+
maybeThrowProtocolError(result);
|
|
180
104
|
});
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
addressParameters,
|
|
198
|
-
]);
|
|
199
|
-
return [4 /*yield*/, this.transport.send(0xe0, CHECK_REFUND_ADDRESS, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses)];
|
|
200
|
-
case 1:
|
|
201
|
-
result = _a.sent();
|
|
202
|
-
maybeThrowProtocolError(result);
|
|
203
|
-
return [2 /*return*/];
|
|
204
|
-
}
|
|
205
|
-
});
|
|
105
|
+
}
|
|
106
|
+
checkRefundAddress(refundCurrencyConfig, currencyConfigSignature, addressParameters) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
(0, invariant_1.default)(refundCurrencyConfig.length <= 255, "Currency config is too big");
|
|
109
|
+
(0, invariant_1.default)(addressParameters.length <= 255, "Address parameter is too big.");
|
|
110
|
+
(0, invariant_1.default)(currencyConfigSignature.length >= 67 &&
|
|
111
|
+
currencyConfigSignature.length <= 73, "Signature should be DER serialized and have length in [67, 73] bytes.");
|
|
112
|
+
const bufferToSend = Buffer.concat([
|
|
113
|
+
Buffer.from([refundCurrencyConfig.length]),
|
|
114
|
+
refundCurrencyConfig,
|
|
115
|
+
currencyConfigSignature,
|
|
116
|
+
Buffer.from([addressParameters.length]),
|
|
117
|
+
addressParameters,
|
|
118
|
+
]);
|
|
119
|
+
const result = yield this.transport.send(0xe0, CHECK_REFUND_ADDRESS, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses);
|
|
120
|
+
maybeThrowProtocolError(result);
|
|
206
121
|
});
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
switch (_a.label) {
|
|
213
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, SIGN_COIN_TRANSACTION, this.transactionRate, this.transactionType, Buffer.alloc(0), this.allowedStatuses)];
|
|
214
|
-
case 1:
|
|
215
|
-
result = _a.sent();
|
|
216
|
-
maybeThrowProtocolError(result);
|
|
217
|
-
return [2 /*return*/];
|
|
218
|
-
}
|
|
219
|
-
});
|
|
122
|
+
}
|
|
123
|
+
signCoinTransaction() {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
const result = yield this.transport.send(0xe0, SIGN_COIN_TRANSACTION, this.transactionRate, this.transactionType, Buffer.alloc(0), this.allowedStatuses);
|
|
126
|
+
maybeThrowProtocolError(result);
|
|
220
127
|
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
exports["default"] = Exchange;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.default = Exchange;
|
|
225
131
|
//# sourceMappingURL=Exchange.js.map
|
package/lib/Exchange.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Exchange.js","sourceRoot":"","sources":["../src/Exchange.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Exchange.js","sourceRoot":"","sources":["../src/Exchange.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,6CAAwD;AACxD,0DAAkC;AAalC,MAAM,6BAA6B,GAAG,IAAI,CAAC;AAC3C,MAAM,uBAAuB,GAAG,IAAI,CAAC;AACrC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,4BAA4B,GAAG,IAAI,CAAC;AAC1C,MAAM,2BAA2B,GAAG,IAAI,CAAC;AACzC,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAEnC,MAAM,uBAAuB,GAAG,CAAC,MAAc,EAAQ,EAAE;IACvD,IAAA,mBAAS,EAAC,MAAM,CAAC,MAAM,IAAI,CAAC,EAAE,6CAA6C,CAAC,CAAC;IAC7E,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAE1D,IAAI,UAAU,KAAK,MAAM,EAAE;QACzB,MAAM,IAAI,6BAAoB,CAAC,UAAU,CAAC,CAAC;KAC5C;AACH,CAAC,CAAC;AAEF,MAAqB,QAAQ;IAS3B,YACE,SAAoB,EACpB,eAA8B,EAC9B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AM,IAAI,EAAE,EACtC,uEAAuE,CACxE,CAAC;YACF,MAAM,YAAY,GAAW,MAAM,CAAC,MAAM,CAAC;gBACzC,MAAM,CAAC,IAAI,CAAC,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;gBAC1C,oBAAoB;gBACpB,uBAAuB;gBACvB,MAAM,CAAC,IAAI,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;gBACvC,iBAAiB;aAClB,CAAC,CAAC;YACH,MAAM,MAAM,GAAW,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC9C,IAAI,EACJ,oBAAoB,EACpB,IAAI,CAAC,eAAe,EACpB,IAAI,CAAC,eAAe,EACpB,YAAY,EACZ,IAAI,CAAC,eAAe,CACrB,CAAC;YACF,uBAAuB,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC;KAAA;IAEK,mBAAmB;;YACvB,MAAM,MAAM,GAAW,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC9C,IAAI,EACJ,qBAAqB,EACrB,IAAI,CAAC,eAAe,EACpB,IAAI,CAAC,eAAe,EACpB,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EACf,IAAI,CAAC,eAAe,CACrB,CAAC;YACF,uBAAuB,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC;KAAA;CACF;AA1KD,2BA0KC"}
|
package/lib/Exchange.test.js
CHANGED
|
@@ -8,123 +8,66 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
13
|
};
|
|
41
|
-
exports
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
describe("Contrustructor",
|
|
46
|
-
[2 /* ExchangeTypes.Fund */, 1 /* ExchangeTypes.Sell */, 0 /* ExchangeTypes.Swap */].forEach(
|
|
47
|
-
it(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
}); });
|
|
60
|
-
[0 /* RateTypes.Fixed */, 1 /* RateTypes.Floating */].forEach(function (rateType) {
|
|
61
|
-
it("Exchange (value of ".concat(exchangeType, ") init with rate types of value ").concat(rateType), function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
62
|
-
var transport, exchange;
|
|
63
|
-
return __generator(this, function (_a) {
|
|
64
|
-
switch (_a.label) {
|
|
65
|
-
case 0: return [4 /*yield*/, (0, hw_transport_mocker_1.openTransportReplayer)(hw_transport_mocker_1.RecordStore.fromString(""))];
|
|
66
|
-
case 1:
|
|
67
|
-
transport = _a.sent();
|
|
68
|
-
exchange = new Exchange_1["default"](transport, exchangeType, rateType);
|
|
69
|
-
expect(exchange.transactionRate).toBe(rateType);
|
|
70
|
-
return [2 /*return*/];
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
}); });
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const hw_transport_mocker_1 = require("@ledgerhq/hw-transport-mocker");
|
|
16
|
+
const Exchange_1 = __importDefault(require("./Exchange"));
|
|
17
|
+
const util_1 = require("util");
|
|
18
|
+
describe("Contrustructor", () => {
|
|
19
|
+
[2 /* ExchangeTypes.Fund */, 1 /* ExchangeTypes.Sell */, 0 /* ExchangeTypes.Swap */].forEach((exchangeType) => {
|
|
20
|
+
it(`Exchange (value of ${exchangeType}) init with default rate types of fixed`, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
const transport = yield (0, hw_transport_mocker_1.openTransportReplayer)(hw_transport_mocker_1.RecordStore.fromString(""));
|
|
22
|
+
const exchange = new Exchange_1.default(transport, exchangeType);
|
|
23
|
+
expect(exchange.transactionRate).toBe(0 /* RateTypes.Fixed */);
|
|
24
|
+
}));
|
|
25
|
+
[0 /* RateTypes.Fixed */, 1 /* RateTypes.Floating */].forEach((rateType) => {
|
|
26
|
+
it(`Exchange (value of ${exchangeType}) init with rate types of value ${rateType}`, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
const transport = yield (0, hw_transport_mocker_1.openTransportReplayer)(hw_transport_mocker_1.RecordStore.fromString(""));
|
|
28
|
+
const exchange = new Exchange_1.default(transport, exchangeType, rateType);
|
|
29
|
+
expect(exchange.transactionRate).toBe(rateType);
|
|
30
|
+
}));
|
|
74
31
|
});
|
|
75
32
|
});
|
|
76
33
|
});
|
|
77
|
-
describe("startNewTransaction",
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
it("sends a correct sequence of APDU",
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
expect(recordStore.queue[0][0]).toBe(expectCommand);
|
|
103
|
-
expect(result).toEqual(mockResponse);
|
|
104
|
-
return [2 /*return*/];
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
}); });
|
|
34
|
+
describe("startNewTransaction", () => {
|
|
35
|
+
const textEncoder = new util_1.TextEncoder();
|
|
36
|
+
const mockResponse = "2ac38f187c"; // Response of length 10
|
|
37
|
+
it("sends a correct sequence of APDU", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
+
// Given
|
|
39
|
+
const recordStore = new hw_transport_mocker_1.RecordStore();
|
|
40
|
+
const mockTransport = new hw_transport_mocker_1.MockTransport(Buffer.concat([
|
|
41
|
+
textEncoder.encode(mockResponse),
|
|
42
|
+
Buffer.from([0x90, 0x00]), // StatusCodes.OK
|
|
43
|
+
]));
|
|
44
|
+
const transport = (0, hw_transport_mocker_1.createTransportRecorder)(mockTransport, recordStore);
|
|
45
|
+
const exchange = new Exchange_1.default(new transport(mockTransport), 0 /* ExchangeTypes.Swap */);
|
|
46
|
+
// When
|
|
47
|
+
const result = yield exchange.startNewTransaction();
|
|
48
|
+
// Then
|
|
49
|
+
const expectCommand = Buffer.from([
|
|
50
|
+
0xe0,
|
|
51
|
+
0x03,
|
|
52
|
+
0 /* ExchangeTypes.Swap */,
|
|
53
|
+
0 /* RateTypes.Fixed */,
|
|
54
|
+
0x00, // Data
|
|
55
|
+
]).toString("hex");
|
|
56
|
+
expect(recordStore.queue[0][0]).toBe(expectCommand);
|
|
57
|
+
expect(result).toEqual(mockResponse);
|
|
58
|
+
}));
|
|
108
59
|
// TOFIX: TransportError is not recognize as an Error type
|
|
109
|
-
xit("throws an error if status is not ok",
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
switch (_a.label) {
|
|
122
|
-
case 0: return [4 /*yield*/, exchange.startNewTransaction()];
|
|
123
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
124
|
-
}
|
|
125
|
-
}); }); }).toThrowError();
|
|
126
|
-
return [2 /*return*/];
|
|
127
|
-
});
|
|
128
|
-
}); });
|
|
60
|
+
xit("throws an error if status is not ok", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
// Given
|
|
62
|
+
const recordStore = new hw_transport_mocker_1.RecordStore();
|
|
63
|
+
const mockTransport = new hw_transport_mocker_1.MockTransport(Buffer.concat([
|
|
64
|
+
textEncoder.encode(mockResponse),
|
|
65
|
+
Buffer.from([0x6a, 0x80]),
|
|
66
|
+
]));
|
|
67
|
+
const transport = (0, hw_transport_mocker_1.createTransportRecorder)(mockTransport, recordStore);
|
|
68
|
+
const exchange = new Exchange_1.default(new transport(mockTransport), 0 /* ExchangeTypes.Swap */);
|
|
69
|
+
// When & Then
|
|
70
|
+
expect(() => __awaiter(void 0, void 0, void 0, function* () { return yield exchange.startNewTransaction(); })).toThrowError();
|
|
71
|
+
}));
|
|
129
72
|
});
|
|
130
73
|
//# sourceMappingURL=Exchange.test.js.map
|
package/lib/Exchange.test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Exchange.test.js","sourceRoot":"","sources":["../src/Exchange.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Exchange.test.js","sourceRoot":"","sources":["../src/Exchange.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,uEAKuC;AACvC,0DAAgE;AAChE,+BAAmC;AAEnC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC9B,oFAA4D,CAAC,OAAO,CAClE,CAAC,YAAY,EAAE,EAAE;QACf,EAAE,CAAC,sBAAsB,YAAY,yCAAyC,EAAE,GAAS,EAAE;YACzF,MAAM,SAAS,GAAG,MAAM,IAAA,2CAAqB,EAC3C,iCAAW,CAAC,UAAU,CAAC,EAAE,CAAC,CAC3B,CAAC;YACF,MAAM,QAAQ,GAAG,IAAI,kBAAQ,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;YACvD,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,IAAI,yBAAiB,CAAC;QACzD,CAAC,CAAA,CAAC,CAAC;QAEH,qDAAqC,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YACzD,EAAE,CAAC,sBAAsB,YAAY,mCAAmC,QAAQ,EAAE,EAAE,GAAS,EAAE;gBAC7F,MAAM,SAAS,GAAG,MAAM,IAAA,2CAAqB,EAC3C,iCAAW,CAAC,UAAU,CAAC,EAAE,CAAC,CAC3B,CAAC;gBACF,MAAM,QAAQ,GAAG,IAAI,kBAAQ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AM,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IACvC,CAAC,CAAA,CAAC,CAAC;IAEH,0DAA0D;IAC1D,GAAG,CAAC,qCAAqC,EAAE,GAAS,EAAE;QACpD,QAAQ;QACR,MAAM,WAAW,GAAG,IAAI,iCAAW,EAAE,CAAC;QACtC,MAAM,aAAa,GAAG,IAAI,mCAAa,CACrC,MAAM,CAAC,MAAM,CAAC;YACZ,WAAW,CAAC,MAAM,CAAC,YAAY,CAAC;YAChC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;SAC1B,CAAC,CACH,CAAC;QACF,MAAM,SAAS,GAAG,IAAA,6CAAuB,EAAC,aAAa,EAAE,WAAW,CAAC,CAAC;QACtE,MAAM,QAAQ,GAAG,IAAI,kBAAQ,CAC3B,IAAI,SAAS,CAAC,aAAa,CAAC,6BAE7B,CAAC;QAEF,cAAc;QACd,MAAM,CAAC,GAAS,EAAE,kDAAC,OAAA,MAAM,QAAQ,CAAC,mBAAmB,EAAE,CAAA,GAAA,CAAC,CAAC,YAAY,EAAE,CAAC;IAC1E,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/lib-es/Exchange.js
CHANGED
|
@@ -7,53 +7,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
10
|
import { TransportStatusError } from "@ledgerhq/errors";
|
|
38
11
|
import invariant from "invariant";
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
12
|
+
const START_NEW_TRANSACTION_COMMAND = 0x03;
|
|
13
|
+
const SET_PARTNER_KEY_COMMAND = 0x04;
|
|
14
|
+
const CHECK_PARTNER_COMMAND = 0x05;
|
|
15
|
+
const PROCESS_TRANSACTION_RESPONSE = 0x06;
|
|
16
|
+
const CHECK_TRANSACTION_SIGNATURE = 0x07;
|
|
17
|
+
const CHECK_PAYOUT_ADDRESS = 0x08;
|
|
18
|
+
const CHECK_ASSET_IN = 0x08;
|
|
19
|
+
const CHECK_REFUND_ADDRESS = 0x09;
|
|
20
|
+
const SIGN_COIN_TRANSACTION = 0x0a;
|
|
21
|
+
const maybeThrowProtocolError = (result) => {
|
|
49
22
|
invariant(result.length >= 2, "ExchangeTransport: Unexpected result length");
|
|
50
|
-
|
|
23
|
+
const resultCode = result.readUInt16BE(result.length - 2);
|
|
51
24
|
if (resultCode !== 0x9000) {
|
|
52
25
|
throw new TransportStatusError(resultCode);
|
|
53
26
|
}
|
|
54
27
|
};
|
|
55
|
-
|
|
56
|
-
|
|
28
|
+
export default class Exchange {
|
|
29
|
+
constructor(transport, transactionType, transactionRate) {
|
|
57
30
|
this.allowedStatuses = [
|
|
58
31
|
0x9000, 0x6a80, 0x6a81, 0x6a82, 0x6a83, 0x6a84, 0x6a85, 0x6e00, 0x6d00,
|
|
59
32
|
0x9d1a,
|
|
@@ -62,159 +35,91 @@ var Exchange = /** @class */ (function () {
|
|
|
62
35
|
this.transactionRate = transactionRate || 0 /* RateTypes.Fixed */;
|
|
63
36
|
this.transport = transport;
|
|
64
37
|
}
|
|
65
|
-
|
|
66
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (this.transactionType === 1 /* ExchangeTypes.Sell */ ||
|
|
75
|
-
this.transactionType === 2 /* ExchangeTypes.Fund */) {
|
|
76
|
-
return [2 /*return*/, result.slice(0, 32).toString("base64")];
|
|
77
|
-
}
|
|
78
|
-
return [2 /*return*/, result.toString("ascii", 0, 10)];
|
|
79
|
-
}
|
|
80
|
-
});
|
|
38
|
+
startNewTransaction() {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
const result = yield this.transport.send(0xe0, START_NEW_TRANSACTION_COMMAND, this.transactionRate, this.transactionType, Buffer.alloc(0), this.allowedStatuses);
|
|
41
|
+
maybeThrowProtocolError(result);
|
|
42
|
+
if (this.transactionType === 1 /* ExchangeTypes.Sell */ ||
|
|
43
|
+
this.transactionType === 2 /* ExchangeTypes.Fund */) {
|
|
44
|
+
return result.slice(0, 32).toString("base64");
|
|
45
|
+
}
|
|
46
|
+
return result.toString("ascii", 0, 10);
|
|
81
47
|
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
switch (_a.label) {
|
|
88
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, SET_PARTNER_KEY_COMMAND, this.transactionRate, this.transactionType, partnerNameAndPublicKey, this.allowedStatuses)];
|
|
89
|
-
case 1:
|
|
90
|
-
result = _a.sent();
|
|
91
|
-
maybeThrowProtocolError(result);
|
|
92
|
-
return [2 /*return*/];
|
|
93
|
-
}
|
|
94
|
-
});
|
|
48
|
+
}
|
|
49
|
+
setPartnerKey(partnerNameAndPublicKey) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const result = yield this.transport.send(0xe0, SET_PARTNER_KEY_COMMAND, this.transactionRate, this.transactionType, partnerNameAndPublicKey, this.allowedStatuses);
|
|
52
|
+
maybeThrowProtocolError(result);
|
|
95
53
|
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
switch (_a.label) {
|
|
102
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, CHECK_PARTNER_COMMAND, this.transactionRate, this.transactionType, signatureOfPartnerData, this.allowedStatuses)];
|
|
103
|
-
case 1:
|
|
104
|
-
result = _a.sent();
|
|
105
|
-
maybeThrowProtocolError(result);
|
|
106
|
-
return [2 /*return*/];
|
|
107
|
-
}
|
|
108
|
-
});
|
|
54
|
+
}
|
|
55
|
+
checkPartner(signatureOfPartnerData) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
const result = yield this.transport.send(0xe0, CHECK_PARTNER_COMMAND, this.transactionRate, this.transactionType, signatureOfPartnerData, this.allowedStatuses);
|
|
58
|
+
maybeThrowProtocolError(result);
|
|
109
59
|
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
feeHex,
|
|
125
|
-
]);
|
|
126
|
-
return [4 /*yield*/, this.transport.send(0xe0, PROCESS_TRANSACTION_RESPONSE, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses)];
|
|
127
|
-
case 1:
|
|
128
|
-
result = _a.sent();
|
|
129
|
-
maybeThrowProtocolError(result);
|
|
130
|
-
return [2 /*return*/];
|
|
131
|
-
}
|
|
132
|
-
});
|
|
60
|
+
}
|
|
61
|
+
processTransaction(transaction, fee) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
let hex = fee.toString(16);
|
|
64
|
+
hex = hex.padStart(hex.length + (hex.length % 2), "0");
|
|
65
|
+
const feeHex = Buffer.from(hex, "hex");
|
|
66
|
+
const bufferToSend = Buffer.concat([
|
|
67
|
+
Buffer.from([transaction.length]),
|
|
68
|
+
transaction,
|
|
69
|
+
Buffer.from([feeHex.length]),
|
|
70
|
+
feeHex,
|
|
71
|
+
]);
|
|
72
|
+
const result = yield this.transport.send(0xe0, PROCESS_TRANSACTION_RESPONSE, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses);
|
|
73
|
+
maybeThrowProtocolError(result);
|
|
133
74
|
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
switch (_a.label) {
|
|
140
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, CHECK_TRANSACTION_SIGNATURE, this.transactionRate, this.transactionType, transactionSignature, this.allowedStatuses)];
|
|
141
|
-
case 1:
|
|
142
|
-
result = _a.sent();
|
|
143
|
-
maybeThrowProtocolError(result);
|
|
144
|
-
return [2 /*return*/];
|
|
145
|
-
}
|
|
146
|
-
});
|
|
75
|
+
}
|
|
76
|
+
checkTransactionSignature(transactionSignature) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const result = yield this.transport.send(0xe0, CHECK_TRANSACTION_SIGNATURE, this.transactionRate, this.transactionType, transactionSignature, this.allowedStatuses);
|
|
79
|
+
maybeThrowProtocolError(result);
|
|
147
80
|
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return [4 /*yield*/, this.transport.send(0xe0, this.transactionType === 0 /* ExchangeTypes.Swap */
|
|
167
|
-
? CHECK_PAYOUT_ADDRESS
|
|
168
|
-
: CHECK_ASSET_IN, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses)];
|
|
169
|
-
case 1:
|
|
170
|
-
result = _a.sent();
|
|
171
|
-
maybeThrowProtocolError(result);
|
|
172
|
-
return [2 /*return*/];
|
|
173
|
-
}
|
|
174
|
-
});
|
|
81
|
+
}
|
|
82
|
+
checkPayoutAddress(payoutCurrencyConfig, currencyConfigSignature, addressParameters) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
invariant(payoutCurrencyConfig.length <= 255, "Currency config is too big");
|
|
85
|
+
invariant(addressParameters.length <= 255, "Address parameter is too big.");
|
|
86
|
+
invariant(currencyConfigSignature.length >= 67 &&
|
|
87
|
+
currencyConfigSignature.length <= 73, "Signature should be DER serialized and have length in [67, 73] bytes.");
|
|
88
|
+
const bufferToSend = Buffer.concat([
|
|
89
|
+
Buffer.from([payoutCurrencyConfig.length]),
|
|
90
|
+
payoutCurrencyConfig,
|
|
91
|
+
currencyConfigSignature,
|
|
92
|
+
Buffer.from([addressParameters.length]),
|
|
93
|
+
addressParameters,
|
|
94
|
+
]);
|
|
95
|
+
const result = yield this.transport.send(0xe0, this.transactionType === 0 /* ExchangeTypes.Swap */
|
|
96
|
+
? CHECK_PAYOUT_ADDRESS
|
|
97
|
+
: CHECK_ASSET_IN, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses);
|
|
98
|
+
maybeThrowProtocolError(result);
|
|
175
99
|
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
addressParameters,
|
|
193
|
-
]);
|
|
194
|
-
return [4 /*yield*/, this.transport.send(0xe0, CHECK_REFUND_ADDRESS, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses)];
|
|
195
|
-
case 1:
|
|
196
|
-
result = _a.sent();
|
|
197
|
-
maybeThrowProtocolError(result);
|
|
198
|
-
return [2 /*return*/];
|
|
199
|
-
}
|
|
200
|
-
});
|
|
100
|
+
}
|
|
101
|
+
checkRefundAddress(refundCurrencyConfig, currencyConfigSignature, addressParameters) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
invariant(refundCurrencyConfig.length <= 255, "Currency config is too big");
|
|
104
|
+
invariant(addressParameters.length <= 255, "Address parameter is too big.");
|
|
105
|
+
invariant(currencyConfigSignature.length >= 67 &&
|
|
106
|
+
currencyConfigSignature.length <= 73, "Signature should be DER serialized and have length in [67, 73] bytes.");
|
|
107
|
+
const bufferToSend = Buffer.concat([
|
|
108
|
+
Buffer.from([refundCurrencyConfig.length]),
|
|
109
|
+
refundCurrencyConfig,
|
|
110
|
+
currencyConfigSignature,
|
|
111
|
+
Buffer.from([addressParameters.length]),
|
|
112
|
+
addressParameters,
|
|
113
|
+
]);
|
|
114
|
+
const result = yield this.transport.send(0xe0, CHECK_REFUND_ADDRESS, this.transactionRate, this.transactionType, bufferToSend, this.allowedStatuses);
|
|
115
|
+
maybeThrowProtocolError(result);
|
|
201
116
|
});
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
switch (_a.label) {
|
|
208
|
-
case 0: return [4 /*yield*/, this.transport.send(0xe0, SIGN_COIN_TRANSACTION, this.transactionRate, this.transactionType, Buffer.alloc(0), this.allowedStatuses)];
|
|
209
|
-
case 1:
|
|
210
|
-
result = _a.sent();
|
|
211
|
-
maybeThrowProtocolError(result);
|
|
212
|
-
return [2 /*return*/];
|
|
213
|
-
}
|
|
214
|
-
});
|
|
117
|
+
}
|
|
118
|
+
signCoinTransaction() {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
const result = yield this.transport.send(0xe0, SIGN_COIN_TRANSACTION, this.transactionRate, this.transactionType, Buffer.alloc(0), this.allowedStatuses);
|
|
121
|
+
maybeThrowProtocolError(result);
|
|
215
122
|
});
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
}());
|
|
219
|
-
export default Exchange;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
220
125
|
//# sourceMappingURL=Exchange.js.map
|
package/lib-es/Exchange.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Exchange.js","sourceRoot":"","sources":["../src/Exchange.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Exchange.js","sourceRoot":"","sources":["../src/Exchange.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,SAAS,MAAM,WAAW,CAAC;AAalC,MAAM,6BAA6B,GAAG,IAAI,CAAC;AAC3C,MAAM,uBAAuB,GAAG,IAAI,CAAC;AACrC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,4BAA4B,GAAG,IAAI,CAAC;AAC1C,MAAM,2BAA2B,GAAG,IAAI,CAAC;AACzC,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAEnC,MAAM,uBAAuB,GAAG,CAAC,MAAc,EAAQ,EAAE;IACvD,SAAS,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,EAAE,6CAA6C,CAAC,CAAC;IAC7E,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAE1D,IAAI,UAAU,KAAK,MAAM,EAAE;QACzB,MAAM,IAAI,oBAAoB,CAAC,UAAU,CAAC,CAAC;KAC5C;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,QAAQ;IAS3B,YACE,SAAoB,EACpB,eAA8B,EAC9B,eAA2B;QAR7B,oBAAe,GAAkB;YAC/B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;YACtE,MAAM;SACP,CAAC;QAOA,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC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M,CAAC,CAAC;gBACvC,iBAAiB;aAClB,CAAC,CAAC;YACH,MAAM,MAAM,GAAW,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC9C,IAAI,EACJ,oBAAoB,EACpB,IAAI,CAAC,eAAe,EACpB,IAAI,CAAC,eAAe,EACpB,YAAY,EACZ,IAAI,CAAC,eAAe,CACrB,CAAC;YACF,uBAAuB,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC;KAAA;IAEK,mBAAmB;;YACvB,MAAM,MAAM,GAAW,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC9C,IAAI,EACJ,qBAAqB,EACrB,IAAI,CAAC,eAAe,EACpB,IAAI,CAAC,eAAe,EACpB,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EACf,IAAI,CAAC,eAAe,CACrB,CAAC;YACF,uBAAuB,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC;KAAA;CACF"}
|
package/lib-es/Exchange.test.js
CHANGED
|
@@ -7,119 +7,62 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
10
|
import { createTransportRecorder, MockTransport, openTransportReplayer, RecordStore, } from "@ledgerhq/hw-transport-mocker";
|
|
38
11
|
import Exchange from "./Exchange";
|
|
39
12
|
import { TextEncoder } from "util";
|
|
40
|
-
describe("Contrustructor",
|
|
41
|
-
[2 /* ExchangeTypes.Fund */, 1 /* ExchangeTypes.Sell */, 0 /* ExchangeTypes.Swap */].forEach(
|
|
42
|
-
it(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
}); });
|
|
55
|
-
[0 /* RateTypes.Fixed */, 1 /* RateTypes.Floating */].forEach(function (rateType) {
|
|
56
|
-
it("Exchange (value of ".concat(exchangeType, ") init with rate types of value ").concat(rateType), function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
57
|
-
var transport, exchange;
|
|
58
|
-
return __generator(this, function (_a) {
|
|
59
|
-
switch (_a.label) {
|
|
60
|
-
case 0: return [4 /*yield*/, openTransportReplayer(RecordStore.fromString(""))];
|
|
61
|
-
case 1:
|
|
62
|
-
transport = _a.sent();
|
|
63
|
-
exchange = new Exchange(transport, exchangeType, rateType);
|
|
64
|
-
expect(exchange.transactionRate).toBe(rateType);
|
|
65
|
-
return [2 /*return*/];
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}); });
|
|
13
|
+
describe("Contrustructor", () => {
|
|
14
|
+
[2 /* ExchangeTypes.Fund */, 1 /* ExchangeTypes.Sell */, 0 /* ExchangeTypes.Swap */].forEach((exchangeType) => {
|
|
15
|
+
it(`Exchange (value of ${exchangeType}) init with default rate types of fixed`, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const transport = yield openTransportReplayer(RecordStore.fromString(""));
|
|
17
|
+
const exchange = new Exchange(transport, exchangeType);
|
|
18
|
+
expect(exchange.transactionRate).toBe(0 /* RateTypes.Fixed */);
|
|
19
|
+
}));
|
|
20
|
+
[0 /* RateTypes.Fixed */, 1 /* RateTypes.Floating */].forEach((rateType) => {
|
|
21
|
+
it(`Exchange (value of ${exchangeType}) init with rate types of value ${rateType}`, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
const transport = yield openTransportReplayer(RecordStore.fromString(""));
|
|
23
|
+
const exchange = new Exchange(transport, exchangeType, rateType);
|
|
24
|
+
expect(exchange.transactionRate).toBe(rateType);
|
|
25
|
+
}));
|
|
69
26
|
});
|
|
70
27
|
});
|
|
71
28
|
});
|
|
72
|
-
describe("startNewTransaction",
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
it("sends a correct sequence of APDU",
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
expect(recordStore.queue[0][0]).toBe(expectCommand);
|
|
98
|
-
expect(result).toEqual(mockResponse);
|
|
99
|
-
return [2 /*return*/];
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
}); });
|
|
29
|
+
describe("startNewTransaction", () => {
|
|
30
|
+
const textEncoder = new TextEncoder();
|
|
31
|
+
const mockResponse = "2ac38f187c"; // Response of length 10
|
|
32
|
+
it("sends a correct sequence of APDU", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
+
// Given
|
|
34
|
+
const recordStore = new RecordStore();
|
|
35
|
+
const mockTransport = new MockTransport(Buffer.concat([
|
|
36
|
+
textEncoder.encode(mockResponse),
|
|
37
|
+
Buffer.from([0x90, 0x00]), // StatusCodes.OK
|
|
38
|
+
]));
|
|
39
|
+
const transport = createTransportRecorder(mockTransport, recordStore);
|
|
40
|
+
const exchange = new Exchange(new transport(mockTransport), 0 /* ExchangeTypes.Swap */);
|
|
41
|
+
// When
|
|
42
|
+
const result = yield exchange.startNewTransaction();
|
|
43
|
+
// Then
|
|
44
|
+
const expectCommand = Buffer.from([
|
|
45
|
+
0xe0,
|
|
46
|
+
0x03,
|
|
47
|
+
0 /* ExchangeTypes.Swap */,
|
|
48
|
+
0 /* RateTypes.Fixed */,
|
|
49
|
+
0x00, // Data
|
|
50
|
+
]).toString("hex");
|
|
51
|
+
expect(recordStore.queue[0][0]).toBe(expectCommand);
|
|
52
|
+
expect(result).toEqual(mockResponse);
|
|
53
|
+
}));
|
|
103
54
|
// TOFIX: TransportError is not recognize as an Error type
|
|
104
|
-
xit("throws an error if status is not ok",
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
switch (_a.label) {
|
|
117
|
-
case 0: return [4 /*yield*/, exchange.startNewTransaction()];
|
|
118
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
119
|
-
}
|
|
120
|
-
}); }); }).toThrowError();
|
|
121
|
-
return [2 /*return*/];
|
|
122
|
-
});
|
|
123
|
-
}); });
|
|
55
|
+
xit("throws an error if status is not ok", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
+
// Given
|
|
57
|
+
const recordStore = new RecordStore();
|
|
58
|
+
const mockTransport = new MockTransport(Buffer.concat([
|
|
59
|
+
textEncoder.encode(mockResponse),
|
|
60
|
+
Buffer.from([0x6a, 0x80]),
|
|
61
|
+
]));
|
|
62
|
+
const transport = createTransportRecorder(mockTransport, recordStore);
|
|
63
|
+
const exchange = new Exchange(new transport(mockTransport), 0 /* ExchangeTypes.Swap */);
|
|
64
|
+
// When & Then
|
|
65
|
+
expect(() => __awaiter(void 0, void 0, void 0, function* () { return yield exchange.startNewTransaction(); })).toThrowError();
|
|
66
|
+
}));
|
|
124
67
|
});
|
|
125
68
|
//# sourceMappingURL=Exchange.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Exchange.test.js","sourceRoot":"","sources":["../src/Exchange.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Exchange.test.js","sourceRoot":"","sources":["../src/Exchange.test.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACL,uBAAuB,EACvB,aAAa,EACb,qBAAqB,EACrB,WAAW,GACZ,MAAM,+BAA+B,CAAC;AACvC,OAAO,QAAsC,MAAM,YAAY,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAEnC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IA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AC,OAAA,MAAM,QAAQ,CAAC,mBAAmB,EAAE,CAAA,GAAA,CAAC,CAAC,YAAY,EAAE,CAAC;IAC1E,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ledgerhq/hw-app-exchange",
|
|
3
|
-
"version": "0.1.0",
|
|
3
|
+
"version": "0.1.1-next.0",
|
|
4
4
|
"description": "Ledger Hardware Wallet Cosmos Application API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ledger",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"bignumber.js": "^9.1.0",
|
|
29
29
|
"invariant": "^2.2.2",
|
|
30
|
-
"@ledgerhq/errors": "^6.12.
|
|
31
|
-
"@ledgerhq/hw-transport": "^6.28.
|
|
30
|
+
"@ledgerhq/errors": "^6.12.5-next.0",
|
|
31
|
+
"@ledgerhq/hw-transport": "^6.28.3-next.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/jest": "^29.5.0",
|
|
@@ -37,8 +37,7 @@
|
|
|
37
37
|
"jest": "^28.1.1",
|
|
38
38
|
"ts-jest": "^28.0.5",
|
|
39
39
|
"ts-node": "^10.4.0",
|
|
40
|
-
"
|
|
41
|
-
"@ledgerhq/hw-transport-mocker": "^6.27.13-nightly.2"
|
|
40
|
+
"@ledgerhq/hw-transport-mocker": "^6.27.14-next.0"
|
|
42
41
|
},
|
|
43
42
|
"scripts": {
|
|
44
43
|
"clean": "rimraf lib lib-es",
|