這篇文章主要介紹了Hyperledger Fabric 2.0中Fabtoken怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
目前成都創(chuàng)新互聯(lián)公司已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、漳縣網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
Hyperledger Fabric 2.0 (alpha)中有一個新特性:Fabtoken,可以原生支持?jǐn)?shù)字加密貨幣的發(fā)行與管理。我們都知道以太坊的ERC20標(biāo)準(zhǔn)可以用來在以太坊區(qū)塊鏈上創(chuàng)建數(shù)字加密代幣,現(xiàn)在有了Fabtoken,開發(fā)者使用Hyperledger Fabric也可以輕松實(shí)現(xiàn)數(shù)字加密貨幣的發(fā)行、轉(zhuǎn)賬等功能了!
首先我們需要先安裝Fabtoken的基礎(chǔ)平臺:Hyperledger Fabric 2.0。使用如下命令下載并進(jìn)行安裝:
curl -sSL http://bit.ly/2ysbOFE | bash -s — 2.0.0-alpha 2.0.0-alpha 0.4.15
注意,為了避免潛在的沖突,如果你之前安裝過其他版本的HyperledgerFabric,請先卸載。
Fabtoken的核心功能如下:
創(chuàng)建新的加密貨幣
數(shù)字加密貨幣轉(zhuǎn)賬
查詢轉(zhuǎn)賬交易
贖回數(shù)字加密貨幣
在大多數(shù)情況下,前三個功能就足夠了。
一旦Fabric 2.0安裝完畢,你可以使用docker images
進(jìn)行快速驗(yàn)證。
現(xiàn)在執(zhí)行如下命令進(jìn)入Fabtoken目錄:
cd $HOME/fabric-samples/fabtoken
Fabtoken的運(yùn)行需要一個Fabric網(wǎng)絡(luò),它包含一個示例node應(yīng)用fabtoken.js以及一個bash腳本startFabric.sh,這兩部分代碼都在當(dāng)前目錄。bash腳本會首先啟動basic-network,然后進(jìn)入javascript目錄,運(yùn)行npm install來安裝必要的npm依賴,最后,啟動fabtoken演示應(yīng)用。
我們強(qiáng)烈建議你看一下這個bash腳本的內(nèi)容。
現(xiàn)在執(zhí)行如下命令運(yùn)行bash腳本:
./startFabric.sh
好了,現(xiàn)在我們就可以開始試試Fabtoken的功能了!
首先為user1創(chuàng)建1000個金幣:
node fabtoken issue user1 goldcoin 1000
你會看到如下輸出:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client side Token arg: goldcoin Token arg: 1000 Start issue token operation Start token issue with args goldcoin,1000 End issue token operation, returns { status: 'SUCCESS', info: '' } — — fabtoken.js — end
顯然,成功了!
現(xiàn)在讓我們看看user1的金幣資產(chǎn)情況:
node fabtoken list user1
輸出結(jié)果如下:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client sideStart list token operation End list token operation, returns [ { id:{ tx_id: 'e0b8a7ce6b248b8733ac7659c32a45b04a571de2548e371ada810a1e8bcf8eac', index: 0 }, type: 'goldcoin', quantity: '1000' } ] — — fabtoken.js — end
不錯!
接下來假設(shè)user1很大方,決定轉(zhuǎn)給user2金幣10個:
node fabtoken transfer user1 user2 10 \ e0b8a7ce6b248b8733ac7659c32a45b04a571de2548e371ada810a1e8bcf8eac 0
上面的命令表示從user1向user2轉(zhuǎn)10個金幣,使用如下交易輸出作為交易輸入:
txid:e0b8a7ce6b248b8733ac7659c32a45b04a571de2548e371ada810a1e8bcf8eac
index:0
你可能會問,為什么轉(zhuǎn)賬命令需要指定交易id和序號?答案是Fabtoken采用的是比特幣的UTXO機(jī)制,而不是以太坊的狀態(tài)賬戶機(jī)制,因此你需要指定有金幣的交易輸出(Transaction Output)。
上面的命令執(zhí)行后可以看到如下結(jié)果:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client side Token arg: user2 Token arg: 10 Token arg: e0b8a7ce6b248b8733ac7659c32a45b04a571de2548e371ada810a1e8bcf8eac Token arg: 0 Start transfer token operation Start token transfer with args 10,e0b8a7ce6b248b8733ac7659c32a45b04a571de2548e371ada810a1e8bcf8eac,0 End transfer token operation, returns { status: 'SUCCESS', info: '' }
接下來,讓我們驗(yàn)證user2確實(shí)得到了10個金幣:
node fabtoken list user2
Wow~看起來結(jié)果的確如此:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client side Start list token operation End list token operation, returns [ { id:{ tx_id: '7e772f9d2e9e94a5c06e3ff2a62d13a41591b7d47daa9886c842aed6dd6d6582', index: 0 }, type: 'goldcoin', quantity: '10' } ] — — fabtoken.js — end
是不是很酷?
你可能會說,user1和user2對我毫無意義,我是sam,有個朋友叫don,我想為我的鉆石生意創(chuàng)建一些數(shù)字貨幣并發(fā)給我的朋友don,該怎么實(shí)現(xiàn)?
首先創(chuàng)建數(shù)字貨幣。
node fabtoken3 issue sam diamondcoin 1000
你可能注意到我使用“fabtoken3” 而不是“fabtoken”,然后用“sam” 代替 “user1”。讓我們看看結(jié)果:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client side Token arg: diamondcoin Token arg: 1000 Start issue token operation Start token issue with args diamondcoin,1000 End issue token operation, returns { status: 'SUCCESS', info: '' } — — fabtoken.js — end
的確,為sam創(chuàng)建了1000個鉆石幣。
我們驗(yàn)證一下:
node fabtoken3 list sam
結(jié)果如下:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client side Start list token operation End list token operation, returns [ { tx_id: '837ac2e3bd7a763f3a11d5a3d32822dac0a215a98f5ee7849d87111b03f631c6', type: 'diamondcoin', quantity: '1000' } ] — — fabtoken.js — end
現(xiàn)在讓我們給don轉(zhuǎn)50個鉆石幣。
node fabtoken3 transfer sam don 50 \ 837ac2e3bd7763f3a11d5a3d32822dac0a215a98f5ee7849d87111b03f631c6 0
結(jié)果如下:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client side Token arg: don Token arg: 50 Token arg: 837ac2e3bd7a763f3a11d5a3d32822dac0a215a98f5ee7849d87111b03f631c6 Token arg: 0 Start transfer token operation Start token transfer with args 50,837ac2e3bd7a763f3a11d5a3d32822dac0a215a98f5ee7849d87111b03f631c6,0 End transfer token operation, returns { status: 'SUCCESS', info: '' } — — fabtoken.js — end
現(xiàn)在驗(yàn)證don是否真的收到50個鉆石幣:
node fabtoken3 list don
結(jié)果如下:
— — fabtoken.js — startSetting up client side network objects Created client side object to represent the channel Created client side object to represent the peer Created client side object to represent the orderer Successfully setup client side Start list token operation End list token operation, returns [ { id:{ tx_id: '74316bd91757907e9c878a78d725b8c9f605b505ccd1801e8afd407bbd8b53b3', index: 0 }, type: 'diamondcoin', quantity: '50' } ] — — fabtoken.js — end
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Hyperledger Fabric 2.0中Fabtoken怎么用”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!