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

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,1155 @@
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
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (_) try {
44
+ 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;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
64
+ var __importDefault = (this && this.__importDefault) || function (mod) {
65
+ return (mod && mod.__esModule) ? mod : { "default": mod };
66
+ };
67
+ Object.defineProperty(exports, "__esModule", { value: true });
68
+ exports.CosmosProtocol = exports.CosmosDelegationActionType = void 0;
69
+ var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
70
+ // @ts-ignore
71
+ var index_1 = require("@airgap/coinlib-core/dependencies/src/bip32-2.0.4/src/index");
72
+ // @ts-ignore
73
+ var index_2 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0/index");
74
+ var cosmjs_1 = require("@airgap/coinlib-core/dependencies/src/cosmjs");
75
+ // @ts-ignore
76
+ var SECP256K1 = require("@airgap/coinlib-core/dependencies/src/secp256k1-3.7.1/elliptic");
77
+ // @ts-ignore
78
+ var sha = require("@airgap/coinlib-core/dependencies/src/sha.js-2.4.11/index");
79
+ var errors_1 = require("@airgap/coinlib-core/errors");
80
+ var coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
81
+ var NonExtendedProtocol_1 = require("@airgap/coinlib-core/protocols/NonExtendedProtocol");
82
+ var assert_1 = require("@airgap/coinlib-core/utils/assert");
83
+ var ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
84
+ var CosmosDelegateMessage_1 = require("./cosmos-message/CosmosDelegateMessage");
85
+ var CosmosMessage_1 = require("./cosmos-message/CosmosMessage");
86
+ var CosmosSendMessage_1 = require("./cosmos-message/CosmosSendMessage");
87
+ var CosmosWithdrawDelegationRewardMessage_1 = require("./cosmos-message/CosmosWithdrawDelegationRewardMessage");
88
+ var CosmosAddress_1 = require("./CosmosAddress");
89
+ var CosmosCoin_1 = require("./CosmosCoin");
90
+ var CosmosCryptoClient_1 = require("./CosmosCryptoClient");
91
+ var CosmosFee_1 = require("./CosmosFee");
92
+ var CosmosTypes_1 = require("./CosmosTypes");
93
+ var CosmosProtocolOptions_1 = require("./CosmosProtocolOptions");
94
+ var CosmosTransaction_1 = require("./CosmosTransaction");
95
+ var CosmosDelegationActionType;
96
+ (function (CosmosDelegationActionType) {
97
+ CosmosDelegationActionType["DELEGATE"] = "delegate";
98
+ CosmosDelegationActionType["UNDELEGATE"] = "undelegate";
99
+ CosmosDelegationActionType["WITHDRAW_ALL_REWARDS"] = "withdraw_all_rewards";
100
+ CosmosDelegationActionType["WITHDRAW_VALIDATOR_REWARDS"] = "withdraw_validator_rewards";
101
+ })(CosmosDelegationActionType = exports.CosmosDelegationActionType || (exports.CosmosDelegationActionType = {}));
102
+ var CosmosProtocol = /** @class */ (function (_super) {
103
+ __extends(CosmosProtocol, _super);
104
+ function CosmosProtocol(options) {
105
+ if (options === void 0) { options = new CosmosProtocolOptions_1.CosmosProtocolOptions(); }
106
+ var _this = _super.call(this) || this;
107
+ _this.options = options;
108
+ _this.symbol = 'ATOM';
109
+ _this.name = 'Cosmos';
110
+ _this.marketSymbol = 'atom';
111
+ _this.feeSymbol = 'atom';
112
+ _this.feeDefaults = {
113
+ low: '0.0005',
114
+ medium: '0.005',
115
+ high: '0.0075'
116
+ };
117
+ _this.decimals = 6;
118
+ _this.feeDecimals = 6;
119
+ _this.identifier = ProtocolSymbols_1.MainProtocolSymbols.COSMOS;
120
+ _this.units = [
121
+ {
122
+ unitSymbol: 'atom',
123
+ factor: '1'
124
+ },
125
+ {
126
+ unitSymbol: 'uatom',
127
+ factor: '0.000001'
128
+ }
129
+ ];
130
+ _this.supportsHD = false;
131
+ _this.standardDerivationPath = "m/44'/118'/0'/0/0";
132
+ _this.addressIsCaseSensitive = false;
133
+ _this.addressValidationPattern = '^(cosmos|cosmosvaloper)[a-zA-Z0-9]{39}$';
134
+ _this.addressPlaceholder = 'cosmos...';
135
+ _this.defaultGas = new bignumber_1.default('200000');
136
+ _this.cryptoClient = new CosmosCryptoClient_1.CosmosCryptoClient();
137
+ return _this;
138
+ }
139
+ Object.defineProperty(CosmosProtocol.prototype, "nodeClient", {
140
+ get: function () {
141
+ return this.options.config.nodeClient;
142
+ },
143
+ enumerable: false,
144
+ configurable: true
145
+ });
146
+ CosmosProtocol.prototype.getSymbol = function () {
147
+ return __awaiter(this, void 0, void 0, function () {
148
+ return __generator(this, function (_a) {
149
+ return [2 /*return*/, this.symbol];
150
+ });
151
+ });
152
+ };
153
+ CosmosProtocol.prototype.getName = function () {
154
+ return __awaiter(this, void 0, void 0, function () {
155
+ return __generator(this, function (_a) {
156
+ return [2 /*return*/, this.name];
157
+ });
158
+ });
159
+ };
160
+ CosmosProtocol.prototype.getMarketSymbol = function () {
161
+ return __awaiter(this, void 0, void 0, function () {
162
+ return __generator(this, function (_a) {
163
+ return [2 /*return*/, this.marketSymbol];
164
+ });
165
+ });
166
+ };
167
+ CosmosProtocol.prototype.getFeeSymbol = function () {
168
+ return __awaiter(this, void 0, void 0, function () {
169
+ return __generator(this, function (_a) {
170
+ return [2 /*return*/, this.feeSymbol];
171
+ });
172
+ });
173
+ };
174
+ CosmosProtocol.prototype.getFeeDefaults = function () {
175
+ return __awaiter(this, void 0, void 0, function () {
176
+ return __generator(this, function (_a) {
177
+ return [2 /*return*/, this.feeDefaults];
178
+ });
179
+ });
180
+ };
181
+ CosmosProtocol.prototype.getDecimals = function () {
182
+ return __awaiter(this, void 0, void 0, function () {
183
+ return __generator(this, function (_a) {
184
+ return [2 /*return*/, this.decimals];
185
+ });
186
+ });
187
+ };
188
+ CosmosProtocol.prototype.getFeeDecimals = function () {
189
+ return __awaiter(this, void 0, void 0, function () {
190
+ return __generator(this, function (_a) {
191
+ return [2 /*return*/, this.feeDecimals];
192
+ });
193
+ });
194
+ };
195
+ CosmosProtocol.prototype.getIdentifier = function () {
196
+ return __awaiter(this, void 0, void 0, function () {
197
+ return __generator(this, function (_a) {
198
+ return [2 /*return*/, this.identifier];
199
+ });
200
+ });
201
+ };
202
+ CosmosProtocol.prototype.getUnits = function () {
203
+ return __awaiter(this, void 0, void 0, function () {
204
+ return __generator(this, function (_a) {
205
+ return [2 /*return*/, this.units];
206
+ });
207
+ });
208
+ };
209
+ CosmosProtocol.prototype.getSupportsHD = function () {
210
+ return __awaiter(this, void 0, void 0, function () {
211
+ return __generator(this, function (_a) {
212
+ return [2 /*return*/, this.supportsHD];
213
+ });
214
+ });
215
+ };
216
+ CosmosProtocol.prototype.getStandardDerivationPath = function () {
217
+ return __awaiter(this, void 0, void 0, function () {
218
+ return __generator(this, function (_a) {
219
+ return [2 /*return*/, this.standardDerivationPath];
220
+ });
221
+ });
222
+ };
223
+ CosmosProtocol.prototype.getAddressIsCaseSensitive = function () {
224
+ return __awaiter(this, void 0, void 0, function () {
225
+ return __generator(this, function (_a) {
226
+ return [2 /*return*/, this.addressIsCaseSensitive];
227
+ });
228
+ });
229
+ };
230
+ CosmosProtocol.prototype.getAddressValidationPattern = function () {
231
+ return __awaiter(this, void 0, void 0, function () {
232
+ return __generator(this, function (_a) {
233
+ return [2 /*return*/, this.addressValidationPattern];
234
+ });
235
+ });
236
+ };
237
+ CosmosProtocol.prototype.getAddressPlaceholder = function () {
238
+ return __awaiter(this, void 0, void 0, function () {
239
+ return __generator(this, function (_a) {
240
+ return [2 /*return*/, this.addressPlaceholder];
241
+ });
242
+ });
243
+ };
244
+ CosmosProtocol.prototype.getOptions = function () {
245
+ return __awaiter(this, void 0, void 0, function () {
246
+ return __generator(this, function (_a) {
247
+ return [2 /*return*/, this.options];
248
+ });
249
+ });
250
+ };
251
+ CosmosProtocol.prototype.getBlockExplorerLinkForAddress = function (address) {
252
+ return __awaiter(this, void 0, void 0, function () {
253
+ return __generator(this, function (_a) {
254
+ return [2 /*return*/, this.options.network.blockExplorer.getAddressLink(address)];
255
+ });
256
+ });
257
+ };
258
+ CosmosProtocol.prototype.getBlockExplorerLinkForTxId = function (txId) {
259
+ return __awaiter(this, void 0, void 0, function () {
260
+ return __generator(this, function (_a) {
261
+ return [2 /*return*/, this.options.network.blockExplorer.getTransactionLink(txId)];
262
+ });
263
+ });
264
+ };
265
+ CosmosProtocol.prototype.generateKeyPair = function (mnemonic, derivationPath, password) {
266
+ if (derivationPath === void 0) { derivationPath = this.standardDerivationPath; }
267
+ (0, index_2.validateMnemonic)(mnemonic);
268
+ var seed = (0, index_2.mnemonicToSeed)(mnemonic, password);
269
+ var node = (0, index_1.fromSeed)(seed);
270
+ return this.generateKeyPairFromNode(node, derivationPath);
271
+ };
272
+ CosmosProtocol.prototype.generateKeyPairFromNode = function (node, derivationPath) {
273
+ var keys = node.derivePath(derivationPath);
274
+ var privateKey = keys.privateKey;
275
+ if (privateKey === undefined) {
276
+ throw new errors_1.InvalidValueError(coinlib_error_1.Domain.COSMOS, 'Cannot generate private key');
277
+ }
278
+ return {
279
+ publicKey: keys.publicKey,
280
+ privateKey: privateKey
281
+ };
282
+ };
283
+ CosmosProtocol.prototype.getPublicKeyFromMnemonic = function (mnemonic, derivationPath, password) {
284
+ return __awaiter(this, void 0, void 0, function () {
285
+ return __generator(this, function (_a) {
286
+ return [2 /*return*/, this.generateKeyPair(mnemonic, derivationPath, password).publicKey.toString('hex')];
287
+ });
288
+ });
289
+ };
290
+ CosmosProtocol.prototype.getPrivateKeyFromMnemonic = function (mnemonic, derivationPath, password) {
291
+ return __awaiter(this, void 0, void 0, function () {
292
+ return __generator(this, function (_a) {
293
+ return [2 /*return*/, this.generateKeyPair(mnemonic, derivationPath, password).privateKey.toString('hex')];
294
+ });
295
+ });
296
+ };
297
+ CosmosProtocol.prototype.getPublicKeyFromHexSecret = function (secret, derivationPath) {
298
+ return __awaiter(this, void 0, void 0, function () {
299
+ var node;
300
+ return __generator(this, function (_a) {
301
+ node = (0, index_1.fromSeed)(Buffer.from(secret, 'hex'));
302
+ return [2 /*return*/, this.generateKeyPairFromNode(node, derivationPath).publicKey.toString('hex')];
303
+ });
304
+ });
305
+ };
306
+ CosmosProtocol.prototype.getPublicKeyFromPrivateKey = function (privateKey) {
307
+ var publicKey = SECP256K1.publicKeyCreate(privateKey);
308
+ return Buffer.from(publicKey, 'binary');
309
+ };
310
+ CosmosProtocol.prototype.getPrivateKeyFromHexSecret = function (secret, derivationPath) {
311
+ return __awaiter(this, void 0, void 0, function () {
312
+ var node;
313
+ return __generator(this, function (_a) {
314
+ node = (0, index_1.fromSeed)(Buffer.from(secret, 'hex'));
315
+ return [2 /*return*/, this.generateKeyPairFromNode(node, derivationPath).privateKey.toString('hex')];
316
+ });
317
+ });
318
+ };
319
+ CosmosProtocol.prototype.getAddressFromPublicKey = function (publicKey, cursor) {
320
+ return __awaiter(this, void 0, void 0, function () {
321
+ var address;
322
+ return __generator(this, function (_a) {
323
+ address = CosmosAddress_1.CosmosAddress.from(publicKey);
324
+ return [2 /*return*/, {
325
+ address: address.asString(),
326
+ cursor: { hasNext: false }
327
+ }];
328
+ });
329
+ });
330
+ };
331
+ CosmosProtocol.prototype.getAddressesFromPublicKey = function (publicKey, cursor) {
332
+ return __awaiter(this, void 0, void 0, function () {
333
+ return __generator(this, function (_a) {
334
+ switch (_a.label) {
335
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey, cursor)];
336
+ case 1: return [2 /*return*/, [_a.sent()]];
337
+ }
338
+ });
339
+ });
340
+ };
341
+ CosmosProtocol.prototype.getTransactionsFromPublicKey = function (publicKey, limit, cursor) {
342
+ return __awaiter(this, void 0, void 0, function () {
343
+ var address;
344
+ return __generator(this, function (_a) {
345
+ switch (_a.label) {
346
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
347
+ case 1:
348
+ address = _a.sent();
349
+ return [2 /*return*/, this.getTransactionsFromAddresses([address.address], limit, cursor)];
350
+ }
351
+ });
352
+ });
353
+ };
354
+ CosmosProtocol.prototype.getTransactionsFromAddresses = function (addresses, limit, cursor) {
355
+ var _a;
356
+ return __awaiter(this, void 0, void 0, function () {
357
+ var address, promises, senderOffset, recipientOffset, senderTotal, recipientTotal, senderLimit, recipientLimit, transactions, sentTransactions, receivedTransactions, allTransactions, result, _loop_1, _i, allTransactions_1, transaction;
358
+ var _b;
359
+ var _this = this;
360
+ return __generator(this, function (_c) {
361
+ switch (_c.label) {
362
+ case 0:
363
+ address = (_a = cursor === null || cursor === void 0 ? void 0 : cursor.address) !== null && _a !== void 0 ? _a : addresses[0];
364
+ promises = [];
365
+ senderOffset = 0;
366
+ recipientOffset = 0;
367
+ senderTotal = 0;
368
+ recipientTotal = 0;
369
+ senderLimit = 0;
370
+ recipientLimit = 0;
371
+ if (!cursor) return [3 /*break*/, 1];
372
+ senderOffset = cursor.sender.offset;
373
+ recipientOffset = cursor.recipient.offset;
374
+ senderTotal = cursor.sender.total;
375
+ recipientTotal = cursor.recipient.total;
376
+ senderLimit = (0, CosmosTypes_1.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
377
+ if (senderOffset <= Math.floor(senderTotal / senderLimit) * senderLimit) {
378
+ promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true));
379
+ }
380
+ else {
381
+ promises.push(new Promise(function (resolve) {
382
+ resolve({ txs: [], tx_responses: [], pagination: { total: String(senderTotal) } });
383
+ }));
384
+ }
385
+ recipientLimit = (0, CosmosTypes_1.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
386
+ if (recipientOffset <= Math.floor(recipientTotal / recipientLimit) * recipientLimit) {
387
+ promises.push(this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
388
+ }
389
+ else {
390
+ promises.push(new Promise(function (resolve) {
391
+ resolve({ txs: [], tx_responses: [], pagination: { total: String(recipientTotal) } });
392
+ }));
393
+ }
394
+ return [3 /*break*/, 3];
395
+ case 1:
396
+ ;
397
+ return [4 /*yield*/, Promise.all([
398
+ this.nodeClient
399
+ .fetchSendTransactionsFor(address, 1, 0, true)
400
+ .then(function (response) { return new bignumber_1.default(response.pagination.total).toNumber(); }),
401
+ this.nodeClient
402
+ .fetchSendTransactionsFor(address, 1, 0, false)
403
+ .then(function (response) { return new bignumber_1.default(response.pagination.total).toNumber(); })
404
+ ])];
405
+ case 2:
406
+ _b = _c.sent(), senderTotal = _b[0], recipientTotal = _b[1];
407
+ senderLimit = (0, CosmosTypes_1.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
408
+ recipientLimit = (0, CosmosTypes_1.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
409
+ promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true), this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
410
+ _c.label = 3;
411
+ case 3: return [4 /*yield*/, Promise.all(promises)];
412
+ case 4:
413
+ transactions = _c.sent();
414
+ sentTransactions = transactions[0];
415
+ receivedTransactions = transactions[1];
416
+ allTransactions = sentTransactions === null || sentTransactions === void 0 ? void 0 : sentTransactions.tx_responses.concat(receivedTransactions === null || receivedTransactions === void 0 ? void 0 : receivedTransactions.tx_responses);
417
+ result = [];
418
+ _loop_1 = function (transaction) {
419
+ var timestamp = new Date(transaction.timestamp).getTime() / 1000;
420
+ var fee = transaction.tx.auth_info.fee.amount
421
+ .filter(function (coin) { return coin.denom === 'uatom'; })
422
+ .map(function (coin) { return new bignumber_1.default(coin.amount); })
423
+ .reduce(function (current, next) { return current.plus(next); });
424
+ result = result.concat(transaction.tx.body.messages.map(function (msg) {
425
+ var tx = {
426
+ fee: fee.toFixed(),
427
+ protocolIdentifier: _this.identifier,
428
+ network: _this.options.network,
429
+ hash: transaction.txhash,
430
+ timestamp: timestamp,
431
+ isInbound: false,
432
+ amount: '0'
433
+ };
434
+ switch (msg['@type']) {
435
+ case CosmosMessage_1.CosmosMessageTypeValue.UNDELEGATE:
436
+ return __assign(__assign({}, tx), { from: [msg.validator_address], to: [msg.delegator_address] });
437
+ case CosmosMessage_1.CosmosMessageTypeValue.WITHDRAW_DELEGATION_REWARD:
438
+ return __assign(__assign({}, tx), { from: [msg.delegator_address], to: [msg.validator_address] });
439
+ case CosmosMessage_1.CosmosMessageTypeValue.DELEGATE:
440
+ return __assign(__assign({}, tx), { from: [msg.delegator_address], to: [msg.validator_address] });
441
+ default:
442
+ return __assign(__assign({}, tx), { from: [msg.from_address], to: [msg.to_address], isInbound: msg.to_address === address, amount: msg.amount[0].amount });
443
+ }
444
+ }));
445
+ };
446
+ for (_i = 0, allTransactions_1 = allTransactions; _i < allTransactions_1.length; _i++) {
447
+ transaction = allTransactions_1[_i];
448
+ _loop_1(transaction);
449
+ }
450
+ return [2 /*return*/, {
451
+ transactions: result,
452
+ cursor: {
453
+ address: address,
454
+ limit: limit,
455
+ sender: {
456
+ total: senderTotal,
457
+ offset: senderOffset + senderLimit
458
+ },
459
+ recipient: {
460
+ total: recipientTotal,
461
+ offset: recipientOffset + recipientLimit
462
+ }
463
+ }
464
+ }];
465
+ }
466
+ });
467
+ });
468
+ };
469
+ CosmosProtocol.prototype.signWithPrivateKey = function (privateKey, transaction) {
470
+ return __awaiter(this, void 0, void 0, function () {
471
+ var privateKeyBuffer, publicKey, encodedObject, signBytes, sha256Hash, hash, signed, sigBase64, txBytes;
472
+ return __generator(this, function (_a) {
473
+ switch (_a.label) {
474
+ case 0:
475
+ privateKeyBuffer = Buffer.from(privateKey, 'hex');
476
+ publicKey = this.getPublicKeyFromPrivateKey(privateKeyBuffer);
477
+ encodedObject = transaction.toEncodeObject();
478
+ return [4 /*yield*/, (0, cosmjs_1.prepareSignBytes)(encodedObject, transaction.fee, Uint8Array.from(publicKey), new bignumber_1.default(transaction.sequence).toNumber(), transaction.chainID, new bignumber_1.default(transaction.accountNumber).toNumber())];
479
+ case 1:
480
+ signBytes = _a.sent();
481
+ sha256Hash = sha('sha256').update(signBytes).digest();
482
+ hash = Buffer.from(sha256Hash);
483
+ signed = SECP256K1.sign(hash, privateKeyBuffer);
484
+ sigBase64 = Buffer.from(signed.signature, 'binary').toString('base64');
485
+ return [4 /*yield*/, (0, cosmjs_1.encodeTxBytes)(encodedObject, transaction.fee, Uint8Array.from(publicKey), new bignumber_1.default(transaction.sequence).toNumber(), {
486
+ signature: sigBase64,
487
+ pub_key: {
488
+ type: 'tendermint/PubKeySecp256k1',
489
+ value: publicKey.toString('base64')
490
+ }
491
+ }, transaction.chainID, new bignumber_1.default(transaction.accountNumber).toNumber())];
492
+ case 2:
493
+ txBytes = _a.sent();
494
+ return [2 /*return*/, Buffer.from(txBytes).toString('base64')];
495
+ }
496
+ });
497
+ });
498
+ };
499
+ CosmosProtocol.prototype.getTransactionDetails = function (transaction) {
500
+ return __awaiter(this, void 0, void 0, function () {
501
+ var result;
502
+ return __generator(this, function (_a) {
503
+ result = transaction.transaction.toAirGapTransactions(this.identifier, this.options.network);
504
+ return [2 /*return*/, result];
505
+ });
506
+ });
507
+ };
508
+ CosmosProtocol.prototype.getTransactionDetailsFromSigned = function (transaction) {
509
+ var _a;
510
+ return __awaiter(this, void 0, void 0, function () {
511
+ var bytes, decoded, fee, result;
512
+ var _this = this;
513
+ return __generator(this, function (_b) {
514
+ switch (_b.label) {
515
+ case 0:
516
+ bytes = Uint8Array.from(Buffer.from(transaction.transaction, 'base64'));
517
+ return [4 /*yield*/, (0, cosmjs_1.decodeTxBytes)(bytes)];
518
+ case 1:
519
+ decoded = _b.sent();
520
+ fee = ((_a = decoded.fee) === null || _a === void 0 ? void 0 : _a.amount)
521
+ .map(function (_a) {
522
+ var amount = _a.amount;
523
+ return new bignumber_1.default(amount);
524
+ })
525
+ .reduce(function (current, next) { return current.plus(next); })
526
+ .toString(10);
527
+ result = decoded.messages
528
+ .map(function (message) {
529
+ var type = message.typeUrl;
530
+ switch (type) {
531
+ case CosmosMessage_1.CosmosMessageType.Send.value:
532
+ var sendMessage = CosmosSendMessage_1.CosmosSendMessage.fromEncodeObject(message);
533
+ return sendMessage.toAirGapTransaction(_this.identifier, _this.options.network, fee);
534
+ case CosmosMessage_1.CosmosMessageType.Undelegate.value:
535
+ case CosmosMessage_1.CosmosMessageType.Delegate.value:
536
+ var delegateMessage = CosmosDelegateMessage_1.CosmosDelegateMessage.fromEncodeObject(message);
537
+ return delegateMessage.toAirGapTransaction(_this.identifier, _this.options.network, fee);
538
+ case CosmosMessage_1.CosmosMessageType.WithdrawDelegationReward.value:
539
+ var withdrawMessage = CosmosWithdrawDelegationRewardMessage_1.CosmosWithdrawDelegationRewardMessage.fromEncodeObject(message);
540
+ return withdrawMessage.toAirGapTransaction(_this.identifier, _this.options.network, fee);
541
+ default:
542
+ throw new errors_1.InvalidValueError(coinlib_error_1.Domain.COSMOS, 'Unknown transaction');
543
+ }
544
+ })
545
+ .map(function (tx) {
546
+ if (!tx.transactionDetails) {
547
+ tx.transactionDetails = {};
548
+ }
549
+ tx.transactionDetails.memo = decoded.memo;
550
+ if (decoded.signerInfos.length > 0) {
551
+ tx.transactionDetails.sequence = decoded.signerInfos[0].sequence.toString(10);
552
+ }
553
+ return tx;
554
+ });
555
+ return [2 /*return*/, result];
556
+ }
557
+ });
558
+ });
559
+ };
560
+ CosmosProtocol.prototype.getBalanceOfAddresses = function (addresses) {
561
+ return __awaiter(this, void 0, void 0, function () {
562
+ return __generator(this, function (_a) {
563
+ return [2 /*return*/, this.getBalance(addresses)];
564
+ });
565
+ });
566
+ };
567
+ CosmosProtocol.prototype.getBalanceOfPublicKey = function (publicKey) {
568
+ return __awaiter(this, void 0, void 0, function () {
569
+ var address;
570
+ return __generator(this, function (_a) {
571
+ switch (_a.label) {
572
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
573
+ case 1:
574
+ address = _a.sent();
575
+ return [2 /*return*/, this.getBalanceOfAddresses([address.address])];
576
+ }
577
+ });
578
+ });
579
+ };
580
+ CosmosProtocol.prototype.getAvailableBalanceOfAddresses = function (addresses) {
581
+ return __awaiter(this, void 0, void 0, function () {
582
+ return __generator(this, function (_a) {
583
+ return [2 /*return*/, this.getBalance(addresses, false)];
584
+ });
585
+ });
586
+ };
587
+ CosmosProtocol.prototype.getBalanceOfPublicKeyForSubProtocols = function (publicKey, subProtocols) {
588
+ return __awaiter(this, void 0, void 0, function () {
589
+ return __generator(this, function (_a) {
590
+ throw Promise.reject('get balance of sub protocols not supported');
591
+ });
592
+ });
593
+ };
594
+ CosmosProtocol.prototype.getBalance = function (addresses, totalBalance) {
595
+ if (totalBalance === void 0) { totalBalance = true; }
596
+ return __awaiter(this, void 0, void 0, function () {
597
+ var promises, _i, addresses_1, address;
598
+ return __generator(this, function (_a) {
599
+ switch (_a.label) {
600
+ case 0:
601
+ promises = [];
602
+ for (_i = 0, addresses_1 = addresses; _i < addresses_1.length; _i++) {
603
+ address = addresses_1[_i];
604
+ promises.push(this.nodeClient.fetchBalance(address, totalBalance));
605
+ }
606
+ return [4 /*yield*/, Promise.all(promises).then(function (balances) {
607
+ return balances.reduce(function (current, next) {
608
+ return current.plus(next);
609
+ });
610
+ })];
611
+ case 1: return [2 /*return*/, (_a.sent()).toString(10)];
612
+ }
613
+ });
614
+ });
615
+ };
616
+ CosmosProtocol.prototype.estimateMaxTransactionValueFromPublicKey = function (publicKey, recipients, fee) {
617
+ return __awaiter(this, void 0, void 0, function () {
618
+ var address, balance, balanceWrapper, maxFee, estimatedFeeDefaults, amountWithoutFees;
619
+ return __generator(this, function (_a) {
620
+ switch (_a.label) {
621
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
622
+ case 1:
623
+ address = (_a.sent()).address;
624
+ return [4 /*yield*/, this.getAvailableBalanceOfAddresses([address])];
625
+ case 2:
626
+ balance = _a.sent();
627
+ balanceWrapper = new bignumber_1.default(balance);
628
+ if (!(fee !== undefined)) return [3 /*break*/, 3];
629
+ maxFee = new bignumber_1.default(fee);
630
+ return [3 /*break*/, 5];
631
+ case 3: return [4 /*yield*/, this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, [balance])];
632
+ case 4:
633
+ estimatedFeeDefaults = _a.sent();
634
+ maxFee = new bignumber_1.default(estimatedFeeDefaults.medium).shiftedBy(this.decimals);
635
+ if (maxFee.gte(balanceWrapper)) {
636
+ maxFee = new bignumber_1.default(0);
637
+ }
638
+ _a.label = 5;
639
+ case 5:
640
+ amountWithoutFees = balanceWrapper.minus(maxFee);
641
+ if (amountWithoutFees.isNegative()) {
642
+ amountWithoutFees = new bignumber_1.default(0);
643
+ }
644
+ return [2 /*return*/, amountWithoutFees.toFixed()];
645
+ }
646
+ });
647
+ });
648
+ };
649
+ CosmosProtocol.prototype.estimateFeeDefaultsFromPublicKey = function (publicKey, recipients, values, data) {
650
+ return __awaiter(this, void 0, void 0, function () {
651
+ return __generator(this, function (_a) {
652
+ return [2 /*return*/, this.feeDefaults];
653
+ });
654
+ });
655
+ };
656
+ CosmosProtocol.prototype.prepareTransactionFromPublicKey = function (publicKey, recipients, values, fee, data) {
657
+ var _a, _b;
658
+ return __awaiter(this, void 0, void 0, function () {
659
+ var wrappedValues, wrappedFee, address, nodeInfo, account, balance, _c, messages, i, message, memo, transaction;
660
+ return __generator(this, function (_d) {
661
+ switch (_d.label) {
662
+ case 0:
663
+ wrappedValues = values.map(function (value) { return new bignumber_1.default(value); });
664
+ wrappedFee = new bignumber_1.default(fee);
665
+ if (recipients.length !== wrappedValues.length) {
666
+ return [2 /*return*/, Promise.reject('recipients length does not match with values')];
667
+ }
668
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
669
+ case 1:
670
+ address = (_d.sent()).address;
671
+ return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
672
+ case 2:
673
+ nodeInfo = _d.sent();
674
+ return [4 /*yield*/, this.nodeClient.fetchAccount(address)];
675
+ case 3:
676
+ account = _d.sent();
677
+ _c = bignumber_1.default.bind;
678
+ return [4 /*yield*/, this.getAvailableBalanceOfAddresses([address])];
679
+ case 4:
680
+ balance = new (_c.apply(bignumber_1.default, [void 0, _d.sent()]))();
681
+ if (balance.lt(values.reduce(function (pv, cv) { return pv.plus(cv); }, wrappedFee))) {
682
+ throw new errors_1.BalanceError(coinlib_error_1.Domain.COSMOS, 'not enough balance');
683
+ }
684
+ messages = [];
685
+ for (i = 0; i < recipients.length; ++i) {
686
+ message = new CosmosSendMessage_1.CosmosSendMessage(address, recipients[i], [
687
+ new CosmosCoin_1.CosmosCoin('uatom', wrappedValues[i].toString(10))
688
+ ]);
689
+ messages.push(message);
690
+ }
691
+ memo = (_a = data === null || data === void 0 ? void 0 : data.memo) !== null && _a !== void 0 ? _a : '';
692
+ transaction = new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin('uatom', wrappedFee.toString(10))], this.defaultGas.toString(10)), memo, nodeInfo.network, account.value.account_number, (_b = account.value.sequence) !== null && _b !== void 0 ? _b : '0');
693
+ return [2 /*return*/, transaction];
694
+ }
695
+ });
696
+ });
697
+ };
698
+ CosmosProtocol.prototype.getDefaultDelegatee = function () {
699
+ return __awaiter(this, void 0, void 0, function () {
700
+ var validators;
701
+ return __generator(this, function (_a) {
702
+ switch (_a.label) {
703
+ case 0: return [4 /*yield*/, this.nodeClient.fetchValidators()];
704
+ case 1:
705
+ validators = _a.sent();
706
+ return [2 /*return*/, validators.length > 0 ? validators[0].operator_address : ''];
707
+ }
708
+ });
709
+ });
710
+ };
711
+ CosmosProtocol.prototype.getCurrentDelegateesForPublicKey = function (publicKey) {
712
+ return __awaiter(this, void 0, void 0, function () {
713
+ var address;
714
+ return __generator(this, function (_a) {
715
+ switch (_a.label) {
716
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
717
+ case 1:
718
+ address = _a.sent();
719
+ return [2 /*return*/, this.getCurrentDelegateesForAddress(address.address)];
720
+ }
721
+ });
722
+ });
723
+ };
724
+ CosmosProtocol.prototype.getCurrentDelegateesForAddress = function (address) {
725
+ return __awaiter(this, void 0, void 0, function () {
726
+ var delegations;
727
+ return __generator(this, function (_a) {
728
+ switch (_a.label) {
729
+ case 0: return [4 /*yield*/, this.nodeClient.fetchDelegations(address)];
730
+ case 1:
731
+ delegations = _a.sent();
732
+ return [2 /*return*/, delegations.map(function (delegation) { return delegation.delegation.validator_address; })];
733
+ }
734
+ });
735
+ });
736
+ };
737
+ CosmosProtocol.prototype.getDelegateeDetails = function (address) {
738
+ return __awaiter(this, void 0, void 0, function () {
739
+ var validator, statusCodes;
740
+ return __generator(this, function (_a) {
741
+ switch (_a.label) {
742
+ case 0: return [4 /*yield*/, this.nodeClient.fetchValidator(address)];
743
+ case 1:
744
+ validator = _a.sent();
745
+ statusCodes = { 0: 'jailed', 1: 'inactive', 2: 'active' };
746
+ return [2 /*return*/, {
747
+ name: validator.description.moniker,
748
+ status: statusCodes[validator.status],
749
+ address: validator.operator_address
750
+ }];
751
+ }
752
+ });
753
+ });
754
+ };
755
+ CosmosProtocol.prototype.isPublicKeyDelegating = function (publicKey) {
756
+ return __awaiter(this, void 0, void 0, function () {
757
+ var address;
758
+ return __generator(this, function (_a) {
759
+ switch (_a.label) {
760
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
761
+ case 1:
762
+ address = _a.sent();
763
+ return [2 /*return*/, this.isAddressDelegating(address.address)];
764
+ }
765
+ });
766
+ });
767
+ };
768
+ CosmosProtocol.prototype.isAddressDelegating = function (address) {
769
+ return __awaiter(this, void 0, void 0, function () {
770
+ var delegations;
771
+ return __generator(this, function (_a) {
772
+ switch (_a.label) {
773
+ case 0: return [4 /*yield*/, this.nodeClient.fetchDelegations(address)];
774
+ case 1:
775
+ delegations = _a.sent();
776
+ return [2 /*return*/, delegations.length > 0];
777
+ }
778
+ });
779
+ });
780
+ };
781
+ CosmosProtocol.prototype.getDelegatorDetailsFromPublicKey = function (publicKey) {
782
+ return __awaiter(this, void 0, void 0, function () {
783
+ var address;
784
+ return __generator(this, function (_a) {
785
+ switch (_a.label) {
786
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
787
+ case 1:
788
+ address = _a.sent();
789
+ return [2 /*return*/, this.getDelegatorDetailsFromAddress(address.address)];
790
+ }
791
+ });
792
+ });
793
+ };
794
+ CosmosProtocol.prototype.getDelegatorDetailsFromAddress = function (address) {
795
+ return __awaiter(this, void 0, void 0, function () {
796
+ return __generator(this, function (_a) {
797
+ return [2 /*return*/, this.getDelegatorDetails(address)];
798
+ });
799
+ });
800
+ };
801
+ CosmosProtocol.prototype.getDelegationDetailsFromPublicKey = function (publicKey, delegatees) {
802
+ return __awaiter(this, void 0, void 0, function () {
803
+ var address;
804
+ return __generator(this, function (_a) {
805
+ switch (_a.label) {
806
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
807
+ case 1:
808
+ address = _a.sent();
809
+ return [2 /*return*/, this.getDelegationDetailsFromAddress(address.address, delegatees)];
810
+ }
811
+ });
812
+ });
813
+ };
814
+ CosmosProtocol.prototype.getDelegationDetailsFromAddress = function (address, delegatees) {
815
+ return __awaiter(this, void 0, void 0, function () {
816
+ var validator, results, delegatorDetails, validatorDetails;
817
+ return __generator(this, function (_a) {
818
+ switch (_a.label) {
819
+ case 0:
820
+ if (delegatees.length > 1) {
821
+ return [2 /*return*/, Promise.reject('Multiple validators for a single delegation are not supported.')];
822
+ }
823
+ validator = delegatees[0];
824
+ return [4 /*yield*/, Promise.all([this.getDelegatorDetails(address, validator), this.getDelegateeDetails(validator)])];
825
+ case 1:
826
+ results = _a.sent();
827
+ delegatorDetails = results[0];
828
+ validatorDetails = results[1];
829
+ return [2 /*return*/, {
830
+ delegator: delegatorDetails,
831
+ delegatees: [validatorDetails]
832
+ }];
833
+ }
834
+ });
835
+ });
836
+ };
837
+ CosmosProtocol.prototype.prepareDelegatorActionFromPublicKey = function (publicKey, type, data) {
838
+ return __awaiter(this, void 0, void 0, function () {
839
+ var _a;
840
+ return __generator(this, function (_b) {
841
+ switch (_b.label) {
842
+ case 0:
843
+ _a = type;
844
+ switch (_a) {
845
+ case CosmosDelegationActionType.DELEGATE: return [3 /*break*/, 1];
846
+ case CosmosDelegationActionType.UNDELEGATE: return [3 /*break*/, 3];
847
+ case CosmosDelegationActionType.WITHDRAW_ALL_REWARDS: return [3 /*break*/, 5];
848
+ case CosmosDelegationActionType.WITHDRAW_VALIDATOR_REWARDS: return [3 /*break*/, 7];
849
+ }
850
+ return [3 /*break*/, 9];
851
+ case 1:
852
+ (0, assert_1.assertFields)("".concat(type, " action"), data, 'validator', 'amount');
853
+ return [4 /*yield*/, this.delegate(publicKey, data.validator, data.amount)];
854
+ case 2: return [2 /*return*/, [_b.sent()]];
855
+ case 3:
856
+ (0, assert_1.assertFields)("".concat(type, " action"), data, 'validator', 'amount');
857
+ return [4 /*yield*/, this.undelegate(publicKey, data.validator, data.amount)];
858
+ case 4: return [2 /*return*/, [_b.sent()]];
859
+ case 5: return [4 /*yield*/, this.withdrawDelegationRewards(publicKey)];
860
+ case 6: return [2 /*return*/, [_b.sent()]];
861
+ case 7:
862
+ (0, assert_1.assertFields)("".concat(type, " action"), data, 'validator');
863
+ return [4 /*yield*/, this.withdrawDelegationRewards(publicKey, [data.validator])];
864
+ case 8: return [2 /*return*/, [_b.sent()]];
865
+ case 9: return [2 /*return*/, Promise.reject("Delegator action type ".concat(type, " is not supported."))];
866
+ }
867
+ });
868
+ });
869
+ };
870
+ CosmosProtocol.prototype.undelegate = function (publicKey, validatorAddress, amount, memo) {
871
+ return __awaiter(this, void 0, void 0, function () {
872
+ return __generator(this, function (_a) {
873
+ return [2 /*return*/, this.delegate(publicKey, validatorAddress, amount, true, memo)];
874
+ });
875
+ });
876
+ };
877
+ CosmosProtocol.prototype.delegate = function (publicKey, validatorAddress, amount, undelegate, memo) {
878
+ var _a;
879
+ if (undelegate === void 0) { undelegate = false; }
880
+ return __awaiter(this, void 0, void 0, function () {
881
+ var address, nodeInfo, account, message;
882
+ return __generator(this, function (_b) {
883
+ switch (_b.label) {
884
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
885
+ case 1:
886
+ address = (_b.sent()).address;
887
+ return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
888
+ case 2:
889
+ nodeInfo = _b.sent();
890
+ return [4 /*yield*/, this.nodeClient.fetchAccount(address)];
891
+ case 3:
892
+ account = _b.sent();
893
+ message = new CosmosDelegateMessage_1.CosmosDelegateMessage(address, Array.isArray(validatorAddress) ? validatorAddress[0] : validatorAddress, new CosmosCoin_1.CosmosCoin('uatom', amount), undelegate);
894
+ return [2 /*return*/, new CosmosTransaction_1.CosmosTransaction([message], new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin('uatom', new bignumber_1.default(this.feeDefaults.low).shiftedBy(this.feeDecimals).toString(10))], this.defaultGas.toString(10)), memo !== undefined ? memo : '', nodeInfo.network, account.value.account_number, (_a = account.value.sequence) !== null && _a !== void 0 ? _a : '0')];
895
+ }
896
+ });
897
+ });
898
+ };
899
+ CosmosProtocol.prototype.withdrawDelegationRewards = function (publicKey, _validatorAddresses, memo) {
900
+ var _a;
901
+ if (_validatorAddresses === void 0) { _validatorAddresses = []; }
902
+ return __awaiter(this, void 0, void 0, function () {
903
+ var validatorAddresses, address_1, rewards, filteredRewards, address, nodeInfo, account, messages;
904
+ return __generator(this, function (_b) {
905
+ switch (_b.label) {
906
+ case 0:
907
+ if (!(_validatorAddresses.length > 0)) return [3 /*break*/, 1];
908
+ validatorAddresses = _validatorAddresses;
909
+ return [3 /*break*/, 4];
910
+ case 1: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
911
+ case 2:
912
+ address_1 = (_b.sent()).address;
913
+ return [4 /*yield*/, this.nodeClient.fetchRewardDetails(address_1)];
914
+ case 3:
915
+ rewards = _b.sent();
916
+ filteredRewards = rewards.filter(function (reward) {
917
+ return reward.reward
918
+ ? reward.reward
919
+ .reduce(function (total, next) { return total.plus(new bignumber_1.default(next.amount)); }, new bignumber_1.default(0))
920
+ .decimalPlaces(0, bignumber_1.default.ROUND_FLOOR)
921
+ .gt(0)
922
+ : false;
923
+ });
924
+ validatorAddresses = filteredRewards.map(function (reward) { return reward.validator_address; });
925
+ _b.label = 4;
926
+ case 4: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
927
+ case 5:
928
+ address = (_b.sent()).address;
929
+ return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
930
+ case 6:
931
+ nodeInfo = _b.sent();
932
+ return [4 /*yield*/, this.nodeClient.fetchAccount(address)];
933
+ case 7:
934
+ account = _b.sent();
935
+ messages = validatorAddresses.map(function (validatorAddress) { return new CosmosWithdrawDelegationRewardMessage_1.CosmosWithdrawDelegationRewardMessage(address, validatorAddress); });
936
+ return [2 /*return*/, new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin('uatom', new bignumber_1.default(this.feeDefaults.low).shiftedBy(this.feeDecimals).toString(10))], this.defaultGas.toString(10)), memo !== undefined ? memo : '', nodeInfo.network, account.value.account_number, (_a = account.value.sequence) !== null && _a !== void 0 ? _a : '0')];
937
+ }
938
+ });
939
+ });
940
+ };
941
+ CosmosProtocol.prototype.withdrawAllDelegationRewards = function (delegatorAddress, fee, memo) {
942
+ var _a;
943
+ return __awaiter(this, void 0, void 0, function () {
944
+ var nodeInfo, account;
945
+ return __generator(this, function (_b) {
946
+ switch (_b.label) {
947
+ case 0: return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
948
+ case 1:
949
+ nodeInfo = _b.sent();
950
+ return [4 /*yield*/, this.nodeClient.fetchAccount(delegatorAddress)];
951
+ case 2:
952
+ account = _b.sent();
953
+ return [2 /*return*/, this.nodeClient.withdrawAllDelegationRewards(delegatorAddress, nodeInfo.network, account.value.account_number, (_a = account.value.sequence) !== null && _a !== void 0 ? _a : '0', this.defaultGas, fee, memo !== undefined ? memo : '')];
954
+ }
955
+ });
956
+ });
957
+ };
958
+ CosmosProtocol.prototype.fetchTotalReward = function (delegatorAddress) {
959
+ return __awaiter(this, void 0, void 0, function () {
960
+ return __generator(this, function (_a) {
961
+ return [2 /*return*/, this.nodeClient.fetchTotalReward(delegatorAddress)];
962
+ });
963
+ });
964
+ };
965
+ CosmosProtocol.prototype.fetchRewardForDelegation = function (delegatorAddress, validatorAddress) {
966
+ return __awaiter(this, void 0, void 0, function () {
967
+ return __generator(this, function (_a) {
968
+ return [2 /*return*/, this.nodeClient.fetchRewardForDelegation(delegatorAddress, validatorAddress)];
969
+ });
970
+ });
971
+ };
972
+ CosmosProtocol.prototype.fetchDelegations = function (address) {
973
+ return __awaiter(this, void 0, void 0, function () {
974
+ return __generator(this, function (_a) {
975
+ return [2 /*return*/, this.nodeClient.fetchDelegations(address)];
976
+ });
977
+ });
978
+ };
979
+ CosmosProtocol.prototype.fetchTotalDelegatedAmount = function (address) {
980
+ return __awaiter(this, void 0, void 0, function () {
981
+ return __generator(this, function (_a) {
982
+ return [2 /*return*/, this.nodeClient.fetchTotalDelegatedAmount(address)];
983
+ });
984
+ });
985
+ };
986
+ CosmosProtocol.prototype.fetchValidator = function (address) {
987
+ return __awaiter(this, void 0, void 0, function () {
988
+ return __generator(this, function (_a) {
989
+ return [2 /*return*/, this.nodeClient.fetchValidator(address)];
990
+ });
991
+ });
992
+ };
993
+ CosmosProtocol.prototype.fetchUnbondingDelegations = function (delegatorAddress) {
994
+ return __awaiter(this, void 0, void 0, function () {
995
+ return __generator(this, function (_a) {
996
+ return [2 /*return*/, this.nodeClient.fetchUnbondingDelegations(delegatorAddress)];
997
+ });
998
+ });
999
+ };
1000
+ CosmosProtocol.prototype.fetchTotalUnbondingAmount = function (address) {
1001
+ return __awaiter(this, void 0, void 0, function () {
1002
+ return __generator(this, function (_a) {
1003
+ return [2 /*return*/, this.nodeClient.fetchTotalUnbondingAmount(address)];
1004
+ });
1005
+ });
1006
+ };
1007
+ CosmosProtocol.prototype.fetchValidators = function () {
1008
+ return __awaiter(this, void 0, void 0, function () {
1009
+ return __generator(this, function (_a) {
1010
+ return [2 /*return*/, this.nodeClient.fetchValidators()];
1011
+ });
1012
+ });
1013
+ };
1014
+ CosmosProtocol.prototype.fetchSelfDelegation = function (address) {
1015
+ return __awaiter(this, void 0, void 0, function () {
1016
+ return __generator(this, function (_a) {
1017
+ return [2 /*return*/, this.nodeClient.fetchSelfDelegation(address)];
1018
+ });
1019
+ });
1020
+ };
1021
+ CosmosProtocol.prototype.broadcastTransaction = function (rawTransaction) {
1022
+ return __awaiter(this, void 0, void 0, function () {
1023
+ return __generator(this, function (_a) {
1024
+ return [2 /*return*/, this.nodeClient.broadcastSignedTransaction(rawTransaction)];
1025
+ });
1026
+ });
1027
+ };
1028
+ CosmosProtocol.prototype.getDelegatorDetails = function (address, validator) {
1029
+ var _a;
1030
+ return __awaiter(this, void 0, void 0, function () {
1031
+ var results, totalBalance, availableBalance, delegations, rewardDetails, unclaimedRewards, unclaimedTotalRewards, unclaimedValidatorRewards, isDelegating, availableActions;
1032
+ return __generator(this, function (_b) {
1033
+ switch (_b.label) {
1034
+ case 0: return [4 /*yield*/, Promise.all([
1035
+ this.getBalanceOfAddresses([address]),
1036
+ this.getAvailableBalanceOfAddresses([address]),
1037
+ this.nodeClient.fetchDelegations(address).catch(function () { return []; }),
1038
+ this.nodeClient.fetchRewardDetails(address).catch(function () { return []; })
1039
+ ])];
1040
+ case 1:
1041
+ results = _b.sent();
1042
+ totalBalance = results[0];
1043
+ availableBalance = new bignumber_1.default(results[1]);
1044
+ delegations = results[2];
1045
+ rewardDetails = results[3];
1046
+ unclaimedRewards = rewardDetails.map(function (details) {
1047
+ var _a, _b;
1048
+ return [
1049
+ details.validator_address,
1050
+ (_b = (_a = details.reward) === null || _a === void 0 ? void 0 : _a.reduce(function (total, next) { return total.plus(next.amount); }, new bignumber_1.default(0)).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR)) !== null && _b !== void 0 ? _b : new bignumber_1.default(0)
1051
+ ];
1052
+ });
1053
+ unclaimedTotalRewards = unclaimedRewards.reduce(function (total, next) { return total.plus(next[1]); }, new bignumber_1.default(0));
1054
+ unclaimedValidatorRewards = validator
1055
+ ? ((_a = unclaimedRewards.find(function (_a) {
1056
+ var validatorAddress = _a[0], _ = _a[1];
1057
+ return validatorAddress === validator;
1058
+ })) === null || _a === void 0 ? void 0 : _a[1]) || new bignumber_1.default(0)
1059
+ : undefined;
1060
+ isDelegating = validator
1061
+ ? delegations.some(function (delegation) { return delegation.delegation.validator_address === validator; })
1062
+ : delegations.length > 0;
1063
+ availableActions = this.getAvailableDelegatorActions(isDelegating, availableBalance, unclaimedTotalRewards, unclaimedValidatorRewards);
1064
+ return [2 /*return*/, {
1065
+ address: address,
1066
+ balance: totalBalance,
1067
+ delegatees: delegations.map(function (delegation) { return delegation.delegation.validator_address; }),
1068
+ availableActions: availableActions
1069
+ }];
1070
+ }
1071
+ });
1072
+ });
1073
+ };
1074
+ CosmosProtocol.prototype.getAvailableDelegatorActions = function (isDelegating, availableBalance, unclaimedTotalRewards, unclaimedDelegationRewards) {
1075
+ var actions = [];
1076
+ var requiredFee = new bignumber_1.default(this.feeDefaults.low).shiftedBy(this.feeDecimals);
1077
+ var hasSufficientBalance = availableBalance.gt(requiredFee);
1078
+ if (hasSufficientBalance) {
1079
+ actions.push({
1080
+ type: CosmosDelegationActionType.DELEGATE,
1081
+ args: ['validator', 'amount']
1082
+ });
1083
+ }
1084
+ if (isDelegating) {
1085
+ actions.push({
1086
+ type: CosmosDelegationActionType.UNDELEGATE,
1087
+ args: ['validator', 'amount']
1088
+ });
1089
+ }
1090
+ if (unclaimedTotalRewards.gt(0)) {
1091
+ actions.push({
1092
+ type: CosmosDelegationActionType.WITHDRAW_ALL_REWARDS
1093
+ });
1094
+ }
1095
+ if (unclaimedDelegationRewards === null || unclaimedDelegationRewards === void 0 ? void 0 : unclaimedDelegationRewards.gt(0)) {
1096
+ actions.push({
1097
+ type: CosmosDelegationActionType.WITHDRAW_VALIDATOR_REWARDS,
1098
+ args: ['validator']
1099
+ });
1100
+ }
1101
+ return actions;
1102
+ };
1103
+ CosmosProtocol.prototype.signMessage = function (message, keypair) {
1104
+ return __awaiter(this, void 0, void 0, function () {
1105
+ return __generator(this, function (_a) {
1106
+ return [2 /*return*/, this.cryptoClient.signMessage(message, keypair)];
1107
+ });
1108
+ });
1109
+ };
1110
+ CosmosProtocol.prototype.verifyMessage = function (message, signature, publicKey) {
1111
+ return __awaiter(this, void 0, void 0, function () {
1112
+ return __generator(this, function (_a) {
1113
+ return [2 /*return*/, this.cryptoClient.verifyMessage(message, signature, publicKey)];
1114
+ });
1115
+ });
1116
+ };
1117
+ CosmosProtocol.prototype.encryptAsymmetric = function (message, publicKey) {
1118
+ return __awaiter(this, void 0, void 0, function () {
1119
+ return __generator(this, function (_a) {
1120
+ return [2 /*return*/, this.cryptoClient.encryptAsymmetric(message, publicKey)];
1121
+ });
1122
+ });
1123
+ };
1124
+ CosmosProtocol.prototype.decryptAsymmetric = function (message, keypair) {
1125
+ return __awaiter(this, void 0, void 0, function () {
1126
+ return __generator(this, function (_a) {
1127
+ return [2 /*return*/, this.cryptoClient.decryptAsymmetric(message, keypair)];
1128
+ });
1129
+ });
1130
+ };
1131
+ CosmosProtocol.prototype.encryptAES = function (message, privateKey) {
1132
+ return __awaiter(this, void 0, void 0, function () {
1133
+ return __generator(this, function (_a) {
1134
+ return [2 /*return*/, this.cryptoClient.encryptAES(message, privateKey)];
1135
+ });
1136
+ });
1137
+ };
1138
+ CosmosProtocol.prototype.decryptAES = function (message, privateKey) {
1139
+ return __awaiter(this, void 0, void 0, function () {
1140
+ return __generator(this, function (_a) {
1141
+ return [2 /*return*/, this.cryptoClient.decryptAES(message, privateKey)];
1142
+ });
1143
+ });
1144
+ };
1145
+ CosmosProtocol.prototype.getTransactionStatuses = function (transactionHashes) {
1146
+ return __awaiter(this, void 0, void 0, function () {
1147
+ return __generator(this, function (_a) {
1148
+ return [2 /*return*/, Promise.reject('Transaction status not implemented')];
1149
+ });
1150
+ });
1151
+ };
1152
+ return CosmosProtocol;
1153
+ }(NonExtendedProtocol_1.NonExtendedProtocol));
1154
+ exports.CosmosProtocol = CosmosProtocol;
1155
+ //# sourceMappingURL=CosmosProtocol.js.map