那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。 A, O5 F" q/ {% |0 f/ S7 L. a
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。) I( r/ X4 r$ U" d/ x ?& S
http://test.blockmeta.com/faucet.php1 [& u1 t* C4 J" |4 t
请求build-transaction接口:
请求参数:. {$ E+ G+ w& D/ p/ }; p% g
{
"base_transaction": null,
"actions": [{
"account_id": "0KTCS3R5G0A02",
"amount": 10000000,5 X6 K4 q( A8 p# i9 N Q
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "spend_account"
}, {
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",2 m7 F2 q+ _9 h5 P2 o$ T% P# g2 l
"type": "spend_account"# d6 `/ K9 l7 Q7 j x5 j4 t+ `% G
}, {
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",, E0 W- C6 |" o: F8 }
"type": "retire"3 ~) m, ^$ n# p. q9 j, Q2 g
}],
"ttl": 0,' `; z1 l7 Z; l
"time_range": 1521625823
}
请求sign-transaction接口:
请求参数:
{
"password": "huangxinglong123",
"transaction": {
"allow_additional_actions": false,
"local": true,. O, S; x7 ?3 R. |0 O$ e C( z2 K) ~
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
"signing_instructions": [{0 c0 Y' R7 I u* I% [
"position": 0,2 y& V6 e9 N* a8 V7 E
"witness_components": [{
"keys": [{, v4 Q( e3 L8 L8 Y' z; j0 {3 m: U
"derivation_path": [
"0000002c",5 S( ? C4 J4 e+ s- t; z
"00000099",
"0100000000000000",
"0100000000000000",
"4600000000000000". ]/ i' Z/ T9 S1 t
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,0 E9 \. l7 h! G3 f- {: A# ~9 ?* O
"signatures": null,
"type": "raw_tx_signature"
}, {/ j% C& c& K' ^
"type": "data",5 f7 M: c, c0 t S4 G, s
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a": v& }! u0 ^1 I$ _
}]0 w O7 t0 V/ [0 B+ J7 C
}, {( l' u' g) E% s1 O% e
"position": 1, a. T4 K% ^1 M% I% \* Z
"witness_components": [{
"keys": [{
"derivation_path": [
"0000002c",
"00000099"," `) ~5 X* W* h( q0 {: S; ~% d
"0100000000000000",
"0100000000000000",
"4700000000000000"5 A3 f6 @- ] ~) I5 }! J, \* W
],# a# v6 t/ T2 q$ x% d$ j! O8 B
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,; K2 p5 q+ F6 N0 J! m7 q
"signatures": null,; w& j. ]8 P; A D7 X
"type": "raw_tx_signature"; \* k. c+ t i$ }5 q
}, {
"type": "data",+ f1 I% X% u. U: P D4 A% B
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"
}]9 ?& z8 K0 ~* c6 E: d
}]7 b1 M2 X" F2 F d& X7 n7 i
}
}4 H5 H4 r3 G- F& R+ b
请求submit-transaction接口:
9 S0 I: K+ j. I! a+ z
请求参数:
{" h$ O/ u7 _5 a0 q
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"9 x1 }3 w5 r: u: V! s8 `
}* {) A" p+ T0 ^0 m5 O6 N
响应参数:
{
"status": "success",
"data": {! v$ t% J: M! _2 G/ s1 e
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"
}. T( s# x/ A2 u8 x/ t
}- l9 h4 |4 n# B, \0 J: S) o R
( l' e* l9 Y9 S5 Y- t9 P( I* H3 Y
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据
" K6 ~# ^$ Y% k, h/ t- E7 y
参考代码:* T: s0 Q, w: F) }- J% i5 `
package main) K/ I( ^( Z1 N0 [
import (
"bytes"7 A' C) H; i9 e6 W8 y! r; b
"encoding/json"
"fmt"
"io/ioutil"' s3 J% w8 M. e8 [+ t9 Z
"net/http"$ ^0 j, S8 |& a$ `" S/ o5 \
)
//build-transaction params" w8 S, Y, A& T% g' V/ }% t q6 z
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction# [" \4 l9 N E% I
type BytomAccount struct {0 H' p* \/ B+ e4 a! t. {
AccountId string `json:"account_id"`- A8 b" X/ I2 \; Q- h
Amount int `json:"amount"`
AssetId string `json:"asset_id"`% D/ @ T3 u5 r' z. C
//Arbitrary string `json:"arbitrary"`5 t5 m7 _! h# H$ C
Type string `json:"type"`# W; s% a4 D$ h- M! \4 D9 V1 x7 ]
}! t) s' X; z) j6 v
type BytomAccount1 struct {
AccountId string `json:"account_id"`" ]5 ?4 c f _9 A3 {6 {
Amount int `json:"amount"`1 c2 ~0 @6 x5 S: Z8 g& p
AssetId string `json:"asset_id"`- N% L4 M- T# [& H
Arbitrary string `json:"arbitrary"`
Type string `json:"type"`+ G( F5 }* t( H5 x7 X" {- B
}
type BaseTransaction struct{}1 v; }, _2 U; q' k5 e" L, B
type TransactionParams struct {- x/ i/ j2 u2 ^8 Z
BaseTransaction *BaseTransaction `json:"base_transaction"`7 A) j& I" ~; F/ g! G
Actions []interface{} `json:"actions"`7 _/ E* N/ k# E- P3 o
Ttl int `json:"ttl"`4 c" G" I0 H% \' v1 ]! b* @ N
TimeRange int `json:"time_range"`
}' z7 n5 a0 s3 x1 ]8 |- G+ ~5 J
//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction4 r: Y7 v- m! U. ]' _" ]+ G
type Transaction struct {
}6 h5 B# V, Y. B, t5 D# Y
type SignParams struct {7 S9 v% o0 k) e% z' o
Password string `json:"password"`
Transaction Transaction `json:"transaction"`
}/ u- C1 ~ a, [( T1 R
//submit-transaction
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction7 _2 \8 d0 \4 a! U
type SubmitParams struct {
RawTransaction string `json:"raw_transaction"`
}: W, i" K$ e* \; G' R" S: b
type SubmitResponse struct {
TxId string `json:"tx_id"`& i' H1 `) h K9 `$ i4 y9 C4 L
}# Y& n% ^1 c; Z1 b/ D0 O, L
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}: N9 s7 n% x/ s y/ b4 w
account1.AccountId = "0KTCS3R5G0A02"; \0 x3 @9 n( ?: H! |
account1.Amount = 10000000
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"" @- O% J) E4 l6 x% }; \# f
account1.Type = "spend_account"
account2.AccountId = "0KTCS3R5G0A02"
account2.Amount = 100$ X% _0 c( q. A1 L& l4 B+ y
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"
account3.AccountId = "0KTCS3R5G0A02"$ S8 j* W8 j. ^5 @6 Z$ \* x
account3.Amount = 100
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"# Z* A0 ]. ]/ x3 W" ~ d/ J
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"# I$ l( y/ W/ P% ?9 k
account3.Type = "retire"8 b% |8 t7 z% j+ y$ A
//var array
var actions []interface{}
//append three params8 s+ ]* |8 t" j9 O& M
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{}* {; x: |$ X9 @) I( ^( G
transaction_params.Actions = array_actions7 ~8 ^: S" j2 w9 Y3 j. {
transaction_params.Ttl = 0
transaction_params.TimeRange = 1521625823
//本地测试网节点
//build-transaction+ A8 [: X: M% h0 L6 Q- V; e3 E
port := "http://127.0.0.1:9888/build-transaction"2 P0 X% i; K' L6 z5 T: p3 P" K% R3 y g
value, err := SendTransactionRetire(transaction_params, port)
if err != nil {0 _' G, Z# e+ N& k4 v3 A
fmt.Println("err:", err)
}
fmt.Println("build-transaction接口返回的参数:", value)
//sign-transaction$ Y. [; Q5 W9 W/ x. i
//...........
//submit-transaction+ e' m$ K( O9 d: O4 i6 G) b
//...........4 y" a, |! k% N& |# K
}
//send post request
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接
ParamsStr, err := json.Marshal(params)
if err != nil {
return nil, err: ^ J# ?0 d8 ~- b- Q2 M( F: `) h
}
jsonStr := bytes.NewBuffer(ParamsStr)
fmt.Println(jsonStr)
req, err := http.NewRequest("POST", port, jsonStr)9 A, @% @% H" B4 C
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
client := &http.Client{}4 m; `$ z% L x0 t4 W4 }! \
resp, err := client.Do(req) L" t# M$ I: G- Z7 A# D( ?
if err != nil {' \* x& f2 f, i: a) B4 h* q
panic(err). ^$ ]7 \# B. Y- l# D2 R
}( [% X5 P3 _1 t; L. \( n8 m0 C4 C! _ Y( Y
defer resp.Body.Close()1 @6 l; i2 i1 z# ?0 { U
var bodyBytes []byte
if resp.StatusCode == 200 {1 F& P& t% }( @/ @8 E, M* R/ {1 e
bodyBytes, err = ioutil.ReadAll(resp.Body)% E! d2 ]. j; h! o& |
if err != nil {6 n/ r0 o+ {) W- i0 k1 C0 r! t
return nil, err% p( t5 W9 K- m) \: ` b( X
}9 p$ y4 L2 ^: B% D% u5 Q# w f
}
return string(bodyBytes), nil# p" ^$ y. F3 l" J {
}+ o& v1 T8 Y* h& z# I
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。+ c/ X2 j% E7 O% V/ ?8 R
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。