1
-
2
1
import { permissionService , sessionService } from '@/background/service' ;
3
2
import { CHAINS , CHAINS_MAP , NETWORK_TYPES , VERSION } from '@/shared/constant' ;
4
-
5
3
import { NetworkType } from '@/shared/types' ;
6
- import { getChainInfo } from '@/shared/utils' ;
4
+ import { getChainInfo , objToUint8Array } from '@/shared/utils' ;
7
5
import { amountToSatoshis } from '@/ui/utils' ;
6
+ import * as encoding from '@cosmjs/encoding' ;
8
7
import { bitcoin } from '@unisat/wallet-sdk/lib/bitcoin-core' ;
9
8
import { verifyMessageOfBIP322Simple } from '@unisat/wallet-sdk/lib/message' ;
10
9
import { toPsbtNetwork } from '@unisat/wallet-sdk/lib/network' ;
11
10
import { ethErrors } from 'eth-rpc-errors' ;
12
11
import BaseController from '../base' ;
13
12
import wallet from '../wallet' ;
14
13
15
- import {
16
- SignDoc
17
- } from "@keplr-wallet/proto-types/cosmos/tx/v1beta1/tx" ;
14
+ import { encodeSecp256k1Signature , makeADR36AminoSignDoc , serializeSignDoc } from '@/background/service/keyring/CosmosKeyring' ;
15
+ import { makeSignBytes } from '@cosmjs/proto-signing' ;
18
16
19
17
function formatPsbtHex ( psbtHex : string ) {
20
18
let formatData = '' ;
@@ -197,7 +195,7 @@ class ProviderController extends BaseController {
197
195
return await wallet . pushTx ( rawtx )
198
196
}
199
197
200
- @Reflect . metadata ( 'APPROVAL' , [ 'SignText' , ( ) => {
198
+ @Reflect . metadata ( 'APPROVAL' , [ 'SignText' , ( req ) => {
201
199
// todo check text
202
200
} ] )
203
201
signMessage = async ( { data : { params : { text, type } } , approvalRes } ) => {
@@ -303,6 +301,13 @@ class ProviderController extends BaseController {
303
301
return utxos ;
304
302
} ;
305
303
304
+
305
+ private _isKeystoneWallet = async ( ) => {
306
+ const currentKeyring = await wallet . getCurrentKeyring ( ) ;
307
+ return currentKeyring ?. type === 'keystone' ;
308
+ }
309
+
310
+
306
311
@Reflect . metadata ( 'APPROVAL' , [ 'CosmosConnect' , ( req ) => {
307
312
// todo check
308
313
} ] )
@@ -312,6 +317,7 @@ class ProviderController extends BaseController {
312
317
}
313
318
} ;
314
319
320
+
315
321
@Reflect . metadata ( 'SAFE' , true )
316
322
cosmosExperimentalSuggestChain = async ( { data :{ params :{ chainData} } } ) => {
317
323
// const chainInfo:CosmosChainInfo = chainData;
@@ -322,76 +328,71 @@ class ProviderController extends BaseController {
322
328
throw new Error ( 'not implemented' )
323
329
}
324
330
331
+
325
332
@Reflect . metadata ( 'SAFE' , true )
326
333
cosmosGetKey = async ( { data : { params : { chainId} } } ) => {
327
334
const cosmosKeyring = await wallet . getCosmosKeyring ( chainId ) ;
328
- if ( ! cosmosKeyring ) {
329
- return null ;
330
- }
331
-
332
- const key = cosmosKeyring . getKey ( ) ;
333
- const _key = Object . assign ( { } , key , {
334
- address :key . address . toString ( ) ,
335
- pubKey :key . pubKey . toString ( )
336
- } )
337
- return _key
335
+ if ( ! cosmosKeyring ) {
336
+ return null ;
337
+ }
338
+
339
+ const key = cosmosKeyring . getKey ( ) ;
340
+ const _key = Object . assign ( { } , key , {
341
+ address :key . address . toString ( ) ,
342
+ pubKey :key . pubKey . toString ( )
343
+ } ) ;
344
+ return _key ;
338
345
}
339
346
347
+
340
348
@Reflect . metadata ( 'APPROVAL' , [ 'CosmosSign' , ( req ) => {
341
- // todo check
349
+ const signDoc = req . data . params . signDoc ;
350
+ signDoc . bodyBytes = objToUint8Array ( signDoc . bodyBytes ) ;
351
+ signDoc . authInfoBytes = objToUint8Array ( signDoc . authInfoBytes ) ;
352
+ const signBytes = makeSignBytes ( signDoc ) ;
353
+ req . data . params . signBytesHex = encoding . toHex ( signBytes ) ;
354
+
342
355
} ] )
343
- cosmosSignDirect = async ( { data : { params :msg } } ) => {
344
- const signDoc = SignDoc . fromPartial ( {
345
- bodyBytes : msg . signDoc . bodyBytes ,
346
- authInfoBytes : msg . signDoc . authInfoBytes ,
347
- chainId : msg . signDoc . chainId ,
348
- accountNumber : msg . signDoc . accountNumber ,
349
- } ) ;
350
-
351
- const cosmosKeyring = await wallet . getCosmosKeyring ( msg . signDoc . chainId ) ;
352
- if ( ! cosmosKeyring ) {
353
- throw new Error ( 'no keyring' )
356
+ cosmosSignDirect = async ( { data : { params : msg } , approvalRes} ) => {
357
+ if ( ! approvalRes ) {
358
+ throw new Error ( 'approvalRes is required' )
354
359
}
355
-
356
- const response = await cosmosKeyring . signDirect (
357
- msg . signDoc . chainId ,
358
- msg . signerAddress ,
359
- signDoc ,
360
- ) ;
361
-
362
- return response
363
-
364
-
360
+ const { bodyBytes, authInfoBytes, chainId, accountNumber} = msg . signDoc ;
361
+ const signature = encodeSecp256k1Signature ( encoding . fromHex ( approvalRes . publicKey ) , encoding . fromHex ( approvalRes . signature ) ) ;
362
+ const respone = {
363
+ signed : {
364
+ bodyBytes :objToUint8Array ( bodyBytes ) ,
365
+ authInfoBytes :objToUint8Array ( authInfoBytes ) ,
366
+ chainId,
367
+ accountNumber,
368
+ } ,
369
+ signature
370
+ } ;
371
+ return respone ;
372
+
365
373
}
366
374
367
- // signArbitrary
368
375
@Reflect . metadata ( 'APPROVAL' , [ 'CosmosSign' , ( req ) => {
369
- // todo check
376
+ const signerAddress = req . data . params . signerAddress ;
377
+ const data = req . data . params . data ;
378
+ const signDoc = makeADR36AminoSignDoc ( signerAddress , data ) ;
379
+ const signBytes = serializeSignDoc ( signDoc ) ;
380
+ req . data . params . signBytesHex = encoding . toHex ( signBytes ) ;
370
381
} ] )
371
- cosmosSignArbitrary = async ( { data : { params :msg } } ) => {
372
-
373
- const cosmosKeyring = await wallet . getCosmosKeyring ( msg . chainId ) ;
374
- if ( ! cosmosKeyring ) {
375
- throw new Error ( 'no keyring' )
382
+ cosmosSignArbitrary = async ( { data : { params :msg } , approvalRes} ) => {
383
+ if ( ! approvalRes ) {
384
+ throw new Error ( 'approvalRes is required' )
376
385
}
377
386
378
- const response = await cosmosKeyring . signAminoADR36 (
379
- msg . chainId ,
380
- msg . signer ,
381
- typeof msg . data === "string" ? Buffer . from ( msg . data ) : msg . data ,
382
- ) ;
383
-
384
- return response
387
+ const signature = encodeSecp256k1Signature ( encoding . fromHex ( approvalRes . publicKey ) , encoding . fromHex ( approvalRes . signature ) ) ;
388
+ const respone = signature
389
+ return respone ;
390
+
391
+ }
385
392
386
393
387
- }
388
394
389
395
390
- @Reflect . metadata ( 'SAFE' , true )
391
- cosmosSignAmino = async ( { data : { params : { signerAddress, signDoc } } } ) => {
392
- throw new Error ( 'not implemented' )
393
- // return signerAddress
394
- }
395
396
}
396
397
397
398
export default new ProviderController ( ) ;
0 commit comments