超级账本Fabric 2.0原生数字货币Fabtoken教程

Hyperledger Fabric 2.0 (alpha)中有一个新特性:Fabtoken,可以原生支持数字加密货币的发行与管理。我们都知道以太坊的ERC20标准可以用来在以太坊区块链上创建数字加密代币,现在有了Fabtoken,开发者使用Hyperledger Fabric也可以轻松实现数字加密货币的发行、转账等功能了!

Hyperledger Fabric链码与应用开发相关教程:

  • Hyperledger Fabric Java开发详解
  • Hyperledger Fabric Node.js开发详解

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的神秘宝石 - 汇智网