@airgap/cosmos 0.13.11-beta.14 → 0.13.11-beta.15

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 (76) hide show
  1. package/LICENSE.md +7 -7
  2. package/package.json +2 -3
  3. package/v1/data/CosmosAddress.d.ts +7 -0
  4. package/v1/data/CosmosAddress.js +29 -0
  5. package/v1/data/CosmosAddress.js.map +1 -0
  6. package/v1/data/CosmosCoin.d.ts +19 -0
  7. package/v1/data/CosmosCoin.js +55 -0
  8. package/v1/data/CosmosCoin.js.map +1 -0
  9. package/v1/data/CosmosFee.d.ts +15 -0
  10. package/v1/data/CosmosFee.js +31 -0
  11. package/v1/data/CosmosFee.js.map +1 -0
  12. package/v1/data/transaction/CosmosTransaction.d.ts +36 -0
  13. package/v1/data/transaction/CosmosTransaction.js +109 -0
  14. package/v1/data/transaction/CosmosTransaction.js.map +1 -0
  15. package/v1/data/transaction/message/CosmosDelegateMessage.d.ts +19 -0
  16. package/v1/data/transaction/message/CosmosDelegateMessage.js +70 -0
  17. package/v1/data/transaction/message/CosmosDelegateMessage.js.map +1 -0
  18. package/v1/data/transaction/message/CosmosMessage.d.ts +38 -0
  19. package/v1/data/transaction/message/CosmosMessage.js +47 -0
  20. package/v1/data/transaction/message/CosmosMessage.js.map +1 -0
  21. package/v1/data/transaction/message/CosmosSendMessage.d.ts +19 -0
  22. package/v1/data/transaction/message/CosmosSendMessage.js +83 -0
  23. package/v1/data/transaction/message/CosmosSendMessage.js.map +1 -0
  24. package/v1/data/transaction/message/CosmosWithdrawDelegationRewardMessage.d.ts +23 -0
  25. package/v1/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js +61 -0
  26. package/v1/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js.map +1 -0
  27. package/v1/index.d.ts +6 -2
  28. package/v1/index.js.map +1 -1
  29. package/v1/module/CosmosModule.js +1 -1
  30. package/v1/module/CosmosModule.js.map +1 -1
  31. package/v1/node/CosmosNodeClient.d.ts +28 -0
  32. package/v1/node/CosmosNodeClient.js +381 -0
  33. package/v1/node/CosmosNodeClient.js.map +1 -0
  34. package/v1/protocol/CosmosCryptoClient.d.ts +8 -0
  35. package/v1/protocol/CosmosCryptoClient.js +90 -0
  36. package/v1/protocol/CosmosCryptoClient.js.map +1 -0
  37. package/v1/protocol/CosmosProtocol.d.ts +47 -10
  38. package/v1/protocol/CosmosProtocol.js +531 -41
  39. package/v1/protocol/CosmosProtocol.js.map +1 -1
  40. package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +7 -0
  41. package/v1/serializer/v3/schemas/converter/transaction-converter.js +37 -0
  42. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -0
  43. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-cosmos.d.ts +4 -0
  44. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-cosmos.js +3 -0
  45. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-cosmos.js.map +1 -0
  46. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-cosmos.d.ts +3 -0
  47. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-cosmos.js +3 -0
  48. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-cosmos.js.map +1 -0
  49. package/v1/serializer/v3/schemas/generated/transaction-sign-request-cosmos.json +100 -0
  50. package/v1/serializer/v3/schemas/generated/transaction-sign-response-cosmos.json +19 -0
  51. package/v1/serializer/v3/serializer-companion.js +7 -7
  52. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  53. package/v1/serializer/v3/validators/transaction-validators.d.ts +7 -0
  54. package/v1/serializer/v3/validators/transaction-validators.js +88 -0
  55. package/v1/serializer/v3/validators/transaction-validators.js.map +1 -0
  56. package/v1/types/crypto.d.ts +2 -0
  57. package/v1/types/crypto.js +3 -0
  58. package/v1/types/crypto.js.map +1 -0
  59. package/v1/types/protocol.d.ts +8 -0
  60. package/v1/types/protocol.js +3 -0
  61. package/v1/types/protocol.js.map +1 -0
  62. package/v1/types/rpc.d.ts +286 -0
  63. package/v1/types/rpc.js +3 -0
  64. package/v1/types/rpc.js.map +1 -0
  65. package/v1/types/transaction.d.ts +44 -0
  66. package/v1/types/transaction.js +10 -0
  67. package/v1/types/transaction.js.map +1 -0
  68. package/v1/utils/key.d.ts +3 -0
  69. package/v1/utils/key.js +20 -0
  70. package/v1/utils/key.js.map +1 -0
  71. package/v1/utils/signature.d.ts +2 -0
  72. package/v1/utils/signature.js +13 -0
  73. package/v1/utils/signature.js.map +1 -0
  74. package/v1/utils/transaction.d.ts +1 -0
  75. package/v1/utils/transaction.js +8 -0
  76. package/v1/utils/transaction.js.map +1 -0
@@ -1,19 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  var __assign = (this && this.__assign) || function () {
18
3
  __assign = Object.assign || function(t) {
19
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -61,22 +46,43 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
61
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
47
  }
63
48
  };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
64
52
  Object.defineProperty(exports, "__esModule", { value: true });
65
53
  exports.createCosmosProtocolOptions = exports.COSMOS_MAINNET_PROTOCOL_NETWORK = exports.createCosmosProtocol = exports.CosmosProtocolImpl = void 0;
66
54
  var coinlib_core_1 = require("@airgap/coinlib-core");
55
+ var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
56
+ // @ts-ignore
57
+ var index_1 = require("@airgap/coinlib-core/dependencies/src/bip32-2.0.4/src/index");
58
+ var cosmjs_1 = require("@airgap/coinlib-core/dependencies/src/cosmjs");
59
+ // @ts-ignore
60
+ var SECP256K1 = require("@airgap/coinlib-core/dependencies/src/secp256k1-3.7.1/elliptic");
61
+ // @ts-ignore
62
+ var sha = require("@airgap/coinlib-core/dependencies/src/sha.js-2.4.11/index");
63
+ var errors_1 = require("@airgap/coinlib-core/errors");
64
+ var crypto_1 = require("@airgap/crypto");
67
65
  var module_kit_1 = require("@airgap/module-kit");
68
- var cosmos_core_1 = require("@airgap/cosmos-core");
66
+ var CosmosAddress_1 = require("../data/CosmosAddress");
67
+ var CosmosCoin_1 = require("../data/CosmosCoin");
68
+ var CosmosFee_1 = require("../data/CosmosFee");
69
+ var CosmosTransaction_1 = require("../data/transaction/CosmosTransaction");
70
+ var CosmosDelegateMessage_1 = require("../data/transaction/message/CosmosDelegateMessage");
71
+ var CosmosMessage_1 = require("../data/transaction/message/CosmosMessage");
72
+ var CosmosSendMessage_1 = require("../data/transaction/message/CosmosSendMessage");
73
+ var CosmosWithdrawDelegationRewardMessage_1 = require("../data/transaction/message/CosmosWithdrawDelegationRewardMessage");
74
+ var CosmosNodeClient_1 = require("../node/CosmosNodeClient");
75
+ var key_1 = require("../utils/key");
76
+ var signature_1 = require("../utils/signature");
77
+ var transaction_1 = require("../utils/transaction");
78
+ var CosmosCryptoClient_1 = require("./CosmosCryptoClient");
69
79
  // Implementation
70
80
  var DEFAULT_GAS = (0, module_kit_1.newAmount)('200000', 'blockchain');
71
- var CosmosProtocolImpl = /** @class */ (function (_super) {
72
- __extends(CosmosProtocolImpl, _super);
81
+ var CosmosProtocolImpl = /** @class */ (function () {
73
82
  function CosmosProtocolImpl(options) {
74
83
  if (options === void 0) { options = {}; }
75
- var _this = this;
76
- var fullOptions = createCosmosProtocolOptions(options);
77
- _this = _super.call(this, fullOptions) || this;
78
84
  // Common
79
- _this.units = {
85
+ this.units = {
80
86
  atom: {
81
87
  symbol: { value: 'ATOM', market: 'atom' },
82
88
  decimals: 6
@@ -86,18 +92,18 @@ var CosmosProtocolImpl = /** @class */ (function (_super) {
86
92
  decimals: 0
87
93
  }
88
94
  };
89
- _this.feeDefaults = {
90
- low: (0, module_kit_1.newAmount)(0.0005, 'atom').blockchain(_this.units),
91
- medium: (0, module_kit_1.newAmount)(0.005, 'atom').blockchain(_this.units),
92
- high: (0, module_kit_1.newAmount)(0.0072, 'atom').blockchain(_this.units)
95
+ this.feeDefaults = {
96
+ low: (0, module_kit_1.newAmount)(0.0005, 'atom').blockchain(this.units),
97
+ medium: (0, module_kit_1.newAmount)(0.005, 'atom').blockchain(this.units),
98
+ high: (0, module_kit_1.newAmount)(0.0072, 'atom').blockchain(this.units)
93
99
  };
94
- _this.metadata = {
100
+ this.metadata = {
95
101
  identifier: coinlib_core_1.MainProtocolSymbols.COSMOS,
96
102
  name: 'Cosmos',
97
- units: _this.units,
103
+ units: this.units,
98
104
  mainUnit: 'atom',
99
105
  fee: {
100
- defaults: _this.feeDefaults
106
+ defaults: this.feeDefaults
101
107
  },
102
108
  account: {
103
109
  standardDerivationPath: "m/44'/118'/0'/0/0",
@@ -113,7 +119,13 @@ var CosmosProtocolImpl = /** @class */ (function (_super) {
113
119
  }
114
120
  }
115
121
  };
116
- return _this;
122
+ // Offline
123
+ this.cryptoConfiguration = {
124
+ algorithm: 'secp256k1'
125
+ };
126
+ this.options = createCosmosProtocolOptions(options.network);
127
+ this.nodeClient = new CosmosNodeClient_1.CosmosNodeClient(this.options.network.rpcUrl, this.options.network.useCORSProxy);
128
+ this.cryptoClient = new CosmosCryptoClient_1.CosmosCryptoClient();
117
129
  }
118
130
  CosmosProtocolImpl.prototype.getMetadata = function () {
119
131
  return __awaiter(this, void 0, void 0, function () {
@@ -122,6 +134,415 @@ var CosmosProtocolImpl = /** @class */ (function (_super) {
122
134
  });
123
135
  });
124
136
  };
137
+ CosmosProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
138
+ return __awaiter(this, void 0, void 0, function () {
139
+ return __generator(this, function (_a) {
140
+ return [2 /*return*/, CosmosAddress_1.CosmosAddress.from(publicKey).asString()];
141
+ });
142
+ });
143
+ };
144
+ CosmosProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, _publicKey) {
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ return __generator(this, function (_a) {
147
+ switch (transaction.type) {
148
+ case 'signed':
149
+ return [2 /*return*/, this.getDetailsFromSignedTransaction(transaction)];
150
+ case 'unsigned':
151
+ return [2 /*return*/, this.getDetailsFromUnsignedTransaction(transaction)];
152
+ default:
153
+ (0, coinlib_core_1.assertNever)(transaction);
154
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.COSMOS, 'Unsupported transaction type.');
155
+ }
156
+ return [2 /*return*/];
157
+ });
158
+ });
159
+ };
160
+ CosmosProtocolImpl.prototype.getDetailsFromSignedTransaction = function (transaction) {
161
+ var _a;
162
+ return __awaiter(this, void 0, void 0, function () {
163
+ var bytes, decoded, fee, result;
164
+ var _this = this;
165
+ return __generator(this, function (_b) {
166
+ switch (_b.label) {
167
+ case 0:
168
+ bytes = Uint8Array.from(Buffer.from(transaction.encoded, 'base64'));
169
+ return [4 /*yield*/, (0, cosmjs_1.decodeTxBytes)(bytes)];
170
+ case 1:
171
+ decoded = _b.sent();
172
+ fee = ((_a = decoded.fee) === null || _a === void 0 ? void 0 : _a.amount)
173
+ .map(function (_a) {
174
+ var amount = _a.amount;
175
+ return new bignumber_1.default(amount);
176
+ })
177
+ .reduce(function (current, next) { return current.plus(next); })
178
+ .toString(10);
179
+ result = decoded.messages
180
+ .map(function (message) {
181
+ var type = message.typeUrl;
182
+ switch (type) {
183
+ case CosmosMessage_1.CosmosMessageType.Send.value:
184
+ var sendMessage = CosmosSendMessage_1.CosmosSendMessage.fromEncodeObject(message);
185
+ return sendMessage.toAirGapTransaction(_this.options.network, fee);
186
+ case CosmosMessage_1.CosmosMessageType.Undelegate.value:
187
+ case CosmosMessage_1.CosmosMessageType.Delegate.value:
188
+ var delegateMessage = CosmosDelegateMessage_1.CosmosDelegateMessage.fromEncodeObject(message);
189
+ return delegateMessage.toAirGapTransaction(_this.options.network, fee);
190
+ case CosmosMessage_1.CosmosMessageType.WithdrawDelegationReward.value:
191
+ var withdrawMessage = CosmosWithdrawDelegationRewardMessage_1.CosmosWithdrawDelegationRewardMessage.fromEncodeObject(message);
192
+ return withdrawMessage.toAirGapTransaction(_this.options.network, fee);
193
+ default:
194
+ throw new errors_1.InvalidValueError(coinlib_core_1.Domain.COSMOS, 'Unknown transaction');
195
+ }
196
+ })
197
+ .map(function (tx) {
198
+ if (!tx.json) {
199
+ tx.json = {};
200
+ }
201
+ tx.json.memo = decoded.memo;
202
+ if (decoded.signerInfos.length > 0) {
203
+ tx.json.sequence = decoded.signerInfos[0].sequence.toString(10);
204
+ }
205
+ tx.arbitraryData = decoded.memo;
206
+ return tx;
207
+ });
208
+ return [2 /*return*/, result];
209
+ }
210
+ });
211
+ });
212
+ };
213
+ CosmosProtocolImpl.prototype.getDetailsFromUnsignedTransaction = function (transaction) {
214
+ return __awaiter(this, void 0, void 0, function () {
215
+ return __generator(this, function (_a) {
216
+ return [2 /*return*/, CosmosTransaction_1.CosmosTransaction.fromJSON(transaction).toAirGapTransactions(this.options.network)];
217
+ });
218
+ });
219
+ };
220
+ CosmosProtocolImpl.prototype.verifyMessageWithPublicKey = function (message, signature, publicKey) {
221
+ return __awaiter(this, void 0, void 0, function () {
222
+ var hexSignature, hexPublicKey;
223
+ return __generator(this, function (_a) {
224
+ hexSignature = (0, signature_1.convertSignature)(signature, 'hex');
225
+ hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
226
+ return [2 /*return*/, this.cryptoClient.verifyMessage(message, hexSignature.value, hexPublicKey.value)];
227
+ });
228
+ });
229
+ };
230
+ CosmosProtocolImpl.prototype.encryptAsymmetricWithPublicKey = function (payload, publicKey) {
231
+ return __awaiter(this, void 0, void 0, function () {
232
+ var hexPublicKey;
233
+ return __generator(this, function (_a) {
234
+ hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
235
+ return [2 /*return*/, this.cryptoClient.encryptAsymmetric(payload, hexPublicKey.value)];
236
+ });
237
+ });
238
+ };
239
+ CosmosProtocolImpl.prototype.getCryptoConfiguration = function () {
240
+ return __awaiter(this, void 0, void 0, function () {
241
+ return __generator(this, function (_a) {
242
+ return [2 /*return*/, this.cryptoConfiguration];
243
+ });
244
+ });
245
+ };
246
+ CosmosProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
247
+ return __awaiter(this, void 0, void 0, function () {
248
+ var bip32Node, bip32, secretKey, publicKey;
249
+ return __generator(this, function (_a) {
250
+ bip32Node = (0, crypto_1.encodeDerivative)('bip32', derivative);
251
+ bip32 = (0, index_1.fromBase58)(bip32Node.secretKey);
252
+ secretKey = bip32.privateKey;
253
+ if (secretKey === undefined) {
254
+ throw new errors_1.InvalidValueError(coinlib_core_1.Domain.COSMOS, 'Cannot generate secret key');
255
+ }
256
+ publicKey = bip32.publicKey;
257
+ return [2 /*return*/, {
258
+ secretKey: (0, module_kit_1.newSecretKey)(secretKey.toString('hex'), 'hex'),
259
+ publicKey: (0, module_kit_1.newPublicKey)(publicKey.toString('hex'), 'hex')
260
+ }];
261
+ });
262
+ });
263
+ };
264
+ CosmosProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
265
+ return __awaiter(this, void 0, void 0, function () {
266
+ var hexSecretKey, privateKeyBuffer, publicKey, hexPublicKey, publicKeyBuffer, encodedObject, signBytes, sha256Hash, hash, signed, sigBase64, txBytes;
267
+ return __generator(this, function (_a) {
268
+ switch (_a.label) {
269
+ case 0:
270
+ hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
271
+ privateKeyBuffer = Buffer.from(hexSecretKey.value, 'hex');
272
+ return [4 /*yield*/, this.getKeyPairFromSecretKey(secretKey)];
273
+ case 1:
274
+ publicKey = (_a.sent()).publicKey;
275
+ hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
276
+ publicKeyBuffer = Buffer.from(hexPublicKey.value, 'hex');
277
+ encodedObject = CosmosTransaction_1.CosmosTransaction.fromJSON(transaction).toEncodeObject();
278
+ return [4 /*yield*/, (0, cosmjs_1.prepareSignBytes)(encodedObject, transaction.fee, Uint8Array.from(publicKeyBuffer), new bignumber_1.default(transaction.sequence).toNumber(), transaction.chainID, new bignumber_1.default(transaction.accountNumber).toNumber())];
279
+ case 2:
280
+ signBytes = _a.sent();
281
+ sha256Hash = sha('sha256').update(signBytes).digest();
282
+ hash = Buffer.from(sha256Hash);
283
+ signed = SECP256K1.sign(hash, privateKeyBuffer);
284
+ sigBase64 = Buffer.from(signed.signature, 'binary').toString('base64');
285
+ return [4 /*yield*/, (0, cosmjs_1.encodeTxBytes)(encodedObject, transaction.fee, Uint8Array.from(publicKeyBuffer), new bignumber_1.default(transaction.sequence).toNumber(), {
286
+ signature: sigBase64,
287
+ pub_key: {
288
+ type: 'tendermint/PubKeySecp256k1',
289
+ value: publicKeyBuffer.toString('base64')
290
+ }
291
+ }, transaction.chainID, new bignumber_1.default(transaction.accountNumber).toNumber())];
292
+ case 3:
293
+ txBytes = _a.sent();
294
+ return [2 /*return*/, (0, module_kit_1.newSignedTransaction)({
295
+ encoded: Buffer.from(txBytes).toString('base64')
296
+ })];
297
+ }
298
+ });
299
+ });
300
+ };
301
+ CosmosProtocolImpl.prototype.signMessageWithKeyPair = function (message, keyPair) {
302
+ return __awaiter(this, void 0, void 0, function () {
303
+ var hexSecretKey, signature;
304
+ return __generator(this, function (_a) {
305
+ switch (_a.label) {
306
+ case 0:
307
+ hexSecretKey = (0, key_1.convertSecretKey)(keyPair.secretKey, 'hex');
308
+ return [4 /*yield*/, this.cryptoClient.signMessage(message, { privateKey: hexSecretKey.value })];
309
+ case 1:
310
+ signature = _a.sent();
311
+ return [2 /*return*/, (0, module_kit_1.newSignature)(signature, 'hex')];
312
+ }
313
+ });
314
+ });
315
+ };
316
+ CosmosProtocolImpl.prototype.decryptAsymmetricWithKeyPair = function (payload, keyPair) {
317
+ return __awaiter(this, void 0, void 0, function () {
318
+ var hexSecretKey, hexPublicKey;
319
+ return __generator(this, function (_a) {
320
+ hexSecretKey = (0, key_1.convertSecretKey)(keyPair.secretKey, 'hex');
321
+ hexPublicKey = (0, key_1.convertPublicKey)(keyPair.publicKey, 'hex');
322
+ return [2 /*return*/, this.cryptoClient.decryptAsymmetric(payload, {
323
+ privateKey: hexSecretKey.value,
324
+ publicKey: hexPublicKey.value
325
+ })];
326
+ });
327
+ });
328
+ };
329
+ CosmosProtocolImpl.prototype.encryptAESWithSecretKey = function (payload, secretKey) {
330
+ return __awaiter(this, void 0, void 0, function () {
331
+ var hexSecretKey;
332
+ return __generator(this, function (_a) {
333
+ hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
334
+ return [2 /*return*/, this.cryptoClient.encryptAES(payload, hexSecretKey.value)];
335
+ });
336
+ });
337
+ };
338
+ CosmosProtocolImpl.prototype.decryptAESWithSecretKey = function (payload, secretKey) {
339
+ return __awaiter(this, void 0, void 0, function () {
340
+ var hexSecretKey;
341
+ return __generator(this, function (_a) {
342
+ hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
343
+ return [2 /*return*/, this.cryptoClient.decryptAES(payload, hexSecretKey.value)];
344
+ });
345
+ });
346
+ };
347
+ // Online
348
+ CosmosProtocolImpl.prototype.getNetwork = function () {
349
+ return __awaiter(this, void 0, void 0, function () {
350
+ return __generator(this, function (_a) {
351
+ return [2 /*return*/, this.options.network];
352
+ });
353
+ });
354
+ };
355
+ CosmosProtocolImpl.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
356
+ return __awaiter(this, void 0, void 0, function () {
357
+ var address;
358
+ return __generator(this, function (_a) {
359
+ switch (_a.label) {
360
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
361
+ case 1:
362
+ address = _a.sent();
363
+ return [2 /*return*/, this.getTransactionsForAddress(address, limit, cursor)];
364
+ }
365
+ });
366
+ });
367
+ };
368
+ CosmosProtocolImpl.prototype.getTransactionsForAddress = function (address, limit, cursor) {
369
+ return __awaiter(this, void 0, void 0, function () {
370
+ var promises, senderOffset, recipientOffset, senderTotal, recipientTotal, senderLimit, recipientLimit, transactions, sentTransactions, receivedTransactions, allTransactions, result, _loop_1, _i, allTransactions_1, transaction;
371
+ var _a;
372
+ var _this = this;
373
+ return __generator(this, function (_b) {
374
+ switch (_b.label) {
375
+ case 0:
376
+ promises = [];
377
+ senderOffset = 0;
378
+ recipientOffset = 0;
379
+ senderTotal = 0;
380
+ recipientTotal = 0;
381
+ senderLimit = 0;
382
+ recipientLimit = 0;
383
+ if (!cursor) return [3 /*break*/, 1];
384
+ senderOffset = cursor.sender.offset;
385
+ recipientOffset = cursor.recipient.offset;
386
+ senderTotal = cursor.sender.total;
387
+ recipientTotal = cursor.recipient.total;
388
+ senderLimit = (0, transaction_1.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
389
+ if (senderOffset <= Math.floor(senderTotal / senderLimit) * senderLimit) {
390
+ promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true));
391
+ }
392
+ else {
393
+ promises.push(new Promise(function (resolve) {
394
+ resolve({ txs: [], tx_responses: [], pagination: { total: String(senderTotal) } });
395
+ }));
396
+ }
397
+ recipientLimit = (0, transaction_1.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
398
+ if (recipientOffset <= Math.floor(recipientTotal / recipientLimit) * recipientLimit) {
399
+ promises.push(this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
400
+ }
401
+ else {
402
+ promises.push(new Promise(function (resolve) {
403
+ resolve({ txs: [], tx_responses: [], pagination: { total: String(recipientTotal) } });
404
+ }));
405
+ }
406
+ return [3 /*break*/, 3];
407
+ case 1:
408
+ ;
409
+ return [4 /*yield*/, Promise.all([
410
+ this.nodeClient
411
+ .fetchSendTransactionsFor(address, 1, 0, true)
412
+ .then(function (response) { return new bignumber_1.default(response.pagination.total).toNumber(); }),
413
+ this.nodeClient
414
+ .fetchSendTransactionsFor(address, 1, 0, false)
415
+ .then(function (response) { return new bignumber_1.default(response.pagination.total).toNumber(); })
416
+ ])];
417
+ case 2:
418
+ _a = _b.sent(), senderTotal = _a[0], recipientTotal = _a[1];
419
+ senderLimit = (0, transaction_1.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
420
+ recipientLimit = (0, transaction_1.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
421
+ promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true), this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
422
+ _b.label = 3;
423
+ case 3: return [4 /*yield*/, Promise.all(promises)];
424
+ case 4:
425
+ transactions = _b.sent();
426
+ sentTransactions = transactions[0];
427
+ receivedTransactions = transactions[1];
428
+ allTransactions = sentTransactions === null || sentTransactions === void 0 ? void 0 : sentTransactions.tx_responses.concat(receivedTransactions === null || receivedTransactions === void 0 ? void 0 : receivedTransactions.tx_responses);
429
+ result = [];
430
+ _loop_1 = function (transaction) {
431
+ var timestamp = new Date(transaction.timestamp).getTime() / 1000;
432
+ var fee = transaction.tx.auth_info.fee.amount
433
+ .filter(function (coin) { return coin.denom === 'uatom'; })
434
+ .map(function (coin) { return new bignumber_1.default(coin.amount); })
435
+ .reduce(function (current, next) { return current.plus(next); });
436
+ result = result.concat(transaction.tx.body.messages.map(function (msg) {
437
+ var tx = {
438
+ isInbound: false,
439
+ amount: (0, module_kit_1.newAmount)('0', 'blockchain'),
440
+ fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
441
+ network: _this.options.network,
442
+ status: {
443
+ type: 'unknown',
444
+ hash: transaction.txhash
445
+ },
446
+ timestamp: timestamp
447
+ };
448
+ switch (msg['@type']) {
449
+ case CosmosMessage_1.CosmosMessageTypeValue.UNDELEGATE:
450
+ return __assign(__assign({}, tx), { from: [msg.validator_address], to: [msg.delegator_address] });
451
+ case CosmosMessage_1.CosmosMessageTypeValue.WITHDRAW_DELEGATION_REWARD:
452
+ return __assign(__assign({}, tx), { from: [msg.delegator_address], to: [msg.validator_address] });
453
+ case CosmosMessage_1.CosmosMessageTypeValue.DELEGATE:
454
+ return __assign(__assign({}, tx), { from: [msg.delegator_address], to: [msg.validator_address] });
455
+ default:
456
+ return __assign(__assign({}, tx), { from: [msg.from_address], to: [msg.to_address], isInbound: msg.to_address === address, amount: (0, module_kit_1.newAmount)(msg.amount[0].amount, 'blockchain') });
457
+ }
458
+ }));
459
+ };
460
+ for (_i = 0, allTransactions_1 = allTransactions; _i < allTransactions_1.length; _i++) {
461
+ transaction = allTransactions_1[_i];
462
+ _loop_1(transaction);
463
+ }
464
+ return [2 /*return*/, {
465
+ transactions: result,
466
+ cursor: {
467
+ hasNext: senderOffset + senderLimit < senderTotal || recipientOffset + recipientLimit < recipientTotal,
468
+ limit: limit,
469
+ sender: {
470
+ total: senderTotal,
471
+ offset: senderOffset + senderLimit
472
+ },
473
+ recipient: {
474
+ total: recipientTotal,
475
+ offset: recipientOffset + recipientLimit
476
+ }
477
+ }
478
+ }];
479
+ }
480
+ });
481
+ });
482
+ };
483
+ CosmosProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey) {
484
+ return __awaiter(this, void 0, void 0, function () {
485
+ var address;
486
+ return __generator(this, function (_a) {
487
+ switch (_a.label) {
488
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
489
+ case 1:
490
+ address = _a.sent();
491
+ return [2 /*return*/, this.getBalanceOfAddress(address)];
492
+ }
493
+ });
494
+ });
495
+ };
496
+ CosmosProtocolImpl.prototype.getBalanceOfAddress = function (address) {
497
+ return __awaiter(this, void 0, void 0, function () {
498
+ var balance;
499
+ return __generator(this, function (_a) {
500
+ switch (_a.label) {
501
+ case 0: return [4 /*yield*/, this.nodeClient.fetchBalance(address)];
502
+ case 1:
503
+ balance = _a.sent();
504
+ return [2 /*return*/, {
505
+ total: (0, module_kit_1.newAmount)(balance.total, 'blockchain'),
506
+ transferable: (0, module_kit_1.newAmount)(balance.available, 'blockchain')
507
+ }];
508
+ }
509
+ });
510
+ });
511
+ };
512
+ CosmosProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
513
+ return __awaiter(this, void 0, void 0, function () {
514
+ var _a, total, transferable, balance, fee, estimatedFee, amountWithoutFees;
515
+ return __generator(this, function (_b) {
516
+ switch (_b.label) {
517
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
518
+ case 1:
519
+ _a = _b.sent(), total = _a.total, transferable = _a.transferable;
520
+ balance = new bignumber_1.default((0, module_kit_1.newAmount)(transferable !== null && transferable !== void 0 ? transferable : total).blockchain(this.units).value);
521
+ if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 2];
522
+ fee = configuration.fee;
523
+ return [3 /*break*/, 4];
524
+ case 2: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, to.map(function (recipient) { return ({
525
+ to: recipient,
526
+ amount: (0, module_kit_1.newAmount)(balance.div(to.length).decimalPlaces(0, bignumber_1.default.ROUND_CEIL), 'blockchain')
527
+ }); }))];
528
+ case 3:
529
+ estimatedFee = _b.sent();
530
+ fee = (0, module_kit_1.newAmount)(estimatedFee.medium).blockchain(this.units);
531
+ if (balance.lte(fee.value)) {
532
+ fee = (0, module_kit_1.newAmount)(0, 'blockchain');
533
+ }
534
+ _b.label = 4;
535
+ case 4:
536
+ fee = (0, module_kit_1.newAmount)(fee).blockchain(this.units);
537
+ amountWithoutFees = balance.minus(fee.value);
538
+ if (amountWithoutFees.isNegative()) {
539
+ amountWithoutFees = new bignumber_1.default(0);
540
+ }
541
+ return [2 /*return*/, (0, module_kit_1.newAmount)(amountWithoutFees, 'blockchain')];
542
+ }
543
+ });
544
+ });
545
+ };
125
546
  CosmosProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (_publicKey, _details) {
126
547
  return __awaiter(this, void 0, void 0, function () {
127
548
  return __generator(this, function (_a) {
@@ -129,8 +550,84 @@ var CosmosProtocolImpl = /** @class */ (function (_super) {
129
550
  });
130
551
  });
131
552
  };
553
+ CosmosProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
554
+ var _a, _b;
555
+ return __awaiter(this, void 0, void 0, function () {
556
+ var fee, estimatedFee, wrappedFee, address, nodeInfo, account, _c, total, transferable, balance, messages, i, message, memo, transaction;
557
+ var _this = this;
558
+ return __generator(this, function (_d) {
559
+ switch (_d.label) {
560
+ case 0:
561
+ if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
562
+ fee = configuration.fee;
563
+ return [3 /*break*/, 3];
564
+ case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, details)];
565
+ case 2:
566
+ estimatedFee = _d.sent();
567
+ fee = estimatedFee.medium;
568
+ _d.label = 3;
569
+ case 3:
570
+ wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(this.units).value);
571
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
572
+ case 4:
573
+ address = _d.sent();
574
+ return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
575
+ case 5:
576
+ nodeInfo = _d.sent();
577
+ return [4 /*yield*/, this.nodeClient.fetchAccount(address)];
578
+ case 6:
579
+ account = _d.sent();
580
+ return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
581
+ case 7:
582
+ _c = _d.sent(), total = _c.total, transferable = _c.transferable;
583
+ balance = new bignumber_1.default((0, module_kit_1.newAmount)(transferable !== null && transferable !== void 0 ? transferable : total).blockchain(this.units).value);
584
+ if (balance.lt(details.reduce(function (acc, next) { return acc.plus((0, module_kit_1.newAmount)(next.amount).blockchain(_this.units).value); }, wrappedFee))) {
585
+ throw new errors_1.BalanceError(coinlib_core_1.Domain.COSMOS, 'not enough balance');
586
+ }
587
+ messages = [];
588
+ for (i = 0; i < details.length; ++i) {
589
+ message = new CosmosSendMessage_1.CosmosSendMessage(address, details[i].to, [
590
+ new CosmosCoin_1.CosmosCoin('uatom', (0, module_kit_1.newAmount)(details[i].amount).blockchain(this.units).value)
591
+ ]);
592
+ messages.push(message);
593
+ }
594
+ memo = (_a = configuration === null || configuration === void 0 ? void 0 : configuration.arbitraryData) !== null && _a !== void 0 ? _a : '';
595
+ transaction = new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin('uatom', wrappedFee.toString(10))], (0, module_kit_1.newAmount)(DEFAULT_GAS).blockchain(this.units).value), memo, nodeInfo.network, account.value.account_number, (_b = account.value.sequence) !== null && _b !== void 0 ? _b : '0');
596
+ return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({
597
+ messages: transaction.messages.map(function (message) { return message.toJSON(); }),
598
+ fee: transaction.fee.toJSON(),
599
+ memo: transaction.memo,
600
+ chainID: transaction.chainID,
601
+ accountNumber: transaction.accountNumber,
602
+ sequence: transaction.sequence
603
+ })];
604
+ }
605
+ });
606
+ });
607
+ };
608
+ CosmosProtocolImpl.prototype.broadcastTransaction = function (transaction) {
609
+ return __awaiter(this, void 0, void 0, function () {
610
+ return __generator(this, function (_a) {
611
+ return [2 /*return*/, this.nodeClient.broadcastSignedTransaction(transaction.encoded)];
612
+ });
613
+ });
614
+ };
615
+ // Custom
616
+ CosmosProtocolImpl.prototype.getKeyPairFromSecretKey = function (secretKey) {
617
+ return __awaiter(this, void 0, void 0, function () {
618
+ var hexSecretKey, publicKey;
619
+ return __generator(this, function (_a) {
620
+ hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
621
+ publicKey = SECP256K1.publicKeyCreate(Buffer.from(secretKey.value, 'hex'));
622
+ return [2 /*return*/, {
623
+ secretKey: hexSecretKey,
624
+ publicKey: (0, module_kit_1.newPublicKey)(Buffer.from(publicKey, 'binary').toString('hex'), 'hex')
625
+ }];
626
+ });
627
+ });
628
+ };
132
629
  return CosmosProtocolImpl;
133
- }(cosmos_core_1.CosmosBaseProtocolImpl));
630
+ }());
134
631
  exports.CosmosProtocolImpl = CosmosProtocolImpl;
135
632
  // Factory
136
633
  function createCosmosProtocol(options) {
@@ -144,17 +641,10 @@ exports.COSMOS_MAINNET_PROTOCOL_NETWORK = {
144
641
  rpcUrl: 'https://cosmos-node.prod.gke.papers.tech'
145
642
  };
146
643
  var DEFAULT_COSMOS_PROTOCOL_NETWORK = exports.COSMOS_MAINNET_PROTOCOL_NETWORK;
147
- function createCosmosProtocolOptions(partialOptions) {
148
- var _a, _b, _c, _d, _e, _f;
149
- if (partialOptions === void 0) { partialOptions = {}; }
644
+ function createCosmosProtocolOptions(network) {
645
+ if (network === void 0) { network = {}; }
150
646
  return {
151
- network: __assign(__assign({}, DEFAULT_COSMOS_PROTOCOL_NETWORK), partialOptions.network),
152
- addressPrefix: (_a = partialOptions.addressPrefix) !== null && _a !== void 0 ? _a : 'cosmos',
153
- baseUnit: (_b = partialOptions.baseUnit) !== null && _b !== void 0 ? _b : 'uatom',
154
- defaultGas: {
155
- value: (_d = (_c = partialOptions.defaultGas) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : DEFAULT_GAS.value,
156
- unit: (_f = (_e = partialOptions.defaultGas) === null || _e === void 0 ? void 0 : _e.unit) !== null && _f !== void 0 ? _f : DEFAULT_GAS.unit
157
- }
647
+ network: __assign(__assign({}, DEFAULT_COSMOS_PROTOCOL_NETWORK), network)
158
648
  };
159
649
  }
160
650
  exports.createCosmosProtocolOptions = createCosmosProtocolOptions;