那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。. |( ^* I. U9 z4 T
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。
http://test.blockmeta.com/faucet.php) x: T, `1 I) S! q! W) ?$ g! R
请求build-transaction接口:
# x& L' z- S( ^9 ~
请求参数:
{
"base_transaction": null,6 M2 m) _7 I& [: F# `
"actions": [{) F/ c' f: ]2 \) K/ b4 x
"account_id": "0KTCS3R5G0A02",
"amount": 10000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "spend_account"
}, {
"account_id": "0KTCS3R5G0A02",
"amount": 100,8 r: t- E# m! q! o) k1 G
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"type": "spend_account"
}, {
"account_id": "0KTCS3R5G0A02",
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",# u& S1 Q( U" c% X
"type": "retire"( b, q. }7 c3 M- P# @, w
}],
"ttl": 0,
"time_range": 15216258233 L: v& K9 |$ Q8 h! {4 z( w
}
请求sign-transaction接口:- O4 _7 p# Y4 C6 d6 w. R! ?* l
* T8 t9 R z" \! N! W
请求参数:+ s4 s( y! A& m8 e
{
"password": "huangxinglong123",
"transaction": {* E- N: }3 M( z& }5 u
"allow_additional_actions": false,7 R* g0 l4 V; ^$ H/ e; n) G
"local": true,
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
"signing_instructions": [{! T3 W4 \$ D) f( f, q/ M0 @
"position": 0,
"witness_components": [{' o4 f* d8 `0 X6 j% j! L
"keys": [{
"derivation_path": [) B1 l5 A1 R/ w0 l1 _: Z
"0000002c"," N0 V: e" Q- V& `
"00000099",
"0100000000000000",% n" Z/ i% j7 L" [: ?
"0100000000000000",9 \- t( `1 J+ f8 ~
"4600000000000000"
],4 X" v, ^; I8 H" V( Z
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f") s' ?" @7 K* @
}],
"quorum": 1,) t! H& ]% j0 N2 L7 U; M
"signatures": null,% w' n) n& n; n- U
"type": "raw_tx_signature"
}, {
"type": "data",! J, t6 V x3 Q0 `6 A2 W; Y
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"
}]& z. s( v7 l) J, J" x4 J
}, {
"position": 1,
"witness_components": [{
"keys": [{
"derivation_path": [
"0000002c",
"00000099",
"0100000000000000",
"0100000000000000",
"4700000000000000"
],* f4 z4 F: d! T: X
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],* l" h5 L' Q- x0 h6 f5 M% u
"quorum": 1,& d, y5 |3 E& b& Q. ~! u6 ?
"signatures": null,1 d8 i# e' w; f a3 Y
"type": "raw_tx_signature"
}, {
"type": "data",
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"
}]: E3 H3 s8 ~8 v% _3 N
}]
}/ C" G3 s( X0 ^4 h
}
请求submit-transaction接口:
5 ]/ x! v8 `* o: g- A
请求参数:
{0 @6 e& u7 N! }- Q
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
}
响应参数:
{
"status": "success",# a" D8 y6 `7 R2 y2 x4 I! A$ u
"data": { Q: _/ J# O$ o$ Q0 B! N' m
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc": M. ^' ?; x- j" W# B
}
} [: `5 u. `! ~; Y% P
6 K2 k4 Z5 @* V; w
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据
% c! Z/ d9 y3 t% I# t5 m8 y6 \
参考代码:
package main
import (
"bytes": @( ~& b; O- |2 ]% n
"encoding/json"
"fmt"8 O4 F+ T6 X1 `4 Q, F2 a* ~
"io/ioutil"! G. U: r6 [. n5 s9 }6 z& P6 b B
"net/http"
)
//build-transaction params' j* i A7 ~3 V. B% z0 m1 F
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction2 }0 I. Z: k9 M, F/ \- t0 R
type BytomAccount struct {# [" B$ x3 E4 P$ w0 {$ D# t# @9 v! a
AccountId string `json:"account_id"`
Amount int `json:"amount"`
AssetId string `json:"asset_id"`
//Arbitrary string `json:"arbitrary"`' x* S- v7 c. y
Type string `json:"type"`6 G& o/ G; A. B8 s7 d
}: y% A0 R) p9 a
type BytomAccount1 struct {
AccountId string `json:"account_id"`! \0 H3 G+ V& f
Amount int `json:"amount"`% }% _& t" x( z1 N ~( j
AssetId string `json:"asset_id"`
Arbitrary string `json:"arbitrary"`
Type string `json:"type"`9 d- y$ _2 a/ q
}
type BaseTransaction struct{}
type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`
Actions []interface{} `json:"actions"`' _- u1 U ~/ c, o/ I5 p
Ttl int `json:"ttl"`+ _2 P3 I2 {/ `6 W1 a. z
TimeRange int `json:"time_range"`4 Y! h5 H$ n) ?
}8 C% ?$ M- u3 @0 \- G( P& t4 n
//sign-transaction params1 n' p! k! I6 m2 z) ~3 }: v9 ~: v$ w
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction" U7 P, ]3 W k# z; _( @
type Transaction struct {
}1 Z. R$ B% p% M/ k
type SignParams struct {
Password string `json:"password"`6 a6 d5 f. l- g1 `3 l5 T$ J
Transaction Transaction `json:"transaction"`& G# w! A! c: O: W
}
//submit-transaction) t6 r+ v9 m. [! n( C% O0 W) N
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type SubmitParams struct {
RawTransaction string `json:"raw_transaction"`- y: a+ f* }' h; n3 w' M
}0 ?# _! A2 l5 b5 D( u3 b A
type SubmitResponse struct {
TxId string `json:"tx_id"`3 a$ b9 M% R$ ~/ I( [" C2 b" b n
}& Z8 u, u, _6 o/ p! z n
func main() {: R4 J% i9 T( D* z+ {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}
account1.AccountId = "0KTCS3R5G0A02"
account1.Amount = 10000000
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"
account2.AccountId = "0KTCS3R5G0A02"- N) Z x0 i& e- I; G
account2.Amount = 100
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"/ s D' g V" C/ r' G. D
account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 100
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"$ {, {5 H# U% G
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"
account3.Type = "retire"
//var array
var actions []interface{}; d$ @; T9 O+ N; \) [
//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 = 1521625823
//本地测试网节点- k: x4 D: m2 H5 j
//build-transaction
port := "http://127.0.0.1:9888/build-transaction"4 b- Q8 z$ y! u: C9 \9 O$ A
value, err := SendTransactionRetire(transaction_params, port)% i% `: Q( \+ }. d1 L- |" u1 _
if err != nil {& |/ h, C& V. U" S( B7 m; |; u
fmt.Println("err:", err)
}
fmt.Println("build-transaction接口返回的参数:", value)2 Y5 ^4 Q' ]# n
//sign-transaction
//...........
//submit-transaction# g4 K% c7 v5 a4 X
//...........
}7 d! Z+ J6 Q; D
//send post request# s# B2 x7 N" l! Z% w) ^6 s
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {% c4 `* m/ }8 X: w6 P
//以本地测试网节点连接* s y2 r- J" `. n5 x$ @8 M2 c
ParamsStr, err := json.Marshal(params)
if err != nil {
return nil, err
}
jsonStr := bytes.NewBuffer(ParamsStr)
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{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}- K+ C# p( s, j' l& }. [
defer resp.Body.Close()
var bodyBytes []byte& i9 W8 o, E% {" l9 I4 R
if resp.StatusCode == 200 {: C. s j! l' J
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {1 @' F9 N2 D- F6 ?' F
return nil, err% b9 g ]+ }, Z4 ~3 S* y
}8 }# X3 ~+ f; h7 _4 P7 q
}% Y, a3 Q# B7 U
return string(bodyBytes), nil
}. I$ b+ P4 i* r8 \8 X4 D
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。