@airgap/icp 0.13.11-beta.1 → 0.13.11-beta.11

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