风险提示:理性看待区块链,提高风险意识!
小蚁NEO钱包账号创建原理解析
首页 > 币界资讯 > 区块链知识 2019-03-06 18:03:22
币界网报道:

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);});

总结

如上,主要说明了neo账号的创建,以及讲述账号各个字段之间的转化关系,本篇使用的api为neon-js,这是coz在github社区开源的neo钱包api,它包含了钱包开发的所有的api,我认为它是学习钱包的很好的材料,当然neo-gui,neo-cli本身也是开源的,完全可以使用它们的源码来学习,只是相较于C#,对于开发跨平台应用,我更喜欢基于electron的方案。为了学习开发neo钱包,我搭建了一个electron的boilerplate,作为快速启动的基础,这样其他同样想用此方案的朋友就不用再一次去搭建环境了。此boilerplate具体集成了以下技术方案:electron,React,dva,flow,antd;后面会抽时间把文中提到的jest也集成进去以便可以开箱即用的写单元测试。

上一篇: IOTA的通证价值基础与激励价值的关系
下一篇: 解密区块链中五个神秘的术语
推荐专栏
Boss Wallet Web3 Econom Pass
专注币圈最新资讯
通俗浅显地聊透Web3大事小情
读懂区块链生态与未来,尽在币界网!
热门币种
更多
币种
美元价格
24H涨跌幅
BTC比特币
60,963.61 USDT
¥435,103.38
-2.72%
ETH以太坊
3,368.69 USDT
¥24,042.67
-0.3%
BNB币安币
570.68 USDT
¥4,073.00
-0.28%
USDT泰达币
1.02 USDT
¥7.25
-0.19%
SOL
135.96 USDT
¥970.36
+7.66%
USDC
1.00 USDT
¥7.15
-0.01%
TON
7.59 USDT
¥54.14
+4.55%
XRP瑞波币
0.47720 USDT
¥3.41
+0.48%
DOGE狗狗币
0.12210 USDT
¥0.87140
+2.43%
ADA艾达币
0.39050 USDT
¥2.79
+3.88%
热搜币种
更多
币种
美元价格
24H涨跌幅
比特币
60962.19 USDT
¥442,884.21
-1.67%
Solana
137 USDT
¥995.29
-1.28%
Filecoin
4.3427 USDT
¥31.55
-2.63%
柚子
0.5716 USDT
¥4.15
-0.8%
Curve
0.2963 USDT
¥2.15
-6.88%
Terra Classic
8.216E-5 USDT
¥0.00
-0.98%
Shiba Inu
1.72E-5 USDT
¥0.00
-3.91%
Conflux
0.1563 USDT
¥1.14
-1.76%
狗狗币
0.1221 USDT
¥0.89
-4.16%
dYdX
1.3771 USDT
¥10.00
-1.76%
Arweave
25.8763 USDT
¥187.99
-10.41%
Uniswap
9.3568 USDT
¥67.98
-1.93%
最新快讯
更多
Symbiotic:sUSDe已达质押上限
2024-06-27 11:05:48
全网BTC期权未平仓头寸为209.2亿美元,ETH期权未平仓头寸为92.4亿美元
2024-06-27 11:04:11
全网ETH合约未平仓头寸超150亿美元
2024-06-27 11:02:28
币安完成MetalDAO(MTL)主网置换,并开放充提业务
2024-06-27 11:01:27
DeFianceCapital联创称BLASTFDV远不及其至少50亿美元的预期
2024-06-27 11:00:51
Gate.io首席安全官提出5项用户必知安全措施,共筑安全防线
2024-06-27 10:57:30
澳大利亚广播公司的YouTube账号遭劫持用来推销加密骗局
2024-06-27 10:52:09
下载币界网APP