@airgap/cosmos 0.13.10 → 0.13.11-beta.1

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