@airgap/cosmos-core 0.13.45-beta.3 → 0.13.45-beta.5
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.
- package/package.json +5 -5
- package/v1/index.js +11 -11
- package/v1/index.js.map +1 -1
- package/v1/node/CosmosNodeClient.js +396 -189
- package/v1/node/CosmosNodeClient.js.map +1 -1
- package/v1/protocol/CosmosBaseProtocol.js +993 -557
- package/v1/protocol/CosmosBaseProtocol.js.map +1 -1
- package/v1/protocol/CosmosCryptoClient.js +82 -19
- package/v1/protocol/CosmosCryptoClient.js.map +1 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +20 -8
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
- package/v1/serializer/v3/validators/transaction-validators.js +59 -12
- package/v1/serializer/v3/validators/transaction-validators.js.map +1 -1
- package/v1/types/crypto.d.ts +1 -1
- package/v1/types/data/CosmosAddress.js +18 -16
- package/v1/types/data/CosmosAddress.js.map +1 -1
- package/v1/types/data/CosmosCoin.js +23 -21
- package/v1/types/data/CosmosCoin.js.map +1 -1
- package/v1/types/data/CosmosFee.js +17 -16
- package/v1/types/data/CosmosFee.js.map +1 -1
- package/v1/types/data/transaction/CosmosTransaction.js +40 -38
- package/v1/types/data/transaction/CosmosTransaction.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosDelegateMessage.js +27 -24
- package/v1/types/data/transaction/message/CosmosDelegateMessage.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosMessage.js +12 -11
- package/v1/types/data/transaction/message/CosmosMessage.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosSendMessage.js +41 -31
- package/v1/types/data/transaction/message/CosmosSendMessage.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js +21 -20
- package/v1/types/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js.map +1 -1
- package/v1/types/transaction.js +1 -1
- package/v1/types/transaction.js.map +1 -1
- package/v1/utils/key.js +5 -4
- package/v1/utils/key.js.map +1 -1
- package/v1/utils/signature.js +4 -3
- package/v1/utils/signature.js.map +1 -1
- package/v1/utils/transaction.js +2 -1
- package/v1/utils/transaction.js.map +1 -1
|
@@ -1,37 +1,84 @@
|
|
|
1
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
|
+
};
|
|
2
49
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
50
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
51
|
};
|
|
5
52
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
53
|
exports.CosmosBaseProtocolImpl = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
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"));
|
|
9
56
|
// @ts-ignore
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
57
|
+
var index_1 = require("@airgap/coinlib-core/dependencies/src/bip32-2.0.4/src/index");
|
|
58
|
+
var cosmjs_1 = require("@airgap/coinlib-core/dependencies/src/cosmjs");
|
|
59
|
+
var SECP256K1 = require("@airgap/coinlib-core/dependencies/src/secp256k1-3.7.1/elliptic");
|
|
13
60
|
// @ts-ignore
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
61
|
+
var sha = require("@airgap/coinlib-core/dependencies/src/sha.js-2.4.11/index");
|
|
62
|
+
var errors_1 = require("@airgap/coinlib-core/errors");
|
|
63
|
+
var assert_1 = require("@airgap/coinlib-core/utils/assert");
|
|
64
|
+
var crypto_1 = require("@airgap/crypto");
|
|
65
|
+
var module_kit_1 = require("@airgap/module-kit");
|
|
66
|
+
var CosmosNodeClient_1 = require("../node/CosmosNodeClient");
|
|
67
|
+
var CosmosAddress_1 = require("../types/data/CosmosAddress");
|
|
68
|
+
var CosmosCoin_1 = require("../types/data/CosmosCoin");
|
|
69
|
+
var CosmosFee_1 = require("../types/data/CosmosFee");
|
|
70
|
+
var CosmosTransaction_1 = require("../types/data/transaction/CosmosTransaction");
|
|
71
|
+
var CosmosDelegateMessage_1 = require("../types/data/transaction/message/CosmosDelegateMessage");
|
|
72
|
+
var CosmosMessage_1 = require("../types/data/transaction/message/CosmosMessage");
|
|
73
|
+
var CosmosSendMessage_1 = require("../types/data/transaction/message/CosmosSendMessage");
|
|
74
|
+
var CosmosWithdrawDelegationRewardMessage_1 = require("../types/data/transaction/message/CosmosWithdrawDelegationRewardMessage");
|
|
75
|
+
var transaction_1 = require("../types/transaction");
|
|
76
|
+
var key_1 = require("../utils/key");
|
|
77
|
+
var signature_1 = require("../utils/signature");
|
|
78
|
+
var transaction_2 = require("../utils/transaction");
|
|
79
|
+
var CosmosCryptoClient_1 = require("./CosmosCryptoClient");
|
|
80
|
+
var CosmosBaseProtocolImpl = /** @class */ (function () {
|
|
81
|
+
function CosmosBaseProtocolImpl(options) {
|
|
35
82
|
this.cryptoConfiguration = {
|
|
36
83
|
algorithm: 'secp256k1'
|
|
37
84
|
};
|
|
@@ -39,550 +86,939 @@ class CosmosBaseProtocolImpl {
|
|
|
39
86
|
this.nodeClient = new CosmosNodeClient_1.CosmosNodeClient(this.options.network.rpcUrl, this.options.network.useCORSProxy);
|
|
40
87
|
this.cryptoClient = new CosmosCryptoClient_1.CosmosCryptoClient();
|
|
41
88
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
async getKeyPairFromDerivative(derivative) {
|
|
54
|
-
const bip32Node = (0, crypto_1.encodeDerivative)('bip32', derivative);
|
|
55
|
-
const bip32 = (0, index_1.fromBase58)(bip32Node.secretKey);
|
|
56
|
-
const secretKey = bip32.privateKey;
|
|
57
|
-
if (secretKey === undefined) {
|
|
58
|
-
throw new errors_1.InvalidValueError(coinlib_core_1.Domain.COSMOS, 'Cannot generate secret key');
|
|
59
|
-
}
|
|
60
|
-
const publicKey = bip32.publicKey;
|
|
61
|
-
return {
|
|
62
|
-
secretKey: (0, module_kit_1.newSecretKey)(secretKey.toString('hex'), 'hex'),
|
|
63
|
-
publicKey: (0, module_kit_1.newPublicKey)(publicKey.toString('hex'), 'hex')
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
async signTransactionWithSecretKey(transaction, secretKey) {
|
|
67
|
-
const hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
|
|
68
|
-
const privateKeyBuffer = Buffer.from(hexSecretKey.value, 'hex');
|
|
69
|
-
const { publicKey } = await this.getKeyPairFromSecretKey(secretKey);
|
|
70
|
-
const hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
71
|
-
const publicKeyBuffer = Buffer.from(hexPublicKey.value, 'hex');
|
|
72
|
-
const encodedObject = CosmosTransaction_1.CosmosTransaction.fromJSON(transaction).toEncodeObject();
|
|
73
|
-
const signBytes = await (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());
|
|
74
|
-
const sha256Hash = sha('sha256').update(signBytes).digest();
|
|
75
|
-
const hash = Buffer.from(sha256Hash);
|
|
76
|
-
const signed = SECP256K1.sign(hash, privateKeyBuffer);
|
|
77
|
-
const sigBase64 = Buffer.from(signed.signature, 'binary').toString('base64');
|
|
78
|
-
const txBytes = await (0, cosmjs_1.encodeTxBytes)(encodedObject, transaction.fee, Uint8Array.from(publicKeyBuffer), new bignumber_1.default(transaction.sequence).toNumber(), {
|
|
79
|
-
signature: sigBase64,
|
|
80
|
-
pub_key: {
|
|
81
|
-
type: 'tendermint/PubKeySecp256k1',
|
|
82
|
-
value: publicKeyBuffer.toString('base64')
|
|
83
|
-
}
|
|
84
|
-
}, transaction.chainID, new bignumber_1.default(transaction.accountNumber).toNumber());
|
|
85
|
-
return (0, module_kit_1.newSignedTransaction)({
|
|
86
|
-
encoded: Buffer.from(txBytes).toString('base64')
|
|
89
|
+
CosmosBaseProtocolImpl.prototype.getKeyPairFromSecretKey = function (secretKey) {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
91
|
+
var hexSecretKey, publicKey;
|
|
92
|
+
return __generator(this, function (_a) {
|
|
93
|
+
hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
|
|
94
|
+
publicKey = SECP256K1.publicKeyCreate(Buffer.from(secretKey.value, 'hex'));
|
|
95
|
+
return [2 /*return*/, {
|
|
96
|
+
secretKey: hexSecretKey,
|
|
97
|
+
publicKey: (0, module_kit_1.newPublicKey)(Buffer.from(publicKey, 'binary').toString('hex'), 'hex')
|
|
98
|
+
}];
|
|
99
|
+
});
|
|
87
100
|
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
case 'signed':
|
|
95
|
-
return this.getDetailsFromSignedTransaction(transaction);
|
|
96
|
-
case 'unsigned':
|
|
97
|
-
return this.getDetailsFromUnsignedTransaction(transaction);
|
|
98
|
-
default:
|
|
99
|
-
(0, coinlib_core_1.assertNever)(transaction);
|
|
100
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.COSMOS, 'Unsupported transaction type.');
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
async getDetailsFromSignedTransaction(transaction) {
|
|
104
|
-
const bytes = Uint8Array.from(Buffer.from(transaction.encoded, 'base64'));
|
|
105
|
-
const decoded = await (0, cosmjs_1.decodeTxBytes)(bytes);
|
|
106
|
-
const fee = (decoded.fee?.amount)
|
|
107
|
-
.map(({ amount }) => new bignumber_1.default(amount))
|
|
108
|
-
.reduce((current, next) => current.plus(next))
|
|
109
|
-
.toString(10);
|
|
110
|
-
const result = decoded.messages
|
|
111
|
-
.map((message) => {
|
|
112
|
-
const type = message.typeUrl;
|
|
113
|
-
switch (type) {
|
|
114
|
-
case CosmosMessage_1.CosmosMessageType.Send.value:
|
|
115
|
-
const sendMessage = CosmosSendMessage_1.CosmosSendMessage.fromEncodeObject(message);
|
|
116
|
-
return sendMessage.toAirGapTransaction(this.options.network, fee);
|
|
117
|
-
case CosmosMessage_1.CosmosMessageType.Undelegate.value:
|
|
118
|
-
case CosmosMessage_1.CosmosMessageType.Delegate.value:
|
|
119
|
-
const delegateMessage = CosmosDelegateMessage_1.CosmosDelegateMessage.fromEncodeObject(message);
|
|
120
|
-
return delegateMessage.toAirGapTransaction(this.options.network, fee);
|
|
121
|
-
case CosmosMessage_1.CosmosMessageType.WithdrawDelegationReward.value:
|
|
122
|
-
const withdrawMessage = CosmosWithdrawDelegationRewardMessage_1.CosmosWithdrawDelegationRewardMessage.fromEncodeObject(message);
|
|
123
|
-
return withdrawMessage.toAirGapTransaction(this.options.network, fee);
|
|
124
|
-
default:
|
|
125
|
-
throw new errors_1.InvalidValueError(coinlib_core_1.Domain.COSMOS, 'Unknown transaction');
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
.map((tx) => {
|
|
129
|
-
if (!tx.json) {
|
|
130
|
-
tx.json = {};
|
|
131
|
-
}
|
|
132
|
-
tx.json.memo = decoded.memo;
|
|
133
|
-
if (decoded.signerInfos.length > 0) {
|
|
134
|
-
tx.json.sequence = decoded.signerInfos[0].sequence.toString(10);
|
|
135
|
-
}
|
|
136
|
-
tx.arbitraryData = decoded.memo;
|
|
137
|
-
return tx;
|
|
101
|
+
};
|
|
102
|
+
CosmosBaseProtocolImpl.prototype.getCryptoConfiguration = function () {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
104
|
+
return __generator(this, function (_a) {
|
|
105
|
+
return [2 /*return*/, this.cryptoConfiguration];
|
|
106
|
+
});
|
|
138
107
|
});
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const units = (await this.getMetadata()).units;
|
|
157
|
-
const { total, transferable } = await this.getBalanceOfPublicKey(publicKey);
|
|
158
|
-
const balance = new bignumber_1.default((0, module_kit_1.newAmount)(transferable ?? total).blockchain(units).value);
|
|
159
|
-
let fee;
|
|
160
|
-
if (configuration?.fee !== undefined) {
|
|
161
|
-
fee = configuration.fee;
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
const estimatedFee = await this.getTransactionFeeWithPublicKey(publicKey, to.map((recipient) => ({
|
|
165
|
-
to: recipient,
|
|
166
|
-
amount: (0, module_kit_1.newAmount)(balance.div(to.length).decimalPlaces(0, bignumber_1.default.ROUND_CEIL), 'blockchain')
|
|
167
|
-
})));
|
|
168
|
-
fee = (0, module_kit_1.newAmount)(estimatedFee.medium).blockchain(units);
|
|
169
|
-
if (balance.lte(fee.value)) {
|
|
170
|
-
fee = (0, module_kit_1.newAmount)(0, 'blockchain');
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
fee = (0, module_kit_1.newAmount)(fee).blockchain(units);
|
|
174
|
-
let amountWithoutFees = balance.minus(fee.value);
|
|
175
|
-
if (amountWithoutFees.isNegative()) {
|
|
176
|
-
amountWithoutFees = new bignumber_1.default(0);
|
|
177
|
-
}
|
|
178
|
-
return (0, module_kit_1.newAmount)(amountWithoutFees, 'blockchain');
|
|
179
|
-
}
|
|
180
|
-
async prepareTransactionWithPublicKey(publicKey, details, configuration) {
|
|
181
|
-
let fee;
|
|
182
|
-
if (configuration?.fee !== undefined) {
|
|
183
|
-
fee = configuration.fee;
|
|
184
|
-
}
|
|
185
|
-
else {
|
|
186
|
-
const estimatedFee = await this.getTransactionFeeWithPublicKey(publicKey, details);
|
|
187
|
-
fee = estimatedFee.medium;
|
|
188
|
-
}
|
|
189
|
-
const units = (await this.getMetadata()).units;
|
|
190
|
-
const wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(units).value);
|
|
191
|
-
const address = await this.getAddressFromPublicKey(publicKey);
|
|
192
|
-
const nodeInfo = await this.nodeClient.fetchNodeInfo();
|
|
193
|
-
const account = await this.nodeClient.fetchAccount(address);
|
|
194
|
-
const { total, transferable } = await this.getBalanceOfPublicKey(publicKey);
|
|
195
|
-
const balance = new bignumber_1.default((0, module_kit_1.newAmount)(transferable ?? total).blockchain(units).value);
|
|
196
|
-
if (balance.lt(details.reduce((acc, next) => acc.plus((0, module_kit_1.newAmount)(next.amount).blockchain(units).value), wrappedFee))) {
|
|
197
|
-
throw new coinlib_core_1.BalanceError(coinlib_core_1.Domain.COSMOS, 'not enough balance');
|
|
198
|
-
}
|
|
199
|
-
const messages = [];
|
|
200
|
-
for (let i = 0; i < details.length; ++i) {
|
|
201
|
-
const message = new CosmosSendMessage_1.CosmosSendMessage(address, details[i].to, [
|
|
202
|
-
new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(details[i].amount).blockchain(units).value)
|
|
203
|
-
]);
|
|
204
|
-
messages.push(message);
|
|
205
|
-
}
|
|
206
|
-
const memo = configuration?.arbitraryData ?? '';
|
|
207
|
-
const transaction = new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin(this.options.baseUnit, wrappedFee.toString(10))], (0, module_kit_1.newAmount)(this.options.defaultGas).blockchain(units).value), memo, nodeInfo.network, account.account_number, account.sequence ?? '0');
|
|
208
|
-
return (0, module_kit_1.newUnsignedTransaction)({
|
|
209
|
-
messages: transaction.messages.map((message) => message.toJSON()),
|
|
210
|
-
fee: transaction.fee.toJSON(),
|
|
211
|
-
memo: transaction.memo,
|
|
212
|
-
chainID: transaction.chainID,
|
|
213
|
-
accountNumber: transaction.accountNumber,
|
|
214
|
-
sequence: transaction.sequence
|
|
108
|
+
};
|
|
109
|
+
CosmosBaseProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
111
|
+
var bip32Node, bip32, secretKey, publicKey;
|
|
112
|
+
return __generator(this, function (_a) {
|
|
113
|
+
bip32Node = (0, crypto_1.encodeDerivative)('bip32', derivative);
|
|
114
|
+
bip32 = (0, index_1.fromBase58)(bip32Node.secretKey);
|
|
115
|
+
secretKey = bip32.privateKey;
|
|
116
|
+
if (secretKey === undefined) {
|
|
117
|
+
throw new errors_1.InvalidValueError(coinlib_core_1.Domain.COSMOS, 'Cannot generate secret key');
|
|
118
|
+
}
|
|
119
|
+
publicKey = bip32.publicKey;
|
|
120
|
+
return [2 /*return*/, {
|
|
121
|
+
secretKey: (0, module_kit_1.newSecretKey)(secretKey.toString('hex'), 'hex'),
|
|
122
|
+
publicKey: (0, module_kit_1.newPublicKey)(publicKey.toString('hex'), 'hex')
|
|
123
|
+
}];
|
|
124
|
+
});
|
|
215
125
|
});
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return this
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
126
|
+
};
|
|
127
|
+
CosmosBaseProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
129
|
+
var hexSecretKey, privateKeyBuffer, publicKey, hexPublicKey, publicKeyBuffer, encodedObject, signBytes, sha256Hash, hash, signed, sigBase64, txBytes;
|
|
130
|
+
return __generator(this, function (_a) {
|
|
131
|
+
switch (_a.label) {
|
|
132
|
+
case 0:
|
|
133
|
+
hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
|
|
134
|
+
privateKeyBuffer = Buffer.from(hexSecretKey.value, 'hex');
|
|
135
|
+
return [4 /*yield*/, this.getKeyPairFromSecretKey(secretKey)];
|
|
136
|
+
case 1:
|
|
137
|
+
publicKey = (_a.sent()).publicKey;
|
|
138
|
+
hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
139
|
+
publicKeyBuffer = Buffer.from(hexPublicKey.value, 'hex');
|
|
140
|
+
encodedObject = CosmosTransaction_1.CosmosTransaction.fromJSON(transaction).toEncodeObject();
|
|
141
|
+
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())];
|
|
142
|
+
case 2:
|
|
143
|
+
signBytes = _a.sent();
|
|
144
|
+
sha256Hash = sha('sha256').update(signBytes).digest();
|
|
145
|
+
hash = Buffer.from(sha256Hash);
|
|
146
|
+
signed = SECP256K1.sign(hash, privateKeyBuffer);
|
|
147
|
+
sigBase64 = Buffer.from(signed.signature, 'binary').toString('base64');
|
|
148
|
+
return [4 /*yield*/, (0, cosmjs_1.encodeTxBytes)(encodedObject, transaction.fee, Uint8Array.from(publicKeyBuffer), new bignumber_1.default(transaction.sequence).toNumber(), {
|
|
149
|
+
signature: sigBase64,
|
|
150
|
+
pub_key: {
|
|
151
|
+
type: 'tendermint/PubKeySecp256k1',
|
|
152
|
+
value: publicKeyBuffer.toString('base64')
|
|
153
|
+
}
|
|
154
|
+
}, transaction.chainID, new bignumber_1.default(transaction.accountNumber).toNumber())];
|
|
155
|
+
case 3:
|
|
156
|
+
txBytes = _a.sent();
|
|
157
|
+
return [2 /*return*/, (0, module_kit_1.newSignedTransaction)({
|
|
158
|
+
encoded: Buffer.from(txBytes).toString('base64')
|
|
159
|
+
})];
|
|
160
|
+
}
|
|
161
|
+
});
|
|
236
162
|
});
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
let recipientOffset = 0;
|
|
254
|
-
let senderTotal = 0;
|
|
255
|
-
let recipientTotal = 0;
|
|
256
|
-
let senderLimit = 0;
|
|
257
|
-
let recipientLimit = 0;
|
|
258
|
-
if (cursor) {
|
|
259
|
-
senderOffset = cursor.sender.offset;
|
|
260
|
-
recipientOffset = cursor.recipient.offset;
|
|
261
|
-
senderTotal = cursor.sender.total;
|
|
262
|
-
recipientTotal = cursor.recipient.total;
|
|
263
|
-
senderLimit = (0, transaction_2.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
|
|
264
|
-
if (senderOffset <= Math.floor(senderTotal / senderLimit) * senderLimit) {
|
|
265
|
-
promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true));
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
promises.push(new Promise((resolve) => {
|
|
269
|
-
resolve({ txs: [], tx_responses: [], pagination: null, total: String(senderTotal) });
|
|
270
|
-
}));
|
|
271
|
-
}
|
|
272
|
-
recipientLimit = (0, transaction_2.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
|
|
273
|
-
if (recipientOffset <= Math.floor(recipientTotal / recipientLimit) * recipientLimit) {
|
|
274
|
-
promises.push(this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
promises.push(new Promise((resolve) => {
|
|
278
|
-
resolve({ txs: [], tx_responses: [], pagination: null, total: String(senderTotal) });
|
|
279
|
-
}));
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
;
|
|
284
|
-
[senderTotal, recipientTotal] = await Promise.all([
|
|
285
|
-
this.nodeClient.fetchSendTransactionsFor(address, 1, 0, true).then((response) => new bignumber_1.default(response.total).toNumber()),
|
|
286
|
-
this.nodeClient.fetchSendTransactionsFor(address, 1, 0, false).then((response) => new bignumber_1.default(response.total).toNumber())
|
|
287
|
-
]);
|
|
288
|
-
senderLimit = (0, transaction_2.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
|
|
289
|
-
recipientLimit = (0, transaction_2.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
|
|
290
|
-
promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true), this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
|
|
291
|
-
}
|
|
292
|
-
const transactions = await Promise.all(promises);
|
|
293
|
-
const sentTransactions = transactions[0];
|
|
294
|
-
const receivedTransactions = transactions[1];
|
|
295
|
-
const allTransactions = sentTransactions?.tx_responses.concat(receivedTransactions?.tx_responses);
|
|
296
|
-
let result = [];
|
|
297
|
-
for (const transaction of allTransactions) {
|
|
298
|
-
const timestamp = new Date(transaction.timestamp).getTime() / 1000;
|
|
299
|
-
const fee = transaction.tx.auth_info.fee.amount.reduce((current, next) => (next.denom === this.options.baseUnit ? current.plus(new bignumber_1.default(next.amount)) : current), new bignumber_1.default(0));
|
|
300
|
-
result = result.concat(transaction.tx.body.messages
|
|
301
|
-
.filter((msg) => msg['@type'] !== CosmosMessage_1.CosmosMessageTypeValue.MSGMULTISEND)
|
|
302
|
-
.map((msg) => {
|
|
303
|
-
const tx = {
|
|
304
|
-
isInbound: false,
|
|
305
|
-
amount: (0, module_kit_1.newAmount)('0', 'blockchain'),
|
|
306
|
-
fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
|
|
307
|
-
network: this.options.network,
|
|
308
|
-
status: {
|
|
309
|
-
type: 'unknown',
|
|
310
|
-
hash: transaction.txhash
|
|
311
|
-
},
|
|
312
|
-
timestamp
|
|
313
|
-
};
|
|
314
|
-
switch (msg['@type']) {
|
|
315
|
-
case CosmosMessage_1.CosmosMessageTypeValue.UNDELEGATE:
|
|
316
|
-
return {
|
|
317
|
-
...tx,
|
|
318
|
-
from: [msg.validator_address],
|
|
319
|
-
to: [msg.delegator_address]
|
|
320
|
-
};
|
|
321
|
-
case CosmosMessage_1.CosmosMessageTypeValue.WITHDRAW_DELEGATION_REWARD:
|
|
322
|
-
return {
|
|
323
|
-
...tx,
|
|
324
|
-
from: [msg.delegator_address],
|
|
325
|
-
to: [msg.validator_address]
|
|
326
|
-
};
|
|
327
|
-
case CosmosMessage_1.CosmosMessageTypeValue.DELEGATE:
|
|
328
|
-
return {
|
|
329
|
-
...tx,
|
|
330
|
-
from: [msg.delegator_address],
|
|
331
|
-
to: [msg.validator_address]
|
|
332
|
-
};
|
|
163
|
+
};
|
|
164
|
+
CosmosBaseProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
166
|
+
return __generator(this, function (_a) {
|
|
167
|
+
return [2 /*return*/, CosmosAddress_1.CosmosAddress.from(publicKey, this.options.addressPrefix).asString()];
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
CosmosBaseProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, publicKey) {
|
|
172
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
173
|
+
return __generator(this, function (_a) {
|
|
174
|
+
switch (transaction.type) {
|
|
175
|
+
case 'signed':
|
|
176
|
+
return [2 /*return*/, this.getDetailsFromSignedTransaction(transaction)];
|
|
177
|
+
case 'unsigned':
|
|
178
|
+
return [2 /*return*/, this.getDetailsFromUnsignedTransaction(transaction)];
|
|
333
179
|
default:
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
180
|
+
(0, coinlib_core_1.assertNever)(transaction);
|
|
181
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.COSMOS, 'Unsupported transaction type.');
|
|
182
|
+
}
|
|
183
|
+
return [2 /*return*/];
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
CosmosBaseProtocolImpl.prototype.getDetailsFromSignedTransaction = function (transaction) {
|
|
188
|
+
var _a;
|
|
189
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
190
|
+
var bytes, decoded, fee, result;
|
|
191
|
+
var _this = this;
|
|
192
|
+
return __generator(this, function (_b) {
|
|
193
|
+
switch (_b.label) {
|
|
194
|
+
case 0:
|
|
195
|
+
bytes = Uint8Array.from(Buffer.from(transaction.encoded, 'base64'));
|
|
196
|
+
return [4 /*yield*/, (0, cosmjs_1.decodeTxBytes)(bytes)];
|
|
197
|
+
case 1:
|
|
198
|
+
decoded = _b.sent();
|
|
199
|
+
fee = ((_a = decoded.fee) === null || _a === void 0 ? void 0 : _a.amount)
|
|
200
|
+
.map(function (_a) {
|
|
201
|
+
var amount = _a.amount;
|
|
202
|
+
return new bignumber_1.default(amount);
|
|
203
|
+
})
|
|
204
|
+
.reduce(function (current, next) { return current.plus(next); })
|
|
205
|
+
.toString(10);
|
|
206
|
+
result = decoded.messages
|
|
207
|
+
.map(function (message) {
|
|
208
|
+
var type = message.typeUrl;
|
|
209
|
+
switch (type) {
|
|
210
|
+
case CosmosMessage_1.CosmosMessageType.Send.value:
|
|
211
|
+
var sendMessage = CosmosSendMessage_1.CosmosSendMessage.fromEncodeObject(message);
|
|
212
|
+
return sendMessage.toAirGapTransaction(_this.options.network, fee);
|
|
213
|
+
case CosmosMessage_1.CosmosMessageType.Undelegate.value:
|
|
214
|
+
case CosmosMessage_1.CosmosMessageType.Delegate.value:
|
|
215
|
+
var delegateMessage = CosmosDelegateMessage_1.CosmosDelegateMessage.fromEncodeObject(message);
|
|
216
|
+
return delegateMessage.toAirGapTransaction(_this.options.network, fee);
|
|
217
|
+
case CosmosMessage_1.CosmosMessageType.WithdrawDelegationReward.value:
|
|
218
|
+
var withdrawMessage = CosmosWithdrawDelegationRewardMessage_1.CosmosWithdrawDelegationRewardMessage.fromEncodeObject(message);
|
|
219
|
+
return withdrawMessage.toAirGapTransaction(_this.options.network, fee);
|
|
220
|
+
default:
|
|
221
|
+
throw new errors_1.InvalidValueError(coinlib_core_1.Domain.COSMOS, 'Unknown transaction');
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
.map(function (tx) {
|
|
225
|
+
if (!tx.json) {
|
|
226
|
+
tx.json = {};
|
|
227
|
+
}
|
|
228
|
+
tx.json.memo = decoded.memo;
|
|
229
|
+
if (decoded.signerInfos.length > 0) {
|
|
230
|
+
tx.json.sequence = decoded.signerInfos[0].sequence.toString(10);
|
|
231
|
+
}
|
|
232
|
+
tx.arbitraryData = decoded.memo;
|
|
233
|
+
return tx;
|
|
234
|
+
});
|
|
235
|
+
return [2 /*return*/, result];
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
CosmosBaseProtocolImpl.prototype.getDetailsFromUnsignedTransaction = function (transaction) {
|
|
241
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
242
|
+
return __generator(this, function (_a) {
|
|
243
|
+
return [2 /*return*/, CosmosTransaction_1.CosmosTransaction.fromJSON(transaction).toAirGapTransactions(this.options.network)];
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
};
|
|
247
|
+
CosmosBaseProtocolImpl.prototype.getNetwork = function () {
|
|
248
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
249
|
+
return __generator(this, function (_a) {
|
|
250
|
+
return [2 /*return*/, this.options.network];
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
CosmosBaseProtocolImpl.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
|
|
255
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
256
|
+
var address;
|
|
257
|
+
return __generator(this, function (_a) {
|
|
258
|
+
switch (_a.label) {
|
|
259
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
260
|
+
case 1:
|
|
261
|
+
address = _a.sent();
|
|
262
|
+
return [2 /*return*/, this.getTransactionsForAddress(address, limit, cursor)];
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
CosmosBaseProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey, configuration) {
|
|
268
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
269
|
+
var address;
|
|
270
|
+
return __generator(this, function (_a) {
|
|
271
|
+
switch (_a.label) {
|
|
272
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
273
|
+
case 1:
|
|
274
|
+
address = _a.sent();
|
|
275
|
+
return [2 /*return*/, this.getBalanceOfAddress(address)];
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
CosmosBaseProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
|
|
281
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
282
|
+
var units, _a, total, transferable, balance, fee, estimatedFee, amountWithoutFees;
|
|
283
|
+
return __generator(this, function (_b) {
|
|
284
|
+
switch (_b.label) {
|
|
285
|
+
case 0: return [4 /*yield*/, this.getMetadata()];
|
|
286
|
+
case 1:
|
|
287
|
+
units = (_b.sent()).units;
|
|
288
|
+
return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
|
|
289
|
+
case 2:
|
|
290
|
+
_a = _b.sent(), total = _a.total, transferable = _a.transferable;
|
|
291
|
+
balance = new bignumber_1.default((0, module_kit_1.newAmount)(transferable !== null && transferable !== void 0 ? transferable : total).blockchain(units).value);
|
|
292
|
+
if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 3];
|
|
293
|
+
fee = configuration.fee;
|
|
294
|
+
return [3 /*break*/, 5];
|
|
295
|
+
case 3: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, to.map(function (recipient) { return ({
|
|
296
|
+
to: recipient,
|
|
297
|
+
amount: (0, module_kit_1.newAmount)(balance.div(to.length).decimalPlaces(0, bignumber_1.default.ROUND_CEIL), 'blockchain')
|
|
298
|
+
}); }))];
|
|
299
|
+
case 4:
|
|
300
|
+
estimatedFee = _b.sent();
|
|
301
|
+
fee = (0, module_kit_1.newAmount)(estimatedFee.medium).blockchain(units);
|
|
302
|
+
if (balance.lte(fee.value)) {
|
|
303
|
+
fee = (0, module_kit_1.newAmount)(0, 'blockchain');
|
|
304
|
+
}
|
|
305
|
+
_b.label = 5;
|
|
306
|
+
case 5:
|
|
307
|
+
fee = (0, module_kit_1.newAmount)(fee).blockchain(units);
|
|
308
|
+
amountWithoutFees = balance.minus(fee.value);
|
|
309
|
+
if (amountWithoutFees.isNegative()) {
|
|
310
|
+
amountWithoutFees = new bignumber_1.default(0);
|
|
311
|
+
}
|
|
312
|
+
return [2 /*return*/, (0, module_kit_1.newAmount)(amountWithoutFees, 'blockchain')];
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
CosmosBaseProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
|
|
318
|
+
var _a, _b;
|
|
319
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
320
|
+
var fee, estimatedFee, units, wrappedFee, address, nodeInfo, account, _c, total, transferable, balance, messages, i, message, memo, transaction;
|
|
321
|
+
return __generator(this, function (_d) {
|
|
322
|
+
switch (_d.label) {
|
|
323
|
+
case 0:
|
|
324
|
+
if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
|
|
325
|
+
fee = configuration.fee;
|
|
326
|
+
return [3 /*break*/, 3];
|
|
327
|
+
case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, details)];
|
|
328
|
+
case 2:
|
|
329
|
+
estimatedFee = _d.sent();
|
|
330
|
+
fee = estimatedFee.medium;
|
|
331
|
+
_d.label = 3;
|
|
332
|
+
case 3: return [4 /*yield*/, this.getMetadata()];
|
|
333
|
+
case 4:
|
|
334
|
+
units = (_d.sent()).units;
|
|
335
|
+
wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(units).value);
|
|
336
|
+
return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
337
|
+
case 5:
|
|
338
|
+
address = _d.sent();
|
|
339
|
+
return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
|
|
340
|
+
case 6:
|
|
341
|
+
nodeInfo = _d.sent();
|
|
342
|
+
return [4 /*yield*/, this.nodeClient.fetchAccount(address)];
|
|
343
|
+
case 7:
|
|
344
|
+
account = _d.sent();
|
|
345
|
+
return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
|
|
346
|
+
case 8:
|
|
347
|
+
_c = _d.sent(), total = _c.total, transferable = _c.transferable;
|
|
348
|
+
balance = new bignumber_1.default((0, module_kit_1.newAmount)(transferable !== null && transferable !== void 0 ? transferable : total).blockchain(units).value);
|
|
349
|
+
if (balance.lt(details.reduce(function (acc, next) { return acc.plus((0, module_kit_1.newAmount)(next.amount).blockchain(units).value); }, wrappedFee))) {
|
|
350
|
+
throw new coinlib_core_1.BalanceError(coinlib_core_1.Domain.COSMOS, 'not enough balance');
|
|
351
|
+
}
|
|
352
|
+
messages = [];
|
|
353
|
+
for (i = 0; i < details.length; ++i) {
|
|
354
|
+
message = new CosmosSendMessage_1.CosmosSendMessage(address, details[i].to, [
|
|
355
|
+
new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(details[i].amount).blockchain(units).value)
|
|
356
|
+
]);
|
|
357
|
+
messages.push(message);
|
|
358
|
+
}
|
|
359
|
+
memo = (_a = configuration === null || configuration === void 0 ? void 0 : configuration.arbitraryData) !== null && _a !== void 0 ? _a : '';
|
|
360
|
+
transaction = new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin(this.options.baseUnit, wrappedFee.toString(10))], (0, module_kit_1.newAmount)(this.options.defaultGas).blockchain(units).value), memo, nodeInfo.network, account.account_number, (_b = account.sequence) !== null && _b !== void 0 ? _b : '0');
|
|
361
|
+
return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({
|
|
362
|
+
messages: transaction.messages.map(function (message) { return message.toJSON(); }),
|
|
363
|
+
fee: transaction.fee.toJSON(),
|
|
364
|
+
memo: transaction.memo,
|
|
365
|
+
chainID: transaction.chainID,
|
|
366
|
+
accountNumber: transaction.accountNumber,
|
|
367
|
+
sequence: transaction.sequence
|
|
368
|
+
})];
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
CosmosBaseProtocolImpl.prototype.broadcastTransaction = function (transaction) {
|
|
374
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
375
|
+
return __generator(this, function (_a) {
|
|
376
|
+
return [2 /*return*/, this.nodeClient.broadcastSignedTransaction(transaction.encoded)];
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
CosmosBaseProtocolImpl.prototype.signMessageWithKeyPair = function (message, keyPair) {
|
|
381
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
382
|
+
var hexSecretKey, signature;
|
|
383
|
+
return __generator(this, function (_a) {
|
|
384
|
+
switch (_a.label) {
|
|
385
|
+
case 0:
|
|
386
|
+
hexSecretKey = (0, key_1.convertSecretKey)(keyPair.secretKey, 'hex');
|
|
387
|
+
return [4 /*yield*/, this.cryptoClient.signMessage(message, { privateKey: hexSecretKey.value })];
|
|
388
|
+
case 1:
|
|
389
|
+
signature = _a.sent();
|
|
390
|
+
return [2 /*return*/, (0, module_kit_1.newSignature)(signature, 'hex')];
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
CosmosBaseProtocolImpl.prototype.verifyMessageWithPublicKey = function (message, signature, publicKey) {
|
|
396
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
397
|
+
var hexSignature, hexPublicKey;
|
|
398
|
+
return __generator(this, function (_a) {
|
|
399
|
+
hexSignature = (0, signature_1.convertSignature)(signature, 'hex');
|
|
400
|
+
hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
401
|
+
return [2 /*return*/, this.cryptoClient.verifyMessage(message, hexSignature.value, hexPublicKey.value)];
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
};
|
|
405
|
+
CosmosBaseProtocolImpl.prototype.decryptAsymmetricWithKeyPair = function (payload, keyPair) {
|
|
406
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
407
|
+
var hexSecretKey, hexPublicKey;
|
|
408
|
+
return __generator(this, function (_a) {
|
|
409
|
+
hexSecretKey = (0, key_1.convertSecretKey)(keyPair.secretKey, 'hex');
|
|
410
|
+
hexPublicKey = (0, key_1.convertPublicKey)(keyPair.publicKey, 'hex');
|
|
411
|
+
return [2 /*return*/, this.cryptoClient.decryptAsymmetric(payload, {
|
|
412
|
+
privateKey: hexSecretKey.value,
|
|
413
|
+
publicKey: hexPublicKey.value
|
|
414
|
+
})];
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
CosmosBaseProtocolImpl.prototype.encryptAsymmetricWithPublicKey = function (payload, publicKey) {
|
|
419
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
420
|
+
var hexPublicKey;
|
|
421
|
+
return __generator(this, function (_a) {
|
|
422
|
+
hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
423
|
+
return [2 /*return*/, this.cryptoClient.encryptAsymmetric(payload, hexPublicKey.value)];
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
};
|
|
427
|
+
CosmosBaseProtocolImpl.prototype.encryptAESWithSecretKey = function (payload, secretKey) {
|
|
428
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
429
|
+
var hexSecretKey;
|
|
430
|
+
return __generator(this, function (_a) {
|
|
431
|
+
hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
|
|
432
|
+
return [2 /*return*/, this.cryptoClient.encryptAES(payload, hexSecretKey.value)];
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
CosmosBaseProtocolImpl.prototype.decryptAESWithSecretKey = function (payload, secretKey) {
|
|
437
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
438
|
+
var hexSecretKey;
|
|
439
|
+
return __generator(this, function (_a) {
|
|
440
|
+
hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
|
|
441
|
+
return [2 /*return*/, this.cryptoClient.decryptAES(payload, hexSecretKey.value)];
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
CosmosBaseProtocolImpl.prototype.getTransactionsForAddress = function (address, limit, cursor) {
|
|
446
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
447
|
+
var promises, senderOffset, recipientOffset, senderTotal, recipientTotal, senderLimit, recipientLimit, transactions, sentTransactions, receivedTransactions, allTransactions, result, _loop_1, _i, allTransactions_1, transaction;
|
|
448
|
+
var _a;
|
|
449
|
+
var _this = this;
|
|
450
|
+
return __generator(this, function (_b) {
|
|
451
|
+
switch (_b.label) {
|
|
452
|
+
case 0:
|
|
453
|
+
promises = [];
|
|
454
|
+
senderOffset = 0;
|
|
455
|
+
recipientOffset = 0;
|
|
456
|
+
senderTotal = 0;
|
|
457
|
+
recipientTotal = 0;
|
|
458
|
+
senderLimit = 0;
|
|
459
|
+
recipientLimit = 0;
|
|
460
|
+
if (!cursor) return [3 /*break*/, 1];
|
|
461
|
+
senderOffset = cursor.sender.offset;
|
|
462
|
+
recipientOffset = cursor.recipient.offset;
|
|
463
|
+
senderTotal = cursor.sender.total;
|
|
464
|
+
recipientTotal = cursor.recipient.total;
|
|
465
|
+
senderLimit = (0, transaction_2.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
|
|
466
|
+
if (senderOffset <= Math.floor(senderTotal / senderLimit) * senderLimit) {
|
|
467
|
+
promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true));
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
promises.push(new Promise(function (resolve) {
|
|
471
|
+
resolve({ txs: [], tx_responses: [], pagination: null, total: String(senderTotal) });
|
|
472
|
+
}));
|
|
473
|
+
}
|
|
474
|
+
recipientLimit = (0, transaction_2.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
|
|
475
|
+
if (recipientOffset <= Math.floor(recipientTotal / recipientLimit) * recipientLimit) {
|
|
476
|
+
promises.push(this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
promises.push(new Promise(function (resolve) {
|
|
480
|
+
resolve({ txs: [], tx_responses: [], pagination: null, total: String(senderTotal) });
|
|
481
|
+
}));
|
|
482
|
+
}
|
|
483
|
+
return [3 /*break*/, 3];
|
|
484
|
+
case 1:
|
|
485
|
+
;
|
|
486
|
+
return [4 /*yield*/, Promise.all([
|
|
487
|
+
this.nodeClient.fetchSendTransactionsFor(address, 1, 0, true).then(function (response) { return new bignumber_1.default(response.total).toNumber(); }),
|
|
488
|
+
this.nodeClient.fetchSendTransactionsFor(address, 1, 0, false).then(function (response) { return new bignumber_1.default(response.total).toNumber(); })
|
|
489
|
+
])];
|
|
490
|
+
case 2:
|
|
491
|
+
_a = _b.sent(), senderTotal = _a[0], recipientTotal = _a[1];
|
|
492
|
+
senderLimit = (0, transaction_2.calculateTransactionLimit)(limit, senderTotal, recipientTotal, senderOffset, recipientOffset);
|
|
493
|
+
recipientLimit = (0, transaction_2.calculateTransactionLimit)(limit, recipientTotal, senderTotal, recipientOffset, senderOffset);
|
|
494
|
+
promises.push(this.nodeClient.fetchSendTransactionsFor(address, senderLimit, senderOffset, true), this.nodeClient.fetchSendTransactionsFor(address, recipientLimit, recipientOffset, false));
|
|
495
|
+
_b.label = 3;
|
|
496
|
+
case 3: return [4 /*yield*/, Promise.all(promises)];
|
|
497
|
+
case 4:
|
|
498
|
+
transactions = _b.sent();
|
|
499
|
+
sentTransactions = transactions[0];
|
|
500
|
+
receivedTransactions = transactions[1];
|
|
501
|
+
allTransactions = sentTransactions === null || sentTransactions === void 0 ? void 0 : sentTransactions.tx_responses.concat(receivedTransactions === null || receivedTransactions === void 0 ? void 0 : receivedTransactions.tx_responses);
|
|
502
|
+
result = [];
|
|
503
|
+
_loop_1 = function (transaction) {
|
|
504
|
+
var timestamp = new Date(transaction.timestamp).getTime() / 1000;
|
|
505
|
+
var fee = transaction.tx.auth_info.fee.amount.reduce(function (current, next) { return (next.denom === _this.options.baseUnit ? current.plus(new bignumber_1.default(next.amount)) : current); }, new bignumber_1.default(0));
|
|
506
|
+
result = result.concat(transaction.tx.body.messages
|
|
507
|
+
.filter(function (msg) { return msg['@type'] !== CosmosMessage_1.CosmosMessageTypeValue.MSGMULTISEND; })
|
|
508
|
+
.map(function (msg) {
|
|
509
|
+
var tx = {
|
|
510
|
+
isInbound: false,
|
|
511
|
+
amount: (0, module_kit_1.newAmount)('0', 'blockchain'),
|
|
512
|
+
fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
|
|
513
|
+
network: _this.options.network,
|
|
514
|
+
status: {
|
|
515
|
+
type: 'unknown',
|
|
516
|
+
hash: transaction.txhash
|
|
517
|
+
},
|
|
518
|
+
timestamp: timestamp
|
|
519
|
+
};
|
|
520
|
+
switch (msg['@type']) {
|
|
521
|
+
case CosmosMessage_1.CosmosMessageTypeValue.UNDELEGATE:
|
|
522
|
+
return __assign(__assign({}, tx), { from: [msg.validator_address], to: [msg.delegator_address] });
|
|
523
|
+
case CosmosMessage_1.CosmosMessageTypeValue.WITHDRAW_DELEGATION_REWARD:
|
|
524
|
+
return __assign(__assign({}, tx), { from: [msg.delegator_address], to: [msg.validator_address] });
|
|
525
|
+
case CosmosMessage_1.CosmosMessageTypeValue.DELEGATE:
|
|
526
|
+
return __assign(__assign({}, tx), { from: [msg.delegator_address], to: [msg.validator_address] });
|
|
527
|
+
default:
|
|
528
|
+
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') });
|
|
529
|
+
}
|
|
530
|
+
}));
|
|
340
531
|
};
|
|
532
|
+
for (_i = 0, allTransactions_1 = allTransactions; _i < allTransactions_1.length; _i++) {
|
|
533
|
+
transaction = allTransactions_1[_i];
|
|
534
|
+
_loop_1(transaction);
|
|
535
|
+
}
|
|
536
|
+
return [2 /*return*/, {
|
|
537
|
+
transactions: result,
|
|
538
|
+
cursor: {
|
|
539
|
+
hasNext: senderOffset + senderLimit < senderTotal || recipientOffset + recipientLimit < recipientTotal,
|
|
540
|
+
limit: limit,
|
|
541
|
+
sender: {
|
|
542
|
+
total: senderTotal,
|
|
543
|
+
offset: senderOffset + senderLimit
|
|
544
|
+
},
|
|
545
|
+
recipient: {
|
|
546
|
+
total: recipientTotal,
|
|
547
|
+
offset: recipientOffset + recipientLimit
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}];
|
|
341
551
|
}
|
|
342
|
-
})
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
};
|
|
555
|
+
CosmosBaseProtocolImpl.prototype.getBalanceOfAddress = function (address, _configuration) {
|
|
556
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
557
|
+
var metadata, _a, total, available;
|
|
558
|
+
return __generator(this, function (_b) {
|
|
559
|
+
switch (_b.label) {
|
|
560
|
+
case 0: return [4 /*yield*/, this.getMetadata()];
|
|
561
|
+
case 1:
|
|
562
|
+
metadata = _b.sent();
|
|
563
|
+
return [4 /*yield*/, this.nodeClient.fetchBalance(address, this.options.baseUnit)];
|
|
564
|
+
case 2:
|
|
565
|
+
_a = _b.sent(), total = _a.total, available = _a.available;
|
|
566
|
+
return [2 /*return*/, {
|
|
567
|
+
total: (0, module_kit_1.newAmount)(total).blockchain(metadata.units),
|
|
568
|
+
transferable: (0, module_kit_1.newAmount)(available).blockchain(metadata.units)
|
|
569
|
+
}];
|
|
356
570
|
}
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
async getBalanceOfAddress(address, _configuration) {
|
|
361
|
-
const metadata = await this.getMetadata();
|
|
362
|
-
const { total, available } = await this.nodeClient.fetchBalance(address, this.options.baseUnit);
|
|
363
|
-
return {
|
|
364
|
-
total: (0, module_kit_1.newAmount)(total).blockchain(metadata.units),
|
|
365
|
-
transferable: (0, module_kit_1.newAmount)(available).blockchain(metadata.units)
|
|
366
|
-
};
|
|
367
|
-
}
|
|
571
|
+
});
|
|
572
|
+
});
|
|
573
|
+
};
|
|
368
574
|
// DELEGATION
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
return delegations.map((delegation) => delegation.delegation.validator_address);
|
|
380
|
-
}
|
|
381
|
-
async getDelegateeDetails(address) {
|
|
382
|
-
const validator = await this.nodeClient.fetchValidator(address);
|
|
383
|
-
const statusCodes = { 0: 'jailed', 1: 'inactive', 2: 'active' };
|
|
384
|
-
return {
|
|
385
|
-
name: validator.description.moniker,
|
|
386
|
-
status: statusCodes[validator.status],
|
|
387
|
-
address: validator.operator_address
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
async isPublicKeyDelegating(publicKey) {
|
|
391
|
-
const address = await this.getAddressFromPublicKey(publicKey);
|
|
392
|
-
return this.isAddressDelegating(address);
|
|
393
|
-
}
|
|
394
|
-
async isAddressDelegating(address) {
|
|
395
|
-
const delegations = await this.nodeClient.fetchDelegations(address);
|
|
396
|
-
return delegations.length > 0;
|
|
397
|
-
}
|
|
398
|
-
async getDelegatorDetailsFromPublicKey(publicKey) {
|
|
399
|
-
const address = await this.getAddressFromPublicKey(publicKey);
|
|
400
|
-
return this.getDelegatorDetailsFromAddress(address);
|
|
401
|
-
}
|
|
402
|
-
async getDelegatorDetailsFromAddress(address) {
|
|
403
|
-
return this.getDelegatorDetails(address);
|
|
404
|
-
}
|
|
405
|
-
async getDelegatorDetails(address, validator) {
|
|
406
|
-
const results = await Promise.all([
|
|
407
|
-
this.getBalanceOfAddress(address),
|
|
408
|
-
this.nodeClient.fetchDelegations(address).catch(() => []),
|
|
409
|
-
this.nodeClient.fetchRewardDetails(address).catch(() => [])
|
|
410
|
-
]);
|
|
411
|
-
const metadata = await this.getMetadata();
|
|
412
|
-
const totalBalance = (0, module_kit_1.newAmount)(results[0].total).blockchain(metadata.units);
|
|
413
|
-
const availableBalance = results[0].transferable ? (0, module_kit_1.newAmount)(results[0].transferable).blockchain(metadata.units) : totalBalance;
|
|
414
|
-
const delegations = results[1];
|
|
415
|
-
const rewardDetails = results[2];
|
|
416
|
-
const unclaimedRewards = rewardDetails.map((details) => [
|
|
417
|
-
details.validator_address,
|
|
418
|
-
details.reward?.reduce((total, next) => total.plus(next.amount), new bignumber_1.default(0)).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR) ??
|
|
419
|
-
new bignumber_1.default(0)
|
|
420
|
-
]);
|
|
421
|
-
const unclaimedTotalRewards = unclaimedRewards.reduce((total, next) => total.plus(next[1]), new bignumber_1.default(0));
|
|
422
|
-
const unclaimedValidatorRewards = validator
|
|
423
|
-
? unclaimedRewards.find(([validatorAddress, _]) => validatorAddress === validator)?.[1] || new bignumber_1.default(0)
|
|
424
|
-
: undefined;
|
|
425
|
-
const isDelegating = validator
|
|
426
|
-
? delegations.some((delegation) => delegation.delegation.validator_address === validator)
|
|
427
|
-
: delegations.length > 0;
|
|
428
|
-
const availableActions = await this.getAvailableDelegatorActions(isDelegating, new bignumber_1.default(availableBalance.value), unclaimedTotalRewards, unclaimedValidatorRewards);
|
|
429
|
-
return {
|
|
430
|
-
address,
|
|
431
|
-
balance: totalBalance.value,
|
|
432
|
-
delegatees: delegations.map((delegation) => delegation.delegation.validator_address),
|
|
433
|
-
availableActions
|
|
434
|
-
};
|
|
435
|
-
}
|
|
436
|
-
async getAvailableDelegatorActions(isDelegating, availableBalance, unclaimedTotalRewards, unclaimedDelegationRewards) {
|
|
437
|
-
const actions = [];
|
|
438
|
-
const metadata = await this.getMetadata();
|
|
439
|
-
const requiredFee = new bignumber_1.default(metadata.fee?.defaults?.medium.value ?? '0');
|
|
440
|
-
const hasSufficientBalance = availableBalance.gt(requiredFee);
|
|
441
|
-
if (hasSufficientBalance) {
|
|
442
|
-
actions.push({
|
|
443
|
-
type: transaction_1.CosmosDelegationActionType.DELEGATE,
|
|
444
|
-
args: ['validator', 'amount']
|
|
575
|
+
CosmosBaseProtocolImpl.prototype.getDefaultDelegatee = function () {
|
|
576
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
577
|
+
var validators;
|
|
578
|
+
return __generator(this, function (_a) {
|
|
579
|
+
switch (_a.label) {
|
|
580
|
+
case 0: return [4 /*yield*/, this.nodeClient.fetchValidators()];
|
|
581
|
+
case 1:
|
|
582
|
+
validators = _a.sent();
|
|
583
|
+
return [2 /*return*/, validators.length > 0 ? validators[0].operator_address : ''];
|
|
584
|
+
}
|
|
445
585
|
});
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
586
|
+
});
|
|
587
|
+
};
|
|
588
|
+
CosmosBaseProtocolImpl.prototype.getCurrentDelegateesForPublicKey = function (publicKey) {
|
|
589
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
590
|
+
var address;
|
|
591
|
+
return __generator(this, function (_a) {
|
|
592
|
+
switch (_a.label) {
|
|
593
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
594
|
+
case 1:
|
|
595
|
+
address = _a.sent();
|
|
596
|
+
return [2 /*return*/, this.getCurrentDelegateesForAddress(address)];
|
|
597
|
+
}
|
|
451
598
|
});
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
599
|
+
});
|
|
600
|
+
};
|
|
601
|
+
CosmosBaseProtocolImpl.prototype.getCurrentDelegateesForAddress = function (address) {
|
|
602
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
603
|
+
var delegations;
|
|
604
|
+
return __generator(this, function (_a) {
|
|
605
|
+
switch (_a.label) {
|
|
606
|
+
case 0: return [4 /*yield*/, this.nodeClient.fetchDelegations(address)];
|
|
607
|
+
case 1:
|
|
608
|
+
delegations = _a.sent();
|
|
609
|
+
return [2 /*return*/, delegations.map(function (delegation) { return delegation.delegation.validator_address; })];
|
|
610
|
+
}
|
|
456
611
|
});
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
612
|
+
});
|
|
613
|
+
};
|
|
614
|
+
CosmosBaseProtocolImpl.prototype.getDelegateeDetails = function (address) {
|
|
615
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
616
|
+
var validator, statusCodes;
|
|
617
|
+
return __generator(this, function (_a) {
|
|
618
|
+
switch (_a.label) {
|
|
619
|
+
case 0: return [4 /*yield*/, this.nodeClient.fetchValidator(address)];
|
|
620
|
+
case 1:
|
|
621
|
+
validator = _a.sent();
|
|
622
|
+
statusCodes = { 0: 'jailed', 1: 'inactive', 2: 'active' };
|
|
623
|
+
return [2 /*return*/, {
|
|
624
|
+
name: validator.description.moniker,
|
|
625
|
+
status: statusCodes[validator.status],
|
|
626
|
+
address: validator.operator_address
|
|
627
|
+
}];
|
|
628
|
+
}
|
|
462
629
|
});
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
const validator = delegatees[0];
|
|
475
|
-
const results = await Promise.all([this.getDelegatorDetails(address, validator), this.getDelegateeDetails(validator)]);
|
|
476
|
-
const delegatorDetails = results[0];
|
|
477
|
-
const validatorDetails = results[1];
|
|
478
|
-
return {
|
|
479
|
-
delegator: delegatorDetails,
|
|
480
|
-
delegatees: [validatorDetails]
|
|
481
|
-
};
|
|
482
|
-
}
|
|
483
|
-
async prepareDelegatorActionFromPublicKey(publicKey, type, data) {
|
|
484
|
-
switch (type) {
|
|
485
|
-
case transaction_1.CosmosDelegationActionType.DELEGATE:
|
|
486
|
-
(0, assert_1.assertFields)(`${type} action`, data, 'validator', 'amount');
|
|
487
|
-
return [await this.delegate(publicKey, data.validator, { value: data.amount, unit: 'blockchain' }, false)];
|
|
488
|
-
case transaction_1.CosmosDelegationActionType.UNDELEGATE:
|
|
489
|
-
(0, assert_1.assertFields)(`${type} action`, data, 'validator', 'amount');
|
|
490
|
-
return [await this.undelegate(publicKey, data.validator, { value: data.amount, unit: 'blockchain' })];
|
|
491
|
-
case transaction_1.CosmosDelegationActionType.WITHDRAW_ALL_REWARDS:
|
|
492
|
-
return [await this.withdrawDelegationRewards(publicKey, [])];
|
|
493
|
-
case transaction_1.CosmosDelegationActionType.WITHDRAW_VALIDATOR_REWARDS:
|
|
494
|
-
(0, assert_1.assertFields)(`${type} action`, data, 'validator');
|
|
495
|
-
return [await this.withdrawDelegationRewards(publicKey, [data.validator])];
|
|
496
|
-
default:
|
|
497
|
-
return Promise.reject(`Delegator action type ${type} is not supported.`);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
async undelegate(publicKey, validatorAddress, amount, memo) {
|
|
501
|
-
return this.delegate(publicKey, validatorAddress, amount, true, memo);
|
|
502
|
-
}
|
|
503
|
-
async delegate(publicKey, validatorAddress, amount, undelegate, memo) {
|
|
504
|
-
const address = await this.getAddressFromPublicKey(publicKey);
|
|
505
|
-
const nodeInfo = await this.nodeClient.fetchNodeInfo();
|
|
506
|
-
const account = await this.nodeClient.fetchAccount(address);
|
|
507
|
-
const metadata = await this.getMetadata();
|
|
508
|
-
const message = new CosmosDelegateMessage_1.CosmosDelegateMessage(address, Array.isArray(validatorAddress) ? validatorAddress[0] : validatorAddress, new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(amount).blockchain(metadata.units).value), undelegate);
|
|
509
|
-
return new CosmosTransaction_1.CosmosTransaction([message], new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(metadata.fee.defaults.medium).blockchain(metadata.units).value)], (0, module_kit_1.newAmount)(this.options.defaultGas).blockchain(metadata.units).value), memo !== undefined ? memo : '', nodeInfo.network, account.account_number, account.sequence ?? '0');
|
|
510
|
-
}
|
|
511
|
-
async withdrawDelegationRewards(publicKey, _validatorAddresses, memo) {
|
|
512
|
-
let validatorAddresses;
|
|
513
|
-
if (_validatorAddresses.length > 0) {
|
|
514
|
-
validatorAddresses = _validatorAddresses;
|
|
515
|
-
}
|
|
516
|
-
else {
|
|
517
|
-
const address = await this.getAddressFromPublicKey(publicKey);
|
|
518
|
-
const rewards = await this.nodeClient.fetchRewardDetails(address);
|
|
519
|
-
const filteredRewards = rewards.filter((reward) => reward.reward
|
|
520
|
-
? reward.reward
|
|
521
|
-
.reduce((total, next) => total.plus(new bignumber_1.default(next.amount)), new bignumber_1.default(0))
|
|
522
|
-
.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR)
|
|
523
|
-
.gt(0)
|
|
524
|
-
: false);
|
|
525
|
-
validatorAddresses = filteredRewards.map((reward) => reward.validator_address);
|
|
526
|
-
}
|
|
527
|
-
const address = await this.getAddressFromPublicKey(publicKey);
|
|
528
|
-
const nodeInfo = await this.nodeClient.fetchNodeInfo();
|
|
529
|
-
const account = await this.nodeClient.fetchAccount(address);
|
|
530
|
-
const messages = validatorAddresses.map((validatorAddress) => new CosmosWithdrawDelegationRewardMessage_1.CosmosWithdrawDelegationRewardMessage(address, validatorAddress));
|
|
531
|
-
const metadata = await this.getMetadata();
|
|
532
|
-
const cosmosSimulateTx = new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(metadata.fee.defaults.high).blockchain(metadata.units).value)], (0, module_kit_1.newAmount)(this.options.defaultGas).blockchain(metadata.units).toBigNumber().times(messages.length).toFixed()), memo !== undefined ? memo : '', nodeInfo.network, account.account_number, account.sequence ?? '0');
|
|
533
|
-
let fee;
|
|
534
|
-
try {
|
|
535
|
-
const hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
536
|
-
const publicKeyBuffer = Buffer.from(hexPublicKey.value, 'hex');
|
|
537
|
-
const dummySigB64 = Buffer.alloc(64, 0).toString('base64');
|
|
538
|
-
const txBytes = await (0, cosmjs_1.encodeTxBytes)(cosmosSimulateTx.toEncodeObject(), cosmosSimulateTx.fee, Uint8Array.from(publicKeyBuffer), new bignumber_1.default(cosmosSimulateTx.sequence).toNumber(), {
|
|
539
|
-
signature: dummySigB64,
|
|
540
|
-
pub_key: {
|
|
541
|
-
type: 'tendermint/PubKeySecp256k1',
|
|
542
|
-
value: publicKeyBuffer.toString('base64')
|
|
630
|
+
});
|
|
631
|
+
};
|
|
632
|
+
CosmosBaseProtocolImpl.prototype.isPublicKeyDelegating = function (publicKey) {
|
|
633
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
634
|
+
var address;
|
|
635
|
+
return __generator(this, function (_a) {
|
|
636
|
+
switch (_a.label) {
|
|
637
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
638
|
+
case 1:
|
|
639
|
+
address = _a.sent();
|
|
640
|
+
return [2 /*return*/, this.isAddressDelegating(address)];
|
|
543
641
|
}
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
642
|
+
});
|
|
643
|
+
});
|
|
644
|
+
};
|
|
645
|
+
CosmosBaseProtocolImpl.prototype.isAddressDelegating = function (address) {
|
|
646
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
647
|
+
var delegations;
|
|
648
|
+
return __generator(this, function (_a) {
|
|
649
|
+
switch (_a.label) {
|
|
650
|
+
case 0: return [4 /*yield*/, this.nodeClient.fetchDelegations(address)];
|
|
651
|
+
case 1:
|
|
652
|
+
delegations = _a.sent();
|
|
653
|
+
return [2 /*return*/, delegations.length > 0];
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
});
|
|
657
|
+
};
|
|
658
|
+
CosmosBaseProtocolImpl.prototype.getDelegatorDetailsFromPublicKey = function (publicKey) {
|
|
659
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
660
|
+
var address;
|
|
661
|
+
return __generator(this, function (_a) {
|
|
662
|
+
switch (_a.label) {
|
|
663
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
664
|
+
case 1:
|
|
665
|
+
address = _a.sent();
|
|
666
|
+
return [2 /*return*/, this.getDelegatorDetailsFromAddress(address)];
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
});
|
|
670
|
+
};
|
|
671
|
+
CosmosBaseProtocolImpl.prototype.getDelegatorDetailsFromAddress = function (address) {
|
|
672
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
673
|
+
return __generator(this, function (_a) {
|
|
674
|
+
return [2 /*return*/, this.getDelegatorDetails(address)];
|
|
675
|
+
});
|
|
676
|
+
});
|
|
677
|
+
};
|
|
678
|
+
CosmosBaseProtocolImpl.prototype.getDelegatorDetails = function (address, validator) {
|
|
679
|
+
var _a;
|
|
680
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
681
|
+
var results, metadata, totalBalance, availableBalance, delegations, rewardDetails, unclaimedRewards, unclaimedTotalRewards, unclaimedValidatorRewards, isDelegating, availableActions;
|
|
682
|
+
return __generator(this, function (_b) {
|
|
683
|
+
switch (_b.label) {
|
|
684
|
+
case 0: return [4 /*yield*/, Promise.all([
|
|
685
|
+
this.getBalanceOfAddress(address),
|
|
686
|
+
this.nodeClient.fetchDelegations(address).catch(function () { return []; }),
|
|
687
|
+
this.nodeClient.fetchRewardDetails(address).catch(function () { return []; })
|
|
688
|
+
])];
|
|
689
|
+
case 1:
|
|
690
|
+
results = _b.sent();
|
|
691
|
+
return [4 /*yield*/, this.getMetadata()];
|
|
692
|
+
case 2:
|
|
693
|
+
metadata = _b.sent();
|
|
694
|
+
totalBalance = (0, module_kit_1.newAmount)(results[0].total).blockchain(metadata.units);
|
|
695
|
+
availableBalance = results[0].transferable ? (0, module_kit_1.newAmount)(results[0].transferable).blockchain(metadata.units) : totalBalance;
|
|
696
|
+
delegations = results[1];
|
|
697
|
+
rewardDetails = results[2];
|
|
698
|
+
unclaimedRewards = rewardDetails.map(function (details) {
|
|
699
|
+
var _a, _b;
|
|
700
|
+
return [
|
|
701
|
+
details.validator_address,
|
|
702
|
+
(_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)
|
|
703
|
+
];
|
|
704
|
+
});
|
|
705
|
+
unclaimedTotalRewards = unclaimedRewards.reduce(function (total, next) { return total.plus(next[1]); }, new bignumber_1.default(0));
|
|
706
|
+
unclaimedValidatorRewards = validator
|
|
707
|
+
? ((_a = unclaimedRewards.find(function (_a) {
|
|
708
|
+
var validatorAddress = _a[0], _ = _a[1];
|
|
709
|
+
return validatorAddress === validator;
|
|
710
|
+
})) === null || _a === void 0 ? void 0 : _a[1]) || new bignumber_1.default(0)
|
|
711
|
+
: undefined;
|
|
712
|
+
isDelegating = validator
|
|
713
|
+
? delegations.some(function (delegation) { return delegation.delegation.validator_address === validator; })
|
|
714
|
+
: delegations.length > 0;
|
|
715
|
+
return [4 /*yield*/, this.getAvailableDelegatorActions(isDelegating, new bignumber_1.default(availableBalance.value), unclaimedTotalRewards, unclaimedValidatorRewards)];
|
|
716
|
+
case 3:
|
|
717
|
+
availableActions = _b.sent();
|
|
718
|
+
return [2 /*return*/, {
|
|
719
|
+
address: address,
|
|
720
|
+
balance: totalBalance.value,
|
|
721
|
+
delegatees: delegations.map(function (delegation) { return delegation.delegation.validator_address; }),
|
|
722
|
+
availableActions: availableActions
|
|
723
|
+
}];
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
});
|
|
727
|
+
};
|
|
728
|
+
CosmosBaseProtocolImpl.prototype.getAvailableDelegatorActions = function (isDelegating, availableBalance, unclaimedTotalRewards, unclaimedDelegationRewards) {
|
|
729
|
+
var _a, _b, _c;
|
|
730
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
731
|
+
var actions, metadata, requiredFee, hasSufficientBalance;
|
|
732
|
+
return __generator(this, function (_d) {
|
|
733
|
+
switch (_d.label) {
|
|
734
|
+
case 0:
|
|
735
|
+
actions = [];
|
|
736
|
+
return [4 /*yield*/, this.getMetadata()];
|
|
737
|
+
case 1:
|
|
738
|
+
metadata = _d.sent();
|
|
739
|
+
requiredFee = new bignumber_1.default((_c = (_b = (_a = metadata.fee) === null || _a === void 0 ? void 0 : _a.defaults) === null || _b === void 0 ? void 0 : _b.medium.value) !== null && _c !== void 0 ? _c : '0');
|
|
740
|
+
hasSufficientBalance = availableBalance.gt(requiredFee);
|
|
741
|
+
if (hasSufficientBalance) {
|
|
742
|
+
actions.push({
|
|
743
|
+
type: transaction_1.CosmosDelegationActionType.DELEGATE,
|
|
744
|
+
args: ['validator', 'amount']
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
if (isDelegating) {
|
|
748
|
+
actions.push({
|
|
749
|
+
type: transaction_1.CosmosDelegationActionType.UNDELEGATE,
|
|
750
|
+
args: ['validator', 'amount']
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
if (unclaimedTotalRewards.gt(0)) {
|
|
754
|
+
actions.push({
|
|
755
|
+
type: transaction_1.CosmosDelegationActionType.WITHDRAW_ALL_REWARDS
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
if (unclaimedDelegationRewards === null || unclaimedDelegationRewards === void 0 ? void 0 : unclaimedDelegationRewards.gt(0)) {
|
|
759
|
+
actions.push({
|
|
760
|
+
type: transaction_1.CosmosDelegationActionType.WITHDRAW_VALIDATOR_REWARDS,
|
|
761
|
+
args: ['validator']
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
return [2 /*return*/, actions];
|
|
765
|
+
}
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
};
|
|
769
|
+
CosmosBaseProtocolImpl.prototype.getDelegationDetailsFromPublicKey = function (publicKey, delegatees) {
|
|
770
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
771
|
+
var address;
|
|
772
|
+
return __generator(this, function (_a) {
|
|
773
|
+
switch (_a.label) {
|
|
774
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
775
|
+
case 1:
|
|
776
|
+
address = _a.sent();
|
|
777
|
+
return [2 /*return*/, this.getDelegationDetailsFromAddress(address, delegatees)];
|
|
778
|
+
}
|
|
779
|
+
});
|
|
780
|
+
});
|
|
781
|
+
};
|
|
782
|
+
CosmosBaseProtocolImpl.prototype.getDelegationDetailsFromAddress = function (address, delegatees) {
|
|
783
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
784
|
+
var validator, results, delegatorDetails, validatorDetails;
|
|
785
|
+
return __generator(this, function (_a) {
|
|
786
|
+
switch (_a.label) {
|
|
787
|
+
case 0:
|
|
788
|
+
if (delegatees.length > 1) {
|
|
789
|
+
return [2 /*return*/, Promise.reject('Multiple validators for a single delegation are not supported.')];
|
|
790
|
+
}
|
|
791
|
+
validator = delegatees[0];
|
|
792
|
+
return [4 /*yield*/, Promise.all([this.getDelegatorDetails(address, validator), this.getDelegateeDetails(validator)])];
|
|
793
|
+
case 1:
|
|
794
|
+
results = _a.sent();
|
|
795
|
+
delegatorDetails = results[0];
|
|
796
|
+
validatorDetails = results[1];
|
|
797
|
+
return [2 /*return*/, {
|
|
798
|
+
delegator: delegatorDetails,
|
|
799
|
+
delegatees: [validatorDetails]
|
|
800
|
+
}];
|
|
801
|
+
}
|
|
802
|
+
});
|
|
803
|
+
});
|
|
804
|
+
};
|
|
805
|
+
CosmosBaseProtocolImpl.prototype.prepareDelegatorActionFromPublicKey = function (publicKey, type, data) {
|
|
806
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
807
|
+
var _a;
|
|
808
|
+
return __generator(this, function (_b) {
|
|
809
|
+
switch (_b.label) {
|
|
810
|
+
case 0:
|
|
811
|
+
_a = type;
|
|
812
|
+
switch (_a) {
|
|
813
|
+
case transaction_1.CosmosDelegationActionType.DELEGATE: return [3 /*break*/, 1];
|
|
814
|
+
case transaction_1.CosmosDelegationActionType.UNDELEGATE: return [3 /*break*/, 3];
|
|
815
|
+
case transaction_1.CosmosDelegationActionType.WITHDRAW_ALL_REWARDS: return [3 /*break*/, 5];
|
|
816
|
+
case transaction_1.CosmosDelegationActionType.WITHDRAW_VALIDATOR_REWARDS: return [3 /*break*/, 7];
|
|
817
|
+
}
|
|
818
|
+
return [3 /*break*/, 9];
|
|
819
|
+
case 1:
|
|
820
|
+
(0, assert_1.assertFields)("".concat(type, " action"), data, 'validator', 'amount');
|
|
821
|
+
return [4 /*yield*/, this.delegate(publicKey, data.validator, { value: data.amount, unit: 'blockchain' }, false)];
|
|
822
|
+
case 2: return [2 /*return*/, [_b.sent()]];
|
|
823
|
+
case 3:
|
|
824
|
+
(0, assert_1.assertFields)("".concat(type, " action"), data, 'validator', 'amount');
|
|
825
|
+
return [4 /*yield*/, this.undelegate(publicKey, data.validator, { value: data.amount, unit: 'blockchain' })];
|
|
826
|
+
case 4: return [2 /*return*/, [_b.sent()]];
|
|
827
|
+
case 5: return [4 /*yield*/, this.withdrawDelegationRewards(publicKey, [])];
|
|
828
|
+
case 6: return [2 /*return*/, [_b.sent()]];
|
|
829
|
+
case 7:
|
|
830
|
+
(0, assert_1.assertFields)("".concat(type, " action"), data, 'validator');
|
|
831
|
+
return [4 /*yield*/, this.withdrawDelegationRewards(publicKey, [data.validator])];
|
|
832
|
+
case 8: return [2 /*return*/, [_b.sent()]];
|
|
833
|
+
case 9: return [2 /*return*/, Promise.reject("Delegator action type ".concat(type, " is not supported."))];
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
});
|
|
837
|
+
};
|
|
838
|
+
CosmosBaseProtocolImpl.prototype.undelegate = function (publicKey, validatorAddress, amount, memo) {
|
|
839
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
840
|
+
return __generator(this, function (_a) {
|
|
841
|
+
return [2 /*return*/, this.delegate(publicKey, validatorAddress, amount, true, memo)];
|
|
842
|
+
});
|
|
843
|
+
});
|
|
844
|
+
};
|
|
845
|
+
CosmosBaseProtocolImpl.prototype.delegate = function (publicKey, validatorAddress, amount, undelegate, memo) {
|
|
846
|
+
var _a;
|
|
847
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
848
|
+
var address, nodeInfo, account, metadata, message;
|
|
849
|
+
return __generator(this, function (_b) {
|
|
850
|
+
switch (_b.label) {
|
|
851
|
+
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
852
|
+
case 1:
|
|
853
|
+
address = _b.sent();
|
|
854
|
+
return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
|
|
855
|
+
case 2:
|
|
856
|
+
nodeInfo = _b.sent();
|
|
857
|
+
return [4 /*yield*/, this.nodeClient.fetchAccount(address)];
|
|
858
|
+
case 3:
|
|
859
|
+
account = _b.sent();
|
|
860
|
+
return [4 /*yield*/, this.getMetadata()];
|
|
861
|
+
case 4:
|
|
862
|
+
metadata = _b.sent();
|
|
863
|
+
message = new CosmosDelegateMessage_1.CosmosDelegateMessage(address, Array.isArray(validatorAddress) ? validatorAddress[0] : validatorAddress, new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(amount).blockchain(metadata.units).value), undelegate);
|
|
864
|
+
return [2 /*return*/, new CosmosTransaction_1.CosmosTransaction([message], new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(metadata.fee.defaults.medium).blockchain(metadata.units).value)], (0, module_kit_1.newAmount)(this.options.defaultGas).blockchain(metadata.units).value), memo !== undefined ? memo : '', nodeInfo.network, account.account_number, (_a = account.sequence) !== null && _a !== void 0 ? _a : '0')];
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
});
|
|
868
|
+
};
|
|
869
|
+
CosmosBaseProtocolImpl.prototype.withdrawDelegationRewards = function (publicKey, _validatorAddresses, memo) {
|
|
870
|
+
var _a, _b;
|
|
871
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
872
|
+
var validatorAddresses, address_1, rewards, filteredRewards, address, nodeInfo, account, messages, metadata, cosmosSimulateTx, fee, hexPublicKey, publicKeyBuffer, dummySigB64, txBytes, calcullatedFee, error_1;
|
|
873
|
+
return __generator(this, function (_c) {
|
|
874
|
+
switch (_c.label) {
|
|
875
|
+
case 0:
|
|
876
|
+
if (!(_validatorAddresses.length > 0)) return [3 /*break*/, 1];
|
|
877
|
+
validatorAddresses = _validatorAddresses;
|
|
878
|
+
return [3 /*break*/, 4];
|
|
879
|
+
case 1: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
880
|
+
case 2:
|
|
881
|
+
address_1 = _c.sent();
|
|
882
|
+
return [4 /*yield*/, this.nodeClient.fetchRewardDetails(address_1)];
|
|
883
|
+
case 3:
|
|
884
|
+
rewards = _c.sent();
|
|
885
|
+
filteredRewards = rewards.filter(function (reward) {
|
|
886
|
+
return reward.reward
|
|
887
|
+
? reward.reward
|
|
888
|
+
.reduce(function (total, next) { return total.plus(new bignumber_1.default(next.amount)); }, new bignumber_1.default(0))
|
|
889
|
+
.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR)
|
|
890
|
+
.gt(0)
|
|
891
|
+
: false;
|
|
892
|
+
});
|
|
893
|
+
validatorAddresses = filteredRewards.map(function (reward) { return reward.validator_address; });
|
|
894
|
+
_c.label = 4;
|
|
895
|
+
case 4: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
896
|
+
case 5:
|
|
897
|
+
address = _c.sent();
|
|
898
|
+
return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
|
|
899
|
+
case 6:
|
|
900
|
+
nodeInfo = _c.sent();
|
|
901
|
+
return [4 /*yield*/, this.nodeClient.fetchAccount(address)];
|
|
902
|
+
case 7:
|
|
903
|
+
account = _c.sent();
|
|
904
|
+
messages = validatorAddresses.map(function (validatorAddress) { return new CosmosWithdrawDelegationRewardMessage_1.CosmosWithdrawDelegationRewardMessage(address, validatorAddress); });
|
|
905
|
+
return [4 /*yield*/, this.getMetadata()];
|
|
906
|
+
case 8:
|
|
907
|
+
metadata = _c.sent();
|
|
908
|
+
cosmosSimulateTx = new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin(this.options.baseUnit, (0, module_kit_1.newAmount)(metadata.fee.defaults.high).blockchain(metadata.units).value)], (0, module_kit_1.newAmount)(this.options.defaultGas).blockchain(metadata.units).toBigNumber().times(messages.length).toFixed()), memo !== undefined ? memo : '', nodeInfo.network, account.account_number, (_a = account.sequence) !== null && _a !== void 0 ? _a : '0');
|
|
909
|
+
_c.label = 9;
|
|
910
|
+
case 9:
|
|
911
|
+
_c.trys.push([9, 12, , 13]);
|
|
912
|
+
hexPublicKey = (0, key_1.convertPublicKey)(publicKey, 'hex');
|
|
913
|
+
publicKeyBuffer = Buffer.from(hexPublicKey.value, 'hex');
|
|
914
|
+
dummySigB64 = Buffer.alloc(64, 0).toString('base64');
|
|
915
|
+
return [4 /*yield*/, (0, cosmjs_1.encodeTxBytes)(cosmosSimulateTx.toEncodeObject(), cosmosSimulateTx.fee, Uint8Array.from(publicKeyBuffer), new bignumber_1.default(cosmosSimulateTx.sequence).toNumber(), {
|
|
916
|
+
signature: dummySigB64,
|
|
917
|
+
pub_key: {
|
|
918
|
+
type: 'tendermint/PubKeySecp256k1',
|
|
919
|
+
value: publicKeyBuffer.toString('base64')
|
|
920
|
+
}
|
|
921
|
+
}, cosmosSimulateTx.chainID, new bignumber_1.default(cosmosSimulateTx.accountNumber).toNumber())];
|
|
922
|
+
case 10:
|
|
923
|
+
txBytes = _c.sent();
|
|
924
|
+
return [4 /*yield*/, this.nodeClient.simulateTx(Buffer.from(txBytes).toString('base64'))];
|
|
925
|
+
case 11:
|
|
926
|
+
calcullatedFee = _c.sent();
|
|
927
|
+
fee = calcullatedFee.gas_used.toString();
|
|
928
|
+
return [3 /*break*/, 13];
|
|
929
|
+
case 12:
|
|
930
|
+
error_1 = _c.sent();
|
|
931
|
+
fee = (0, module_kit_1.newAmount)(metadata.fee.defaults.high).blockchain(metadata.units).value;
|
|
932
|
+
return [3 /*break*/, 13];
|
|
933
|
+
case 13: return [2 /*return*/, new CosmosTransaction_1.CosmosTransaction(messages, new CosmosFee_1.CosmosFee([new CosmosCoin_1.CosmosCoin(this.options.baseUnit, fee)], (0, module_kit_1.newAmount)(this.options.defaultGas).blockchain(metadata.units).toBigNumber().times(messages.length).toFixed()), memo !== undefined ? memo : '', nodeInfo.network, account.account_number, (_b = account.sequence) !== null && _b !== void 0 ? _b : '0')];
|
|
934
|
+
}
|
|
935
|
+
});
|
|
936
|
+
});
|
|
937
|
+
};
|
|
938
|
+
CosmosBaseProtocolImpl.prototype.withdrawAllDelegationRewards = function (delegatorAddress, fee, memo) {
|
|
939
|
+
var _a;
|
|
940
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
941
|
+
var nodeInfo, account, metadata;
|
|
942
|
+
return __generator(this, function (_b) {
|
|
943
|
+
switch (_b.label) {
|
|
944
|
+
case 0: return [4 /*yield*/, this.nodeClient.fetchNodeInfo()];
|
|
945
|
+
case 1:
|
|
946
|
+
nodeInfo = _b.sent();
|
|
947
|
+
return [4 /*yield*/, this.nodeClient.fetchAccount(delegatorAddress)];
|
|
948
|
+
case 2:
|
|
949
|
+
account = _b.sent();
|
|
950
|
+
return [4 /*yield*/, this.getMetadata()];
|
|
951
|
+
case 3:
|
|
952
|
+
metadata = _b.sent();
|
|
953
|
+
return [2 /*return*/, this.nodeClient.withdrawAllDelegationRewards(delegatorAddress, nodeInfo.network, account.account_number, (_a = account.sequence) !== null && _a !== void 0 ? _a : '0', new bignumber_1.default((0, module_kit_1.newAmount)(metadata.fee.defaults.high).blockchain(metadata.units).value), new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(metadata.units).value), memo !== undefined ? memo : '')];
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
});
|
|
957
|
+
};
|
|
958
|
+
CosmosBaseProtocolImpl.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, this.options.baseUnit)];
|
|
962
|
+
});
|
|
963
|
+
});
|
|
964
|
+
};
|
|
965
|
+
CosmosBaseProtocolImpl.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, this.options.baseUnit)];
|
|
969
|
+
});
|
|
970
|
+
});
|
|
971
|
+
};
|
|
972
|
+
CosmosBaseProtocolImpl.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
|
+
CosmosBaseProtocolImpl.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, this.options.baseUnit)];
|
|
983
|
+
});
|
|
984
|
+
});
|
|
985
|
+
};
|
|
986
|
+
CosmosBaseProtocolImpl.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
|
+
CosmosBaseProtocolImpl.prototype.fetchValidators = function () {
|
|
994
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
995
|
+
return __generator(this, function (_a) {
|
|
996
|
+
return [2 /*return*/, this.nodeClient.fetchValidators()];
|
|
997
|
+
});
|
|
998
|
+
});
|
|
999
|
+
};
|
|
1000
|
+
CosmosBaseProtocolImpl.prototype.fetchUnbondingDelegations = function (delegatorAddress) {
|
|
1001
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1002
|
+
return __generator(this, function (_a) {
|
|
1003
|
+
return [2 /*return*/, this.nodeClient.fetchUnbondingDelegations(delegatorAddress)];
|
|
1004
|
+
});
|
|
1005
|
+
});
|
|
1006
|
+
};
|
|
1007
|
+
CosmosBaseProtocolImpl.prototype.fetchTotalUnbondingAmount = function (address) {
|
|
1008
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1009
|
+
return __generator(this, function (_a) {
|
|
1010
|
+
return [2 /*return*/, this.nodeClient.fetchTotalUnbondingAmount(address, this.options.baseUnit)];
|
|
1011
|
+
});
|
|
1012
|
+
});
|
|
1013
|
+
};
|
|
1014
|
+
CosmosBaseProtocolImpl.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
|
+
return CosmosBaseProtocolImpl;
|
|
1022
|
+
}());
|
|
587
1023
|
exports.CosmosBaseProtocolImpl = CosmosBaseProtocolImpl;
|
|
588
1024
|
//# sourceMappingURL=CosmosBaseProtocol.js.map
|