那如何实现信息上链呢?使用特殊的Retire操作,这个操作可以进行销毁资产的操作,但因为其可以附带信息,所以就可以实现信息上链的功能。) o; D$ ?; ?0 x7 w
请往下看,也用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。6 |: ^8 q6 U* I
http://test.blockmeta.com/faucet.php
请求build-transaction接口:
请求参数:
{. }2 k, A# E( S$ `: q
"base_transaction": null, O6 Q' j- B r( |
"actions": [{* ~& ^7 E5 \# _' Z1 W
"account_id": "0KTCS3R5G0A02",
"amount": 10000000,% ^. t( F d" `2 `- J1 N) n
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",* I' P1 c, j0 e6 b8 a* [
"type": "spend_account", ~) D" K2 F" u, O: Q$ N% E( S, Q- b% Y
}, {
"account_id": "0KTCS3R5G0A02",! e" a* l# M! p8 H* a
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
"type": "spend_account"
}, {1 b/ k. ~1 ], p# g. P4 d
"account_id": "0KTCS3R5G0A02",0 ^3 R6 p$ s5 `
"amount": 100,
"asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",- p" f0 s& `! `# R/ C. o. u/ c! N
"arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",- V7 S, z. L3 ^: [
"type": "retire"
}],/ ~9 ?1 R5 T: p4 a k# B
"ttl": 0,
"time_range": 1521625823$ E5 o2 Q9 A" @7 J! y4 y
}' R2 G5 i0 t/ p6 ]: ~% R
请求sign-transaction接口:
. h" }; j+ g" J* k
请求参数:
{% w* k# i! o' J k+ @- `& V
"password": "huangxinglong123",8 p/ l( V" y! \$ b5 C' K
"transaction": {
"allow_additional_actions": false,/ ?6 w' S% B& d8 `- i. t4 t
"local": true,3 p/ K g0 l& @) _
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
"signing_instructions": [{- R* b% X9 b: T4 U
"position": 0,
"witness_components": [{: k& y( O8 j; y- k+ X6 B
"keys": [{
"derivation_path": [
"0000002c",
"00000099",0 G/ W5 u; c: P$ `3 ^6 s P- B: k
"0100000000000000",
"0100000000000000",6 u: ~! X: e8 B9 N; |
"4600000000000000"6 Y) L$ S6 S' B# n8 S. V* D
],
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,, ~$ S- h2 \4 P9 z! I
"signatures": null,
"type": "raw_tx_signature"
}, {( N9 G( ]" M/ Z
"type": "data",
"value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"- W/ M; c# x3 d5 V' [* W+ m
}]8 |( Z7 L' X% }9 y7 ~6 c
}, {) a) `* Y; j5 c7 d g, W9 z5 g
"position": 1,7 [$ o' e/ ?: s' M
"witness_components": [{/ L1 }' o9 N* X
"keys": [{
"derivation_path": [* J. N( A @& i
"0000002c",% w5 W* v$ c0 `
"00000099", Y3 G1 |% n% `- U+ C% @
"0100000000000000",
"0100000000000000",; N7 F1 P% S) \$ w+ S
"4700000000000000"
],* x! n# L( `: y7 N, c8 t( |
"xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
}],
"quorum": 1,
"signatures": null,; e9 L, y2 `+ s$ D; O; V
"type": "raw_tx_signature"' j7 E: f0 Y, O
}, {
"type": "data",
"value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"7 e1 X1 i5 C( B5 G" C
}]- F: h. ^9 N3 t8 c, ~7 F, k: R; P
}]) p! Q; {4 @( \4 N5 I5 ^0 y6 P% U
}
}
请求submit-transaction接口:
请求参数:
{0 M+ V7 P% y( E) B6 k
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
}2 N+ O( d% M) r+ t) P3 R. Y
响应参数:- N. Q. F( f0 C$ `
{9 }& q2 f7 A/ q" Y, C) [
"status": "success",* I. O9 J9 B3 @0 F6 h
"data": {
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"
}3 s4 b+ U7 c; | F
}6 _: R2 u. p6 G* [$ I' f
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据$ l: e. o: Q' {# i7 u. ~5 I( g
参考代码:
package main
import (
"bytes"9 j7 s5 n) |" f5 _. e$ b3 B, E
"encoding/json". Q1 R0 @: V- |5 M9 |; R
"fmt"% Y- G" ~! Q9 g( Z- b
"io/ioutil"
"net/http"
)
//build-transaction params" X# E) @+ ?0 q* A( [3 K3 K' d
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction2 ]' r+ g. F: x$ M
type BytomAccount struct {
AccountId string `json:"account_id"`
Amount int `json:"amount"`
AssetId string `json:"asset_id"`9 N' e: L- k: e% o. U( _
//Arbitrary string `json:"arbitrary"`
Type string `json:"type"`
}
type BytomAccount1 struct {
AccountId string `json:"account_id"`
Amount int `json:"amount"`% {7 T" y d3 a% C: |, c# l! o. p
AssetId string `json:"asset_id"`: J" O# P3 y" q: E2 G, V5 e; f8 ]
Arbitrary string `json:"arbitrary"`1 H" u# T3 \; C! c! w" H3 S
Type string `json:"type"`8 ~- Z/ B# R# \' D2 u. }. U, u% A
}- y7 h8 C. G# P2 v& v
type BaseTransaction struct{}
type TransactionParams struct {6 o4 v+ R. G Q- E& o ^
BaseTransaction *BaseTransaction `json:"base_transaction"`% F6 U2 i9 h( o: {7 b7 w( c" u- K
Actions []interface{} `json:"actions"`) [+ E! E N. x+ N: N5 j
Ttl int `json:"ttl"`, u- h1 ~- f8 P P8 l
TimeRange int `json:"time_range"`
}
//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction# H" u# u* T7 [/ o% y$ ?
type Transaction struct {
}
type SignParams struct {& { m0 \5 D) F& q
Password string `json:"password"`3 A4 q9 ^$ ~, m d: l; X* l
Transaction Transaction `json:"transaction"`
}% Q3 p( t1 p; O1 J% @3 V; q8 `
//submit-transaction
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type SubmitParams struct {$ R) W8 V. G3 h
RawTransaction string `json:"raw_transaction"`
}6 \. b5 v5 P2 {
type SubmitResponse struct {
TxId string `json:"tx_id"`" s0 n/ |7 U- h
}- R% H7 N1 Y0 K: Z. i
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}
account1.AccountId = "0KTCS3R5G0A02"# t2 ]5 |- I. _2 P
account1.Amount = 100000004 h Q" S2 t- k7 w1 F1 Y# w0 ~
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"- v. P% Z4 [+ w$ G4 L' Z% L
account1.Type = "spend_account"& P0 [ U0 m. R
account2.AccountId = "0KTCS3R5G0A02"+ z, P2 H* @- b, H% C
account2.Amount = 1004 V; T0 i/ f, n
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"7 I3 |2 W4 H: F2 A& p- n# U4 q9 k$ m: ?! t
account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 100- @5 f7 P+ C. I' |* F) a
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"
account3.Type = "retire"
//var array4 w6 h r7 g/ r7 r- J {! Z
var actions []interface{}
//append three params
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{}
transaction_params.Actions = array_actions: w# l1 Y. C4 }3 @
transaction_params.Ttl = 0
transaction_params.TimeRange = 15216258232 ~* K9 h- [/ H& @1 s; B' K" |( I
//本地测试网节点
//build-transaction
port := "http://127.0.0.1:9888/build-transaction" z7 _4 @2 I* U! F
value, err := SendTransactionRetire(transaction_params, port)
if err != nil {4 p- f7 X4 P0 D4 C$ l3 T
fmt.Println("err:", err)
}1 C% ]- o3 ^8 ], u
fmt.Println("build-transaction接口返回的参数:", value)
//sign-transaction
//...........0 e% Y# P+ Y0 l8 w7 U
//submit-transaction) w( D% A' G4 h
//...........
}
//send post request
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接/ h: B5 E+ f7 N0 L! N0 G! P
ParamsStr, err := json.Marshal(params)
if err != nil {
return nil, err2 B/ x0 f/ m% M5 {) s" F+ V/ R
}
jsonStr := bytes.NewBuffer(ParamsStr)
fmt.Println(jsonStr)* h* l* \ h# p. q2 I- A8 H
req, err := http.NewRequest("POST", port, jsonStr)0 y& I9 c" Q+ |" _. N" M6 G4 h
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
client := &http.Client{}2 A/ Y* i1 p3 f$ X
resp, err := client.Do(req)1 \6 @- l9 ~. l- t3 c( x8 {4 z
if err != nil {
panic(err)1 O5 C! V3 ^& W, [( H5 M2 G: n. H- W$ p
}
defer resp.Body.Close()
var bodyBytes []byte
if resp.StatusCode == 200 { }) t T9 [8 `& p
bodyBytes, err = ioutil.ReadAll(resp.Body); b8 Y" Q; @7 R0 Z7 N
if err != nil {
return nil, err
}
}
return string(bodyBytes), nil7 S7 U& x7 K9 ? q
}
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们,https://github.com/Bytom/bytom。