比原链合约入门教程

简介:

比原项目仓库:

Github地址:https://github.com/Bytom/bytom

Gitee地址:https://gitee.com/BytomBlockchain/bytom

一、合约简述

equity是bytom的一种智能合约语言,是一门声明性谓词语言。详细说明请参考官方equity合约相关介绍。

二、锁定合约流程

1、合约编写

合约的编写可以通过参考合约模板来进行构造,如果需要更进一步的了解,可以阅读一下合约相关的说明文档。以典型的锁定publickey合约为例,其合约代码如下:

contract LockWithPublicKey(publicKey: PublicKey) locks locked { clause unlockWithSig(sig: Signature) { verify checkTxSig(publicKey, sig) unlock locked } }

2、编译合约

编译合约的工具暂未提供,建议使用api调用的方式来编译合约。调用的方式可以使用 curl 命令行 或 postman 工具等。

curl 调用方式如下:

curl -X POST http://localhost:9888/create-key -d '{"alias": "alice", "password": "123456"}'

postman 需要到官网下载工具:

https://www.getpostman.com/

1)bytomd默认启动或开启auth认证,需要通过curl的方式调用API接口 获取access toekn

curl -X POST http://localhost:9888/create-access-token -d '{"id":"token1"}'

返回结果如下:

{ "token": "token1:1fee70f537128a201338bd5f25a3adbf33dad02eae4f4c9ac43f336a069df8f3", "id": "token1", "created_at": "2018-03-20T18:56:01.043919771+08:00" }

2)选择 basic auth 认证方式,填入 access-token 的用户名和密码,即上述返回结果的 token1 和 1fee70f537128a201338bd5f25a3adbf33dad02eae4f4c9ac43f336a069df8f3

3)如果合约参数包含类型PublicKey,便需要获取合约参数 PublicKey, publicKey可通过API接口获取。这里需要将结果保存下来,以后解锁合约的时候需要用到 root_xpub 和 pubkey对应的 derivation_path。

4)编译合约,返回结果program 便是可锁定的合约。(直接编译合约产生的是合约执行步骤,如果部署这样的合约,用户可以直接构造对应的参数便能解锁)

3、构建锁定合约交易

下面表示锁定 1000 个 777e3586d2ec47c4974d262e0ff86fd3c1c063d242d32bdef71d6d16eed6763e 类型的资产到合约 204e925f0fcc2f2618d96b7a3dce2aad28e60ab3339377760d48aea8ae8169ae417403ae7cac00c0 之中。具体内容参考构建交易的wiki: https://github.com/Bytom/bytom/wiki/API-Reference#build-transaction

4、签名交易

具体内容参考签名交易的wiki:https://github.com/Bytom/bytom/wiki/API-Reference#sign-transaction

5、提交交易

具体方式参考提交交易的wiki:https://github.com/Bytom/bytom/wiki/API-Reference#submit-transaction

一旦合约交易打包成功,这样合约交易便发送成功了。

三、解锁合约流程

1、合约交易被区块打包成功之后,可以查看具体的合约交易内容,找到对应的outputID。

其中ID字段便表示 outputID

2、根据outputID通过list-unspent-outputs 接口API 查找合约的UTXO,查看合约锁定的资产类型和数量是否匹配

3、构建解锁合约交易,解锁clause unlockWithSig 的参数为 Signature, 但是签名结果只能在 sign-transaction 成功之后才能得到,所以我们只需构建完成签名参数即可。于之前构建的publicKey 为 4e925f0fcc2f2618d96b7a3dce2aad28e60ab3339377760d48aea8ae8169ae41,那么签名参数为root_xpub 和 derivation_path,对应的值分别为: "dc903a862a14966d47dc6bc935c687c6cccb7a8f9c70f13bb82a41a0fe2696596cb141ff1840f90c75f8f25099f0dc50e005e1e36817d184b2b1eb1354b61575" 和 [ "010400000000000000", "0100000000000000" ] 。 其结构如下:(具体字段可参考官方智能合约文档说明)

4、签名交易,具体内容参考签名交易的wiki:https://github.com/Bytom/bytom/wiki/API-Reference#sign-transaction

5、提交交易,具体方式参考提交交易的wiki:https://github.com/Bytom/bytom/wiki/API-Reference#submit-transaction

解锁合约交易打包成功之后,根据 outputID 通过list-unspent-outputs 接口API 查找原来的合约 UTXO,将显示为空,否则合约交易解锁失败。

相关文章
|
6月前
|
安全 IDE 区块链
BTC比特链/ETH以太链/BSC币安链/TRX波场链/Matic马蹄链智能合约系统开发稳定版丨指南步骤丨源码详情
Developing a smart contract system is actually the process of writing and executing code on a specific blockchain (such as Bitcoin, Ethereum, Binance Smart Chain, Tron, or Polygon (Matic)). These codes (conventions) have the ability to automatically execute and facilitate transactions without interm
|
6月前
|
机器学习/深度学习 人工智能 自然语言处理
秒合约开发原理丨秒合约系统开发(详细规则)丨秒合约源码案例部署
秒合约开发原理丨秒合约系统开发(详细规则)丨秒合约源码案例部署
|
6月前
|
运维 安全 算法
TRX链/BSC链/ARB链智能合约系统开发方案逻辑丨详细项目丨规则玩法丨案例详情丨源码出售
需求分析:与客户沟通,了解其业务需求和期望,明确系统的功能和性能要求。确定在哪个链上进行开发(TRX链、BSC链还是ARB链)。
|
7月前
|
安全 区块链
TRX链/BSC链/ARB链/Matic马蹄链公链智能合约系统开发指南需求丨步骤逻辑丨规则方案丨案例开发丨项目程序丨源码说明
Chain selection and environment construction: Select suitable public chains as development environments, such as TRX chains, BSC chains, ARB chains, or Matic horseshoe chains. Establish a corresponding development environment, including node deployment, development tools, and testing network.
|
8月前
|
安全
合约跟单丨合约交易丨秒合约丨交易所系统开发规则介绍/成熟技术/细节方案/步骤逻辑/项目详细/源码部署
Second contract is a high-frequency trading strategy or mode, also known as High Frequency Trading (HFT). It refers to the use of computer algorithms and high-speed trading systems to conduct a large number of trading operations in a very short time span.
|
8月前
|
安全
dapp丨defi代币合约质押项目系统开发逻辑详细/规则说明/案例设计/步骤细节/源码程序
The smart contract liquidity mining system is a financial application system based on smart contract technology, aimed at providing liquidity and receiving rewards by injecting users' funds into the liquidity pool and locking them in the smart contract.
|
9月前
|
存储 供应链 前端开发
智能合约Dapp系统链上开发部署源码规则解析
智能合约Dapp系统链上开发部署源码规则解析
|
9月前
|
区块链
以太坊竞猜合约开发源码规则
以太坊竞猜合约开发源码规则
|
9月前
|
存储 人工智能 安全
BSC链DAPP智能合约项目系统开发案例分析/需求项目/源码逻辑
  区块链技术的去中心化和安全性质,使得它与人工智能技术的结合更加有意义。区块链可以提供安全的数据存储和传输,而人工智能可以提供更加智能化和自动化的操作方式。
|
10月前
|
安全 JavaScript 前端开发
TRON波场链USDT合约开发源码规则部署
TRON波场链USDT合约开发源码规则部署

热门文章

最新文章