那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。* V9 K7 b% i# u5 p9 a
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。' c9 l3 Y/ w( A; |
http://test.blockmeta.com/faucet.php3 I2 n5 h; _: F! f( x4 y. ?9 u* ]1 _
请求build-transaction接口:1 b: ]% W2 ~( F, M
请求参数:1 p3 \( K9 g6 q' B {; }6 p7 k
{) l% Z, J1 |7 o# o: L" G
"base_transaction": null,
"actions": [{
"account_id": "0KTCS3R5G0A02", Q1 x }% N: o+ E7 h/ e
"amount": 10000000,% P! v; ]% _. `' ~' O' v
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",' Q% h" T- @; e& M; `
"type": "spend_account"9 G3 t! u0 j6 A" M. X
}, {
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",5 z( Z/ V. W5 R% N6 {
"type": "spend_account"
}, {! S) s' V- n5 _; x! P
"account_id": "0KTCS3R5G0A02",0 v7 u6 z, ~. x; j9 _
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",5 A, ? i9 _* a8 ?) J2 ^
"type": "retire"
}],
"ttl": 0,2 [, O( P" w- @5 @) F" n$ _& |
"time_range": 1521625823- y# E! ^ v9 M8 L) b9 k, H' D
}
请求sign-transaction接口:: R% P8 d3 O, T4 k# Z" {6 F6 p
请求参数:) \% O, r1 S5 {4 |" h% W3 E
{0 r% R; a! q( ~: a" r7 w/ r4 B7 ?6 ]
"password": "huangxinglong123",8 O; O( x! R @) j2 w! ^, e
"transaction": {
"allow_additional_actions": false,+ o$ k0 f( k* ]5 r% d; b
"local": true,' _. b9 {" T2 @) p' t
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",7 `' [& N2 V% ]' P; |) B
"signing_instructions": [{6 l3 Q7 w+ p, I9 n. `8 R
"position": 0,+ P$ c: i5 F1 G, O
"witness_components": [{
"keys": [{' c: }+ L/ i2 N& Y I
"derivation_path": [: H3 Z6 c8 C/ J* K
"0000002c",! U9 y7 ]7 e5 Q! _0 K
"00000099",
"0100000000000000",, R' w/ k/ ?" I* D
"0100000000000000",- w6 ?$ l O4 W) y$ L7 J$ A/ `
"4600000000000000"
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f": `" {, h% Z" M7 x& S8 G8 N
}],
"quorum": 1,
"signatures": null,
"type": "raw_tx_signature"/ l) ?9 {+ P5 A" A
}, {
"type": "data",
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"
}]
}, {
"position": 1,- N5 a4 u( Q) [
"witness_components": [{
"keys": [{. q! t: E1 [/ |- p: O+ ~5 {2 }& t0 C
"derivation_path": [, a3 ? ^0 s9 K8 c+ |( l' g
"0000002c",
"00000099",
"0100000000000000",
"0100000000000000",3 x" N; Z6 r+ D2 Z4 P
"4700000000000000"
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,
"signatures": null,9 ?7 S- c) `: i. o
"type": "raw_tx_signature"1 N% K8 P+ ^, N F% w2 s! [
}, {' h) S$ Q! F. i) x, b
"type": "data",
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa" m, S# w( @ q% T/ W
}]( X6 w' F% |* n V
}]
}
}4 p" F3 r' B5 _7 I5 Y4 p% e
请求submit-transaction接口:1 [- w) |, t( Y, Z/ _8 d/ b* {
( C, S0 b. z& j6 A
请求参数:
{/ N, ?+ x4 [9 ~ [0 ^4 y8 q/ @0 P5 S
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00" [2 y* s6 P# s" I! r9 ?
}
响应参数:
{
"status": "success",
"data": {
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"% p8 M- R' R7 _
}
}
/ ?5 E2 Y$ [' p6 T
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据+ Z! m; ~ p# k
参考代码:; ~. L& |( } T4 @% o H
package main
import (5 S8 J* |+ A) g6 ^; O+ m$ l! F
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction* k- V4 b9 ~- u+ i+ X" J) f% C
type BytomAccount struct {& A" w/ W/ g: S+ x
AccountId string `json:"account_id"`
Amount int `json:"amount"`% y4 \4 z" i& p$ c* E6 c
AssetId string `json:"asset_id"`% h% Q S6 _5 ? F9 ]3 z! B! a
//Arbitrary string `json:"arbitrary"`
Type string `json:"type"`
}
type BytomAccount1 struct {. o5 f8 g' N' j
AccountId string `json:"account_id"`" J3 K' R3 ?. ~1 W+ @0 P* x
Amount int `json:"amount"`
AssetId string `json:"asset_id"`
Arbitrary string `json:"arbitrary"`
Type string `json:"type"`
}
type BaseTransaction struct{}& U [2 I! o' X* Q: f- R! \2 e
type TransactionParams struct {; {4 W l; j9 n) o$ {
BaseTransaction *BaseTransaction `json:"base_transaction"`
Actions []interface{} `json:"actions"`# W+ z3 `4 f, S6 b
Ttl int `json:"ttl"`
TimeRange int `json:"time_range"`0 U$ j& ~9 G; r0 D
}
//sign-transaction params5 P4 ?# F- E g. U2 t d( c/ | S
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction, J; r! u$ |: r7 x8 x7 B
type Transaction struct {/ k1 Z, n4 Y2 ^ S2 O) P. S
}
type SignParams struct {
Password string `json:"password"`
Transaction Transaction `json:"transaction"`) u7 ~4 a5 Q( p/ R# p0 w: I
}
//submit-transaction
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type SubmitParams struct { [. A+ G) \' T8 U
RawTransaction string `json:"raw_transaction"`! ~1 H: O6 Z' t- A
}
type SubmitResponse struct {
TxId string `json:"tx_id"`
}9 E# x' \9 y5 k" h" V
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}( G! m% s8 l! _% a: _3 ]
account1.AccountId = "0KTCS3R5G0A02"
account1.Amount = 100000008 k5 j9 i# ?, A9 ?5 c3 c
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"" ~9 L1 N( M4 I0 a# g0 Z
account1.Type = "spend_account"0 R5 e. @ W5 e+ x1 ?" \
account2.AccountId = "0KTCS3R5G0A02"1 r9 F3 q* V9 Z- Y% i# ?: f
account2.Amount = 100
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"4 i, |, v, }; v, E R
account2.Type = "spend_account"- l# H9 H" i# D
account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 100& u2 l8 o( j% l' t% n
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"! q4 M/ b) [# Z4 N8 t6 o
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c") K3 @0 j7 N9 o: p5 K
account3.Type = "retire"5 Z$ d! E% K0 e, W0 m! n1 N8 S
//var array: j; J/ {" _ \, l4 Q( M, j) i
var actions []interface{}
//append three params
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{}' @) Y( B h1 L# w* [# Q
transaction_params.Actions = array_actions
transaction_params.Ttl = 00 f l1 X6 l1 U; Z9 \# Z( _' P
transaction_params.TimeRange = 15216258234 q* j- K( ] m; l: L1 \/ X
//本地测试网节点0 X u- @ J3 ?( [: c4 K6 J
//build-transaction; M/ b$ [5 w! F3 Q6 J( K8 x/ ?/ R
port := "http://127.0.0.1:9888/build-transaction"
value, err := SendTransactionRetire(transaction_params, port)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("build-transaction接口返回的参数:", value)
//sign-transaction
//...........
//submit-transaction
//...........
}
//send post request
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接+ R7 C# _2 P: h, e
ParamsStr, err := json.Marshal(params)% b5 a0 W& ~/ L0 g; l4 K
if err != nil {( G" H; m2 Q1 m
return nil, err
}
jsonStr := bytes.NewBuffer(ParamsStr)' h+ o0 I. s z; g# q
fmt.Println(jsonStr)
req, err := http.NewRequest("POST", port, jsonStr)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")5 G# m3 j6 C( \* y' J8 [
client := &http.Client{}8 x6 K; [) K! f8 e8 S$ R
resp, err := client.Do(req)4 N, \5 I, T+ r$ Z
if err != nil {+ }9 D0 D' B: w W7 p8 U1 H
panic(err)
}
defer resp.Body.Close()
var bodyBytes []byte
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)9 g1 a8 K; ?; r, Q6 e
if err != nil {
return nil, err
}- p7 _6 `5 w( O
}; L0 v% ^2 z& p
return string(bodyBytes), nil) f" P s( g/ n3 |
}" U* k: }. m0 M7 q* T; Y
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。 A- v# { n0 D) t. r) Z7 U' L) Z9 p
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。