Bytom信息上链教程
比原项目仓库:
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockchain/bytom
很多了解比原链的都知道,比原链是专注信息和数字资产在链上交互和流转的公链项目。那如何实现信息上链呢?请往下看,我用postman请求演示,然后用golang写了一个接口的demo, 在用golang代码实现之前,我们先要做一些准备工作。首先确保自己在本地已经搭建好了比原的节点,如果你还没有搭建好节点,请参考开发文档:https://github.com/Bytom/bytom/blob/master/README.md
step1:确保自己账户是有足够BTM的,如果没有可以去比原链水龙头领取BTM,领取地址:
http://test.blockmeta.com/faucet.php
step2:发行自己的资产,参考:http://8btc.com/forum.php?mod=viewthread&tid=242940&extra=
step3: 信息上链的本质就是其实就是创建并发送一笔交易,我们都知道通过api发起交易主要有三个步骤,先 build → sign → submit,分别对应的api是 build-transaction、sign-transaction、submit-transaction。用postman请求过程如下:
请求build-transaction接口:
请求参数:
{
"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
}
请求sign-transaction接口:
请求参数:
{
"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"
}]
}]
}
}
请求submit-transaction接口:
请求参数:
{
"raw_transaction": "0701dfd5c8d505020160015e560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0989fe3020001160014adb6632c5b10c6d5b6f97b8d1250f6e409e11c01630240c7004022db674ff2961b540d4edab846d550429ae9a92311ba375a4f452331422961fdcde3bf79631755dd12df409e24a849158d4aeab919cab81520fb7d1e02204f089176a5bca95ec9227b8a87dfec947c59453805bf46d3f5a18f8032255b5a0161015f560352e415b41be7648b2241ffdabf56259bc618525f62ac123dce32002110f0608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd9cc5b191f3190101160014dcfd9b78c24260823e318153665d511d6c4ecb1b6302406b75ef5a9decfa31d4f5ae06e0fb14ca507ba4a03715874d1d831516945121573b9b858e4d7527d209c1f89f74e0aa4c4e38afd098cbadaff31b9107167099012067512f9250f559699e32c72c8af29096b1556af145f6ecc0c306e6acc88bbfaa03013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0ebbcde02011600147a9baebd37dba3f14960624ed8e6ca3cc9d5f73800013e608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cdb8c4b191f31901160014f0370fdf7a7bec7b34cc62fd5291071a3dc3d9b0000147608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd6401246a2277656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c00"
}
响应参数:
{
"status": "success",
"data": {
"tx_id": "5ef27b930646d468bbb436d3406972ff201aa63702518f777e31dd6a2147dddc"
}
}
用上面返回的tx_id去比原的浏览器中去查看交易详情,就可以查看到我们上传的数据
参考代码:
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
//本地测试网节点
//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接口返回的参数:", value)
//sign-transaction
//...........
//submit-transaction
//...........
}
//send post request
func SendTransactionRetire(params *TransactionParams, port string) (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 []byte
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
}
return string(bodyBytes), nil
}
上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息,区块链浏览器地址:http://52.82.46.157:8082/。
好了,通过以上的4个步骤,我们就可以借助比原链实现信息上链。如果你有什么疑问或者不明白,请在我们的社区联系我们。
Bytom信息上链教程的更多相关文章
- CUBRID学习笔记 35 net驱动错误码和信息 cubrid教程示例
DO.NET Error Code Number Error Code Error Message Note 0 ER_NO_ERROR "No Error" 1 ER_NOT ...
- Jrebel6.3.3破解,配置图文教程
JRebel是个很好的开发工具,我在网上找了好久都没有找到很详细的教程,破解与配置教程千篇一律,步骤不详细.编写这篇教程,综合网络上的知识,加上了自己的理解与详细图文步骤. 安装 一般最新的插件是没有 ...
- IDApython教程(一)
IDAPython是IDA的一个功能强大的扩展特性,对外提供了大量的IDA API调用.另外,还能在使用python 脚本语言的过程中获得能力提升,所以我强烈推荐所有的逆向工程师使用它. 然而不幸的是 ...
- Kubernetes 1.8.x 全手动安装教程----转自Kubernetes中文社区(部分内容根据实验环境做了些修改,特此感谢Kubernetes中文社区)
Kubernetes 提供了许多云端平台与操作系统的安装方式,本章将以全手动安装方式来部署,主要是学习与了解 Kubernetes 创建流程.若想要了解更多平台的部署可以参考 Picking the ...
- TIAGo ROS模拟教程2 - 自主机器人导航
TIAGo ROS Simulation Tutorial 2 – Autonomous robot navigation TIAGo ROS模拟教程2 - 自主机器人导航 发表于 12月 23,20 ...
- 【黑金原创教程】【Modelsim】【第四章】激励文本就是仿真环境
声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/ ...
- Atitit.获取主板与bios序列号获取硬件设备信息 Wmi wmic 的作用
Atitit.获取主板与bios序列号获取硬件设备信息 Wmi wmic 的作用 1 获取硬件核心基础核心基础Wmi1 2 其他资料2 3 Wmic WMI 命令行接口2 4 Atitit.获取主板 ...
- SpringBoot中获取微信用户信息从未如此简单!
前言 不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息.获取用户信息就是获取公众号下微信用户的信息,今天我就来讲讲如何从公众号下获取微信用户信息. 需要 ...
- 【STM32H7教程】第51章 STM32H7的LTDC应用之LCD汉字显示和2D图形显示
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第51章 STM32H7的LTDC应用之LCD汉字 ...
随机推荐
- java个人博客源码
初入博客园,请各位多关照,来而不往非礼也. 如需要源码以及学习内容,qq:1397617269,我看到就回你们资源. 直接给链接: 链接:https://pan.baidu.com/s/1S_awtg ...
- image的srcset属性
介绍 响应式页面中经常用到根据屏幕密度设置不同的图片.这个时候肯定会用到image标签的srcset属性.srcset属性用于设置不同屏幕密度下,image自动加载不同的图片.用法如下: <im ...
- SpringBoot介绍
SpringBoot作用:对框架整合做了简化,和分布式集成.pom.xml中的spring-parent中有很多已经集成好的东西,拿来直接用 SpringBoot核心功能: 1.独立运行的Spring ...
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- centos7更改网卡名称
1.编辑/etc/sysconfig/grub文件,加入net.ifnames=0 biosdevname=0 2.执行命令grub2-mkconfig -o /boot/grub2/grub.cfg ...
- 部署WEB项目到服务器(四)部署WEB项目Forum到linux服务器(Ubuntu)详解
突发奇想,想在自己电脑上部署一个web网站. 1,使用Navicat for MYSQL客户端创建WEB项目数据库: Navicat for MYSQL连接虚拟机中的mysql数据库 启动mysql数 ...
- 斑马打印机ZT410中文打印
^XA ^CW1, E:SIMSUN.TTF^CI28^FO50,50^A1N,50,50^FD汉字^FS^XZ ******************************************* ...
- Jmeter使用之-断言
添加响应断言 ,一般下面这样选择断言的设置就可以了 个人习惯是直接从响应数据中直接复制正确的接口返回值到断言的测试模式中,如下 !!!但是这样复制的是有问题的 ,需要在测试模式中去掉 ...
- Oracle单行函数
一.尽管各个数据库都是支持sql语句的.可是每一个数据库也有每一个数据库所支持的操作函数,这些就是单行函数.假设想进行数据库开发的话.除了要回使用sql语句外,就是要多学习函数. 1.单行函数的分类: ...
- block,inline和inline-block概念和区别(转载)
转自: http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-lev ...