當前位置:
首頁 > 最新 > 小蟻NEO錢包賬號創建原理解析

小蟻NEO錢包賬號創建原理解析

neo 區塊鏈錢包賬號

錢包賬號

neo錢包主要包括address,privateKey,publicKey,scriptHash,WIF這幾個部分;它們之間存在一定的轉化關係:

NEPWIFPrivate => Public => ScriptHashAddress

不開玩笑,除了 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來查看是否正確轉化。

NEPWIFPrivate => Public => ScriptHashAddress

私鑰到公鑰的轉化

這裡使用上面生成的測試賬號進行測試,注意因為是測試賬號以後我不會再使用,所以暴露出來,如果是你的個人賬號,請一定不要暴露WIF以及privateKey。

單元測試代碼如下:

import 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 from "@cityofzion/neon-js";

const walletAccount = {

address: "AUMgtJsw3kBrcA7poBYNaonZTGMNoqZNLy",

privateKey: "10bb026b4015c481f0b5142aa5de36de40a99fc8c26e956011a3f820f7708fba",

publicKey: "03af2ad8725c4926632d6816f5502d8f749dec369afadfe0bb5ac697fe22a0ef77",

scriptHash: "a8f677c132f2c82d73ff138e817e784c25ab068a",

WIF: "KwnETPM2m8wTAY3qySvDVLf3Vpfg77nvJhxR2Qyi8uMWKoqP1Q3f",

encryptedWIF: "6PYVHykkA1TfyQ2344wftv5e7vRoecV4iVwEVZ62aqCAx3dh3LDrRV19AS",

passphase: "123456"

};

// WIFprivateKey

test("WIFprivateKey",()=>{

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:$`,()=>{

const pubKey = wallet.getPublicKeyFromPrivateKey(walletAccount.privateKey);

expect(pubKey).toBe(walletAccount.publicKey);

});

// publicKey => ScriptHash

test(`publicKey => ScriptHash:$`,()=>{

const scriptHash = wallet.getScriptHashFromPublicKey(walletAccount.publicKey);

expect(scriptHash).toBe(walletAccount.scriptHash);

});

// scriptHashaddress

test("scriptHashaddress",()=>{

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也集成進去以便可以開箱即用的寫單元測試。


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 太平洋電腦網 的精彩文章:

Google Pixel 3 XL渲染圖/實機諜照完全泄露:劉海屏
三星也要出二合一了?Galaxy Book或將於8月發售

TAG:太平洋電腦網 |