溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Bytom 記錄功能教程

發(fā)布時(shí)間:2020-05-29 02:40:16 來(lái)源:網(wǎng)絡(luò) 閱讀:244 作者:比原鏈Bytom 欄目:編程語(yǔ)言

比原項(xiàng)目倉(cāng)庫(kù):

Github地址:https://github.com/Bytom/bytom

Gitee地址:https://gitee.com/BytomBlockchain/bytom

很多了解比原鏈的都知道,比原鏈?zhǔn)菍W⑿畔⒑蛿?shù)字資產(chǎn)在鏈上交互和流轉(zhuǎn)的公鏈項(xiàng)目,信息上鏈不是比原鏈核心能力,所以并沒(méi)有在錢包端做一個(gè)功能入口,但是比原鏈提供了相關(guān)的接口可以將一些信息寫到鏈上去。
那如何實(shí)現(xiàn)信息上鏈呢?使用特殊的Retire操作,這個(gè)操作可以進(jìn)行銷毀資產(chǎn)的操作,但因?yàn)槠淇梢愿綆畔?,所以就可以?shí)現(xiàn)信息上鏈的功能。

請(qǐng)往下看,也用postman請(qǐng)求演示,然后用golang寫了一個(gè)接口的demo, 在用golang代碼實(shí)現(xiàn)之前,我們先要做一些準(zhǔn)備工作。

  • 首先確保自己在本地已經(jīng)搭建好了比原的節(jié)點(diǎn),如果你還沒(méi)有搭建好節(jié)點(diǎn),請(qǐng)參考開發(fā)文檔:<https://docs.bytom.io/mydoc_build_environment.cn.html>
  • 確保自己賬戶是有足夠BTM測(cè)試幣,如果沒(méi)有可以去比原鏈水龍頭領(lǐng)取BTM測(cè)試幣,領(lǐng)取地址:

<http://test.blockmeta.com/faucet.php>

  • 發(fā)行自己的資產(chǎn),參考:<http://8btc.com/forum.php?mod=viewthread&tid=242940&extra=>
  • 信息上鏈的本質(zhì)就是其實(shí)就是創(chuàng)建并發(fā)送一筆交易,我們都知道通過(guò)api發(fā)起交易主要有三個(gè)步驟,先 build → sign → submit,分別對(duì)應(yīng)的api是 build-transaction、sign-transaction、submit-transaction。用postman請(qǐng)求過(guò)程如下:

請(qǐng)求build-transaction接口:

Bytom 記錄功能教程

請(qǐng)求參數(shù):

{
"base_transaction": null,
"actions": [{
    "account_id": "0KTCS3R5G0A02",
    "amount": 10000000,
    "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
    "type": "spend_account"
}, {
    "account_id": "0KTCS3R5G0A02",
    "amount": 100,
    "asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
    "type": "spend_account"
}, {
    "account_id": "0KTCS3R5G0A02",
    "amount": 100,
    "asset_id": "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd",
    "arbitrary": "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c",
    "type": "retire"
}],
"ttl": 0,
"time_range": 1521625823
}

請(qǐng)求sign-transaction接口:

Bytom 記錄功能教程

請(qǐng)求參數(shù):

{
"password": "huangxinglong123",
"transaction": {
    "allow_additional_actions": false,
    "local": true,
    "raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c0101000161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b010003013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00",
    "signing_instructions": [{
        "position": 0,
        "witness_components": [{
            "keys": [{
                "derivation_path": [
                    "0000002c",
                    "00000099",
                    "0100000000000000",
                    "0100000000000000",
                    "4600000000000000"
                ],
                "xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
            }],
            "quorum": 1,
            "signatures": null,
            "type": "raw_tx_signature"
        }, {
            "type": "data",
            "value": "4f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a"
        }]
    }, {
        "position": 1,
        "witness_components": [{
            "keys": [{
                "derivation_path": [
                    "0000002c",
                    "00000099",
                    "0100000000000000",
                    "0100000000000000",
                    "4700000000000000"
                ],
                "xpub": "1c03161a08a4dbb7df153815a28f733fec1ac7579f954c4834e5ce9f0ad8deb260ecb2066a8623b69aa936f5798f4dcb9572bc476f2c8171953ce054d58a759f"
            }],
            "quorum": 1,
            "signatures": null,
            "type": "raw_tx_signature"
        }, {
            "type": "data",
            "value": "67512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa"
        }]
    }]
}
}

請(qǐng)求submit-transaction接口:

Bytom 記錄功能教程

請(qǐng)求參數(shù):

 {
 "raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
  }

響應(yīng)參數(shù):

 {
"status": "success",
"data": {
    "tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"
  }
}

?
用上面返回的tx_id去比原的瀏覽器中去查看交易詳情,就可以查看到我們上傳的數(shù)據(jù)

Bytom 記錄功能教程

參考代碼:

package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type BytomAccount struct {
AccountId string `json:"account_id"`
Amount    int    `json:"amount"`
AssetId   string `json:"asset_id"`
//Arbitrary string `json:"arbitrary"`
Type string `json:"type"`
}
type BytomAccount1 struct {
AccountId string `json:"account_id"`
Amount    int    `json:"amount"`
AssetId   string `json:"asset_id"`
Arbitrary string `json:"arbitrary"`
Type      string `json:"type"`
}

type BaseTransaction struct{}

type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`
Actions         []interface{}    `json:"actions"`
Ttl             int              `json:"ttl"`
TimeRange       int              `json:"time_range"`
}

//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type Transaction struct {
}

type SignParams struct {
Password    string      `json:"password"`
Transaction Transaction `json:"transaction"`
}

//submit-transaction
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type 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 = 10000000
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"

account2.AccountId = "0KTCS3R5G0A02"
account2.Amount = 100
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"

account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 100
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account3.Arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"
account3.Type = "retire"

//var array
var actions []interface{}
//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

//本地測(cè)試網(wǎng)節(jié)點(diǎn)
//build-transaction
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接口返回的參數(shù):", value)

//sign-transaction
//...........

//submit-transaction
//...........

}

//send post request
func SendTransactionRetire(params *TransactionParams, port   string) (v interface{}, err error) {
//以本地測(cè)試網(wǎng)節(jié)點(diǎn)連接
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 []byte
if resp.StatusCode == 200 {
    bodyBytes, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
}

return string(bodyBytes), nil
}

上面的代碼只是build-transaction一個(gè)步驟,另外sign-transaction和submit-transaction請(qǐng)求需要自己去組織參數(shù)進(jìn)行請(qǐng)求。請(qǐng)求完submit-transaction 獲得返回的交易hash,去區(qū)塊鏈瀏覽器上查看自己的上鏈信息,區(qū)塊鏈瀏覽器地址:http://52.82.46.157:8082/。

好了,通過(guò)以上的4個(gè)步驟,我們就可以借助比原鏈實(shí)現(xiàn)信息上鏈。如果你有什么疑問(wèn)或者不明白,請(qǐng)?jiān)谖覀兊纳鐓^(qū)聯(lián)系我們,https://github.com/Bytom/bytom。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI