Skip to content

Commit 5286187

Browse files
author
slient-coder
committed
fix: fix signArbitrary
1 parent 32e326f commit 5286187

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/background/service/keyring/CosmosKeyring.ts

+33-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Buffer } from 'buffer/';
44
import { WalletController } from '@/background/controller/wallet';
55
import { objToUint8Array } from '@/shared/utils';
66
import { incentivequery } from '@babylonlabs-io/babylon-proto-ts';
7+
import { Secp256k1, sha256 } from '@cosmjs/crypto';
78
import { AccountData, DirectSecp256k1Wallet, DirectSignResponse } from '@cosmjs/proto-signing';
89
import { GasPrice, QueryClient, SigningStargateClient, createProtobufRpcClient } from '@cosmjs/stargate';
910
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
@@ -13,6 +14,33 @@ const REWARD_GAUGE_KEY_BTC_DELEGATION = 'btc_delegation';
1314
export const DEFAULT_BBN_GAS_PRICE = '0.007';
1415
export const DEFAULT_BBN_GAS_LIMIT = '300000';
1516

17+
export function sortObjectByKey(obj: Record<string, any>): any {
18+
if (typeof obj !== 'object' || obj === null) {
19+
return obj;
20+
}
21+
if (Array.isArray(obj)) {
22+
return obj.map(sortObjectByKey);
23+
}
24+
const sortedKeys = Object.keys(obj).sort();
25+
const result: Record<string, any> = {};
26+
sortedKeys.forEach((key) => {
27+
result[key] = sortObjectByKey(obj[key]);
28+
});
29+
return result;
30+
}
31+
32+
export function sortedJsonByKeyStringify(obj: Record<string, any>): string {
33+
return JSON.stringify(sortObjectByKey(obj));
34+
}
35+
36+
export function escapeHTML(str: string): string {
37+
return str.replace(/</g, '\\u003c').replace(/>/g, '\\u003e').replace(/&/g, '\\u0026');
38+
}
39+
40+
export function serializeSignDoc(signDoc: any): Uint8Array {
41+
return Buffer.from(escapeHTML(sortedJsonByKeyStringify(signDoc)));
42+
}
43+
1644
export function makeADR36AminoSignDoc(signer: string, data: string | Uint8Array) {
1745
if (typeof data === 'string') {
1846
data = Buffer.from(data).toString('base64');
@@ -216,13 +244,12 @@ export class CosmosKeyring {
216244
}
217245

218246
const signDoc = makeADR36AminoSignDoc(signerAddress, data);
247+
const toSignData = serializeSignDoc(signDoc);
219248

220-
const _sig = await this.signer.signDirect(signerAddress, signDoc as any);
221-
const signature = Buffer.from(_sig.signature.signature, 'base64') as any;
222-
return {
223-
signed: _sig.signed,
224-
signature: encodeSecp256k1Signature(key.pubKey, signature)
225-
} as any;
249+
const messageHash = sha256(toSignData);
250+
const _sig = await Secp256k1.createSignature(messageHash, (this.signer as any).privkey);
251+
const signature = new Uint8Array([..._sig.r(32), ..._sig.s(32)]);
252+
return encodeSecp256k1Signature(key.pubKey, signature);
226253
}
227254

228255
async sendTokens(tokenBalance: { denom: string; amount: string }, recipient: string, memo: string) {

0 commit comments

Comments
 (0)