那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。. c( H3 K+ Y# I& `0 t* E
http://test.blockmeta.com/faucet.php, t# \) _2 n+ a& B
请求build-transaction接口:% \8 S3 M1 u* B3 [3 H# v- ~
请求参数: A" p+ T' r) k- z
{; i1 F. \: K6 [- Y {( t
"base_transaction": null,
"actions": [{9 T9 M7 Y# `" O/ t. |% ]
"account_id": "0KTCS3R5G0A02",) n+ N* V) O! `( s. D( J
"amount": 10000000," K/ H& `6 a! X* s1 r
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",+ k8 \+ I' o& G3 O' v8 J! U m
"type": "spend_account"( b* {2 j8 ], E9 F( v8 T
}, {5 I9 b- ]9 u3 b+ R0 ~
"account_id": "0KTCS3R5G0A02",
"amount": 100,; e3 g3 ~4 r( a+ J! J2 E1 f$ |
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"type": "spend_account"
}, {7 O" C: U# q* Y& B z$ ?* V* y0 i
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",
"type": "retire"3 U6 ?( L3 R$ r( m: A4 m0 h
}],
"ttl": 0,3 P" m7 J3 u; l2 |, G
"time_range": 1521625823
}
请求sign-transaction接口:4 c- d7 x4 I2 G* s
4 { |% c E4 _; R2 w
请求参数:) h0 C% \+ {. P+ f+ Y9 }
{/ I: G9 W% e* f7 g1 Z) }8 l, R
"password": "huangxinglong123",
"transaction": {: g. L! Y; I1 |0 ?
"allow_additional_actions": false,
"local": true,5 O& R+ k9 U1 J$ C" E
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
"signing_instructions": [{
"position": 0,
"witness_components": [{
"keys": [{
"derivation_path": [' A( \) `) D9 E) X/ q6 l3 t2 _
"0000002c",
"00000099"," D$ ^5 p( c% ~: C# Y5 s- Y
"0100000000000000",
"0100000000000000",- T0 a! e& L- |# V1 e
"4600000000000000"
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"& W% u3 j8 {' L D
}],
"quorum": 1,
"signatures": null,
"type": "raw_tx_signature"
}, {
"type": "data",
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"
}]( y7 h* f. q4 s3 }1 j
}, {
"position": 1,
"witness_components": [{
"keys": [{
"derivation_path": [
"0000002c",8 k$ b _5 O& f" n' K( K, @' I% t
"00000099",
"0100000000000000",
"0100000000000000",
"4700000000000000"' |' b8 u" y5 C; q
],- r5 y2 D4 Q9 P
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"/ e. l1 Q3 i$ f; C- l
}],! n2 \; J$ ]' W# I6 V1 V
"quorum": 1,
"signatures": null,
"type": "raw_tx_signature"& m! u- O$ J# L0 E% P4 M9 d1 n2 f
}, {
"type": "data",
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"+ h/ B7 v' R2 L5 Z$ p5 j2 |
}]
}]
}# t5 e1 t3 G9 h) e/ Y+ O
}
请求submit-transaction接口:
请求参数:/ R% J* D. X9 L2 J1 T. D
{
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"; T% L# l& n& [! O$ ]
}
响应参数:
{
"status": "success",; b2 D# E- g9 S: J
"data": {6 L8 |3 n8 q5 S. q, K& C4 h* B! I/ h
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc" L# C$ b- G& j G" j
}$ E( y" E X+ {
}8 }# k5 ^/ J5 }: G) v4 m
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据
- g' }; q. T& w
参考代码:
package main
import (
"bytes"
"encoding/json"
"fmt"9 w% G" }, J8 P( Y
"io/ioutil"( P- I$ l9 B i( M* L7 q" U8 ^- Y
"net/http": I! ]: h+ m' R$ L4 t1 @1 V9 b
): }# Z5 b2 f5 B5 N3 g% P
//build-transaction params ~1 }8 B+ l! u9 l4 a
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type BytomAccount struct {6 j! p2 e, P) X9 |+ u v ]
AccountId string `json:"account_id"`: W. k( h- c1 x) v; N
Amount int `json:"amount"`
AssetId string `json:"asset_id"`6 N' W/ Q; o D* }2 t7 }
//Arbitrary string `json:"arbitrary"`
Type string `json:"type"`
}& |( f% p. Y4 K+ Q* E, x
type BytomAccount1 struct {
AccountId string `json:"account_id"`
Amount int `json:"amount"`# x* L0 p* `0 u+ t
AssetId string `json:"asset_id"`9 n# j5 E' Z9 v9 m2 b. r
Arbitrary string `json:"arbitrary"`
Type string `json:"type"`) U# z% x, n2 z- D' q
}
type BaseTransaction struct{}& } n, V9 z' a! F
type TransactionParams struct {/ e" N! G1 w% K! K
BaseTransaction *BaseTransaction `json:"base_transaction"`$ o$ M( z* w8 x1 c. Z
Actions []interface{} `json:"actions"`
Ttl int `json:"ttl"`" y2 O- |: H7 w* J; g: J! e6 t1 Q
TimeRange int `json:"time_range"`0 e4 Q: A a6 N7 j7 }
}& m; l+ Q7 \9 U/ d& M4 M1 ~* s6 I( \
//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction P8 B7 v: \. M
type Transaction struct {
} N. ]( l$ F! \, b& ^
type SignParams struct {
Password string `json:"password"`
Transaction Transaction `json:"transaction"`
}
//submit-transaction, _; I! q( ~+ f" N
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type SubmitParams struct {% B; T0 A" [. v5 h: r4 x8 E; k+ N
RawTransaction string `json:"raw_transaction"`
}
type SubmitResponse struct {
TxId string `json:"tx_id"`* j7 `, I4 R) A
}" |( b- F- ]: {) m5 f5 w
func main() {# s- C, F: S( s0 L. L
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}6 s8 }4 `" z+ q8 v- K8 P+ H
account1.AccountId = "0KTCS3R5G0A02"
account1.Amount = 10000000/ `0 A+ q2 `, v3 x+ @3 S
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"; H" J5 N7 f- K* h& w
account2.AccountId = "0KTCS3R5G0A02"/ O# Z5 ^; l& Z: N. d/ R8 S% S$ N3 R, d# z
account2.Amount = 100
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"3 a& G) I* g9 B. f. A9 G
account2.Type = "spend_account"
account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 100( |. E0 R8 u/ |" X- }# O
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"
account3.Type = "retire"
//var array- j! M H! `; I2 e2 H( S k
var actions []interface{}/ B- F+ H0 v5 n3 t# S
//append three params
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{}
transaction_params.Actions = array_actions
transaction_params.Ttl = 0
transaction_params.TimeRange = 15216258238 _1 |3 c6 l& O7 p
//本地测试网节点 r$ i- S+ i; G/ L& B( m* O( L D
//build-transaction3 H3 U6 ?4 A8 T
port := "http://127.0.0.1:9888/build-transaction"; S, C3 x) R2 m9 o" [" d# Q
value, err := SendTransactionRetire(transaction_params, port)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("build-transaction接口返回的参数:", value)( \4 n: z$ w* Y8 [3 a7 f7 e
//sign-transaction2 U. B8 I* s! u7 S# B
//...........! f# \2 m3 `3 K$ q8 a* r5 h
//submit-transaction
//.........../ h2 w4 P2 [2 m, z' G$ H, E2 s: h
}
//send post request( M; ~( Q& P4 Q% y4 s
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {* O& H4 ~7 f: I2 N* M6 B0 _6 I% U
//以本地测试网节点连接
ParamsStr, err := json.Marshal(params)
if err != nil {
return nil, err3 @3 r U5 y1 Z( E/ G
}
jsonStr := bytes.NewBuffer(ParamsStr), ?; y# L4 f$ h1 o) X2 q0 V1 N
fmt.Println(jsonStr)2 G2 ~# m. M5 P; k T1 b* C6 o0 r
req, err := http.NewRequest("POST", port, jsonStr)
req.Header.Set("Content-Type", "application/json")# v0 c7 l" Q/ g
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)8 |! B% @& M3 [* y& c6 y& l" w
}6 |. W) {& }- ]6 {& A2 h
defer resp.Body.Close()
var bodyBytes []byte
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err$ j7 u' N7 ?; o: }1 u
}
}
return string(bodyBytes), nil/ _ L" V% w C7 J+ C
}# E! C# l- B, i, R0 a
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。4 U5 u, Q; c) I
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。