@airgap/icp 0.13.11-beta.5 → 0.13.11-beta.6

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.
Files changed (46) hide show
  1. package/package.json +3 -2
  2. package/v1/index.d.ts +3 -2
  3. package/v1/index.js +4 -1
  4. package/v1/index.js.map +1 -1
  5. package/v1/module/ICPModule.d.ts +2 -3
  6. package/v1/module/ICPModule.js +22 -10
  7. package/v1/module/ICPModule.js.map +1 -1
  8. package/v1/protocol/ICPImplementation.d.ts +12 -4
  9. package/v1/protocol/ICPImplementation.js +87 -55
  10. package/v1/protocol/ICPImplementation.js.map +1 -1
  11. package/v1/protocol/ICPProtocol.d.ts +4 -1
  12. package/v1/protocol/ICPProtocol.js +24 -17
  13. package/v1/protocol/ICPProtocol.js.map +1 -1
  14. package/v1/protocol/icrc/CkBTCProtocol.d.ts +16 -0
  15. package/v1/protocol/icrc/CkBTCProtocol.js +367 -0
  16. package/v1/protocol/icrc/CkBTCProtocol.js.map +1 -0
  17. package/v1/protocol/icrc/ICRC1Protocol.d.ts +91 -0
  18. package/v1/protocol/icrc/ICRC1Protocol.js +699 -0
  19. package/v1/protocol/icrc/ICRC1Protocol.js.map +1 -0
  20. package/v1/serializer/v3/serializer-companion.js +16 -0
  21. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  22. package/v1/types/ckbtc/metadata.d.ts +4 -0
  23. package/v1/types/ckbtc/metadata.js +3 -0
  24. package/v1/types/ckbtc/metadata.js.map +1 -0
  25. package/v1/types/icrc/account.d.ts +4 -0
  26. package/v1/types/icrc/account.js +3 -0
  27. package/v1/types/icrc/account.js.map +1 -0
  28. package/v1/types/icrc/ledger.d.ts +15 -0
  29. package/v1/types/icrc/ledger.js +92 -0
  30. package/v1/types/icrc/ledger.js.map +1 -0
  31. package/v1/types/icrc/metadata.d.ts +6 -0
  32. package/v1/types/icrc/metadata.js +3 -0
  33. package/v1/types/icrc/metadata.js.map +1 -0
  34. package/v1/types/icrc/transfer.d.ts +9 -0
  35. package/v1/types/icrc/transfer.js +3 -0
  36. package/v1/types/icrc/transfer.js.map +1 -0
  37. package/v1/types/ledger.d.ts +0 -1
  38. package/v1/types/ledger.js +1 -2
  39. package/v1/types/ledger.js.map +1 -1
  40. package/v1/types/protocol.d.ts +21 -1
  41. package/v1/utils/convert.d.ts +3 -1
  42. package/v1/utils/convert.js +12 -1
  43. package/v1/utils/convert.js.map +1 -1
  44. package/v1/utils/icrc1.d.ts +20 -0
  45. package/v1/utils/icrc1.js +171 -0
  46. package/v1/utils/icrc1.js.map +1 -0
@@ -0,0 +1,699 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
+ return new (P || (P = Promise))(function (resolve, reject) {
39
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ };
45
+ var __generator = (this && this.__generator) || function (thisArg, body) {
46
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
+ function verb(n) { return function (v) { return step([n, v]); }; }
49
+ function step(op) {
50
+ if (f) throw new TypeError("Generator is already executing.");
51
+ while (_) try {
52
+ 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;
53
+ if (y = 0, t) op = [op[0] & 2, t.value];
54
+ switch (op[0]) {
55
+ case 0: case 1: t = op; break;
56
+ case 4: _.label++; return { value: op[1], done: false };
57
+ case 5: _.label++; y = op[1]; op = [0]; continue;
58
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
+ default:
60
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
+ if (t[2]) _.ops.pop();
65
+ _.trys.pop(); continue;
66
+ }
67
+ op = body.call(thisArg, _);
68
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
+ }
71
+ };
72
+ var __read = (this && this.__read) || function (o, n) {
73
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
74
+ if (!m) return o;
75
+ var i = m.call(o), r, ar = [], e;
76
+ try {
77
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
78
+ }
79
+ catch (error) { e = { error: error }; }
80
+ finally {
81
+ try {
82
+ if (r && !r.done && (m = i["return"])) m.call(i);
83
+ }
84
+ finally { if (e) throw e.error; }
85
+ }
86
+ return ar;
87
+ };
88
+ var __importDefault = (this && this.__importDefault) || function (mod) {
89
+ return (mod && mod.__esModule) ? mod : { "default": mod };
90
+ };
91
+ Object.defineProperty(exports, "__esModule", { value: true });
92
+ exports.ICRC1OnlineProtocolImpl = exports.ICRC1OfflineProtocolImpl = void 0;
93
+ // tslint:disable: max-classes-per-file
94
+ var coinlib_core_1 = require("@airgap/coinlib-core");
95
+ var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
96
+ var errors_1 = require("@airgap/coinlib-core/errors");
97
+ var module_kit_1 = require("@airgap/module-kit");
98
+ var ledger_1 = require("../../types/icrc/ledger");
99
+ var actor_1 = require("../../utils/actor");
100
+ var auth_1 = require("../../utils/auth");
101
+ var Cbor = __importStar(require("../../utils/cbor"));
102
+ var convert_1 = require("../../utils/convert");
103
+ var icrc1_1 = require("../../utils/icrc1");
104
+ var IDL = __importStar(require("../../utils/idl"));
105
+ var principal_1 = require("../../utils/principal");
106
+ var secp256k1_1 = __importDefault(require("../../utils/secp256k1"));
107
+ var ICPImplementation_1 = require("../ICPImplementation");
108
+ var ICPProtocol_1 = require("../ICPProtocol");
109
+ // Implementation
110
+ function ICRC1_TRANSACTION_METADATA() {
111
+ return {
112
+ arbitraryData: {
113
+ inner: { name: 'memo' }
114
+ }
115
+ };
116
+ }
117
+ var ICRC1CommonProtocolImpl = /** @class */ (function () {
118
+ function ICRC1CommonProtocolImpl() {
119
+ }
120
+ // SubProtocol
121
+ ICRC1CommonProtocolImpl.prototype.getType = function () {
122
+ return __awaiter(this, void 0, void 0, function () {
123
+ return __generator(this, function (_a) {
124
+ return [2 /*return*/, 'token'];
125
+ });
126
+ });
127
+ };
128
+ ICRC1CommonProtocolImpl.prototype.mainProtocol = function () {
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ return __generator(this, function (_a) {
131
+ return [2 /*return*/, coinlib_core_1.MainProtocolSymbols.ICP];
132
+ });
133
+ });
134
+ };
135
+ // Common
136
+ ICRC1CommonProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
137
+ return __awaiter(this, void 0, void 0, function () {
138
+ return __generator(this, function (_a) {
139
+ return [2 /*return*/, (0, icrc1_1.getICRC1AddressFromPublicKey)(publicKey.value)];
140
+ });
141
+ });
142
+ };
143
+ ICRC1CommonProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, publicKey, network, defaultFee) {
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ return __generator(this, function (_a) {
146
+ switch (transaction.type) {
147
+ case 'signed':
148
+ return [2 /*return*/, this.getDetailsFromSignedTransaction(transaction, publicKey, network, defaultFee)];
149
+ case 'unsigned':
150
+ return [2 /*return*/, this.getDetailsFromUnsignedTransaction(transaction, publicKey, network, defaultFee)];
151
+ default:
152
+ (0, coinlib_core_1.assertNever)(transaction);
153
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ICP, 'Unsupported transaction type.');
154
+ }
155
+ return [2 /*return*/];
156
+ });
157
+ });
158
+ };
159
+ ICRC1CommonProtocolImpl.prototype.getDetailsFromSignedTransaction = function (transaction, publicKey, network, defaultFee) {
160
+ var transferArgs = (0, icrc1_1.decodeICRC1TransferArgs)(Cbor.decode((0, convert_1.hexStringToArrayBuffer)(transaction.transaction)));
161
+ return (0, icrc1_1.getDetailsFromTransferArgs)(transferArgs, publicKey.value, network, defaultFee);
162
+ };
163
+ ICRC1CommonProtocolImpl.prototype.getDetailsFromUnsignedTransaction = function (transaction, publicKey, network, defaultFee) {
164
+ var transferArgs = (0, icrc1_1.decodeICRC1TransferArgs)(Buffer.from(transaction.transaction, 'hex'));
165
+ return (0, icrc1_1.getDetailsFromTransferArgs)(transferArgs, publicKey.value, network, defaultFee);
166
+ };
167
+ return ICRC1CommonProtocolImpl;
168
+ }());
169
+ var ICRC1OfflineProtocolImpl = /** @class */ (function () {
170
+ function ICRC1OfflineProtocolImpl(options) {
171
+ this.options = options;
172
+ this.icp = new ICPProtocol_1.ICPProtocolImpl();
173
+ this.commonImpl = new ICRC1CommonProtocolImpl();
174
+ this.metadata = {
175
+ identifier: options.identifier,
176
+ name: options.name,
177
+ units: options.units,
178
+ mainUnit: options.mainUnit,
179
+ fee: { defaults: options.feeDefaults },
180
+ account: ICPProtocol_1.ICP_ACCOUNT_METADATA,
181
+ transaction: ICRC1_TRANSACTION_METADATA()
182
+ };
183
+ }
184
+ // SubProtocol
185
+ ICRC1OfflineProtocolImpl.prototype.getType = function () {
186
+ return __awaiter(this, void 0, void 0, function () {
187
+ return __generator(this, function (_a) {
188
+ return [2 /*return*/, this.commonImpl.getType()];
189
+ });
190
+ });
191
+ };
192
+ ICRC1OfflineProtocolImpl.prototype.mainProtocol = function () {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ return __generator(this, function (_a) {
195
+ return [2 /*return*/, this.commonImpl.mainProtocol()];
196
+ });
197
+ });
198
+ };
199
+ ICRC1OfflineProtocolImpl.prototype.getMetadata = function () {
200
+ return __awaiter(this, void 0, void 0, function () {
201
+ return __generator(this, function (_a) {
202
+ return [2 /*return*/, this.metadata];
203
+ });
204
+ });
205
+ };
206
+ ICRC1OfflineProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
207
+ return __awaiter(this, void 0, void 0, function () {
208
+ return __generator(this, function (_a) {
209
+ return [2 /*return*/, this.commonImpl.getAddressFromPublicKey(publicKey)];
210
+ });
211
+ });
212
+ };
213
+ ICRC1OfflineProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, publicKey) {
214
+ return __awaiter(this, void 0, void 0, function () {
215
+ var _a, _b, _c, _d;
216
+ return __generator(this, function (_e) {
217
+ switch (_e.label) {
218
+ case 0:
219
+ _b = (_a = this.commonImpl).getDetailsFromTransaction;
220
+ _c = [transaction,
221
+ publicKey];
222
+ _d = [{}];
223
+ return [4 /*yield*/, this.icp.getNetwork()];
224
+ case 1: return [2 /*return*/, _b.apply(_a, _c.concat([__assign.apply(void 0, [__assign.apply(void 0, _d.concat([(_e.sent())])), { canisterId: this.options.canisterId }]), this.options.feeDefaults.medium]))];
225
+ }
226
+ });
227
+ });
228
+ };
229
+ // Offline
230
+ ICRC1OfflineProtocolImpl.prototype.getCryptoConfiguration = function () {
231
+ return __awaiter(this, void 0, void 0, function () {
232
+ return __generator(this, function (_a) {
233
+ return [2 /*return*/, this.icp.getCryptoConfiguration()];
234
+ });
235
+ });
236
+ };
237
+ ICRC1OfflineProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
238
+ return __awaiter(this, void 0, void 0, function () {
239
+ return __generator(this, function (_a) {
240
+ return [2 /*return*/, this.icp.getKeyPairFromDerivative(derivative)];
241
+ });
242
+ });
243
+ };
244
+ ICRC1OfflineProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
245
+ return __awaiter(this, void 0, void 0, function () {
246
+ var TransferArg, arg, signedTransaction;
247
+ return __generator(this, function (_a) {
248
+ switch (_a.label) {
249
+ case 0:
250
+ TransferArg = (0, ledger_1.icrcIDLTypes)(IDL).TransferArg;
251
+ arg = IDL.decode([TransferArg], Buffer.from(transaction.transaction, 'hex'));
252
+ return [4 /*yield*/, (0, ICPImplementation_1.signTransaction)(secretKey.value, this.options.canisterId, arg)];
253
+ case 1:
254
+ signedTransaction = _a.sent();
255
+ return [2 /*return*/, (0, module_kit_1.newSignedTransaction)({ transaction: signedTransaction })];
256
+ }
257
+ });
258
+ });
259
+ };
260
+ return ICRC1OfflineProtocolImpl;
261
+ }());
262
+ exports.ICRC1OfflineProtocolImpl = ICRC1OfflineProtocolImpl;
263
+ var ICRC1OnlineProtocolImpl = /** @class */ (function () {
264
+ function ICRC1OnlineProtocolImpl(options) {
265
+ this.options = options;
266
+ // Common
267
+ this._metadata = undefined;
268
+ // Custom
269
+ this._name = undefined;
270
+ this._symbol = undefined;
271
+ this._decimals = undefined;
272
+ this._fee = undefined;
273
+ this._baseMetadata = undefined;
274
+ this._totalSupply = undefined;
275
+ this._mintingAccount = null;
276
+ this.icp = new ICPProtocol_1.ICPProtocolImpl({ network: options.network });
277
+ this.commonImpl = new ICRC1CommonProtocolImpl();
278
+ }
279
+ // SubProtocol
280
+ ICRC1OnlineProtocolImpl.prototype.getType = function () {
281
+ return __awaiter(this, void 0, void 0, function () {
282
+ return __generator(this, function (_a) {
283
+ return [2 /*return*/, this.commonImpl.getType()];
284
+ });
285
+ });
286
+ };
287
+ ICRC1OnlineProtocolImpl.prototype.mainProtocol = function () {
288
+ return __awaiter(this, void 0, void 0, function () {
289
+ return __generator(this, function (_a) {
290
+ return [2 /*return*/, this.commonImpl.mainProtocol()];
291
+ });
292
+ });
293
+ };
294
+ ICRC1OnlineProtocolImpl.prototype.getMetadata = function () {
295
+ return __awaiter(this, void 0, void 0, function () {
296
+ var _a, _b;
297
+ return __generator(this, function (_c) {
298
+ switch (_c.label) {
299
+ case 0:
300
+ if (!(this._metadata === undefined)) return [3 /*break*/, 2];
301
+ _a = this;
302
+ _b = [{ identifier: this.options.identifier }];
303
+ return [4 /*yield*/, this.getPartialMetadata()];
304
+ case 1:
305
+ _a._metadata = __assign.apply(void 0, [__assign.apply(void 0, _b.concat([(_c.sent())])), { account: ICPProtocol_1.ICP_ACCOUNT_METADATA, transaction: ICRC1_TRANSACTION_METADATA() }]);
306
+ _c.label = 2;
307
+ case 2: return [2 /*return*/, this._metadata];
308
+ }
309
+ });
310
+ });
311
+ };
312
+ ICRC1OnlineProtocolImpl.prototype.getPartialMetadata = function () {
313
+ var _a, _b;
314
+ return __awaiter(this, void 0, void 0, function () {
315
+ var mainUnit, icrc1Metadata, _c, name, symbol, decimals, feeDefaults, fetchedUnits;
316
+ var _d;
317
+ return __generator(this, function (_e) {
318
+ switch (_e.label) {
319
+ case 0:
320
+ mainUnit = (_a = this.options.mainUnit) !== null && _a !== void 0 ? _a : (this.options.units ? Object.keys(this.options.units)[0] : undefined);
321
+ if (this.options.name && this.options.units && mainUnit) {
322
+ return [2 /*return*/, {
323
+ name: this.options.name,
324
+ units: this.options.units,
325
+ mainUnit: mainUnit,
326
+ fee: {
327
+ defaults: this.options.feeDefaults,
328
+ units: this.options.units,
329
+ mainUnit: mainUnit
330
+ }
331
+ }];
332
+ }
333
+ return [4 /*yield*/, this.metadata()];
334
+ case 1:
335
+ icrc1Metadata = _e.sent();
336
+ return [4 /*yield*/, Promise.all([
337
+ this.options.name ? Promise.resolve(this.options.name) : icrc1Metadata.name ? Promise.resolve(icrc1Metadata.name) : this.name(),
338
+ mainUnit
339
+ ? Promise.resolve(mainUnit)
340
+ : (icrc1Metadata.symbol ? Promise.resolve(icrc1Metadata.symbol) : this.symbol()).then(function (symbol) { return symbol; }),
341
+ mainUnit && this.options.units
342
+ ? Promise.resolve(this.options.units[mainUnit].decimals)
343
+ : icrc1Metadata.decimals
344
+ ? Promise.resolve(icrc1Metadata.decimals)
345
+ : this.decimals(),
346
+ this.options.feeDefaults
347
+ ? Promise.resolve(this.options.feeDefaults)
348
+ : (icrc1Metadata.fee ? Promise.resolve(icrc1Metadata.fee) : this.fee()).then(function (fee) { return ({
349
+ low: (0, module_kit_1.newAmount)(fee, 'blockchain'),
350
+ medium: (0, module_kit_1.newAmount)(fee, 'blockchain'),
351
+ high: (0, module_kit_1.newAmount)(fee, 'blockchain')
352
+ }); })
353
+ ])];
354
+ case 2:
355
+ _c = __read.apply(void 0, [_e.sent(), 4]), name = _c[0], symbol = _c[1], decimals = _c[2], feeDefaults = _c[3];
356
+ fetchedUnits = (_d = {},
357
+ _d[symbol] = {
358
+ symbol: { value: symbol },
359
+ decimals: decimals
360
+ },
361
+ _d);
362
+ return [2 /*return*/, {
363
+ name: name,
364
+ units: (_b = this.options.units) !== null && _b !== void 0 ? _b : fetchedUnits,
365
+ mainUnit: symbol,
366
+ fee: {
367
+ defaults: feeDefaults
368
+ }
369
+ }];
370
+ }
371
+ });
372
+ });
373
+ };
374
+ ICRC1OnlineProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
375
+ return __awaiter(this, void 0, void 0, function () {
376
+ return __generator(this, function (_a) {
377
+ return [2 /*return*/, this.commonImpl.getAddressFromPublicKey(publicKey)];
378
+ });
379
+ });
380
+ };
381
+ ICRC1OnlineProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, publicKey) {
382
+ return __awaiter(this, void 0, void 0, function () {
383
+ var fee;
384
+ return __generator(this, function (_a) {
385
+ switch (_a.label) {
386
+ case 0: return [4 /*yield*/, this.fee()];
387
+ case 1:
388
+ fee = _a.sent();
389
+ return [2 /*return*/, this.commonImpl.getDetailsFromTransaction(transaction, publicKey, this.options.network, (0, module_kit_1.newAmount)(fee, 'blockchain'))];
390
+ }
391
+ });
392
+ });
393
+ };
394
+ // Online
395
+ ICRC1OnlineProtocolImpl.prototype.getNetwork = function () {
396
+ return __awaiter(this, void 0, void 0, function () {
397
+ return __generator(this, function (_a) {
398
+ return [2 /*return*/, this.options.network];
399
+ });
400
+ });
401
+ };
402
+ ICRC1OnlineProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey) {
403
+ return __awaiter(this, void 0, void 0, function () {
404
+ var principal, balance;
405
+ return __generator(this, function (_a) {
406
+ switch (_a.label) {
407
+ case 0:
408
+ principal = (0, ICPImplementation_1.getPrincipalFromPublicKey)(publicKey.value);
409
+ return [4 /*yield*/, this.balanceOf({ owner: principal.toText() })];
410
+ case 1:
411
+ balance = _a.sent();
412
+ return [2 /*return*/, { total: (0, module_kit_1.newAmount)(balance, 'blockchain') }];
413
+ }
414
+ });
415
+ });
416
+ };
417
+ ICRC1OnlineProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
418
+ return __awaiter(this, void 0, void 0, function () {
419
+ var balance, balanceBn, fee, feeEstimation, amountWithoutFees;
420
+ return __generator(this, function (_a) {
421
+ switch (_a.label) {
422
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
423
+ case 1:
424
+ balance = _a.sent();
425
+ balanceBn = new bignumber_1.default((0, module_kit_1.newAmount)(balance.total).value);
426
+ if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 2];
427
+ fee = new bignumber_1.default((0, module_kit_1.newAmount)(configuration.fee).value);
428
+ return [3 /*break*/, 4];
429
+ case 2: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, [])];
430
+ case 3:
431
+ feeEstimation = _a.sent();
432
+ fee = new bignumber_1.default((0, module_kit_1.newAmount)(feeEstimation).value);
433
+ if (fee.gte(balanceBn)) {
434
+ fee = new bignumber_1.default(0);
435
+ }
436
+ _a.label = 4;
437
+ case 4:
438
+ amountWithoutFees = balanceBn.minus(fee);
439
+ if (amountWithoutFees.isNegative()) {
440
+ amountWithoutFees = new bignumber_1.default(0);
441
+ }
442
+ return [2 /*return*/, (0, module_kit_1.newAmount)(amountWithoutFees.toFixed(), 'blockchain')];
443
+ }
444
+ });
445
+ });
446
+ };
447
+ ICRC1OnlineProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (publicKey, details) {
448
+ var _a;
449
+ return __awaiter(this, void 0, void 0, function () {
450
+ var metadata, fee;
451
+ return __generator(this, function (_b) {
452
+ switch (_b.label) {
453
+ case 0: return [4 /*yield*/, this.getMetadata()];
454
+ case 1:
455
+ metadata = _b.sent();
456
+ if ((_a = metadata.fee) === null || _a === void 0 ? void 0 : _a.defaults) {
457
+ return [2 /*return*/, (0, module_kit_1.newAmount)(metadata.fee.defaults.medium).blockchain(metadata.units)];
458
+ }
459
+ return [4 /*yield*/, this.fee()];
460
+ case 2:
461
+ fee = _b.sent();
462
+ return [2 /*return*/, (0, module_kit_1.newAmount)(fee, 'blockchain')];
463
+ }
464
+ });
465
+ });
466
+ };
467
+ ICRC1OnlineProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
468
+ return __awaiter(this, void 0, void 0, function () {
469
+ var TransferArg, metadata, to, amount, fee, _a, _b, memo, transferArg, encodedTransferArg;
470
+ return __generator(this, function (_c) {
471
+ switch (_c.label) {
472
+ case 0:
473
+ TransferArg = (0, ledger_1.icrcIDLTypes)(IDL).TransferArg;
474
+ return [4 /*yield*/, this.getMetadata()];
475
+ case 1:
476
+ metadata = _c.sent();
477
+ to = (0, icrc1_1.getICRC1AccountFromAddress)(details[0].to);
478
+ amount = (0, module_kit_1.newAmount)(details[0].amount).blockchain(metadata.units);
479
+ if (!(configuration === null || configuration === void 0 ? void 0 : configuration.fee)) return [3 /*break*/, 2];
480
+ _a = (0, module_kit_1.newAmount)(configuration.fee).blockchain(metadata.units);
481
+ return [3 /*break*/, 4];
482
+ case 2:
483
+ _b = module_kit_1.newAmount;
484
+ return [4 /*yield*/, this.fee()];
485
+ case 3:
486
+ _a = _b.apply(void 0, [_c.sent(), 'blockchain']);
487
+ _c.label = 4;
488
+ case 4:
489
+ fee = _a;
490
+ memo = details[0].arbitraryData;
491
+ transferArg = {
492
+ to: {
493
+ owner: principal_1.Principal.fromText(to.owner),
494
+ subaccount: to.subaccount ? [Buffer.from(to.subaccount, 'hex')] : []
495
+ },
496
+ fee: [BigInt(fee.value)],
497
+ memo: memo ? [Buffer.from(memo, 'hex')] : [],
498
+ from_subaccount: [],
499
+ created_at_time: [],
500
+ amount: BigInt(amount.value)
501
+ };
502
+ encodedTransferArg = Buffer.from(IDL.encode([TransferArg], [transferArg]));
503
+ return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({
504
+ transaction: encodedTransferArg.toString('hex'),
505
+ networkId: '' // TODO: do we need this?
506
+ })];
507
+ }
508
+ });
509
+ });
510
+ };
511
+ ICRC1OnlineProtocolImpl.prototype.broadcastTransaction = function (transaction) {
512
+ return __awaiter(this, void 0, void 0, function () {
513
+ return __generator(this, function (_a) {
514
+ return [2 /*return*/, this.icp.broadcastTransaction(transaction)];
515
+ });
516
+ });
517
+ };
518
+ ICRC1OnlineProtocolImpl.prototype.name = function () {
519
+ return __awaiter(this, void 0, void 0, function () {
520
+ var actor, name_1;
521
+ return __generator(this, function (_a) {
522
+ switch (_a.label) {
523
+ case 0:
524
+ if (!!this._name) return [3 /*break*/, 2];
525
+ actor = this.createActor();
526
+ return [4 /*yield*/, actor.icrc1_name()];
527
+ case 1:
528
+ name_1 = _a.sent();
529
+ this._name = name_1;
530
+ _a.label = 2;
531
+ case 2: return [2 /*return*/, this._name];
532
+ }
533
+ });
534
+ });
535
+ };
536
+ ICRC1OnlineProtocolImpl.prototype.symbol = function () {
537
+ return __awaiter(this, void 0, void 0, function () {
538
+ var actor, symbol;
539
+ return __generator(this, function (_a) {
540
+ switch (_a.label) {
541
+ case 0:
542
+ if (!!this._symbol) return [3 /*break*/, 2];
543
+ actor = this.createActor();
544
+ return [4 /*yield*/, actor.icrc1_symbol()];
545
+ case 1:
546
+ symbol = _a.sent();
547
+ this._symbol = symbol;
548
+ _a.label = 2;
549
+ case 2: return [2 /*return*/, this._symbol];
550
+ }
551
+ });
552
+ });
553
+ };
554
+ ICRC1OnlineProtocolImpl.prototype.decimals = function () {
555
+ return __awaiter(this, void 0, void 0, function () {
556
+ var actor, decimals;
557
+ return __generator(this, function (_a) {
558
+ switch (_a.label) {
559
+ case 0:
560
+ if (!!this._decimals) return [3 /*break*/, 2];
561
+ actor = this.createActor();
562
+ return [4 /*yield*/, actor.icrc1_decimals()];
563
+ case 1:
564
+ decimals = _a.sent();
565
+ this._decimals = decimals;
566
+ _a.label = 2;
567
+ case 2: return [2 /*return*/, this._decimals];
568
+ }
569
+ });
570
+ });
571
+ };
572
+ ICRC1OnlineProtocolImpl.prototype.fee = function () {
573
+ return __awaiter(this, void 0, void 0, function () {
574
+ var actor, fee;
575
+ return __generator(this, function (_a) {
576
+ switch (_a.label) {
577
+ case 0:
578
+ if (!!this._fee) return [3 /*break*/, 2];
579
+ actor = this.createActor();
580
+ return [4 /*yield*/, actor.icrc1_fee()];
581
+ case 1:
582
+ fee = _a.sent();
583
+ this._fee = fee.toString();
584
+ _a.label = 2;
585
+ case 2: return [2 /*return*/, this._fee];
586
+ }
587
+ });
588
+ });
589
+ };
590
+ ICRC1OnlineProtocolImpl.prototype.baseMetadata = function () {
591
+ return __awaiter(this, void 0, void 0, function () {
592
+ var actor, metadata;
593
+ return __generator(this, function (_a) {
594
+ switch (_a.label) {
595
+ case 0:
596
+ if (!!this._baseMetadata) return [3 /*break*/, 2];
597
+ actor = this.createActor();
598
+ return [4 /*yield*/, actor.icrc1_metadata()];
599
+ case 1:
600
+ metadata = _a.sent();
601
+ this._baseMetadata = (0, icrc1_1.decodeICRC1Metadata)(metadata);
602
+ _a.label = 2;
603
+ case 2: return [2 /*return*/, this._baseMetadata];
604
+ }
605
+ });
606
+ });
607
+ };
608
+ ICRC1OnlineProtocolImpl.prototype.totalSupply = function () {
609
+ return __awaiter(this, void 0, void 0, function () {
610
+ var actor, totalSupply;
611
+ return __generator(this, function (_a) {
612
+ switch (_a.label) {
613
+ case 0:
614
+ if (!!this._totalSupply) return [3 /*break*/, 2];
615
+ actor = this.createActor();
616
+ return [4 /*yield*/, actor.icrc1_total_supply()];
617
+ case 1:
618
+ totalSupply = _a.sent();
619
+ this._totalSupply = totalSupply.toString();
620
+ _a.label = 2;
621
+ case 2: return [2 /*return*/, this._totalSupply];
622
+ }
623
+ });
624
+ });
625
+ };
626
+ ICRC1OnlineProtocolImpl.prototype.mintingAccount = function () {
627
+ return __awaiter(this, void 0, void 0, function () {
628
+ var actor, mintingAccount;
629
+ return __generator(this, function (_a) {
630
+ switch (_a.label) {
631
+ case 0:
632
+ if (!(this._mintingAccount === null)) return [3 /*break*/, 2];
633
+ actor = this.createActor();
634
+ return [4 /*yield*/, actor.icrc1_minting_account()];
635
+ case 1:
636
+ mintingAccount = _a.sent();
637
+ this._mintingAccount = (0, icrc1_1.decodeOptionalICRC1Account)(mintingAccount);
638
+ _a.label = 2;
639
+ case 2: return [2 /*return*/, this._mintingAccount];
640
+ }
641
+ });
642
+ });
643
+ };
644
+ ICRC1OnlineProtocolImpl.prototype.balanceOf = function (account) {
645
+ return __awaiter(this, void 0, void 0, function () {
646
+ var actor, balance;
647
+ return __generator(this, function (_a) {
648
+ switch (_a.label) {
649
+ case 0:
650
+ actor = this.createActor();
651
+ return [4 /*yield*/, actor.icrc1_balance_of((0, icrc1_1.encodeICRC1Account)(account))];
652
+ case 1:
653
+ balance = _a.sent();
654
+ return [2 /*return*/, balance];
655
+ }
656
+ });
657
+ });
658
+ };
659
+ ICRC1OnlineProtocolImpl.prototype.transfer = function (privateKey, args) {
660
+ return __awaiter(this, void 0, void 0, function () {
661
+ var actor, result;
662
+ return __generator(this, function (_a) {
663
+ switch (_a.label) {
664
+ case 0:
665
+ actor = this.createActor(ledger_1.icrcIDLFactory, secp256k1_1.default.fromSecretKey((0, convert_1.hexStringToArrayBuffer)(privateKey)));
666
+ return [4 /*yield*/, actor.icrc1_transfer({
667
+ to: (0, icrc1_1.encodeICRC1Account)(args.to),
668
+ fee: args.fee ? [BigInt(args.fee)] : [],
669
+ memo: args.memo ? [Buffer.from(args.memo)] : [],
670
+ from_subaccount: args.fromSubaccount ? [Buffer.from(args.fromSubaccount)] : [],
671
+ created_at_time: args.createdAtTime ? [BigInt(args.createdAtTime)] : [],
672
+ amount: BigInt(args.amount)
673
+ })];
674
+ case 1:
675
+ result = _a.sent();
676
+ if (result.Ok) {
677
+ return [2 /*return*/, result.Ok.toString()];
678
+ }
679
+ else {
680
+ throw new coinlib_core_1.TransactionError(coinlib_core_1.Domain.ICP, "ICRC1 transfer failed (".concat(result.Err.GenericError.error_code.toString(), ": ").concat(result.Err.GenericError.message, ")"));
681
+ }
682
+ return [2 /*return*/];
683
+ }
684
+ });
685
+ });
686
+ };
687
+ ICRC1OnlineProtocolImpl.prototype.createActor = function (idlFactory, identity) {
688
+ if (idlFactory === void 0) { idlFactory = ledger_1.icrcIDLFactory; }
689
+ if (identity === void 0) { identity = new auth_1.AnonymousIdentity(); }
690
+ var agent = (0, ICPImplementation_1.createHttpAgent)(this.options.network.rpcUrl, identity);
691
+ return actor_1.Actor.createActor(idlFactory, {
692
+ agent: agent,
693
+ canisterId: this.options.network.canisterId
694
+ });
695
+ };
696
+ return ICRC1OnlineProtocolImpl;
697
+ }());
698
+ exports.ICRC1OnlineProtocolImpl = ICRC1OnlineProtocolImpl;
699
+ //# sourceMappingURL=ICRC1Protocol.js.map