neo 区块链钱包账号
钱包账号
neo钱包主要包括address,privateKey,publicKey,scriptHash,WIF这几个部分;它们之间存在一定的转化关系:NEP<=>WIF <=> Private => Public => ScriptHash <=> Address不开玩笑,除了 address 可以告诉别人以外,其它的对谁都不要说,address类似一个银行卡号,是别人想给你转账的时候使用的
创建钱包账号
import { wallet } from '@cityofzion/neon-js';/** * 根据passpase生产一个新的neo账号 * @param {*} passphase * @param {*} confirmPassphase * @param {*} wif */export function createWalletAccount(passphase: string, confirmPassphase: string, wif?: string): ?WalletAccount { if (passphase !== confirmPassphase) return null; var account = new wallet.Account(wif || wallet.generatePrivateKey()); const encryptedWIF = wallet.encrypt(account.WIF, passphase); const result: WalletAccount = { address: account.address, privateKey: account.privateKey, publicKey: account.publicKey, scriptHash: account.scriptHash, WIF: account.WIF, encryptedWIF, passphase, } return result;}
创建钱包账号单元测试
通过jest对创建钱包的方法进行单元测试test('createWalletAccount ', () => { const passphase = '123456'; const account = createWalletAccount(passphase, passphase); console.log(account); expect(account.address !== null).toBe(true);});运行结果如下所示,就是当前生成的账号信息,可以使用neo-wallet登录一下试试。{ address: 'AUMgtJsw3kBrcA7poBYNaonZTGMNoqZNLy', privateKey: '10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba', publicKey: '03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77', scriptHash: 'a8f677c132f2c82d73ff138e817e784c25ab068a', WIF: 'KwnETPM2m8wTAY3qySvDVLf3Vpfg77nvJhxR2Qyi8uMWKoqP1Q3f', encryptedWIF: '6PYVHykkA1TfyQ2344wftv5e7vRoecV4iVwEVZ62aqCAx3dh3LDrRV19AS', passphase: '123456' }
转化
正如前面所述的,钱包里的各个数据存在一定的转化关系,至于详细的相互之间转化的原理,先不至讨论,我们可以使用neon-js的相关API来查看是否正确转化。NEP<=>WIF <=> Private => Public => ScriptHash <=> Address下面先新建一个jtest的单元测试文件Core.spec.js
私钥到公钥的转化
这里使用上面生成的测试账号进行测试,注意因为是测试账号以后我不会再使用,所以暴露出来,如果是你的个人账号,请一定不要暴露WIF以及privateKey。单元测试代码如下:import {wallet} from '@cityofzion/neon-js';test('private:10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba get PUb_Key',()=>{ const pubKey = wallet.getPublicKeyFromPrivateKey('10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba'); expect(pubKey).toBe('03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77');});使用命令运行单元测试:npm run test __test__/neo/Core.spec.js运行结果:
所有转化
完整的转化关系如下:import {wallet} from '@cityofzion/neon-js';const walletAccount = { address: 'AUMgtJsw3kBrcA7poBYNaonZTGMNoqZNLy', privateKey: '10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba', publicKey: '03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77', scriptHash: 'a8f677c132f2c82d73ff138e817e784c25ab068a', WIF: 'KwnETPM2m8wTAY3qySvDVLf3Vpfg77nvJhxR2Qyi8uMWKoqP1Q3f', encryptedWIF: '6PYVHykkA1TfyQ2344wftv5e7vRoecV4iVwEVZ62aqCAx3dh3LDrRV19AS', passphase: '123456' };// WIF <=> privateKeytest('WIF <=> privateKey',()=>{ const wif = wallet.getWIFFromPrivateKey(walletAccount.privateKey); expect(wif).toBe(walletAccount.WIF); const privateKey = wallet.getPrivateKeyFromWIF(walletAccount.WIF); expect(privateKey).toBe(walletAccount.privateKey);});// privateKey => publicKeytest(`privateKey => publicKey:${walletAccount.privateKey}`,()=>{ const pubKey = wallet.getPublicKeyFromPrivateKey(walletAccount.privateKey); expect(pubKey).toBe(walletAccount.publicKey);});// publicKey => ScriptHashtest(`publicKey => ScriptHash:${walletAccount.publicKey}`,()=>{ const scriptHash = wallet.getScriptHashFromPublicKey(walletAccount.publicKey); expect(scriptHash).toBe(walletAccount.scriptHash);});// scriptHash <=> addresstest('scriptHash <=> address',()=>{ const address = wallet.getAddressFromScriptHash(walletAccount.scriptHash); expect(address).toBe(walletAccount.address); const scriptHash = wallet.getScriptHashFromAddress(walletAccount.address); expect(scriptHash).toBe(walletAccount.scriptHash);});