Bytom信息上链教程( 二 )


响应参数:
{"status": "success","data": {"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"}}
?
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据

Bytom信息上链教程

文章插图
参考代码:
package mainimport ("bytes""encoding/json""fmt""io/ioutil""net/http")//build-transaction params//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transactiontype BytomAccount struct {AccountId string `json:"account_id"`Amountint`json:"amount"`AssetIdstring `json:"asset_id"`//Arbitrary string `json:"arbitrary"`Type string `json:"type"`}type BytomAccount1 struct {AccountId string `json:"account_id"`Amountint`json:"amount"`AssetIdstring `json:"asset_id"`Arbitrary string `json:"arbitrary"`Typestring `json:"type"`}type BaseTransaction struct{}type TransactionParams struct {BaseTransaction *BaseTransaction `json:"base_transaction"`Actions[]interface{}`json:"actions"`Ttlint`json:"ttl"`TimeRangeint`json:"time_range"`}//sign-transaction params//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transactiontype Transaction struct {}type SignParams struct {Passwordstring`json:"password"`Transaction Transaction `json:"transaction"`}//submit-transaction//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transactiontype SubmitParams struct {RawTransaction string `json:"raw_transaction"`}type SubmitResponse struct {TxId string `json:"tx_id"`}func main() {account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount1{}account1.AccountId = "0KTCS3R5G0A02"account1.Amount = 10000000account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"account1.Type = "spend_account"account2.AccountId = "0KTCS3R5G0A02"account2.Amount = 100account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"account2.Type = "spend_account"account3.AccountId = "0KTCS3R5G0A02"account3.Amount = 100account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"account3.Type = "retire"//var arrayvar actions []interface{}//append three paramsarray_actions := append(actions, account1, account2, account3)transaction_params := &TransactionParams{}transaction_params.Actions = array_actionstransaction_params.Ttl = 0transaction_params.TimeRange = 1521625823//本地测试网节点//build-transactionport := "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 requestfunc SendTransactionRetire(params *TransactionParams, portstring) (v interface{}, err error) {//以本地测试网节点连接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)}defer resp.Body.Close()var bodyBytes []byteif resp.StatusCode == 200 {bodyBytes, err = ioutil.ReadAll(resp.Body)if err != nil {return nil, err}}return string(bodyBytes), nil}
上面的代码只是build-一个步骤,另外sign-和-请求需要自己去组织参数进行请求 。请求完- 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址::8082/ 。
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链 。如果你有什么疑问或者不明白,请在我们的社区联系我们,。