那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。0 ?! j5 Q$ {1 z1 ^! }' i3 [: D
http://test.blockmeta.com/faucet.php5 t6 J' S6 W2 L% R" H. g- g
请求build-transaction接口:6 j o" b$ n0 r% g
请求参数:
{
"base_transaction": null,! u0 }! j6 L1 J l! B7 h" Y
"actions": [{ o* A( ?/ P9 m8 N, }* W
"account_id": "0KTCS3R5G0A02",. ~/ U# G1 ~$ ]# X% k' j
"amount": 10000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",6 }0 A0 s* Z. b l1 q3 N
"type": "spend_account"
}, {3 K/ S* h8 z0 A: v1 j& Q1 K# L4 I
"account_id": "0KTCS3R5G0A02",1 v8 l6 M; P% X( ~
"amount": 100,3 q0 D- e! ?8 I$ v7 B/ i
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"type": "spend_account"7 P: a5 A4 i( ?* I% `) C1 {
}, {
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",( K9 s) F) R/ X4 u" a/ Q( M
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",6 _. J \- @. o8 C. \* C! O5 ^" w+ Q
"type": "retire"
}],- B2 h$ @" f% v
"ttl": 0,8 u2 K9 o9 _& x0 N* y/ K+ Z
"time_range": 1521625823/ _& n5 x" o% ]0 i" F1 P
}
请求sign-transaction接口:
请求参数:
{/ T- L5 A; I2 B6 @6 E5 ?; r
"password": "huangxinglong123", p" m+ {/ i6 `+ c0 H* e7 M* e
"transaction": {6 [" q: O- |- j
"allow_additional_actions": false,/ R: u3 t0 m$ c; E) D6 E; f! F- F: |
"local": true,
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
"signing_instructions": [{
"position": 0,2 K" K3 Y/ `6 m$ b7 v/ @
"witness_components": [{
"keys": [{
"derivation_path": [
"0000002c",
"00000099"," v% o) @7 o' e; P8 d2 s9 {7 Z
"0100000000000000",
"0100000000000000", B/ D" N) P) y0 i
"4600000000000000"
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,
"signatures": null,
"type": "raw_tx_signature"
}, {
"type": "data",& H2 K2 R6 ]7 x& p" t5 d
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"/ J% r o9 B0 f
}]
}, {& e6 p0 W; C7 O1 {/ e# a# E% M0 V1 e
"position": 1,0 K$ {" B2 L8 U' j8 w
"witness_components": [{
"keys": [{
"derivation_path": [9 f# D6 P7 Q% x$ x' |2 O+ J
"0000002c",
"00000099",
"0100000000000000",
"0100000000000000", }2 C& X1 r9 D! [& P) X
"4700000000000000"
],# T1 Y$ Y7 \. c# F
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,% U ~+ Y, j6 ^' Z2 H
"signatures": null,
"type": "raw_tx_signature"5 `% g* D# A& N+ t( i
}, {
"type": "data",
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"
}]/ W2 [# F6 E$ S6 m7 ~
}]: Z% r1 [3 r' h% x; h8 Q
}
}4 ~/ y7 I+ T, p5 h J
请求submit-transaction接口:, }9 }. {3 K2 t( o: x3 |7 W
* o% ^: Q- G7 z, L
请求参数:# }- }7 ?" g0 ~7 s
{4 A. r( s4 d! f0 V$ i$ S7 D
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
}: i& T0 V7 Z$ N6 t& ?& l5 J. M
响应参数:
{7 U4 J( _6 j* S% M7 _
"status": "success",! {+ U1 t( k( r
"data": {2 E8 T# W+ ^9 u" K2 }+ x. l
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"7 ^6 F8 M; Y) p* \8 |
}
}. Z' m5 L( h# r
: ]# r2 Q2 R& s A6 X# t1 S
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据. c7 V: h; q/ a! M8 ^8 m
2 V' s/ m6 M' j1 @7 b
参考代码:
package main
import (! i' t% H' `: J5 F, t% |
"bytes", q$ O% b1 o% I
"encoding/json"
"fmt"# k, L/ Y) v! T1 G$ m! A' t! R" M
"io/ioutil"
"net/http"
)
//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type BytomAccount struct {
AccountId string `json:"account_id"`
Amount int `json:"amount"`; l# U' z/ i( l) K: J
AssetId string `json:"asset_id"`
//Arbitrary string `json:"arbitrary"`
Type string `json:"type"`. f, A' H0 l6 W+ G+ q- F' E
}; ~9 X. F6 q: W: g7 G
type BytomAccount1 struct {
AccountId string `json:"account_id"`
Amount int `json:"amount"`
AssetId string `json:"asset_id"`2 H$ P- i3 _) O x
Arbitrary string `json:"arbitrary"`
Type string `json:"type"`2 U5 x. B0 u9 l3 b' D' T4 }4 I
}0 Y$ B7 f4 ~: S; J+ Y/ n
type BaseTransaction struct{}2 K _' g* |: K4 ~# A8 J
type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`$ C; D$ ^7 x$ K2 X* \
Actions []interface{} `json:"actions"`# B. ]3 f8 V. O `
Ttl int `json:"ttl"`5 n0 h. Z5 R+ _ B
TimeRange int `json:"time_range"`- m/ G9 A4 d# g
}' I c g& Q# W8 `
//sign-transaction params- I. e6 M0 z5 j! {
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction" @4 S" l2 N1 h1 G
type Transaction struct {' q- ~+ M. ^+ K% q
}
type SignParams struct {
Password string `json:"password"`2 b) a4 R I4 E1 ^$ A5 P0 X, y
Transaction Transaction `json:"transaction"`
}! O1 {* R1 y3 O1 ~" Y+ t+ g
//submit-transaction/ t' W4 u5 a* E7 e
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction7 j: c' N9 C9 V9 J0 Y8 L/ M" d
type SubmitParams struct {
RawTransaction string `json:"raw_transaction"`
}" `, N% `" U4 h/ w: X$ B6 L* {! C( q
type SubmitResponse struct {
TxId string `json:"tx_id"`
}( M" r2 Z2 ^& X
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}2 J" |! t$ N7 p6 `
account1.AccountId = "0KTCS3R5G0A02"
account1.Amount = 10000000 {' y9 H8 u$ T" f6 G
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"
account2.AccountId = "0KTCS3R5G0A02"
account2.Amount = 1004 |; x& j. a: J5 ^( a
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"0 H% H8 B0 J) W
account2.Type = "spend_account", W& b0 [" ?/ F& [8 `
account3.AccountId = "0KTCS3R5G0A02"; Y2 z% g* M* B" e
account3.Amount = 100
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"4 m9 O) P. W1 _7 {
account3.Type = "retire"
//var array7 D1 ~# e$ S# \6 w) ?
var actions []interface{}/ U& t$ s( ?, P# R R! [
//append three params( a2 y' N2 L7 `0 [0 o
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{}, Z7 J. i# W7 q' Z- e6 _
transaction_params.Actions = array_actions
transaction_params.Ttl = 0
transaction_params.TimeRange = 1521625823
//本地测试网节点
//build-transaction
port := "http://127.0.0.1:9888/build-transaction"6 G! l1 V9 p7 U% q
value, err := SendTransactionRetire(transaction_params, port)( x" n; s" {' f7 @. J
if err != nil {
fmt.Println("err:", err)
}7 j( h" U O7 M& q
fmt.Println("build-transaction接口返回的参数:", value)
//sign-transaction
//...........
//submit-transaction
//...........2 ^& }# |4 s" S+ Y
}
//send post request9 F# X1 m+ \6 }- o, n
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {4 v5 w% D q% b$ R; P
//以本地测试网节点连接$ T5 ]. K& V; k* {* o
ParamsStr, err := json.Marshal(params)+ m! ~0 V* x2 A0 I* n8 r
if err != nil {4 w1 l O: x- x* Y* O
return nil, err' r! @' \6 R: s0 Q. s& e, x: `
}# S! o: I; t$ q" [$ O
jsonStr := bytes.NewBuffer(ParamsStr)8 |) c& M- v9 c1 C2 s1 N% z6 S
fmt.Println(jsonStr)
req, err := http.NewRequest("POST", port, jsonStr)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
client := &http.Client{}8 g3 p" ^& ~, p7 o' ^9 K
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()& U( _2 i$ q9 b& J
var bodyBytes []byte
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {" b. N. h" Z1 e$ T
return nil, err1 @+ v V) N3 r
}
}
return string(bodyBytes), nil( r; K! ?: V6 m! O
}
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。