Hyperledger Fabric 2.0 (alpha)中有一個新特性:Fabtoken,可以原生支援數字加密貨币的發行與管理。我們都知道以太坊的ERC20标準可以用來在以太坊區塊鍊上建立數字加密代币,現在有了Fabtoken,開發者使用Hyperledger Fabric也可以輕松實作數字加密貨币的發行、轉賬等功能了!
Hyperledger Fabric鍊碼與應用開發相關教程:
1、安裝Hyperledger Fabric 2.0
首先我們需要先安裝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,請先解除安裝。
2、Fabtoken的核心功能
Fabtoken的核心功能如下:
- 建立新的加密貨币
- 數字加密貨币轉賬
- 查詢轉賬交易
- 贖回數字加密貨币
在大多數情況下,前三個功能就足夠了。
3、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
是不是很酷?
4、用Fabtoken發行特定應用的代币
你可能會說,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
的确如此!don收到50個鑽石币!
原文連結:
Fabtoken - Hyperledger Fabric 2.0的神秘寶石 - 彙智網