這篇文章主要介紹了Hyperledger Fabric 2.0中Fabtoken怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Hyperledger Fabric 2.0 (alpha)中有一個新特性:Fabtoken,可以原生支持數字加密貨幣的發行與管理。我們都知道以太坊的ERC20標準可以用來在以太坊區塊鏈上創建數字加密代幣,現在有了Fabtoken,開發者使用Hyperledger Fabric也可以輕松實現數字加密貨幣的發行、轉賬等功能了!
首先我們需要先安裝Fabtoken的基礎平臺:Hyperledger Fabric 2.0。使用如下命令下載并進行安裝:
curl -sSL http://bit.ly/2ysbOFE | bash -s — 2.0.0-alpha 2.0.0-alpha 0.4.15
注意,為了避免潛在的沖突,如果你之前安裝過其他版本的HyperledgerFabric,請先卸載。
Fabtoken的核心功能如下:
創建新的加密貨幣
數字加密貨幣轉賬
查詢轉賬交易
贖回數字加密貨幣
在大多數情況下,前三個功能就足夠了。
一旦Fabric 2.0安裝完畢,你可以使用docker images
進行快速驗證。
現在執行如下命令進入Fabtoken目錄:
cd $HOME/fabric-samples/fabtoken
Fabtoken的運行需要一個Fabric網絡,它包含一個示例node應用fabtoken.js以及一個bash腳本startFabric.sh,這兩部分代碼都在當前目錄。bash腳本會首先啟動basic-network,然后進入javascript目錄,運行npm install來安裝必要的npm依賴,最后,啟動fabtoken演示應用。
我們強烈建議你看一下這個bash腳本的內容。
現在執行如下命令運行bash腳本:
./startFabric.sh
好了,現在我們就可以開始試試Fabtoken的功能了!
首先為user1創建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
顯然,成功了!
現在讓我們看看user1的金幣資產情況:
node fabtoken list user1
輸出結果如下:
— — 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
不錯!
接下來假設user1很大方,決定轉給user2金幣10個:
node fabtoken transfer user1 user2 10 \ e0b8a7ce6b248b8733ac7659c32a45b04a571de2548e371ada810a1e8bcf8eac 0
上面的命令表示從user1向user2轉10個金幣,使用如下交易輸出作為交易輸入:
txid:e0b8a7ce6b248b8733ac7659c32a45b04a571de2548e371ada810a1e8bcf8eac
index:0
你可能會問,為什么轉賬命令需要指定交易id和序號?答案是Fabtoken采用的是比特幣的UTXO機制,而不是以太坊的狀態賬戶機制,因此你需要指定有金幣的交易輸出(Transaction Output)。
上面的命令執行后可以看到如下結果:
— — 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: '' }
接下來,讓我們驗證user2確實得到了10個金幣:
node fabtoken list user2
Wow~看起來結果的確如此:
— — 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,我想為我的鉆石生意創建一些數字貨幣并發給我的朋友don,該怎么實現?
首先創建數字貨幣。
node fabtoken3 issue sam diamondcoin 1000
你可能注意到我使用“fabtoken3” 而不是“fabtoken”,然后用“sam” 代替 “user1”。讓我們看看結果:
— — 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創建了1000個鉆石幣。
我們驗證一下:
node fabtoken3 list sam
結果如下:
— — 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
現在讓我們給don轉50個鉆石幣。
node fabtoken3 transfer sam don 50 \ 837ac2e3bd7763f3a11d5a3d32822dac0a215a98f5ee7849d87111b03f631c6 0
結果如下:
— — 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
現在驗證don是否真的收到50個鉆石幣:
node fabtoken3 list don
結果如下:
— — 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
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Hyperledger Fabric 2.0中Fabtoken怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。