-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcosmo_sequence.ts
272 lines (271 loc) · 8.39 KB
/
cosmo_sequence.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
import {
DirectSecp256k1HdWallet,
Registry,
makeSignDoc,
makeAuthInfoBytes,
makeSignBytes,
isOfflineDirectSigner,
EncodeObject,
OfflineSigner,
encodePubkey,
// anyToSinglePubkey
} from "@cosmjs/proto-signing";
import { AminoTypes } from "@cosmjs/stargate";
import { fromBase64 } from "@cosmjs/encoding";
import { assert, assertDefined } from "@cosmjs/utils";
import {
OfflineAminoSigner,
decodeSignature,
makeSignDoc as makeSignDocAmino,
} from "@cosmjs/amino";
import { decodeAminoPubkey } from "@cosmjs/amino";
import { decodeTxRaw } from "@cosmjs/proto-signing";
import {
defaultRegistryTypes,
createDefaultAminoConverters,
} from "@cosmjs/stargate";
import { connectComet } from "@cosmjs/tendermint-rpc";
import { TxBody } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import {
coins,
GasPrice,
StargateClient,
SigningStargateClient,
StdFee,
SignerData,
SigningStargateClientOptions,
defaultRegistryTypes as defaultStargateTypes,
} from "@cosmjs/stargate";
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import "dotenv/config";
import { CometClient } from "@cosmjs/tendermint-rpc";
import { encodeSecp256k1Pubkey } from "@cosmjs/amino";
import { TxBodyEncodeObject } from "@cosmjs/proto-signing";
import { Int53 } from "@cosmjs/math";
class CustomSigningStargateClient extends SigningStargateClient {
// public override signer:OfflineSigner;
protected readonly not_private_signer: OfflineSigner;
protected readonly not_private_aminoTypes: AminoTypes;
constructor(
cometClient: CometClient | undefined,
signer: OfflineSigner,
options: SigningStargateClientOptions
) {
super(cometClient, signer, options);
this.not_private_signer = signer;
const {
registry = new Registry(defaultRegistryTypes),
aminoTypes = new AminoTypes(createDefaultAminoConverters()),
} = options;
this.not_private_aminoTypes = aminoTypes;
}
// public static async connectWithSigner(
// endpoint: string | HttpEndpoint,
// signer: OfflineSigner,
// options: SigningStargateClientOptions = {},
// ): Promise<SigningStargateClient> {
// const cometClient = await connectComet(endpoint);
// return SigningStargateClient.createWithSigner(cometClient, signer, options);
// }
/**
* Creates an instance from a manually created Comet client.
* Use this to use `Comet38Client` or `Tendermint37Client` instead of `Tendermint34Client`.
*/
public static async alter_createWithSigner(
endpoint: string,
// cometClient: CometClient,
signer: OfflineSigner,
options: SigningStargateClientOptions = {}
): Promise<CustomSigningStargateClient> {
const cometClient = await connectComet(endpoint);
return new CustomSigningStargateClient(cometClient, signer, options);
}
public async signAndBroadcastSyncWithSequence(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
sequence: number,
memo: string,
timeoutHeight?: bigint | undefined
): Promise<string> {
let usedFee: StdFee;
// if (fee == "auto" || typeof fee === "number") {
// assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used.");
// const gasEstimation = await this.simulate(signerAddress, messages, memo);
// const multiplier = typeof fee === "number" ? fee : 1.3;
// usedFee = calculateFee(Math.round(gasEstimation * multiplier), this.gasPrice);
// } else {
usedFee = fee;
// }
const txRaw = await this.signWithSequenceAppointed(
signerAddress,
messages,
usedFee,
memo,
sequence,
undefined,
timeoutHeight
);
const txBytes = TxRaw.encode(txRaw).finish();
return this.broadcastTxSync(txBytes);
}
public async signWithSequenceAppointed(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
memo: string,
sequence_appointed: number,
explicitSignerData?: SignerData,
timeoutHeight?: bigint
): Promise<TxRaw> {
let signerData: SignerData;
if (explicitSignerData) {
signerData = explicitSignerData;
} else {
let { accountNumber, sequence } = await this.getSequence(signerAddress);
sequence = sequence_appointed;
const chainId = await this.getChainId();
signerData = {
accountNumber: accountNumber,
sequence: sequence,
chainId: chainId,
};
}
//只有DirectSigner?
return isOfflineDirectSigner(this.not_private_signer)
? this.alter_signDirect(
signerAddress,
messages,
fee,
memo,
signerData,
timeoutHeight
)
: this.alter_signAmino(
signerAddress,
messages,
fee,
memo,
signerData,
timeoutHeight
);
}
public async alter_signDirect(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
memo: string,
{ accountNumber, sequence, chainId }: SignerData,
timeoutHeight?: bigint
): Promise<TxRaw> {
assert(isOfflineDirectSigner(this.not_private_signer));
const accountFromSigner = (
await this.not_private_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,
timeoutHeight: timeoutHeight,
},
};
const txBodyBytes = this.registry.encode(txBodyEncodeObject);
const gasLimit = Int53.fromString(fee.gas).toNumber();
const authInfoBytes = makeAuthInfoBytes(
[{ pubkey, sequence }],
fee.amount,
gasLimit,
fee.granter,
fee.payer
);
const signDoc = makeSignDoc(
txBodyBytes,
authInfoBytes,
chainId,
accountNumber
);
const { signature, signed } = await this.not_private_signer.signDirect(
signerAddress,
signDoc
);
return TxRaw.fromPartial({
bodyBytes: signed.bodyBytes,
authInfoBytes: signed.authInfoBytes,
signatures: [fromBase64(signature.signature)],
});
}
public async alter_signAmino(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee,
memo: string,
{ accountNumber, sequence, chainId }: SignerData,
timeoutHeight?: bigint
): Promise<TxRaw> {
assert(!isOfflineDirectSigner(this.not_private_signer));
const accountFromSigner = (
await this.not_private_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.not_private_aminoTypes.toAmino(msg)
);
const signDoc = makeSignDocAmino(
msgs,
fee,
chainId,
memo,
accountNumber,
sequence,
timeoutHeight
);
const { signature, signed } = await this.not_private_signer.signAmino(
signerAddress,
signDoc
);
const signedTxBody = {
messages: signed.msgs.map((msg) =>
this.not_private_aminoTypes.fromAmino(msg)
),
memo: signed.memo,
timeoutHeight: timeoutHeight,
};
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, sequence: signedSequence }],
signed.fee.amount,
signedGasLimit,
signed.fee.granter,
signed.fee.payer,
signMode
);
return TxRaw.fromPartial({
bodyBytes: signedTxBodyBytes,
authInfoBytes: signedAuthInfoBytes,
signatures: [fromBase64(signature.signature)],
});
}
}
export { CustomSigningStargateClient };
// const memo = "";