# 合约交易构造
# 编译合约
需要首先运行Summoner编译器,教程请参考上一章
func unlockWithNumber(num int64) {
verify(num == 1)
}
合约实现了,如果传入的参数为1,则解锁该UTXO,否则无法解锁
** global variable section *********************************
** function section ****************************************
*** [0] unlockWithNumber ***
void unlockWithNumber ( num)
0 DUP
1 1
2 EQUAL
3 OVER
4 1
5 EQUAL
6 VERIFY
7 DROP
*** line number ***
*** assemble opcodes ***
7651877851876975
*** end of unlockWithNumber ***
** type section ********************************************
** toplevel ********************
*** line number ***
获取构造交易需要使用的opcodes:7651877851876975
# 合约上链
使用全节点钱包将合约上链
# 构造交易
将op码作为control_program参数传入build-transaction接口
http://localhost:9888/build-transaction (opens new window)
{
"base_transaction": null,
"actions": [
{
"account_id": "6b8bd994-960d-4658-897c-4f5fed4c7f5b",
"amount": 100000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "spend_account"
},
{
"amount": 30000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"control_program": "7651877851876975",
"type": "control_program"
}
],
"ttl": 0,
"time_range": 1521625823
}
返回
{
"status": "success",
"data": {
"raw_transaction": "0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b350001000201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000",
"signing_instructions": [
{
"position": 0,
"witness_components": [
{
"type": "raw_tx_signature",
"quorum": 1,
"keys": [
{
"xpub": "25bd0d1cefc98661c51dcd0174105775bc1357a76393cf4f85c14c69716e26c02c98550eb9ea10b851e34de6b2814a447b077b57e3ee56fc6d8a29715788e9c7",
"derivation_path": [
"2c000000",
"99000000",
"01000000",
"00000000",
"01000000"
]
}
],
"signatures": null
},
{
"type": "data",
"value": "e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df9"
}
]
}
],
"fee": 70000000,
"allow_additional_actions": false
}
}
# 签名交易
将构造好的交易体放入sign-transaction接口签名
{"password":"xxxxx","transaction":{
"raw_transaction": "0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b350001000201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000",
"signing_instructions": [
{
"position": 0,
"witness_components": [
{
"type": "raw_tx_signature",
"quorum": 1,
"keys": [
{
"xpub": "25bd0d1cefc98661c51dcd0174105775bc1357a76393cf4f85c14c69716e26c02c98550eb9ea10b851e34de6b2814a447b077b57e3ee56fc6d8a29715788e9c7",
"derivation_path": [
"2c000000",
"99000000",
"01000000",
"00000000",
"01000000"
]
}
],
"signatures": null
},
{
"type": "data",
"value": "e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df9"
}
]
}
],
"fee": 70000000,
"allow_additional_actions": false
}}
返回
{
"status": "success",
"data": {
"transaction": {
"raw_transaction": "0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35006302405d90ea1c5b73e505a560dc46623e63d1bb9471ed65f7ee333d22ba2a9e6e1116fd8e98e877fd51afba81d691638aacac8babaabb606e58b65cf01205d3092a0c20e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df90201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000",
"signing_instructions": [
{
"position": 0,
"witness_components": [
{
"type": "raw_tx_signature",
"quorum": 1,
"keys": [
{
"xpub": "25bd0d1cefc98661c51dcd0174105775bc1357a76393cf4f85c14c69716e26c02c98550eb9ea10b851e34de6b2814a447b077b57e3ee56fc6d8a29715788e9c7",
"derivation_path": [
"2c000000",
"99000000",
"01000000",
"00000000",
"01000000"
]
}
],
"signatures": [
"5d90ea1c5b73e505a560dc46623e63d1bb9471ed65f7ee333d22ba2a9e6e1116fd8e98e877fd51afba81d691638aacac8babaabb606e58b65cf01205d3092a0c"
]
},
{
"type": "data",
"value": "e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df9"
}
]
}
],
"fee": 70000000,
"allow_additional_actions": false
},
"sign_complete": true
}
}
# 提交交易
使用submit-transaction接口将合约提交上链
http://localhost:9888/submit-transaction (opens new window)
{"raw_transaction":"0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35006302405d90ea1c5b73e505a560dc46623e63d1bb9471ed65f7ee333d22ba2a9e6e1116fd8e98e877fd51afba81d691638aacac8babaabb606e58b65cf01205d3092a0c20e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df90201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000"}
返回
{
"status": "success",
"data": {
"tx_id": "f9cc23f393c6a6b9c87c599586409a360123c0b0bc41f9664992b1e39ccd99bb"
}
}
在全节点钱包中根据交易id获取合约的output id
id: bf3a6e44fdf2929d0923f83053d85236d3843cd1bf1ec3bd2d7299b61a050d50
# 解锁合约
当前合约解锁需要使用到Blockcenter构建高级交易
# 构建交易
使用build-advanced-tx来构建交易
[http://bcapi.movapi.com/bycoin/bytom2/v1/merchant/build-advanced-tx?address=bn1q4ckxhay8zn8s6qxh0wg8dzx2zs9xcggmru3fl4]
{
"fee":"0.1",
"inputs":[
{
"output_id":"bf3a6e44fdf2929d0923f83053d85236d3843cd1bf1ec3bd2d7299b61a050d50",
"type":"spend_utxo"
}
]
}
返回
{
"code": 200,
"msg": "",
"data": [
{
"tx": {
"hash": "f54978e7bf0fd9dbdb7a6261e487e014c8ccf628706e00d0da2f5a79c1a32e9c",
"submission_timestamp": 0,
"inputs": [
{
"script": "7651877851876975",
"address": "smart contract",
"asset": {
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"symbol": "BTM"
},
"amount": "0.3",
"type": "spend"
}
],
"outputs": [
{
"utxo_id": "1f1513beec9ba33e73b1cbad5804bf6a7c9760813a44cc3992f621f92a70380a",
"script": "0014ae2c6bf48714cf0d00d77b907688ca140a6c211b",
"address": "bn1q4ckxhay8zn8s6qxh0wg8dzx2zs9xcggmru3fl4",
"asset": {
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"symbol": "BTM"
},
"amount": "0.2",
"type": "control"
}
],
"fee": "0.1",
"balances": [
{
"asset": {
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"symbol": "BTM"
},
"amount": "0.2"
}
],
"types": [
"ordinary"
],
"min_veto_height": 0
},
"raw_transaction": "07010001015201502f5f52ba8fd87dae065112df3902d006c71ac992399620e2782051882afeda37ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e01010876518778518769750001000101003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80dac40901160014ae2c6bf48714cf0d00d77b907688ca140a6c211b0000",
"signing_instructions": [
{
"derivation_path": null,
"sign_data": [
"3997891ced33c944d75b2795c10e1fa21caa4653c140e4f967252e8cefd6c8ba"
]
}
]
}
]
}
# 提交交易
使用submit-payment接口来提交交易,在signatures中带入参数,本合约需要传入一个数值,传入为1即可顺利解锁
[http://bcapi.movapi.com/bycoin/bytom2/v1/merchant/submit-payment?address=bn1q4ckxhay8zn8s6qxh0wg8dzx2zs9xcggmru3fl4]
{
"raw_transaction": "07010001015201502f5f52ba8fd87dae065112df3902d006c71ac992399620e2782051882afeda37ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e01010876518778518769750001000101003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80dac40901160014ae2c6bf48714cf0d00d77b907688ca140a6c211b0000",
"signatures": [["1"]]
}
返回
{
"code": 200,
"msg": "",
"data": {
"tx_hash": "f54978e7bf0fd9dbdb7a6261e487e014c8ccf628706e00d0da2f5a79c1a32e9c"
}
}
完成合约解锁