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

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 +95 -0
  27. package/v1/protocol/icrc/ICRC1Protocol.js +717 -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,717 @@
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())])), { ledgerCanisterId: this.options.ledgerCanisterId }]), 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.ledgerCanisterId, 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;
405
+ return __generator(this, function (_a) {
406
+ principal = (0, ICPImplementation_1.getPrincipalFromPublicKey)(publicKey.value);
407
+ return [2 /*return*/, this.getBalanceOfAccount({ owner: principal.toText() })];
408
+ });
409
+ });
410
+ };
411
+ ICRC1OnlineProtocolImpl.prototype.getBalanceOfAddress = function (address) {
412
+ return __awaiter(this, void 0, void 0, function () {
413
+ var account;
414
+ return __generator(this, function (_a) {
415
+ account = (0, icrc1_1.getICRC1AccountFromAddress)(address);
416
+ return [2 /*return*/, this.getBalanceOfAccount(account)];
417
+ });
418
+ });
419
+ };
420
+ ICRC1OnlineProtocolImpl.prototype.getBalanceOfAccount = function (account) {
421
+ return __awaiter(this, void 0, void 0, function () {
422
+ var balance;
423
+ return __generator(this, function (_a) {
424
+ switch (_a.label) {
425
+ case 0: return [4 /*yield*/, this.balanceOf(account)];
426
+ case 1:
427
+ balance = _a.sent();
428
+ return [2 /*return*/, { total: (0, module_kit_1.newAmount)(balance, 'blockchain') }];
429
+ }
430
+ });
431
+ });
432
+ };
433
+ ICRC1OnlineProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
434
+ return __awaiter(this, void 0, void 0, function () {
435
+ var balance, balanceBn, fee, feeEstimation, amountWithoutFees;
436
+ return __generator(this, function (_a) {
437
+ switch (_a.label) {
438
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
439
+ case 1:
440
+ balance = _a.sent();
441
+ balanceBn = new bignumber_1.default((0, module_kit_1.newAmount)(balance.total).value);
442
+ if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 2];
443
+ fee = new bignumber_1.default((0, module_kit_1.newAmount)(configuration.fee).value);
444
+ return [3 /*break*/, 4];
445
+ case 2: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, [])];
446
+ case 3:
447
+ feeEstimation = _a.sent();
448
+ fee = new bignumber_1.default((0, module_kit_1.newAmount)(feeEstimation).value);
449
+ if (fee.gte(balanceBn)) {
450
+ fee = new bignumber_1.default(0);
451
+ }
452
+ _a.label = 4;
453
+ case 4:
454
+ amountWithoutFees = balanceBn.minus(fee);
455
+ if (amountWithoutFees.isNegative()) {
456
+ amountWithoutFees = new bignumber_1.default(0);
457
+ }
458
+ return [2 /*return*/, (0, module_kit_1.newAmount)(amountWithoutFees.toFixed(), 'blockchain')];
459
+ }
460
+ });
461
+ });
462
+ };
463
+ ICRC1OnlineProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (publicKey, details) {
464
+ var _a;
465
+ return __awaiter(this, void 0, void 0, function () {
466
+ var metadata, fee;
467
+ return __generator(this, function (_b) {
468
+ switch (_b.label) {
469
+ case 0: return [4 /*yield*/, this.getMetadata()];
470
+ case 1:
471
+ metadata = _b.sent();
472
+ if ((_a = metadata.fee) === null || _a === void 0 ? void 0 : _a.defaults) {
473
+ return [2 /*return*/, (0, module_kit_1.newAmount)(metadata.fee.defaults.medium).blockchain(metadata.units)];
474
+ }
475
+ return [4 /*yield*/, this.fee()];
476
+ case 2:
477
+ fee = _b.sent();
478
+ return [2 /*return*/, (0, module_kit_1.newAmount)(fee, 'blockchain')];
479
+ }
480
+ });
481
+ });
482
+ };
483
+ ICRC1OnlineProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
484
+ return __awaiter(this, void 0, void 0, function () {
485
+ var TransferArg, metadata, to, amount, fee, _a, _b, memo, transferArg, encodedTransferArg;
486
+ return __generator(this, function (_c) {
487
+ switch (_c.label) {
488
+ case 0:
489
+ TransferArg = (0, ledger_1.icrcIDLTypes)(IDL).TransferArg;
490
+ return [4 /*yield*/, this.getMetadata()];
491
+ case 1:
492
+ metadata = _c.sent();
493
+ to = (0, icrc1_1.getICRC1AccountFromAddress)(details[0].to);
494
+ amount = (0, module_kit_1.newAmount)(details[0].amount).blockchain(metadata.units);
495
+ if (!(configuration === null || configuration === void 0 ? void 0 : configuration.fee)) return [3 /*break*/, 2];
496
+ _a = (0, module_kit_1.newAmount)(configuration.fee).blockchain(metadata.units);
497
+ return [3 /*break*/, 4];
498
+ case 2:
499
+ _b = module_kit_1.newAmount;
500
+ return [4 /*yield*/, this.fee()];
501
+ case 3:
502
+ _a = _b.apply(void 0, [_c.sent(), 'blockchain']);
503
+ _c.label = 4;
504
+ case 4:
505
+ fee = _a;
506
+ memo = details[0].arbitraryData;
507
+ transferArg = {
508
+ to: {
509
+ owner: principal_1.Principal.fromText(to.owner),
510
+ subaccount: to.subaccount ? [Buffer.from(to.subaccount, 'hex')] : []
511
+ },
512
+ fee: [BigInt(fee.value)],
513
+ memo: memo ? [Buffer.from(memo, 'hex')] : [],
514
+ from_subaccount: [],
515
+ created_at_time: [],
516
+ amount: BigInt(amount.value)
517
+ };
518
+ encodedTransferArg = Buffer.from(IDL.encode([TransferArg], [transferArg]));
519
+ return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({
520
+ transaction: encodedTransferArg.toString('hex'),
521
+ networkId: '' // TODO: do we need this?
522
+ })];
523
+ }
524
+ });
525
+ });
526
+ };
527
+ ICRC1OnlineProtocolImpl.prototype.broadcastTransaction = function (transaction) {
528
+ return __awaiter(this, void 0, void 0, function () {
529
+ return __generator(this, function (_a) {
530
+ return [2 /*return*/, this.icp.broadcastTransaction(transaction)];
531
+ });
532
+ });
533
+ };
534
+ ICRC1OnlineProtocolImpl.prototype.name = function () {
535
+ return __awaiter(this, void 0, void 0, function () {
536
+ var actor, name_1;
537
+ return __generator(this, function (_a) {
538
+ switch (_a.label) {
539
+ case 0:
540
+ if (!!this._name) return [3 /*break*/, 2];
541
+ actor = this.createLedgerActor();
542
+ return [4 /*yield*/, actor.icrc1_name()];
543
+ case 1:
544
+ name_1 = _a.sent();
545
+ this._name = name_1;
546
+ _a.label = 2;
547
+ case 2: return [2 /*return*/, this._name];
548
+ }
549
+ });
550
+ });
551
+ };
552
+ ICRC1OnlineProtocolImpl.prototype.symbol = function () {
553
+ return __awaiter(this, void 0, void 0, function () {
554
+ var actor, symbol;
555
+ return __generator(this, function (_a) {
556
+ switch (_a.label) {
557
+ case 0:
558
+ if (!!this._symbol) return [3 /*break*/, 2];
559
+ actor = this.createLedgerActor();
560
+ return [4 /*yield*/, actor.icrc1_symbol()];
561
+ case 1:
562
+ symbol = _a.sent();
563
+ this._symbol = symbol;
564
+ _a.label = 2;
565
+ case 2: return [2 /*return*/, this._symbol];
566
+ }
567
+ });
568
+ });
569
+ };
570
+ ICRC1OnlineProtocolImpl.prototype.decimals = function () {
571
+ return __awaiter(this, void 0, void 0, function () {
572
+ var actor, decimals;
573
+ return __generator(this, function (_a) {
574
+ switch (_a.label) {
575
+ case 0:
576
+ if (!!this._decimals) return [3 /*break*/, 2];
577
+ actor = this.createLedgerActor();
578
+ return [4 /*yield*/, actor.icrc1_decimals()];
579
+ case 1:
580
+ decimals = _a.sent();
581
+ this._decimals = decimals;
582
+ _a.label = 2;
583
+ case 2: return [2 /*return*/, this._decimals];
584
+ }
585
+ });
586
+ });
587
+ };
588
+ ICRC1OnlineProtocolImpl.prototype.fee = function () {
589
+ return __awaiter(this, void 0, void 0, function () {
590
+ var actor, fee;
591
+ return __generator(this, function (_a) {
592
+ switch (_a.label) {
593
+ case 0:
594
+ if (!!this._fee) return [3 /*break*/, 2];
595
+ actor = this.createLedgerActor();
596
+ return [4 /*yield*/, actor.icrc1_fee()];
597
+ case 1:
598
+ fee = _a.sent();
599
+ this._fee = fee.toString();
600
+ _a.label = 2;
601
+ case 2: return [2 /*return*/, this._fee];
602
+ }
603
+ });
604
+ });
605
+ };
606
+ ICRC1OnlineProtocolImpl.prototype.baseMetadata = function () {
607
+ return __awaiter(this, void 0, void 0, function () {
608
+ var actor, metadata;
609
+ return __generator(this, function (_a) {
610
+ switch (_a.label) {
611
+ case 0:
612
+ if (!!this._baseMetadata) return [3 /*break*/, 2];
613
+ actor = this.createLedgerActor();
614
+ return [4 /*yield*/, actor.icrc1_metadata()];
615
+ case 1:
616
+ metadata = _a.sent();
617
+ this._baseMetadata = (0, icrc1_1.decodeICRC1Metadata)(metadata);
618
+ _a.label = 2;
619
+ case 2: return [2 /*return*/, this._baseMetadata];
620
+ }
621
+ });
622
+ });
623
+ };
624
+ ICRC1OnlineProtocolImpl.prototype.totalSupply = function () {
625
+ return __awaiter(this, void 0, void 0, function () {
626
+ var actor, totalSupply;
627
+ return __generator(this, function (_a) {
628
+ switch (_a.label) {
629
+ case 0:
630
+ if (!!this._totalSupply) return [3 /*break*/, 2];
631
+ actor = this.createLedgerActor();
632
+ return [4 /*yield*/, actor.icrc1_total_supply()];
633
+ case 1:
634
+ totalSupply = _a.sent();
635
+ this._totalSupply = totalSupply.toString();
636
+ _a.label = 2;
637
+ case 2: return [2 /*return*/, this._totalSupply];
638
+ }
639
+ });
640
+ });
641
+ };
642
+ ICRC1OnlineProtocolImpl.prototype.mintingAccount = function () {
643
+ return __awaiter(this, void 0, void 0, function () {
644
+ var actor, mintingAccount;
645
+ return __generator(this, function (_a) {
646
+ switch (_a.label) {
647
+ case 0:
648
+ if (!(this._mintingAccount === null)) return [3 /*break*/, 2];
649
+ actor = this.createLedgerActor();
650
+ return [4 /*yield*/, actor.icrc1_minting_account()];
651
+ case 1:
652
+ mintingAccount = _a.sent();
653
+ this._mintingAccount = (0, icrc1_1.decodeOptionalICRC1Account)(mintingAccount);
654
+ _a.label = 2;
655
+ case 2: return [2 /*return*/, this._mintingAccount];
656
+ }
657
+ });
658
+ });
659
+ };
660
+ ICRC1OnlineProtocolImpl.prototype.balanceOf = function (account) {
661
+ return __awaiter(this, void 0, void 0, function () {
662
+ var actor, balance;
663
+ return __generator(this, function (_a) {
664
+ switch (_a.label) {
665
+ case 0:
666
+ actor = this.createLedgerActor();
667
+ return [4 /*yield*/, actor.icrc1_balance_of((0, icrc1_1.encodeICRC1Account)(account))];
668
+ case 1:
669
+ balance = _a.sent();
670
+ return [2 /*return*/, balance];
671
+ }
672
+ });
673
+ });
674
+ };
675
+ ICRC1OnlineProtocolImpl.prototype.transfer = function (privateKey, args) {
676
+ return __awaiter(this, void 0, void 0, function () {
677
+ var actor, result;
678
+ return __generator(this, function (_a) {
679
+ switch (_a.label) {
680
+ case 0:
681
+ actor = this.createLedgerActor(ledger_1.icrcIDLFactory, secp256k1_1.default.fromSecretKey((0, convert_1.hexStringToArrayBuffer)(privateKey)));
682
+ return [4 /*yield*/, actor.icrc1_transfer({
683
+ to: (0, icrc1_1.encodeICRC1Account)(args.to),
684
+ fee: args.fee ? [BigInt(args.fee)] : [],
685
+ memo: args.memo ? [Buffer.from(args.memo)] : [],
686
+ from_subaccount: args.fromSubaccount ? [Buffer.from(args.fromSubaccount)] : [],
687
+ created_at_time: args.createdAtTime ? [BigInt(args.createdAtTime)] : [],
688
+ amount: BigInt(args.amount)
689
+ })];
690
+ case 1:
691
+ result = _a.sent();
692
+ if (result.Ok) {
693
+ return [2 /*return*/, result.Ok.toString()];
694
+ }
695
+ else {
696
+ 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, ")"));
697
+ }
698
+ return [2 /*return*/];
699
+ }
700
+ });
701
+ });
702
+ };
703
+ ICRC1OnlineProtocolImpl.prototype.createLedgerActor = function (idlFactory, identity) {
704
+ if (idlFactory === void 0) { idlFactory = ledger_1.icrcIDLFactory; }
705
+ if (identity === void 0) { identity = new auth_1.AnonymousIdentity(); }
706
+ return this.createActor(this.options.network.ledgerCanisterId, idlFactory, identity);
707
+ };
708
+ ICRC1OnlineProtocolImpl.prototype.createActor = function (canisterId, idlFactory, identity) {
709
+ if (idlFactory === void 0) { idlFactory = ledger_1.icrcIDLFactory; }
710
+ if (identity === void 0) { identity = new auth_1.AnonymousIdentity(); }
711
+ var agent = (0, ICPImplementation_1.createHttpAgent)(this.options.network.rpcUrl, identity);
712
+ return actor_1.Actor.createActor(idlFactory, { agent: agent, canisterId: canisterId });
713
+ };
714
+ return ICRC1OnlineProtocolImpl;
715
+ }());
716
+ exports.ICRC1OnlineProtocolImpl = ICRC1OnlineProtocolImpl;
717
+ //# sourceMappingURL=ICRC1Protocol.js.map