@@ -4,6 +4,7 @@ import { Buffer } from 'buffer/';
4
4
import { WalletController } from '@/background/controller/wallet' ;
5
5
import { objToUint8Array } from '@/shared/utils' ;
6
6
import { incentivequery } from '@babylonlabs-io/babylon-proto-ts' ;
7
+ import { Secp256k1 , sha256 } from '@cosmjs/crypto' ;
7
8
import { AccountData , DirectSecp256k1Wallet , DirectSignResponse } from '@cosmjs/proto-signing' ;
8
9
import { GasPrice , QueryClient , SigningStargateClient , createProtobufRpcClient } from '@cosmjs/stargate' ;
9
10
import { Tendermint34Client } from '@cosmjs/tendermint-rpc' ;
@@ -13,6 +14,33 @@ const REWARD_GAUGE_KEY_BTC_DELEGATION = 'btc_delegation';
13
14
export const DEFAULT_BBN_GAS_PRICE = '0.007' ;
14
15
export const DEFAULT_BBN_GAS_LIMIT = '300000' ;
15
16
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
+
16
44
export function makeADR36AminoSignDoc ( signer : string , data : string | Uint8Array ) {
17
45
if ( typeof data === 'string' ) {
18
46
data = Buffer . from ( data ) . toString ( 'base64' ) ;
@@ -216,13 +244,12 @@ export class CosmosKeyring {
216
244
}
217
245
218
246
const signDoc = makeADR36AminoSignDoc ( signerAddress , data ) ;
247
+ const toSignData = serializeSignDoc ( signDoc ) ;
219
248
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 ) ;
226
253
}
227
254
228
255
async sendTokens ( tokenBalance : { denom : string ; amount : string } , recipient : string , memo : string ) {
0 commit comments