Skip to content

Commit 6ccc0f0

Browse files
committed
feat: test
1 parent f7e99b8 commit 6ccc0f0

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

packages/neuron-wallet/tests/services/address.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SystemScriptInfo from '../../src/models/system-script-info'
22
import { OutputStatus } from '../../src/models/chain/output'
33
import OutputEntity from '../../src/database/chain/entities/output'
4+
import { bytes } from '@ckb-lumos/lumos/codec'
45
import { hd } from '@ckb-lumos/lumos'
56
import { Address } from '../../src/models/address'
67
import Transaction from '../../src/database/chain/entities/transaction'
@@ -9,6 +10,8 @@ import { when } from 'jest-when'
910
import HdPublicKeyInfo from '../../src/database/chain/entities/hd-public-key-info'
1011
import { closeConnection, getConnection, initConnection } from '../setupAndTeardown'
1112
import { NetworkType } from '../../src/models/network'
13+
import SignMessage from '../../src/services/sign-message'
14+
import WalletService from '../../src/services/wallets'
1215

1316
const { 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

Comments
 (0)