11import SystemScriptInfo from '../../src/models/system-script-info'
22import { OutputStatus } from '../../src/models/chain/output'
33import OutputEntity from '../../src/database/chain/entities/output'
4+ import { bytes } from '@ckb-lumos/lumos/codec'
45import { hd } from '@ckb-lumos/lumos'
56import { Address } from '../../src/models/address'
67import Transaction from '../../src/database/chain/entities/transaction'
@@ -9,6 +10,8 @@ import { when } from 'jest-when'
910import HdPublicKeyInfo from '../../src/database/chain/entities/hd-public-key-info'
1011import { closeConnection , getConnection , initConnection } from '../setupAndTeardown'
1112import { NetworkType } from '../../src/models/network'
13+ import SignMessage from '../../src/services/sign-message'
14+ import WalletService from '../../src/services/wallets'
1215
1316const { AddressType, AccountExtendedPublicKey } = hd
1417
@@ -707,5 +710,65 @@ describe('integration tests for AddressService', () => {
707710 expect ( stubbedAddressDbChangedSubjectNext ) . toHaveBeenCalledTimes ( 1 )
708711 } )
709712 } )
713+
714+ describe ( 'getPrivateKeyByAddress' , ( ) => {
715+ const walletService = WalletService . getInstance ( )
716+ const mnemonic = 'tank planet champion pottery together intact quick police asset flower sudden question'
717+ const password = '1234abc~'
718+ const message = 'Hello World'
719+ let address = 'ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwe5xyvcxv95l22x8c5ra8tkc0jgxhvrqsda3p3k'
720+
721+ beforeEach ( ( ) => {
722+ walletService . clearAll ( )
723+ } )
724+
725+ it ( 'return private key' , async ( ) => {
726+ const seed = hd . mnemonic . mnemonicToSeedSync ( mnemonic )
727+ const masterKeychain = hd . Keychain . fromSeed ( seed )
728+ const extendedKey = new hd . ExtendedPrivateKey (
729+ bytes . hexify ( masterKeychain . privateKey ) ,
730+ bytes . hexify ( masterKeychain . chainCode )
731+ )
732+
733+ const keystore = hd . Keystore . create ( extendedKey , password )
734+
735+ const accountKeychain = masterKeychain . derivePath ( hd . AccountExtendedPublicKey . ckbAccountPath )
736+ const accountExtendedPublicKey = new hd . AccountExtendedPublicKey (
737+ bytes . hexify ( accountKeychain . publicKey ) ,
738+ bytes . hexify ( accountKeychain . chainCode )
739+ )
740+
741+ const wallet = walletService . create ( {
742+ id : '' ,
743+ name : 'Test Wallet' ,
744+ extendedKey : accountExtendedPublicKey . serialize ( ) ,
745+ keystore,
746+ } )
747+
748+ const addresses = await wallet . checkAndGenerateAddresses ( false , 5 , 5 )
749+
750+ if ( addresses ) {
751+ const obj = addresses [ Math . floor ( Math . random ( ) * addresses . length ) ]
752+ address = obj . address
753+ }
754+
755+ const privateKey = await AddressService . getPrivateKeyByAddress ( {
756+ walletID : wallet . id ,
757+ password : password ,
758+ address,
759+ } )
760+
761+ // @ts -ignore: Private method
762+ const sig = SignMessage . signByPrivateKey ( privateKey , message )
763+ const signature = await SignMessage . sign ( {
764+ walletID : wallet . id ,
765+ password : password ,
766+ message : message ,
767+ address,
768+ } )
769+
770+ expect ( sig ) . toEqual ( signature )
771+ } )
772+ } )
710773 } )
711774} )
0 commit comments