NEO 区块链钱包账号
博客园
发表于 2022-11-30 20:08:36
125
0
0
neo钱包主要包括address,privateKey,publicKey,scriptHash,WIF这几个部分;它们之间存在一定的转化关系: O9 @) ^! e* X5 d' m! T* B
NEPWIF Private => Public => ScriptHash Address
不开玩笑,除了 address 可以告诉别人以外,其它的对谁都不要说,address类似一个银行卡号,是别人想给你转账的时候使用的* z6 o5 e0 |" y3 e6 H1 b
创建钱包账号1 ~1 @7 a$ X7 p4 E/ Z! b& c
import { wallet } from ‘@cityofzion/neon-js’;
/**
* 根据passpase生产一个新的neo账号* p% }6 v* y7 S) m- L5 [# V9 k# i
* @param {*} passphase
* @param {*} confirmPassphase
* @param {*} wif ; |2 P* E# E5 n# q7 F Q3 l
*/
export function createWalletAccount(passphase: string, confirmPassphase: string, wif?: string): ?WalletAccount {
if (passphase !== confirmPassphase) return null;
var account = new wallet.Account(wif || wallet.generatePrivateKey());$ P, c1 o! t- A/ B7 f
const encryptedWIF = wallet.encrypt(account.WIF, passphase);
const result: WalletAccount = { U6 ?8 {* E) H1 U- B3 n+ v
address: account.address,
privateKey: account.privateKey,7 Z$ c5 s# J( S9 `3 @9 s
publicKey: account.publicKey,
scriptHash: account.scriptHash,1 n6 |. o4 N% L% K) U
WIF: account.WIF,
encryptedWIF,- ~% q# J8 Y ?0 @
passphase,/ y% i1 [% _5 D4 ^' z/ j! Y% F# D% B4 S3 l
}
return result;
}& ~8 X; u( O' o: v8 g$ f
创建钱包账号单元测试
通过jest对创建钱包的方法进行单元测试
test(‘createWalletAccount ‘, () => {4 C) E! ^* ]: {6 e
const passphase = ‘123456’;
const account = createWalletAccount(passphase, passphase);6 r# J) e. q1 E/ ^' v$ G* p, V3 K* r4 B
console.log(account);) n) |4 ?! ]9 G3 F* C6 g0 g
expect(account.address !== null).toBe(true);. T& q& G5 U$ p0 E8 E. _. J; M
});3 d/ E* a O$ ?
运行结果如下所示,就是当前生成的账号信息,可以使用neo-wallet登录一下试试。6 J; _1 \# W' c/ C9 M/ y
{ 5 W! B% M6 ]& s* u
address: ‘AUMgtJsw3kBrcA7poBYNaonZTGMNoqZNLy’,( ^5 j) Q! ]* H: Q! @$ u
privateKey: ’10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba’,
publicKey: ’03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77′,
scriptHash: ‘a8f677c132f2c82d73ff138e817e784c25ab068a’,
WIF: ‘KwnETPM2m8wTAY3qySvDVLf3Vpfg77nvJhxR2Qyi8uMWKoqP1Q3f’,
encryptedWIF: ‘6PYVHykkA1TfyQ2344wftv5e7vRoecV4iVwEVZ62aqCAx3dh3LDrRV19AS’,
passphase: ‘123456’
}
转化
正如前面所述的,钱包里的各个数据存在一定的转化关系,至于详细的相互之间转化的原理,先不至讨论,我们可以使用neon-js的相关API来查看是否正确转化。+ W) j: c- V9 K, |( a( r. w* y9 X
NEPWIF Private => Public => ScriptHash Address
下面先新建一个jtest的单元测试文件Core.spec.js& N% {- h0 ?$ F/ p: O a
私钥到公钥的转化
这里使用上面生成的测试账号进行测试,注意因为是测试账号以后我不会再使用,所以暴露出来,如果是你的个人账号,请一定不要暴露WIF以及privateKey。
单元测试代码如下:
import {wallet} from ‘@cityofzion/neon-js’;) Y8 Y& C: d2 k, N
test(‘private:10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba get PUb_Key’,()=>{$ q/ `: _; p. e( y
const pubKey = wallet.getPublicKeyFromPrivateKey(’10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba’);
expect(pubKey).toBe(’03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77′);
});
使用命令运行单元测试:- H% F7 ], C5 {
npm run test __test__/neo/Core.spec.js Q, |. L: ]; Z- I$ m
运行结果:. h5 F3 G# O) n
所有转化
完整的转化关系如下:. V0 ~0 V; b0 m8 k4 |5 f# P
import {wallet} from ‘@cityofzion/neon-js’;! [) R4 W5 j8 u9 M: J" P0 G. W, u
const walletAccount = {
address: ‘AUMgtJsw3kBrcA7poBYNaonZTGMNoqZNLy’,: N$ V8 L9 F0 n. @$ `
privateKey: ’10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba’,! z8 Q( P: v7 X
publicKey: ’03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77′,
scriptHash: ‘a8f677c132f2c82d73ff138e817e784c25ab068a’,
WIF: ‘KwnETPM2m8wTAY3qySvDVLf3Vpfg77nvJhxR2Qyi8uMWKoqP1Q3f’,3 Q$ k: L/ M* P- S
encryptedWIF: ‘6PYVHykkA1TfyQ2344wftv5e7vRoecV4iVwEVZ62aqCAx3dh3LDrRV19AS’,1 b. h9 U ^# K# w* ?8 a, i& ]
passphase: ‘123456’ * L! |9 F0 F2 G" f1 K0 |
};
// WIF privateKey2 b% |5 ]2 }6 j8 G
test(‘WIF privateKey’,()=>{6 B4 S/ c3 ^) Y6 H A' F) m4 b
const wif = wallet.getWIFFromPrivateKey(walletAccount.privateKey);
expect(wif).toBe(walletAccount.WIF);
const privateKey = wallet.getPrivateKeyFromWIF(walletAccount.WIF);( t7 V% Y% U5 ^# d3 H
expect(privateKey).toBe(walletAccount.privateKey);
});9 A9 F# y1 |0 q" S: Y2 j6 C
// privateKey => publicKey' |$ p# G3 l1 F6 v/ j& D u8 ?0 @7 p
test(`privateKey => publicKey:${walletAccount.privateKey}`,()=>{% w/ z, L% f) S2 V' f/ B7 a
const pubKey = wallet.getPublicKeyFromPrivateKey(walletAccount.privateKey);" L. B2 m& D3 \) f: I1 k/ y9 L, Q# `
expect(pubKey).toBe(walletAccount.publicKey);* s! [7 Z4 b+ L4 U0 |
});4 Y/ V4 }& e1 e3 S& j, c
// publicKey => ScriptHash
test(`publicKey => ScriptHash:${walletAccount.publicKey}`,()=>{
const scriptHash = wallet.getScriptHashFromPublicKey(walletAccount.publicKey);! x, u# U' f, n+ [1 k- Q
expect(scriptHash).toBe(walletAccount.scriptHash);
});
// scriptHash address6 Q; e8 b9 ?6 {* q% E
test(‘scriptHash address’,()=>{
const address = wallet.getAddressFromScriptHash(walletAccount.scriptHash);2 p {3 J1 e% c4 n" Y, d
expect(address).toBe(walletAccount.address);
const scriptHash = wallet.getScriptHashFromAddress(walletAccount.address);, t3 d! a' w5 i; M9 H) ~ s
expect(scriptHash).toBe(walletAccount.scriptHash);
});" X7 j/ G" l/ s- V
总结
如上,主要说明了neo账号的创建,以及讲述账号各个字段之间的转化关系,本篇使用的api为neon-js,这是coz在github社区开源的neo钱包api,它包含了钱包开发的所有的api,我认为它是学习钱包的很好的材料,当然neo-gui,neo-cli本身也是开源的,完全可以使用它们的源码来学习,只是相较于C#,对于开发跨平台应用,我更喜欢基于electron的方案。+ m) B1 K' o+ @
为了学习开发neo钱包,我搭建了一个electron的boilerplate,作为快速启动的基础,这样其他同样想用此方案的朋友就不用再一次去搭建环境了。此boilerplate具体集成了以下技术方案:electron,React,dva,flow,antd;后面会抽时间把文中提到的jest也集成进去以便可以开箱即用的写单元测试。
成为第一个吐槽的人