那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。
http://test.blockmeta.com/faucet.php
请求build-transaction接口:
请求参数:9 F9 E/ J' S2 Y+ }6 `6 a
{
"base_transaction": null,+ ~. j4 e; l$ R) I( P
"actions": [{
"account_id": "0KTCS3R5G0A02",
"amount": 10000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "spend_account"% c; V, e4 } }/ u5 e* T! ^+ K2 W# ~
}, {
"account_id": "0KTCS3R5G0A02",+ O6 _' j1 Z: B4 z' k, Q8 I
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"type": "spend_account"
}, {4 t/ }. i3 b& ?
"account_id": "0KTCS3R5G0A02",8 \6 J* }& f" Q% m( X
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",7 R: q- Z; O: [$ p% s" }: z g
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",
"type": "retire"
}],4 u; [6 _. G$ r7 S! y0 w
"ttl": 0,
"time_range": 1521625823! e) j8 L) V- x2 I$ e3 d6 z8 X
}
请求sign-transaction接口:0 ]" P6 [4 I9 j- T% U
% d# j1 O% t4 Z
请求参数:
{
"password": "huangxinglong123",% u: z; _' C. \* Z5 x; [6 ]
"transaction": {
"allow_additional_actions": false, @+ ]- ]7 F! i# \7 t8 A# j
"local": true,
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
"signing_instructions": [{
"position": 0,
"witness_components": [{& X; s# |! Y4 [( t8 O" L
"keys": [{ O6 Q7 |& |0 r" B" x
"derivation_path": [6 [, Y* D& b. o4 }0 Y
"0000002c",
"00000099"," i) A8 U: d7 c6 C- ]3 z, R! P A
"0100000000000000",1 T( D" b' S6 ^' C; `
"0100000000000000",/ E* c p, i" ]
"4600000000000000"/ n& q" Q/ W- A; |
],6 O, Z- F2 W& X4 x: Z) {9 _/ p) E
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,4 F, w H( [5 p$ [
"signatures": null,6 K! k- D- s1 V" h
"type": "raw_tx_signature"
}, {
"type": "data",7 ^& P/ j) k: }+ _. n, b. \
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"4 [" v. ?. d N" I& B
}]
}, {7 \* i r) X9 @
"position": 1,
"witness_components": [{
"keys": [{- @- X1 |, x+ {) Q
"derivation_path": [
"0000002c",. e" c2 ~ o& Q
"00000099",$ z5 |2 Q! t; Y1 b4 S& g
"0100000000000000",0 i! `" u4 C: c& a
"0100000000000000"," A% o# w$ B$ n1 }
"4700000000000000"8 f, A/ p( G, M
],8 D6 l# I v& A8 C5 t: o
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"; M; a6 X, \6 v
}],. U# N6 c9 ^5 B f
"quorum": 1,- N% C: {" _, @7 f" y0 `- g5 {
"signatures": null,. s! @4 G$ d8 @/ y) N" v
"type": "raw_tx_signature"
}, {0 P# S2 i o) w/ @4 {
"type": "data",* s1 |" F( ]- C$ L+ j7 h
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"# q2 C9 G L8 `6 y9 P) G
}]7 {1 E f9 \- ~
}]( h, W% d' y B, [
}( e% v& J0 g" s1 a+ c: p
}2 S0 G) v9 w5 g: p' \ }1 P
请求submit-transaction接口:
; z$ P U' ]7 @
请求参数: k9 F: A2 e( j# V4 u: {
{
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
}
响应参数:2 Z6 `5 {+ z3 S: M" h' B4 o
{( s1 l2 X( R! Q" f. ?- s: a P/ P
"status": "success",
"data": { t2 d, Q, f" J+ p9 z1 d$ x; J
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"
}
}- G; l6 m/ x* ?/ a! A" T; |8 o3 D
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据
* M) w; y- m$ ^9 k1 A6 W
参考代码:- X% K. n2 ^8 L+ c
package main( ~% M5 \/ W( Q; S1 V8 m3 N
import (# o: z9 H8 N" E- F
"bytes"
"encoding/json"
"fmt"1 R' L0 V( e! U* t& |
"io/ioutil"9 G* m5 Z6 }( H4 i- r; [
"net/http"
)
//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type BytomAccount struct {: E1 v! g8 W# n% d z, s# X) e
AccountId string `json:"account_id"`
Amount int `json:"amount"`8 ~& z5 O7 E, b/ [" Q
AssetId string `json:"asset_id"`' I( g8 V p7 ^! r, O( z( T5 K
//Arbitrary string `json:"arbitrary"`6 I1 @; i! r5 X1 A$ D
Type string `json:"type"`, I3 t! U3 |8 g- ~. o
}# h8 _+ n, |& B" j$ v
type BytomAccount1 struct {# X2 t( g# O$ v; ]" j
AccountId string `json:"account_id"`+ z) K/ e* [* I8 g$ }; M( x6 i+ j, L
Amount int `json:"amount"`$ g2 c8 r1 n" X6 w$ G9 X
AssetId string `json:"asset_id"`
Arbitrary string `json:"arbitrary"`
Type string `json:"type"`+ ~7 K# q: c! U- ^
} N+ b8 a2 \% U3 x: ]$ }" R
type BaseTransaction struct{}9 M, O& f. x6 R+ V0 o
type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`- Z: i1 B% D* A
Actions []interface{} `json:"actions"`3 c' m! S9 W8 e0 n
Ttl int `json:"ttl"`: X. e6 c7 x9 l' k$ Z" t- r
TimeRange int `json:"time_range"`
}
//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction% k( f: G. T9 i1 D1 T4 F+ N
type Transaction struct {
}& k x" E' I- |' p `& k
type SignParams struct {3 Z. q& r8 p: m% n7 U
Password string `json:"password"`
Transaction Transaction `json:"transaction"`
}
//submit-transaction: w2 L0 y! v+ H6 V6 V0 F
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction. i1 _( O3 \0 B$ C- i
type SubmitParams struct {
RawTransaction string `json:"raw_transaction"`
}9 g3 B7 T+ R9 T/ [; W" y
type SubmitResponse struct {8 }: B- y, F: }
TxId string `json:"tx_id"`
}0 [5 v5 I, T q, _" ^6 M# O8 L5 i l
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}) V) m4 H9 J( v s1 Y
account1.AccountId = "0KTCS3R5G0A02"
account1.Amount = 100000004 b) Y6 }5 \) e, }( ^7 ?9 |2 ?
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"' S" q, \4 q1 e5 n9 k
account2.AccountId = "0KTCS3R5G0A02"
account2.Amount = 100
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"
account3.AccountId = "0KTCS3R5G0A02". H% a$ ~5 o% S
account3.Amount = 1008 m* W. \: R( u8 p
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"5 \6 b3 t4 _6 Q6 Z% C n
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"- n/ y) A9 }6 } z2 z3 P* s
account3.Type = "retire"
//var array
var actions []interface{}
//append three params
array_actions := append(actions, account1, account2, account3)- E8 T) x1 o s! P
transaction_params := &TransactionParams{}4 _& J: c6 O/ t+ K0 S+ C
transaction_params.Actions = array_actions
transaction_params.Ttl = 0+ O4 a1 w/ W; C# {, Q
transaction_params.TimeRange = 1521625823
//本地测试网节点
//build-transaction
port := "http://127.0.0.1:9888/build-transaction"
value, err := SendTransactionRetire(transaction_params, port)
if err != nil {
fmt.Println("err:", err)
}# a {( S' p, I3 w
fmt.Println("build-transaction接口返回的参数:", value)
//sign-transaction9 [7 q1 m4 l$ F. r# i" X8 U: m) L
//...........# o! {$ }- l- s: C) P6 {1 v# ?
//submit-transaction, q5 y! e5 Y. z2 v" B% _
//.........../ \& ]3 h7 [; \0 [6 P x
}7 o7 j# b5 N5 P- `! t$ m
//send post request8 J/ a& n: @' }( a8 S' \, j/ }
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接
ParamsStr, err := json.Marshal(params). a; t! Y) M9 H7 c, C+ m9 T1 F4 s
if err != nil {
return nil, err
}4 N( u( o4 h8 A( v: H. I1 ~
jsonStr := bytes.NewBuffer(ParamsStr)4 B8 M! }3 ~7 H2 S3 l0 X
fmt.Println(jsonStr)& R- @2 l: ~# U. t J
req, err := http.NewRequest("POST", port, jsonStr)
req.Header.Set("Content-Type", "application/json")$ c% ~3 i2 i$ x4 I1 r
req.Header.Add("Accept", "application/json")4 ^, Y9 C) l0 }9 @% S
client := &http.Client{}
resp, err := client.Do(req)8 Q" ~2 g* [* _2 W# n) k
if err != nil {
panic(err)/ y4 A) V: A4 C* z6 S) s
}
defer resp.Body.Close()
var bodyBytes []byte
if resp.StatusCode == 200 {9 k1 N1 j7 f( o3 D3 y; ~# X
bodyBytes, err = ioutil.ReadAll(resp.Body)8 {) v1 \2 H7 l0 z
if err != nil {
return nil, err
}3 Y5 u- c* g. n$ R' P) a! D, v2 M
}
return string(bodyBytes), nil, Z/ n+ g% n1 F: V
}# w1 `/ t6 G% J4 {3 g" `( m5 t
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。