那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。
http://test.blockmeta.com/faucet.php! w- ?' V T7 A1 N0 ?* E# ^
请求build-transaction接口:
请求参数:$ U# U& O) b$ @: G- w$ r) y' @! o
{8 \- @6 S3 i8 z& h
"base_transaction": null,+ v3 W! s7 `4 }5 P3 w) [
"actions": [{4 v4 N+ S& u, o8 H' b
"account_id": "0KTCS3R5G0A02",& }: C5 o$ _, K j- E
"amount": 10000000," Y* C. S1 A; e( Y% E; d
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "spend_account"
}, {; Y5 x1 K6 B, A" z: E1 x7 _6 ~
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",8 p+ O: i9 j6 C. t9 y: @7 i
"type": "spend_account"
}, {( y l5 m9 c! q/ i+ b
"account_id": "0KTCS3R5G0A02",
"amount": 100,. Y3 F/ D% G6 h- N/ |# I5 A
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",/ B: S5 _" j% b
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",0 g' N9 \) N1 G
"type": "retire"0 \ E( y& ^6 M& U7 @
}],& U# D5 i7 b! h5 P' p/ z" x
"ttl": 0,
"time_range": 1521625823
}; f: X+ ^1 q7 s6 a% Q. x
请求sign-transaction接口:
请求参数:/ s# u3 c" M4 q/ e0 L' c2 D* b
{2 b8 D. `+ G {, u
"password": "huangxinglong123",
"transaction": {/ _6 U+ n: P4 z& X7 e9 `
"allow_additional_actions": false,5 j |* _2 m; @0 y" g6 g6 |
"local": true,$ [. ]; N" ?* E2 x9 R' z; W/ J
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",) t) _3 x6 W7 d1 O2 [
"signing_instructions": [{
"position": 0,) g8 \" n9 O8 o9 N7 f% B
"witness_components": [{ Y0 j9 `$ \ W m- p4 F% u1 V% f8 h* k
"keys": [{
"derivation_path": [) R0 G. f/ U% J' f2 c$ L+ i
"0000002c",+ V* s U' w. p) i; ^4 A" L1 Z
"00000099",
"0100000000000000",
"0100000000000000",# ^6 Q& j, Q9 D: y% G" N& Y5 G- s& w
"4600000000000000"
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,# X; H9 d) `, W2 J. |5 D1 C
"signatures": null,0 `9 a" {! G a+ Q4 i8 r0 Z, Q
"type": "raw_tx_signature"8 f. s6 s: c6 m5 V' `9 c
}, {, M V9 h5 f& ^2 i8 ~3 S; L
"type": "data",) I* t; l* X+ p! U% q( f
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a". G. E! \, [4 M4 ^
}]
}, {
"position": 1,! S- ~& C3 U* W
"witness_components": [{
"keys": [{
"derivation_path": [% j2 Q3 O. O( O( a/ T5 K1 `
"0000002c",
"00000099",
"0100000000000000",
"0100000000000000",
"4700000000000000"$ g% P5 D+ L, R
],* P- [! [, {* |6 C; ]6 w2 n
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],2 Q( V+ R1 G. e% o% C3 E- g9 T
"quorum": 1,
"signatures": null,
"type": "raw_tx_signature"
}, {
"type": "data", Q! D$ H& L! D7 |4 Q5 Y7 g
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa" i. g6 X( B9 ?+ y
}]
}]
}2 @2 |% w! {/ C, m2 S3 h. F
}; x. E9 l& ]9 O+ p4 I
请求submit-transaction接口:9 R6 S S, O! k* Q
请求参数:
{
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
}" d; w! Z, V5 y2 g8 t1 U3 R
响应参数:' R3 b; H5 }- n4 b
{6 z$ i7 Q: F _
"status": "success",
"data": { u& i; G! ?7 a0 ^$ J/ g
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"* K' c% V y8 i, E$ @! w9 E
}
}
$ h+ w6 H4 p) g
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据- |3 U2 X: I; H0 y6 e) f
( w6 I+ L- n6 j% C, f/ E' r( K
参考代码:
package main/ A( j& {/ \$ }/ J$ p v3 V
import (
"bytes"
"encoding/json"# y4 G& n* j& R7 i8 M
"fmt"5 M1 G# f0 N" B) L& A7 o
"io/ioutil") c& R, \9 P( ^6 ]7 Y$ I
"net/http" ]7 F, F+ e# C# F
)
//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type BytomAccount struct {4 f+ X+ ^6 c2 @; u" y( K
AccountId string `json:"account_id"`2 F4 \, Z5 g( \- }
Amount int `json:"amount"`8 e4 n6 k1 i" w* E4 _
AssetId string `json:"asset_id"`6 T" e- o# Y5 ^2 d+ V
//Arbitrary string `json:"arbitrary"`
Type string `json:"type"`5 I3 O% ?) _1 I ^
}
type BytomAccount1 struct {7 a/ E# D4 H ?3 N7 \
AccountId string `json:"account_id"`
Amount int `json:"amount"`3 f, a0 h2 S6 V& X7 H" l: w: y# t
AssetId string `json:"asset_id"`- \; Z. X; Q5 S/ r
Arbitrary string `json:"arbitrary"`, c; {% B/ R+ W! n2 {5 F3 F6 R
Type string `json:"type"`- }, ^" q% c) m! }3 l* z: ?
}% ~2 v5 x! K y( f8 r3 w
type BaseTransaction struct{}1 F3 T( z& O5 F8 N
type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`7 b7 _/ R) {' W: c o' N3 Z
Actions []interface{} `json:"actions"`
Ttl int `json:"ttl"`* C# r, f- @! v% j C
TimeRange int `json:"time_range"`4 D0 {9 t6 Z: W- U! Q
}$ g( t: Z: A- i4 G; C: Q
//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction( I" q! S; z# ^/ t
type Transaction struct {
}
type SignParams struct {( }4 _3 |9 ?3 w! {3 c. x% F5 R
Password string `json:"password"`
Transaction Transaction `json:"transaction"`+ Y+ r! P Q! L7 }
}
//submit-transaction
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type SubmitParams struct {
RawTransaction string `json:"raw_transaction"`. X' e; d! N# w6 Q8 A/ L
}6 ?" \! ~" {4 |6 m# R- I5 z4 u n
type SubmitResponse struct {
TxId string `json:"tx_id"`
}
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}
account1.AccountId = "0KTCS3R5G0A02"9 F4 i1 V8 V6 J1 v( G8 L
account1.Amount = 10000000
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"% s# X) s8 O+ I5 b1 Q
account1.Type = "spend_account"
account2.AccountId = "0KTCS3R5G0A02"8 i. T6 b8 w& {' V; }+ @: N, M- \. y
account2.Amount = 100& I0 M* U/ m8 u% a q- M5 p
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"; V1 g3 e, e0 M4 o
account3.AccountId = "0KTCS3R5G0A02"5 I- P/ z# Q$ ~
account3.Amount = 1009 e# S3 S7 ~7 a, g9 R3 w9 K6 o5 N( I
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"( G. c5 Q2 Q# N! y+ l
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"
account3.Type = "retire"
//var array
var actions []interface{}
//append three params
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{} f* t% S5 F& r( s
transaction_params.Actions = array_actions1 O2 W/ z: Q) P! n$ R
transaction_params.Ttl = 0! Q5 e% c6 Q! }$ M+ I. N# w
transaction_params.TimeRange = 15216258239 m8 K' y- O3 ~5 i
//本地测试网节点
//build-transaction0 [0 x; p" v# n! D
port := "http://127.0.0.1:9888/build-transaction") Z s) P" G [( s' o; a1 [
value, err := SendTransactionRetire(transaction_params, port)2 \9 E/ {, ^# [$ c' k
if err != nil {: A) W! _# p6 N! z5 `
fmt.Println("err:", err). E1 Z' H3 v! A9 I, q, C2 D
}4 t" v% o5 J( H d
fmt.Println("build-transaction接口返回的参数:", value)
//sign-transaction
//...........9 L# F D6 [+ E% j. }: G( k* ]( ~" P
//submit-transaction
//...........
}, w4 v; \; n9 D! R! U' s5 X1 l% z
//send post request8 `# T$ k8 |5 j; x" N% e5 E1 I
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接- `" i$ a3 m" h% S8 V$ ^1 z5 X7 R
ParamsStr, err := json.Marshal(params)1 i: K0 V! L- V3 q& D F
if err != nil {5 A# C0 C& q0 X4 l
return nil, err
}& d8 ?) Z+ k3 k" o( y7 Q
jsonStr := bytes.NewBuffer(ParamsStr)/ u s( J p/ ]4 y7 Z+ \' x l" p
fmt.Println(jsonStr)
req, err := http.NewRequest("POST", port, jsonStr)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")0 p0 Y) c* F \; o$ f
client := &http.Client{}
resp, err := client.Do(req)# G* f y9 r; n
if err != nil {/ c1 i# @, Z1 I! @- K
panic(err)2 ~6 X6 y+ i( ~9 ^- W/ V Q3 q s
}* R( ]. [* _& t& k3 \
defer resp.Body.Close()7 T% Z6 j T! x$ N- Q7 \' v( B
var bodyBytes []byte' r% Q5 k' c5 j9 Z
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)3 ^2 Z; P& _% I
if err != nil {) R3 y/ T/ L3 Z! E6 c0 F4 A
return nil, err
}- G( l! T0 D& d4 v
}
return string(bodyBytes), nil5 t N4 F6 ~3 v) j$ D2 k3 W
}5 r3 z5 D0 h$ h) @! z
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。; n8 C! ?. b( B: M9 _) I
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。