-
Notifications
You must be signed in to change notification settings - Fork 336
/
signingstargateclient.ts
430 lines (407 loc) · 15 KB
/
signingstargateclient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino, StdFee } from "@cosmjs/amino";
import { fromBase64 } from "@cosmjs/encoding";
import { Int53 } from "@cosmjs/math";
import {
EncodeObject,
encodePubkey,
GeneratedType,
isOfflineDirectSigner,
makeAuthInfoBytes,
makeSignDoc,
OfflineSigner,
Registry,
TxBodyEncodeObject,
} from "@cosmjs/proto-signing";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { assert } from "@cosmjs/utils";
import Long from "long";
import { AminoTypes } from "./aminotypes";
import { MsgMultiSend } from "./codec/cosmos/bank/v1beta1/tx";
import { Coin } from "./codec/cosmos/base/v1beta1/coin";
import {
MsgFundCommunityPool,
MsgSetWithdrawAddress,
MsgWithdrawDelegatorReward,
MsgWithdrawValidatorCommission,
} from "./codec/cosmos/distribution/v1beta1/tx";
import {
MsgBeginRedelegate,
MsgCreateValidator,
MsgDelegate,
MsgEditValidator,
MsgUndelegate,
} from "./codec/cosmos/staking/v1beta1/tx";
import { SignMode } from "./codec/cosmos/tx/signing/v1beta1/signing";
import { TxRaw } from "./codec/cosmos/tx/v1beta1/tx";
import { MsgTransfer } from "./codec/ibc/applications/transfer/v1/tx";
import {
MsgAcknowledgement,
MsgChannelCloseConfirm,
MsgChannelCloseInit,
MsgChannelOpenAck,
MsgChannelOpenConfirm,
MsgChannelOpenInit,
MsgChannelOpenTry,
MsgRecvPacket,
MsgTimeout,
MsgTimeoutOnClose,
} from "./codec/ibc/core/channel/v1/tx";
import { Height } from "./codec/ibc/core/client/v1/client";
import {
MsgCreateClient,
MsgSubmitMisbehaviour,
MsgUpdateClient,
MsgUpgradeClient,
} from "./codec/ibc/core/client/v1/tx";
import {
MsgConnectionOpenAck,
MsgConnectionOpenConfirm,
MsgConnectionOpenInit,
MsgConnectionOpenTry,
} from "./codec/ibc/core/connection/v1/tx";
import {
MsgDelegateEncodeObject,
MsgSendEncodeObject,
MsgTransferEncodeObject,
MsgUndelegateEncodeObject,
MsgWithdrawDelegatorRewardEncodeObject,
} from "./encodeobjects";
import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
import { BroadcastTxResponse, StargateClient } from "./stargateclient";
/**
* These fees are used by the higher level methods of SigningCosmosClient
*
* This is the same as CosmosFeeTable from @cosmjs/launchpad but those might diverge in the future.
*/
export interface CosmosFeeTable extends FeeTable {
readonly send: StdFee;
readonly delegate: StdFee;
readonly transfer: StdFee;
readonly undelegate: StdFee;
readonly withdraw: StdFee;
}
export const defaultGasPrice = GasPrice.fromString("0.025ucosm");
export const defaultGasLimits: GasLimits<CosmosFeeTable> = {
send: 80_000,
delegate: 160_000,
transfer: 160_000,
undelegate: 160_000,
withdraw: 160_000,
};
export const defaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [
["/cosmos.bank.v1beta1.MsgMultiSend", MsgMultiSend],
["/cosmos.distribution.v1beta1.MsgFundCommunityPool", MsgFundCommunityPool],
["/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", MsgSetWithdrawAddress],
["/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", MsgWithdrawDelegatorReward],
["/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission", MsgWithdrawValidatorCommission],
["/cosmos.staking.v1beta1.MsgBeginRedelegate", MsgBeginRedelegate],
["/cosmos.staking.v1beta1.MsgCreateValidator", MsgCreateValidator],
["/cosmos.staking.v1beta1.MsgDelegate", MsgDelegate],
["/cosmos.staking.v1beta1.MsgEditValidator", MsgEditValidator],
["/cosmos.staking.v1beta1.MsgUndelegate", MsgUndelegate],
["/ibc.core.channel.v1.MsgChannelOpenInit", MsgChannelOpenInit],
["/ibc.core.channel.v1.MsgChannelOpenTry", MsgChannelOpenTry],
["/ibc.core.channel.v1.MsgChannelOpenAck", MsgChannelOpenAck],
["/ibc.core.channel.v1.MsgChannelOpenConfirm", MsgChannelOpenConfirm],
["/ibc.core.channel.v1.MsgChannelCloseInit", MsgChannelCloseInit],
["/ibc.core.channel.v1.MsgChannelCloseConfirm", MsgChannelCloseConfirm],
["/ibc.core.channel.v1.MsgRecvPacket", MsgRecvPacket],
["/ibc.core.channel.v1.MsgTimeout ", MsgTimeout],
["/ibc.core.channel.v1.MsgTimeoutOnClose", MsgTimeoutOnClose],
["/ibc.core.channel.v1.MsgAcknowledgement", MsgAcknowledgement],
["/ibc.core.client.v1.MsgCreateClient", MsgCreateClient],
["/ibc.core.client.v1.MsgUpdateClient", MsgUpdateClient],
["/ibc.core.client.v1.MsgUpgradeClient", MsgUpgradeClient],
["/ibc.core.client.v1.MsgSubmitMisbehaviour", MsgSubmitMisbehaviour],
["/ibc.core.connection.v1.MsgConnectionOpenInit", MsgConnectionOpenInit],
["/ibc.core.connection.v1.MsgConnectionOpenTry", MsgConnectionOpenTry],
["/ibc.core.connection.v1.MsgConnectionOpenAck", MsgConnectionOpenAck],
["/ibc.core.connection.v1.MsgConnectionOpenConfirm", MsgConnectionOpenConfirm],
["/ibc.applications.transfer.v1.MsgTransfer", MsgTransfer],
];
function createDefaultRegistry(): Registry {
return new Registry(defaultRegistryTypes);
}
/**
* Signing information for a single signer that is not included in the transaction.
*
* @see https://github.com/cosmos/cosmos-sdk/blob/v0.42.2/x/auth/signing/sign_mode_handler.go#L23-L37
*/
export interface SignerData {
readonly accountNumber: number;
readonly sequence: number;
readonly chainId: string;
}
/** Use for testing only */
export interface PrivateSigningStargateClient {
readonly fees: CosmosFeeTable;
readonly registry: Registry;
}
export interface SigningStargateClientOptions {
readonly registry?: Registry;
readonly aminoTypes?: AminoTypes;
readonly prefix?: string;
readonly gasPrice?: GasPrice;
readonly gasLimits?: Partial<GasLimits<CosmosFeeTable>>;
readonly broadcastTimeoutMs?: number;
readonly broadcastPollIntervalMs?: number;
}
export class SigningStargateClient extends StargateClient {
public readonly fees: CosmosFeeTable;
public readonly registry: Registry;
public readonly broadcastTimeoutMs: number | undefined;
public readonly broadcastPollIntervalMs: number | undefined;
private readonly signer: OfflineSigner;
private readonly aminoTypes: AminoTypes;
public static async connectWithSigner(
endpoint: string,
signer: OfflineSigner,
options: SigningStargateClientOptions = {},
): Promise<SigningStargateClient> {
const tmClient = await Tendermint34Client.connect(endpoint);
return new SigningStargateClient(tmClient, signer, options);
}
/**
* Creates a client in offline mode.
*
* This should only be used in niche cases where you know exactly what you're doing,
* e.g. when building an offline signing application.
*
* When you try to use online functionality with such a signer, an
* exception will be raised.
*/
public static async offline(
signer: OfflineSigner,
options: SigningStargateClientOptions = {},
): Promise<SigningStargateClient> {
return new SigningStargateClient(undefined, signer, options);
}
protected constructor(
tmClient: Tendermint34Client | undefined,
signer: OfflineSigner,
options: SigningStargateClientOptions,
) {
super(tmClient);
const {
registry = createDefaultRegistry(),
aminoTypes = new AminoTypes({ prefix: options.prefix }),
gasPrice = defaultGasPrice,
gasLimits = {},
} = options;
this.fees = buildFeeTable<CosmosFeeTable>(gasPrice, defaultGasLimits, gasLimits);
this.registry = registry;
this.aminoTypes = aminoTypes;
this.signer = signer;
this.broadcastTimeoutMs = options.broadcastTimeoutMs;
this.broadcastPollIntervalMs = options.broadcastPollIntervalMs;
}
public async sendTokens(
senderAddress: string,
recipientAddress: string,
amount: readonly Coin[],
memo = "",
): Promise<BroadcastTxResponse> {
const sendMsg: MsgSendEncodeObject = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: senderAddress,
toAddress: recipientAddress,
amount: [...amount],
},
};
return this.signAndBroadcast(senderAddress, [sendMsg], this.fees.send, memo);
}
public async delegateTokens(
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
memo = "",
): Promise<BroadcastTxResponse> {
const delegateMsg: MsgDelegateEncodeObject = {
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
value: MsgDelegate.fromPartial({
delegatorAddress: delegatorAddress,
validatorAddress: validatorAddress,
amount: amount,
}),
};
return this.signAndBroadcast(delegatorAddress, [delegateMsg], this.fees.delegate, memo);
}
public async undelegateTokens(
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
memo = "",
): Promise<BroadcastTxResponse> {
const undelegateMsg: MsgUndelegateEncodeObject = {
typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate",
value: MsgUndelegate.fromPartial({
delegatorAddress: delegatorAddress,
validatorAddress: validatorAddress,
amount: amount,
}),
};
return this.signAndBroadcast(delegatorAddress, [undelegateMsg], this.fees.undelegate, memo);
}
public async withdrawRewards(
delegatorAddress: string,
validatorAddress: string,
memo = "",
): Promise<BroadcastTxResponse> {
const withdrawMsg: MsgWithdrawDelegatorRewardEncodeObject = {
typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
value: MsgWithdrawDelegatorReward.fromPartial({
delegatorAddress: delegatorAddress,
validatorAddress: validatorAddress,
}),
};
return this.signAndBroadcast(delegatorAddress, [withdrawMsg], this.fees.withdraw, memo);
}
public async sendIbcTokens(
senderAddress: string,
recipientAddress: string,
transferAmount: Coin,
sourcePort: string,
sourceChannel: string,
timeoutHeight: Height | undefined,
/** timeout in seconds */
timeoutTimestamp: number | undefined,
memo = "",
): Promise<BroadcastTxResponse> {
const timeoutTimestampNanoseconds = timeoutTimestamp
? Long.fromNumber(timeoutTimestamp).multiply(1_000_000_000)
: undefined;
const transferMsg: MsgTransferEncodeObject = {
typeUrl: "/ibc.applications.transfer.v1.MsgTransfer",
value: MsgTransfer.fromPartial({
sourcePort: sourcePort,
sourceChannel: sourceChannel,
sender: senderAddress,
receiver: recipientAddress,
token: transferAmount,
timeoutHeight: timeoutHeight,
timeoutTimestamp: timeoutTimestampNanoseconds,
}),
};
return this.signAndBroadcast(senderAddress, [transferMsg], this.fees.transfer, memo);
}
public async signAndBroadcast(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
memo = "",
): Promise<BroadcastTxResponse> {
const txRaw = await this.sign(signerAddress, messages, fee, memo);
const txBytes = TxRaw.encode(txRaw).finish();
return this.broadcastTx(txBytes, this.broadcastTimeoutMs, this.broadcastPollIntervalMs);
}
/**
* Gets account number and sequence from the API, creates a sign doc,
* creates a single signature and assembles the signed transaction.
*
* The sign mode (SIGN_MODE_DIRECT or SIGN_MODE_LEGACY_AMINO_JSON) is determined by this client's signer.
*
* You can pass signer data (account number, sequence and chain ID) explicitly instead of querying them
* from the chain. This is needed when signing for a multisig account, but it also allows for offline signing
* (See the SigningStargateClient.offline constructor).
*/
public async sign(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
memo: string,
explicitSignerData?: SignerData,
): Promise<TxRaw> {
let signerData: SignerData;
if (explicitSignerData) {
signerData = explicitSignerData;
} else {
const { accountNumber, sequence } = await this.getSequence(signerAddress);
const chainId = await this.getChainId();
signerData = {
accountNumber: accountNumber,
sequence: sequence,
chainId: chainId,
};
}
return isOfflineDirectSigner(this.signer)
? this.signDirect(signerAddress, messages, fee, memo, signerData)
: this.signAmino(signerAddress, messages, fee, memo, signerData);
}
private async signAmino(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
memo: string,
{ accountNumber, sequence, chainId }: SignerData,
): Promise<TxRaw> {
assert(!isOfflineDirectSigner(this.signer));
const accountFromSigner = (await this.signer.getAccounts()).find(
(account) => account.address === signerAddress,
);
if (!accountFromSigner) {
throw new Error("Failed to retrieve account from signer");
}
const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
const msgs = messages.map((msg) => this.aminoTypes.toAmino(msg));
const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence);
const { signature, signed } = await this.signer.signAmino(signerAddress, signDoc);
const signedTxBody = {
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
memo: signed.memo,
};
const signedTxBodyEncodeObject: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: signedTxBody,
};
const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject);
const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber();
const signedSequence = Int53.fromString(signed.sequence).toNumber();
const signedAuthInfoBytes = makeAuthInfoBytes(
[pubkey],
signed.fee.amount,
signedGasLimit,
signedSequence,
signMode,
);
return TxRaw.fromPartial({
bodyBytes: signedTxBodyBytes,
authInfoBytes: signedAuthInfoBytes,
signatures: [fromBase64(signature.signature)],
});
}
private async signDirect(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
memo: string,
{ accountNumber, sequence, chainId }: SignerData,
): Promise<TxRaw> {
assert(isOfflineDirectSigner(this.signer));
const accountFromSigner = (await this.signer.getAccounts()).find(
(account) => account.address === signerAddress,
);
if (!accountFromSigner) {
throw new Error("Failed to retrieve account from signer");
}
const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
const txBodyEncodeObject: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: messages,
memo: memo,
},
};
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
const gasLimit = Int53.fromString(fee.gas).toNumber();
const authInfoBytes = makeAuthInfoBytes([pubkey], fee.amount, gasLimit, sequence);
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber);
const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc);
return TxRaw.fromPartial({
bodyBytes: signed.bodyBytes,
authInfoBytes: signed.authInfoBytes,
signatures: [fromBase64(signature.signature)],
});
}
}