Fabtoken

Stella981
• 阅读 558

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
2年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Peter20 Peter20
3年前
mysql中like用法
like的通配符有两种%(百分号):代表零个、一个或者多个字符。\(下划线):代表一个数字或者字符。1\.name以"李"开头wherenamelike'李%'2\.name中包含"云",“云”可以在任何位置wherenamelike'%云%'3\.第二个和第三个字符是0的值wheresalarylike'\00%'4\
Stella981 Stella981
2年前
ERC20接入Uniswap教程【DeFi】
Uniswap是基于以太坊的去中心化数字加密货币交易协议,它为代币持有者提供了简洁的接口来将一种ERC20代币兑换为另一种,并且交易所需的gas成本很低。区块链开发者可以自己的ERC20代币接入Uniswap以增强其流动性。在这个教程中,我们将学习如何将一个ERC20代币接入Uniswap协议,并且在教程的最后提供完整的实现源代码。用自己熟悉的语言学
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这