@airgap/cosmos 0.13.11-beta.1 → 0.13.11-beta.3

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 (145) 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 +1 -5
  5. package/v0/index.d.ts +10 -0
  6. package/v0/index.js +32 -0
  7. package/v0/index.js.map +1 -0
  8. package/v0/protocol/CosmosAddress.d.ts +8 -0
  9. package/v0/protocol/CosmosAddress.js +28 -0
  10. package/v0/protocol/CosmosAddress.js.map +1 -0
  11. package/v0/protocol/CosmosCoin.d.ts +19 -0
  12. package/v0/protocol/CosmosCoin.js +55 -0
  13. package/v0/protocol/CosmosCoin.js.map +1 -0
  14. package/v0/protocol/CosmosCryptoClient.d.ts +8 -0
  15. package/v0/protocol/CosmosCryptoClient.js +90 -0
  16. package/v0/protocol/CosmosCryptoClient.js.map +1 -0
  17. package/v0/protocol/CosmosFee.d.ts +15 -0
  18. package/v0/protocol/CosmosFee.js +31 -0
  19. package/v0/protocol/CosmosFee.js.map +1 -0
  20. package/v0/protocol/CosmosNodeClient.d.ts +25 -0
  21. package/v0/protocol/CosmosNodeClient.js +382 -0
  22. package/v0/protocol/CosmosNodeClient.js.map +1 -0
  23. package/v0/protocol/CosmosProtocol.d.ts +124 -0
  24. package/v0/protocol/CosmosProtocol.js +1155 -0
  25. package/v0/protocol/CosmosProtocol.js.map +1 -0
  26. package/v0/protocol/CosmosProtocolOptions.d.ts +22 -0
  27. package/v0/protocol/CosmosProtocolOptions.js +116 -0
  28. package/v0/protocol/CosmosProtocolOptions.js.map +1 -0
  29. package/v0/protocol/CosmosTransaction.d.ts +37 -0
  30. package/v0/protocol/CosmosTransaction.js +106 -0
  31. package/v0/protocol/CosmosTransaction.js.map +1 -0
  32. package/v0/protocol/CosmosTypes.d.ts +310 -0
  33. package/v0/protocol/CosmosTypes.js +8 -0
  34. package/v0/protocol/CosmosTypes.js.map +1 -0
  35. package/v0/protocol/cosmos-message/CosmosDelegateMessage.d.ts +20 -0
  36. package/v0/protocol/cosmos-message/CosmosDelegateMessage.js +72 -0
  37. package/v0/protocol/cosmos-message/CosmosDelegateMessage.js.map +1 -0
  38. package/v0/protocol/cosmos-message/CosmosMessage.d.ts +39 -0
  39. package/v0/protocol/cosmos-message/CosmosMessage.js +47 -0
  40. package/v0/protocol/cosmos-message/CosmosMessage.js.map +1 -0
  41. package/v0/protocol/cosmos-message/CosmosSendMessage.d.ts +20 -0
  42. package/v0/protocol/cosmos-message/CosmosSendMessage.js +85 -0
  43. package/v0/protocol/cosmos-message/CosmosSendMessage.js.map +1 -0
  44. package/v0/protocol/cosmos-message/CosmosWithdrawDelegationRewardMessage.d.ts +24 -0
  45. package/v0/protocol/cosmos-message/CosmosWithdrawDelegationRewardMessage.js +61 -0
  46. package/v0/protocol/cosmos-message/CosmosWithdrawDelegationRewardMessage.js.map +1 -0
  47. package/v0/serializer/schemas/v2/transaction-sign-request-cosmos.json +100 -0
  48. package/v0/serializer/schemas/v2/transaction-sign-response-cosmos.json +22 -0
  49. package/v0/serializer/schemas/v3/transaction-sign-request-cosmos.json +100 -0
  50. package/v0/serializer/schemas/v3/transaction-sign-response-cosmos.json +23 -0
  51. package/v0/serializer/validators/transaction-validator.d.ts +15 -0
  52. package/v0/serializer/validators/transaction-validator.js +112 -0
  53. package/v0/serializer/validators/transaction-validator.js.map +1 -0
  54. package/v0/types/signed-transaction-cosmos.d.ts +5 -0
  55. package/v0/types/signed-transaction-cosmos.js +3 -0
  56. package/v0/types/signed-transaction-cosmos.js.map +1 -0
  57. package/v0/types/transaction-cosmos.d.ts +5 -0
  58. package/v0/types/transaction-cosmos.js +3 -0
  59. package/v0/types/transaction-cosmos.js.map +1 -0
  60. package/v0/types/unsigned-transaction-cosmos.d.ts +33 -0
  61. package/v0/types/unsigned-transaction-cosmos.js +10 -0
  62. package/v0/types/unsigned-transaction-cosmos.js.map +1 -0
  63. package/v1/block-explorer/MintscanBlockExplorer.d.ts +9 -0
  64. package/v1/block-explorer/MintscanBlockExplorer.js +74 -0
  65. package/v1/block-explorer/MintscanBlockExplorer.js.map +1 -0
  66. package/v1/data/CosmosAddress.d.ts +7 -0
  67. package/v1/data/CosmosAddress.js +29 -0
  68. package/v1/data/CosmosAddress.js.map +1 -0
  69. package/v1/data/CosmosCoin.d.ts +19 -0
  70. package/v1/data/CosmosCoin.js +55 -0
  71. package/v1/data/CosmosCoin.js.map +1 -0
  72. package/v1/data/CosmosFee.d.ts +15 -0
  73. package/v1/data/CosmosFee.js +31 -0
  74. package/v1/data/CosmosFee.js.map +1 -0
  75. package/v1/data/transaction/CosmosTransaction.d.ts +36 -0
  76. package/v1/data/transaction/CosmosTransaction.js +109 -0
  77. package/v1/data/transaction/CosmosTransaction.js.map +1 -0
  78. package/v1/data/transaction/message/CosmosDelegateMessage.d.ts +19 -0
  79. package/v1/data/transaction/message/CosmosDelegateMessage.js +70 -0
  80. package/v1/data/transaction/message/CosmosDelegateMessage.js.map +1 -0
  81. package/v1/data/transaction/message/CosmosMessage.d.ts +38 -0
  82. package/v1/data/transaction/message/CosmosMessage.js +47 -0
  83. package/v1/data/transaction/message/CosmosMessage.js.map +1 -0
  84. package/v1/data/transaction/message/CosmosSendMessage.d.ts +19 -0
  85. package/v1/data/transaction/message/CosmosSendMessage.js +83 -0
  86. package/v1/data/transaction/message/CosmosSendMessage.js.map +1 -0
  87. package/v1/data/transaction/message/CosmosWithdrawDelegationRewardMessage.d.ts +23 -0
  88. package/v1/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js +61 -0
  89. package/v1/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js.map +1 -0
  90. package/v1/index.d.ts +10 -0
  91. package/v1/index.js +11 -0
  92. package/v1/index.js.map +1 -0
  93. package/v1/module/CosmosModule.d.ts +15 -0
  94. package/v1/module/CosmosModule.js +107 -0
  95. package/v1/module/CosmosModule.js.map +1 -0
  96. package/v1/module.d.ts +3 -0
  97. package/v1/module.js +24 -0
  98. package/v1/module.js.map +1 -0
  99. package/v1/node/CosmosNodeClient.d.ts +28 -0
  100. package/v1/node/CosmosNodeClient.js +381 -0
  101. package/v1/node/CosmosNodeClient.js.map +1 -0
  102. package/v1/protocol/CosmosCryptoClient.d.ts +8 -0
  103. package/v1/protocol/CosmosCryptoClient.js +90 -0
  104. package/v1/protocol/CosmosCryptoClient.js.map +1 -0
  105. package/v1/protocol/CosmosProtocol.d.ts +53 -0
  106. package/v1/protocol/CosmosProtocol.js +651 -0
  107. package/v1/protocol/CosmosProtocol.js.map +1 -0
  108. package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +7 -0
  109. package/v1/serializer/v3/schemas/converter/transaction-converter.js +37 -0
  110. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -0
  111. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-cosmos.d.ts +4 -0
  112. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-cosmos.js +3 -0
  113. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-cosmos.js.map +1 -0
  114. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-cosmos.d.ts +3 -0
  115. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-cosmos.js +3 -0
  116. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-cosmos.js.map +1 -0
  117. package/v1/serializer/v3/schemas/generated/transaction-sign-request-cosmos.json +100 -0
  118. package/v1/serializer/v3/schemas/generated/transaction-sign-response-cosmos.json +19 -0
  119. package/v1/serializer/v3/serializer-companion.d.ts +12 -0
  120. package/v1/serializer/v3/serializer-companion.js +142 -0
  121. package/v1/serializer/v3/serializer-companion.js.map +1 -0
  122. package/v1/serializer/v3/validators/transaction-validators.d.ts +7 -0
  123. package/v1/serializer/v3/validators/transaction-validators.js +88 -0
  124. package/v1/serializer/v3/validators/transaction-validators.js.map +1 -0
  125. package/v1/types/crypto.d.ts +2 -0
  126. package/v1/types/crypto.js +3 -0
  127. package/v1/types/crypto.js.map +1 -0
  128. package/v1/types/protocol.d.ts +8 -0
  129. package/v1/types/protocol.js +3 -0
  130. package/v1/types/protocol.js.map +1 -0
  131. package/v1/types/rpc.d.ts +286 -0
  132. package/v1/types/rpc.js +3 -0
  133. package/v1/types/rpc.js.map +1 -0
  134. package/v1/types/transaction.d.ts +44 -0
  135. package/v1/types/transaction.js +10 -0
  136. package/v1/types/transaction.js.map +1 -0
  137. package/v1/utils/key.d.ts +3 -0
  138. package/v1/utils/key.js +20 -0
  139. package/v1/utils/key.js.map +1 -0
  140. package/v1/utils/signature.d.ts +2 -0
  141. package/v1/utils/signature.js +13 -0
  142. package/v1/utils/signature.js.map +1 -0
  143. package/v1/utils/transaction.d.ts +1 -0
  144. package/v1/utils/transaction.js +8 -0
  145. package/v1/utils/transaction.js.map +1 -0
@@ -0,0 +1,651 @@
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.createCosmosProtocolOptions = exports.COSMOS_MAINNET_PROTOCOL_NETWORK = exports.createCosmosProtocol = exports.CosmosProtocolImpl = void 0;
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");
65
+ var module_kit_1 = require("@airgap/module-kit");
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");
79
+ // Implementation
80
+ var DEFAULT_GAS = (0, module_kit_1.newAmount)('200000', 'blockchain');
81
+ var CosmosProtocolImpl = /** @class */ (function () {
82
+ function CosmosProtocolImpl(options) {
83
+ if (options === void 0) { options = {}; }
84
+ // Common
85
+ this.units = {
86
+ atom: {
87
+ symbol: { value: 'ATOM', market: 'atom' },
88
+ decimals: 6
89
+ },
90
+ uatom: {
91
+ symbol: { value: 'uATOM' },
92
+ decimals: 0
93
+ }
94
+ };
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)
99
+ };
100
+ this.metadata = {
101
+ identifier: coinlib_core_1.MainProtocolSymbols.COSMOS,
102
+ name: 'Cosmos',
103
+ units: this.units,
104
+ mainUnit: 'atom',
105
+ fee: {
106
+ defaults: this.feeDefaults
107
+ },
108
+ account: {
109
+ standardDerivationPath: "m/44'/118'/0'/0/0",
110
+ address: {
111
+ isCaseSensitive: false,
112
+ placeholder: 'cosmos...',
113
+ regex: '^(cosmos|cosmosvaloper)[a-zA-Z0-9]{39}$'
114
+ }
115
+ },
116
+ transaction: {
117
+ arbitraryData: {
118
+ name: 'memo'
119
+ }
120
+ }
121
+ };
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();
129
+ }
130
+ CosmosProtocolImpl.prototype.getMetadata = function () {
131
+ return __awaiter(this, void 0, void 0, function () {
132
+ return __generator(this, function (_a) {
133
+ return [2 /*return*/, this.metadata];
134
+ });
135
+ });
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
+ };
546
+ CosmosProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (_publicKey, _details) {
547
+ return __awaiter(this, void 0, void 0, function () {
548
+ return __generator(this, function (_a) {
549
+ return [2 /*return*/, this.feeDefaults];
550
+ });
551
+ });
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
+ };
629
+ return CosmosProtocolImpl;
630
+ }());
631
+ exports.CosmosProtocolImpl = CosmosProtocolImpl;
632
+ // Factory
633
+ function createCosmosProtocol(options) {
634
+ if (options === void 0) { options = {}; }
635
+ return new CosmosProtocolImpl(options);
636
+ }
637
+ exports.createCosmosProtocol = createCosmosProtocol;
638
+ exports.COSMOS_MAINNET_PROTOCOL_NETWORK = {
639
+ name: 'Mainnet',
640
+ type: 'mainnet',
641
+ rpcUrl: 'https://cosmos-node.prod.gke.papers.tech'
642
+ };
643
+ var DEFAULT_COSMOS_PROTOCOL_NETWORK = exports.COSMOS_MAINNET_PROTOCOL_NETWORK;
644
+ function createCosmosProtocolOptions(network) {
645
+ if (network === void 0) { network = {}; }
646
+ return {
647
+ network: __assign(__assign({}, DEFAULT_COSMOS_PROTOCOL_NETWORK), network)
648
+ };
649
+ }
650
+ exports.createCosmosProtocolOptions = createCosmosProtocolOptions;
651
+ //# sourceMappingURL=CosmosProtocol.js.map