那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。5 x4 x- M1 M( ~* f5 }- w
http://test.blockmeta.com/faucet.php3 O3 T% M+ D6 i
请求build-transaction接口:# x! { u/ t; l- m; U
3 h5 r0 A1 W! k% C7 s
请求参数:
{" L, D8 q' G5 K G
"base_transaction": null,
"actions": [{
"account_id": "0KTCS3R5G0A02",+ W, z* Y7 h: J
"amount": 10000000,0 V& c% B7 p! Y& q9 r& x8 p
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "spend_account" e* x6 R' Y0 F: v+ ^0 @* L
}, {8 f" z8 [6 Q9 M, C! P0 Y
"account_id": "0KTCS3R5G0A02",6 a8 d9 r7 \4 s/ S$ ]$ f! F6 W& X( f
"amount": 100,/ x# b% a3 ~; ^, `( s; ?- |
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",. K2 s: P& k7 A' ` j3 @( }
"type": "spend_account"
}, {& H0 Q; c# f( F4 Z) _
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",2 q; {, D/ e+ `: E3 B
"type": "retire"
}],
"ttl": 0,+ G2 W, v2 a7 ? K
"time_range": 1521625823
}' o" R# m, B) t5 w& p0 u/ s
请求sign-transaction接口:
' ^, ~. a3 o/ g
请求参数:+ I3 c$ b2 q! v1 ^& e5 F1 x
{5 A/ I7 ^" e& `, n& v2 o- q0 ]* n
"password": "huangxinglong123",# {6 s1 J7 }4 W7 L" o7 I/ S* q$ @
"transaction": {: }* r4 `8 \. }# v$ y
"allow_additional_actions": false,9 n* c( n! E9 B$ E, g% ]# F1 z
"local": true,
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",' l0 X4 `- y N( q R
"signing_instructions": [{
"position": 0,% {, h3 u" h. X" s c0 q b
"witness_components": [{
"keys": [{% O8 t. J& q' Q4 Z& Y
"derivation_path": [
"0000002c",
"00000099",1 N: D/ K2 c$ G) f5 M( D& ^+ j$ u
"0100000000000000",/ P ?' |" U; O0 q8 l
"0100000000000000",
"4600000000000000"
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1," y/ X! \' R0 _
"signatures": null,
"type": "raw_tx_signature"- _( _: c" a: Z$ F8 F) x
}, {9 g. r9 y& Q0 G7 q$ K4 C" O" @
"type": "data",
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"
}]
}, {
"position": 1,
"witness_components": [{$ I2 s4 E: W9 I- ^( Y' h% ^
"keys": [{+ f4 Z- k( V3 q+ t
"derivation_path": [5 m: d7 @* `& l8 E' e8 q( n; k( s
"0000002c",( b% K' {! w* \
"00000099",
"0100000000000000",
"0100000000000000",
"4700000000000000"$ g' U* q/ Q; W, _
],3 O. W6 S/ c1 T' C, M* g
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"7 i' O, A8 U F0 t7 L
}],
"quorum": 1,9 ~( q7 u; Q0 E
"signatures": null,
"type": "raw_tx_signature"
}, {/ d; K; v9 @9 h0 C1 v) `
"type": "data",
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"
}]6 b$ b- l4 u% [
}]
}, E" C4 `; D# O4 B9 l, d
}7 a) V D: |& v% s0 u: M$ }
请求submit-transaction接口:0 o N- t9 H) g5 y
请求参数:$ H- T" x% Q9 T3 ^# S
{7 y+ r$ ~& ~4 r! j$ s5 [
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"$ r& T0 B. |: c
}
响应参数:( H" l8 Y! O, V& l' |
{9 m3 H! X$ Y% N& a; c* J* d
"status": "success",6 k: `0 E( ~# @( z. n
"data": { k1 ^* g% ?5 o4 e0 {9 d
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"5 z# ~$ x8 u; Q
}
}1 ^. K8 l' g# W) V
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据4 @9 V2 X+ g% p- B) E3 U% N; T
% Q- R2 Z# z" _, G
参考代码:
package main
import (
"bytes"
"encoding/json"8 N O) P i& e* L% R
"fmt" g' q+ C5 y; K! z- w& T
"io/ioutil") `6 x) ]7 |! U
"net/http"
)
//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type BytomAccount struct {2 t6 R/ y2 Z2 R. L+ @
AccountId string `json:"account_id"`
Amount int `json:"amount"`
AssetId string `json:"asset_id"`
//Arbitrary string `json:"arbitrary"`9 Z' L. C) D+ C' k) n7 U+ a
Type string `json:"type"`
}
type BytomAccount1 struct {2 L: i4 i, s/ T. _+ c$ u0 t2 z/ Z! q
AccountId string `json:"account_id"`
Amount int `json:"amount"`
AssetId string `json:"asset_id"`
Arbitrary string `json:"arbitrary"`
Type string `json:"type"`$ n3 i8 [7 d* {' A! C: J
}2 w% U! e9 m' }% |3 Z
type BaseTransaction struct{}
type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`- L) j7 \1 o* F
Actions []interface{} `json:"actions"`
Ttl int `json:"ttl"`
TimeRange int `json:"time_range"`/ s' n+ o t. s$ v/ e: d# @$ E
}) O8 l( N& v+ k j- w
//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type Transaction struct {
}
type SignParams struct {
Password string `json:"password"`0 G+ L1 B X. d5 K
Transaction Transaction `json:"transaction"`7 L; I% S% ]' G9 w* X. y
}7 q& d) x5 `3 a, G" Y
//submit-transaction" D! l5 E, n; z+ V
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction1 L- _; g! r3 r, z+ t" T
type SubmitParams struct {% a( |; L$ @/ L3 h9 A- t1 G
RawTransaction string `json:"raw_transaction"`
}
type SubmitResponse struct {+ n5 v6 I/ c# @
TxId string `json:"tx_id"`
}: B+ ~! h% t% U
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}% T6 J) G* U! b# X0 o8 G
account1.AccountId = "0KTCS3R5G0A02"0 r, ^ l, W- s9 v
account1.Amount = 10000000
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"- R5 ]' _% A1 s H& Z$ W
account2.AccountId = "0KTCS3R5G0A02"0 V; {6 D$ J: |; i% q
account2.Amount = 1003 ~/ R2 t& }! {- O! h
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"
account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 100* D) t7 W. s9 I; }8 h- `% Y! k
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c") x# B! M, u0 O h- H$ c, w
account3.Type = "retire"
//var array
var actions []interface{}$ K4 G, R) G* C3 g& s( t1 H E
//append three params
array_actions := append(actions, account1, account2, account3), O8 \1 e+ M# V" |/ k* ~$ R
transaction_params := &TransactionParams{}* q# T. @4 N+ h5 u% U: m
transaction_params.Actions = array_actions
transaction_params.Ttl = 0$ A/ G( Q) R7 c) y% j
transaction_params.TimeRange = 15216258232 v, X% C& F. s' y
//本地测试网节点
//build-transaction
port := "http://127.0.0.1:9888/build-transaction"0 b2 w* v! S( w
value, err := SendTransactionRetire(transaction_params, port), u& D! R1 k1 m
if err != nil {/ l* L' P. L6 L' F, y
fmt.Println("err:", err)$ Y8 k% s0 B, i0 S3 x( M6 G
}% e7 j1 m' r' H( i6 c% G1 E
fmt.Println("build-transaction接口返回的参数:", value)
//sign-transaction
//...........! Y3 ?' F) M# l# g
//submit-transaction; r, W$ m- P- x. u$ _- T# n
//...........
}
//send post request
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接2 a2 X+ @9 |, r$ m
ParamsStr, err := json.Marshal(params)
if err != nil {+ q5 X, y5 `& h3 P( t& s- W
return nil, err
}2 W+ \9 G2 y2 o
jsonStr := bytes.NewBuffer(ParamsStr)
fmt.Println(jsonStr)
req, err := http.NewRequest("POST", port, jsonStr)9 ?/ r) ^# H6 R6 D' S, [
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")1 z+ |" u% C" x+ t1 _5 a
client := &http.Client{}7 }8 Z3 K' U, ~' {( y! V
resp, err := client.Do(req)
if err != nil {# R# t& s4 W8 C( _+ Z4 q
panic(err)
}, u9 B+ r4 ?6 [* G$ x3 t9 `- G
defer resp.Body.Close()
var bodyBytes []byte. i+ y' P4 V2 W# s; M' x/ v" |. w
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err1 _" l- @* P& Y3 X: ^9 ]0 U3 q
}
}
return string(bodyBytes), nil) Y0 [3 W; W+ Z4 X* m, X* d
}6 m' `* q; b0 _" u7 _3 _
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。/ ^$ a4 ?) `& W9 a& N
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。