import chainmakerWalletJSSDK from 'chainmaker-wallet-jssdk'; import { getShuffleArray } from './getShuffleArray'; // 不满10补0 export function zero(number: number) { return number < 10 ? `0${number}` : number.toString(); } const { HdWallet, tools } = chainmakerWalletJSSDK const curveName = 'secp256r1'; export function getChainmakerSDk() { const hdWallet = new HdWallet(curveName); const pubKey2Address = tools.handler.pubKey2Address; const checkPriKeyValidity = tools.handler.checkPriKeyValidity; const sign = tools.handler.sign; const pubHex2Pem = hdWallet.pubHex2Pem; const priHex2Pem = hdWallet.priHex2Pem; const priHex2PubHex = hdWallet.priHex2PubHex; return { pubKey2Address, checkPriKeyValidity, pubHex2Pem, priHex2Pem, priHex2PubHex, sign } } export function priPem2PubPem(priPem: string) { const hdWallet = new HdWallet(curveName); return hdWallet.priPem2PubPem(priPem); } export function priKey2Address(priPem: string) { return tools.handler.priKey2Address(priPem) } export function pubKey2Address(pubPem: string) { return tools.handler.pubKey2Address(pubPem) } export function deriveKeyPairByMnemonic(mnemonic: string, index: number = 0) { const hdWallet = new HdWallet(curveName); const ketPair = hdWallet.deriveKeyPairByIndex(mnemonic, index); return ketPair; } export function getMnemonicObject(_mnemonic?: string) { try { const hdWallet = new HdWallet(curveName); const mnemonic = _mnemonic || hdWallet.createMnemonics(); const tempArray = mnemonic.split(' '); const mainnemonicArr = tempArray.map((word, index) => ({ word, index })); const mnemonicShuffleArr = getShuffleArray(tempArray).map((word, index) => ({ word, index })); const ketPair = deriveKeyPairByMnemonic(mnemonic, 0); //等价于 hdWallet.deriveKeyPairByFullPath(words,[66, 0, 0, 0 ,0]); const publicPem = ketPair.pub; const privatePem = ketPair.pri; const address = pubKey2Address(publicPem); return { mnemonic, mainnemonicArr, mnemonicShuffleArr, publicPem, privatePem, address }; } catch (error) { console.debug(error); } return undefined }