风险提示:理性看待区块链,提高风险意识!
NEO钱包账号如何创建?
首页 > 币界资讯 > 区块链知识 2019-06-18 13:55:51
币界网报道:

钱包账号

neo钱包主要包括address,privateKey,publicKey,scriptHash,WIF这几个部分;它们之间存在一定的转化关系:

NEP<=>WIF <=> Private => Public => ScriptHash <=> Address

不开玩笑,除了 address 可以告诉别人以外,其它的对谁都不要说,address类似一个银行卡号,是别人想给你转账的时候使用的

创建钱包账号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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对创建钱包的方法进行单元测试

1
2
3
4
5
6
test(‘createWalletAccount ‘, () => {
 const passphase = ‘123456’;
 const account = createWalletAccount(passphase, passphase);
 console.log(account);
 expect(account.address !== null).toBe(true);
});

运行结果如下所示,就是当前生成的账号信息,可以使用neo-wallet登录一下试试。

1
2
3
4
5
6
7
8
9
{
 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

单元测试代码如下:

1
2
3
4
5
6
import {wallet} from ‘@cityofzion/neon-js’;

test(‘private:10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba get PUb_Key’,()=>{
 const pubKey = wallet.getPublicKeyFromPrivateKey(’10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba’);
 expect(pubKey).toBe(’03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77′);
});

使用命令运行单元测试:

1
npm run test __test__/neo/Core.spec.js

运行结果:

所有转化

完整的转化关系如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {wallet} from ‘@cityofzion/neon-js’;

const walletAccount = {
 address: ‘AUMgtJsw3kBrcA7poBYNaonZTGMNoqZNLy’,
 privateKey: ’10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba’,
 publicKey: ’03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77′,
 scriptHash: ‘a8f677c132f2c82d73ff138e817e784c25ab068a’,
 WIF: ‘KwnETPM2m8wTAY3qySvDVLf3Vpfg77nvJhxR2Qyi8uMWKoqP1Q3f’,
 encryptedWIF: ‘6PYVHykkA1TfyQ2344wftv5e7vRoecV4iVwEVZ62aqCAx3dh3LDrRV19AS’,
 passphase: ‘123456’
};

// WIF <=> privateKey
test(‘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 => publicKey
test(`privateKey => publicKey:${walletAccount.privateKey}`,()=>{
 const pubKey = wallet.getPublicKeyFromPrivateKey(walletAccount.privateKey);
 expect(pubKey).toBe(walletAccount.publicKey);
});

// publicKey => ScriptHash
test(`publicKey => ScriptHash:${walletAccount.publicKey}`,()=>{
 const scriptHash = wallet.getScriptHashFromPublicKey(walletAccount.publicKey);
 expect(scriptHash).toBe(walletAccount.scriptHash);
});

// scriptHash <=> address
test(‘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,这是cozgithub社区开源的neo钱包api,它包含了钱包开发的所有的api,我认为它是学习钱包的很好的材料,当然neo-guineo-cli本身也是开源的,完全可以使用它们的源码来学习,只是相较于C#,对于开发跨平台应用,我更喜欢基于electron的方案。

为了学习开发neo钱包,我搭建了一个electronboilerplate,作为快速启动的基础,这样其他同样想用此方案的朋友就不用再一次去搭建环境了。此boilerplate具体集成了以下技术方案:electronReactdvaflowantd;后面会抽时间把文中提到的jest也集成进去以便可以开箱即用的写单元测试。

boilerplate传送门

文中代码传送门

上一篇: NEO-dBFT共识机制算法的分析与完善
下一篇: NEO智能合约的部署与调用教程
推荐专栏
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涨跌幅
比特币
61034.17 USDT
¥443,407.14
-1.54%
Solana
137.36 USDT
¥997.91
-0.91%
Filecoin
4.3477 USDT
¥31.59
-2.19%
Curve
0.296 USDT
¥2.15
-6.71%
柚子
0.5733 USDT
¥4.16
-0.28%
Terra Classic
8.173E-5 USDT
¥0.00
-1.28%
Shiba Inu
1.72E-5 USDT
¥0.00
-3.75%
Conflux
0.1558 USDT
¥1.13
-1.64%
狗狗币
0.1223 USDT
¥0.89
-3.78%
dYdX
1.3774 USDT
¥10.01
-1.18%
Arweave
25.8768 USDT
¥187.99
-10.03%
Uniswap
9.2944 USDT
¥67.52
-2.17%
最新快讯
更多
新SwellrswETH池已上线Penpie
2024-06-27 11:34:46
Custodia要求上诉法院指示地区法院撤销对Custodia主账户的拒绝
2024-06-27 11:26:52
Custodia寻求法官撤回授权美联储拒绝其主账户申请的决定
2024-06-27 11:26:52
XT即将上线新一期鲨鱼鳍理财
2024-06-27 11:26:33
WEEX唯客将于7月1日开启第二期Launchpool,投入WXT瓜分37,000ZK
2024-06-27 11:25:57
币界网实时价格午报:PEPE站上0.000012722美元/枚,涨幅达3.03%
2024-06-27 11:23:47
数字资产银行Custodia寻求法官撤回授权美联储拒绝其主账户申请的决定
2024-06-27 11:22:40
下载币界网APP