-
Notifications
You must be signed in to change notification settings - Fork 109
使用交易组
QM edited this page Dec 21, 2020
·
1 revision
本文档以coins基础转账为例, 介绍chain33交易组基本用法
在节点中预设A, B, C三个地址账户, 首先构造一个包含分别向B和C转账一个BTY的交易组
#addr
A="12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
B="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
C="1Ka7EPFRqs3v9yreXG6qA4RQbNmbPJCZPj"
#private key
privA="0x4257d8692ef7fe13c68b65d6a52f03933db2fa5ce8faf210b5b8b80c721ced01"
privB="0xcc38546e9e659d15e6b4893f0ab32a06d103931a8230b0bde71459d2b27d6944"
fee=1000000
# 1 bty
amount=100000000
#chain33 rpc
rpc=http://localhost:8801
$ toBHex=$(curl -sd '{"method":"Chain33.CreateRawTransaction","params":[{"to":"'$B'","amount":'$amount'}]}' $rpc | jq .result)
$ toCHex=$(curl -sd '{"method":"Chain33.CreateRawTransaction","params":[{"to":"'$C'","amount":'$amount'}]}' $rpc | jq .result)
$ toBCGroupHex=$(curl -sd '{"method":"Chain33.CreateRawTxGroup", "params":[{"txs":['$toBHex', '$toCHex']}]}' $rpc |jq .result)
经过以上步骤即完成简单转账原始交易组的创建
本例采用将交易发送至节点进行签名,有两种情况, 以下分别介绍
该情况较简单,直接调用系统签名接口
$ signOnlyAHex=$(curl -sd '{"method":"Chain33.SignRawTx","params":[{"expire":"300s","fee":'$fee',"privkey":"'$privA'","txHex":'$toBCGroupHex'}]}' $rpc | jq .result)
每个交易依次签名,且需要指定交易在组内的index值, 第0个交易的index设为1, 以此类推
#用A私钥完成第一笔交易签名, A到B转账
$ signAHex=$(curl -sd '{"method":"Chain33.SignRawTx","params":[{"index":1, "expire":"300s","fee":'$fee',"privkey":"'$privA'","txHex":'$toBCGroupHex'}]}' $rpc | jq .result)
#基于上面的签名数据, 再用B的私钥完成第二笔交易签名, B到C转账, index设为2
$ signAnBHex=$(curl -sd '{"method":"Chain33.SignRawTx","params":[{"index":2, "expire":"300s","fee":'$fee',"privkey":"'$privB'","txHex":'$signAHex'}]}' $rpc | jq .result)
即发送到区块链进行执行, 使用系统发送交易接口
$ hash=$(curl -sd '{"method":"Chain33.SendTransaction","params":[{"data":'$signAnBHex'}]}' $rpc | jq .result)
通常交易组发送完毕后,只返回第一笔交易的哈希, next字段获取下一笔交易哈希
#执行第一笔交易查询操作
$ curl -sd '{"method":"Chain33.QueryTransaction", "params":[{"hash":'$hash'}]}' $rpc |jq .result.tx
可以看到header是自己的哈希, next是第二笔交易哈希
{
"execer": "coins",
"payload": {
"transfer": {
"cointoken": "",
"amount": "100000000",
"note": null,
"to": "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
},
"ty": 1
},
"rawPayload": "0x18010a291080c2d72f222231344b454b6259744b4b516d34774d7468534b394a344c61346e41696964476f7a74",
"signature": {
"ty": 1,
"pubkey": "0x0320bbac09528e19c55b0f89cb37ab265e7e856b1a8c388780322dbbfd194b52ba",
"signature": "0x304402203f1903d30bc1c29059c204b63c4e656ba60e51c21c7796b9b0b7008f269955ed022004f7131072b038e93d66105e99b6b3edcb16780ca119ae1a1dcd3b926130801a"
},
"fee": 200000,
"feefmt": "0.0020",
"expire": 0,
"nonce": 7073711822094738000,
"from": "12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv",
"to": "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt",
"groupCount": 2,
"header": "0xa3e0403195bfabb575e61d55eb90276ffc435608feb34372e2a5f1a842a79f7a",
"next": "0x0d07957315b5c62e11795520b68624cbee1dce03953d48ad844158aab68493da",
"hash": "0xa3e0403195bfabb575e61d55eb90276ffc435608feb34372e2a5f1a842a79f7a"
}