那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。) d* o2 }: Q3 {
http://test.blockmeta.com/faucet.php( Q6 I$ F5 X8 _8 P w( L
请求build-transaction接口:
请求参数:0 G, Q1 f: |- V; Z
{; J+ o. i$ x6 s. a" C1 w7 S
"base_transaction": null,0 Q8 u2 m' x) L5 P; U7 y
"actions": [{7 J8 A( Z1 k7 g% _4 b4 F) o
"account_id": "0KTCS3R5G0A02",
"amount": 10000000,% B* G! ^) ~* [3 p" e& L2 p r* l! h
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",: u6 `9 C9 Q: o- N! T9 V; F8 l2 U
"type": "spend_account"
}, {; \, {# f. ?; A9 f
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"type": "spend_account"% O9 K5 d" T- L! {7 P" H
}, {) Z7 k r ~! P9 D8 `
"account_id": "0KTCS3R5G0A02",. b) _, O& @0 a; \% F
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",* B; \- t1 F) @' s7 ~5 ]; V* Q( i
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",
"type": "retire"
}],' u% A0 g0 g. v3 n1 _
"ttl": 0,
"time_range": 1521625823$ N) [3 t4 M) e% v( u( {4 U
}
请求sign-transaction接口:$ D0 }% d# D2 d! ~. Y3 O. X
请求参数:
{2 O8 j3 F0 E# b5 P
"password": "huangxinglong123",
"transaction": {1 C9 r* B( z4 V. D/ |# B
"allow_additional_actions": false,
"local": true,0 X) K+ X; y5 h( r% Y
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
"signing_instructions": [{
"position": 0,; q& m& E _" c0 M+ u; ^
"witness_components": [{
"keys": [{
"derivation_path": [' _3 E% W3 G8 m
"0000002c",
"00000099",9 N- @) F8 h0 h) h! C
"0100000000000000",
"0100000000000000",( f$ @, q/ h: @/ r+ X
"4600000000000000"$ S5 A( C, e# Y. w8 ?
],- G1 {$ W# v! [* X3 _, I' F; S
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],3 P$ ^& |5 x% F, F" n
"quorum": 1,
"signatures": null,
"type": "raw_tx_signature"6 j2 i' B. y% S7 N, l& k# F
}, {4 ?+ i; `5 e+ ~6 m$ O
"type": "data",2 x0 Y3 c& F9 f6 K, n5 _
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"
}]* I: A0 b6 \! ^# m7 A& h
}, {" ?& i; C9 B# B0 h
"position": 1,
"witness_components": [{
"keys": [{: }+ m5 g3 K" K1 ~6 G; d& @
"derivation_path": [ w) X$ r! {2 {& Z$ V* e
"0000002c",2 \0 l. z, D1 f0 a+ Z
"00000099",
"0100000000000000",/ { _! L+ ?7 @+ e( h- Y# o
"0100000000000000",
"4700000000000000"
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],7 D6 j! m2 P; p% j1 l
"quorum": 1,+ Y- @/ z0 g! D$ _1 x. ~
"signatures": null,
"type": "raw_tx_signature"" X, x0 r0 N% j. f4 y
}, {; O' I5 m4 v9 w) z) Z1 L5 J
"type": "data",9 F6 g- V& Z6 G& {
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa": w; ` k" T3 Q C9 D6 z
}]1 b) k: d2 u( C
}]+ W8 n3 w `& V
}. E4 j+ I& c3 ^+ u x% y+ J& r I0 d
}
请求submit-transaction接口:; r# |& w- f* I$ }
1 Y7 m: {3 Q$ ~- K0 h
请求参数:
{0 C4 b* ~" b3 c: _, _
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
}
响应参数:
{1 T o4 C7 h# @) J' M, p: ~( w
"status": "success",9 Q, N. \* c* P$ t& ?: F
"data": {& |# R- O# g8 X2 B' y% z8 g
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"
}
}
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据9 W B1 h& O" `8 s" V
& ^: H, w. A" h1 Q( \: o) V
参考代码:
package main! c8 T$ w- ?7 c7 g4 T
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction; B$ ]4 ]! B# F* m0 i" v
type BytomAccount struct {) Y6 x }9 g/ M6 J0 {6 `# p# P; j4 c
AccountId string `json:"account_id"`4 U* m) ?; N5 e/ v$ b# Y) X
Amount int `json:"amount"`; W N, x% r* c: g! L- l; [
AssetId string `json:"asset_id"`
//Arbitrary string `json:"arbitrary"`( ]- u( K; G8 j8 e$ n3 g5 ?
Type string `json:"type"`
}
type BytomAccount1 struct {" Z; C) W0 R) }8 j5 t
AccountId string `json:"account_id"`4 X) a8 G5 H+ W, m
Amount int `json:"amount"`
AssetId string `json:"asset_id"`
Arbitrary string `json:"arbitrary"`8 P s9 v; `8 E6 K/ ?2 T( h
Type string `json:"type"`5 v- @( l( l3 D0 U/ Y
}& t( C+ \3 g! A6 D4 _
type BaseTransaction struct{}
type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`
Actions []interface{} `json:"actions"`' j+ o# ^0 ?" o4 `1 d, u' [, q
Ttl int `json:"ttl"`
TimeRange int `json:"time_range"`
}3 [" G2 K, p, T
//sign-transaction params9 O q4 Z2 x- G" C# E8 T
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction( S# e& ]. f/ x* F& m Y1 g4 ]# z
type Transaction struct {$ w" [; c2 |# l. P& w3 _
}7 i1 }. A7 Y' S8 N
type SignParams struct {
Password string `json:"password"`$ x! i" ^; n, U( u7 O$ D
Transaction Transaction `json:"transaction"`0 B; Z) L2 m( Q$ ]" I( i+ c
}; H- @! R9 `& g `& k
//submit-transaction! t- Z# s, U( j' F" J
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type SubmitParams struct {1 }' v; F6 f; }; ~) I" _
RawTransaction string `json:"raw_transaction"`
}, K, U, o& b7 x. p5 s
type SubmitResponse struct {
TxId string `json:"tx_id"`: j3 B; u- |# l, ~3 ]' A: A- {
}
func main() {" w0 u/ T0 Q5 P a7 t
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}6 H! f$ R! n$ j
account1.AccountId = "0KTCS3R5G0A02"' @+ V4 V [0 U
account1.Amount = 100000007 W. \5 K) ]: Y/ U2 x
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"7 L& d+ X, b- X3 A( \4 t
account2.AccountId = "0KTCS3R5G0A02"
account2.Amount = 100
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"3 [4 g+ ?5 W$ f* @
account2.Type = "spend_account"
account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 1008 C9 o0 }, f: f1 }
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"5 q f, \: B6 {- S% O' B5 j- L5 O6 Q! t
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"- f( h: z E+ n% }, s* r* x6 W
account3.Type = "retire"& k( j. ~; p' Q* x% S* Z) H
//var array9 c" ]1 |$ H' a& C( G/ {! j$ M
var actions []interface{}
//append three params( M {( R- ?" E( {
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{}% s$ K x( @$ ?4 i) K7 I
transaction_params.Actions = array_actions
transaction_params.Ttl = 0
transaction_params.TimeRange = 1521625823
//本地测试网节点. x; a+ [5 h( M2 a6 ~& Y, V
//build-transaction1 }$ I" O, `6 \' k& S: r# ], S
port := "http://127.0.0.1:9888/build-transaction"/ @9 | J0 Q( x3 V# x) F
value, err := SendTransactionRetire(transaction_params, port)1 o1 _7 W1 T9 t. r- E; Z3 y
if err != nil {
fmt.Println("err:", err)) s% C* l* y" T% Y4 f
}- _8 v1 ]# m9 C' h) W' V: X- }* D1 q
fmt.Println("build-transaction接口返回的参数:", value)0 e. q8 f$ X1 `# O {4 u% @
//sign-transaction
//...........
//submit-transaction
//...........
}5 D0 O: h1 N, v; `9 h
//send post request7 d4 }9 u. B! u: h9 p
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接
ParamsStr, err := json.Marshal(params)
if err != nil { R O9 B* Y" u: w7 Y/ M
return nil, err
}$ H% T- c8 a" v N8 R% G
jsonStr := bytes.NewBuffer(ParamsStr)
fmt.Println(jsonStr)
req, err := http.NewRequest("POST", port, jsonStr)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")3 |2 M3 |# K' @# R5 Z
client := &http.Client{}
resp, err := client.Do(req)( ]) Y9 s8 f0 v4 x3 N
if err != nil {* x0 ^* O/ l- r4 Q3 r
panic(err)( B7 e. G4 H0 C" N% U& I$ T1 ^
}
defer resp.Body.Close()
var bodyBytes []byte
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)! v6 {$ l( ^# X- p: L$ s
if err != nil {
return nil, err
}" b6 q% i3 @ |& R- d- x3 E
} ]6 p; e6 ^: {7 P, G$ D: G, {
return string(bodyBytes), nil
}
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。