Bytom Java版本离线签名

比原项目仓库:

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

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

tx_signer

Java implementation of signing transaction offline to bytomd.

Pre

Get the source code

$ git clone https://github.com/Bytom/bytom.git $GOPATH/src/github.com/bytom

git checkout

$ git checkout dev

Why need dev branch? Because you could call decode transaction api from dev branch and obtain tx_id and some inputs ids.

Build

$ cd $GOPATH/src/github.com/bytom
$ make bytomd    # build bytomd
$ make bytomcli  # build bytomcli

When successfully building the project, the bytom and bytomcli binary should be present in cmd/bytomd and cmd/bytomcli directory, respectively.

Initialize

First of all, initialize the node:

$ cd ./cmd/bytomd
$ ./bytomd init --chain_id solonet

launch

$ ./bytomd node --mining

Usage

Build jar

  1. first get source code git clone https://github.com/successli/tx_signer.git
  2. get jar package $ mvn assembly:assembly -Dmaven.test.skip=true You can get a jar with dependencies, and you can use it in your project.

Test cases

Need 3 Parameters:

  • Private Keys Array
  • Template Object
  • After call build transaction api return a Template json object. build transaction api
  • use bytom java sdk return a Template object.
  • Raw Transaction
  • call decode raw-transaction api from dev branch. decode raw-transaction api

Call method:

// return a Template object signed offline basically.
Template result = signatures.generateSignatures(privates, template, rawTransaction);
// use result's raw_transaction call sign transaction api to build another data but not need password or private key.

Single-key Example:

@Test
// 使用 SDK 来构造 Template 对象参数, 单签
public void testSignSingleKey() throws BytomException {
    Client client = Client.generateClient();

    String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
    String address = "sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
    // build transaction obtain a Template object
    Template template = new Transaction.Builder()
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G0NLBNU00A02")
        .setAssetId(asset_id)
        .setAmount(40000000)
    )
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G0NLBNU00A02")
        .setAssetId(asset_id)
        .setAmount(300000000)
    )
        .addAction(
        new Transaction.Action.ControlWithAddress()
        .setAddress(address)
        .setAssetId(asset_id)
        .setAmount(30000000)
    ).build(client);
    logger.info("template: " + template.toJson());
    // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object
    RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);
    logger.info("decodeTx: " + decodedTx.toJson());
    // need a private key array
    String[] privateKeys = new String[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};
    logger.info("private key:" + privateKeys[0]);
    // call offline sign method to obtain a basic offline signed template
    Signatures signatures = new SignaturesImpl();
    Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);
    logger.info("basic signed raw: " + basicSigned.toJson());
    // call sign transaction api to calculate whole raw_transaction id
    // sign password is None or another random String
    Template result = new Transaction.SignerBuilder().sign(client,
                                                           basicSigned, "");
    logger.info("result raw_transaction: " + result.toJson());
    // success to submit transaction
}

Multi-keys Example:

Need an account has two or more keys.

@Test
// 使用 SDK 来构造 Template 对象参数, 多签
public void testSignMultiKeys() throws BytomException {
    Client client = Client.generateClient();

    String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
    String address = "sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
    // build transaction obtain a Template object
    // account 0G1RPP6OG0A06 has two keys
    Template template = new Transaction.Builder()
        .setTtl(10)
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G1RPP6OG0A06")
        .setAssetId(asset_id)
        .setAmount(40000000)
    )
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G1RPP6OG0A06")
        .setAssetId(asset_id)
        .setAmount(300000000)
    )
        .addAction(
        new Transaction.Action.ControlWithAddress()
        .setAddress(address)
        .setAssetId(asset_id)
        .setAmount(30000000)
    ).build(client);
    logger.info("template: " + template.toJson());
    // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object
    RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);
    logger.info("decodeTx: " + decodedTx.toJson());
    // need a private key array
    String[] privateKeys = new String[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",
                                        "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};
    logger.info("private key 1:" + privateKeys[0]);
    logger.info("private key 2:" + privateKeys[1]);
    // call offline sign method to obtain a basic offline signed template
    Signatures signatures = new SignaturesImpl();
    Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);
    logger.info("basic signed raw: " + basicSigned.toJson());
    // call sign transaction api to calculate whole raw_transaction id
    // sign password is None or another random String
    Template result = new Transaction.SignerBuilder().sign(client,
                                                           basicSigned, "");
    logger.info("result raw_transaction: " + result.toJson());
    // success to submit transaction
}

Multi-keys and Multi-inputs Example:

@Test
// 使用 SDK 来构造 Template 对象参数, 多签, 多输入
public void testSignMultiKeysMultiInputs() throws BytomException {
    Client client = Client.generateClient();

    String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
    String address = "sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
    // build transaction obtain a Template object
    Template template = new Transaction.Builder()
        .setTtl(10)
        // 1 input
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G1RPP6OG0A06") // Multi-keys account
        .setAssetId(asset_id)
        .setAmount(40000000)
    )
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G1RPP6OG0A06")
        .setAssetId(asset_id)
        .setAmount(300000000)
    )    // 2 input
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G1Q6V1P00A02") // Multi-keys account
        .setAssetId(asset_id)
        .setAmount(40000000)
    )
        .addAction(
        new Transaction.Action.SpendFromAccount()
        .setAccountId("0G1Q6V1P00A02")
        .setAssetId(asset_id)
        .setAmount(300000000)
    )
        .addAction(
        new Transaction.Action.ControlWithAddress()
        .setAddress(address)
        .setAssetId(asset_id)
        .setAmount(60000000)
    ).build(client);
    logger.info("template: " + template.toJson());
    // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object
    RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);
    logger.info("decodeTx: " + decodedTx.toJson());
    // need a private key array
    String[] privateKeys = new String[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",
                                        "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",
                                        "08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};
    logger.info("private key 1:" + privateKeys[0]);
    logger.info("private key 2:" + privateKeys[1]);
    // call offline sign method to obtain a basic offline signed template
    Signatures signatures = new SignaturesImpl();
    Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);
    logger.info("basic signed raw: " + basicSigned.toJson());
    // call sign transaction api to calculate whole raw_transaction id
    // sign password is None or another random String
    Template result = new Transaction.SignerBuilder().sign(client,
                                                           basicSigned, "");
    logger.info("result raw_transaction: " + result.toJson());
    // success to submit transaction
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏IMWeb前端团队

Nodejs基础:stream模块入门介绍与使用

模块概览 nodejs的核心模块,基本上都是stream的的实例,比如process.stdout、http.clientRequest。 对于大部分的node...

1950
来自专栏Java学习网

Java Web Response对象的27个方法及状态码

response表示HttpServletResponse对象,主要将JSP容器处理后的结果传回到客户端。 ? 网络配图 1、void addCookie(...

4367
来自专栏腾讯云数据库(TencentDB)

【腾讯云CDB】教你玩转MyRocks/RocksDB—STATISTICS与后台线程篇

本文将介绍 SHOW ENGINE ROCKSDB STATUS 中关于 STATISTICS 统计值与后台线程的实现原理。

1.3K7
来自专栏wannshan(javaer,RPC)

dubbo监控机制之监控数据上报分析

dubbo对服务运行的监控,是通过从provider和consumer方收集调用信息存盘后,再由监控中心对数据分析绘表的方式完成的。 具体实现是provide...

49711
来自专栏吾真本

四巨头23种设计模式的意图

了解设计模式的意图,是在代码重构中浮现并识别设计模式的关键。 本文将四巨头在《设计模式》一书的23种设计模式的意图放在一个页面里,方便查阅。四巨头把这23种设...

921
来自专栏攻城狮的动态

慎用公共变量~~

2604
来自专栏Hongten

spring开发_Spring+Struts2

http://www.cnblogs.com/hongten/gallery/image/112920.html

902
来自专栏芋道源码1024

数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 执行

本文主要基于 Sharding-JDBC 1.5.0 正式版 1. 概述 2. ExecutorEngine 2.1 ListeningExecutorServ...

3787
来自专栏蓝天

IDL编译器实现入门

本文不对词法和语法、以及flex和bison进行介绍,如有需要,可以阅读《RPC的实现》。本文试图用直接的方式,以最短的篇幅介绍一个最简单的IDL编译器实现。...

2223
来自专栏大学生计算机视觉学习DeepLearning

c++ 网络编程(七)TCP/IP LINUX下 socket编程 基于套接字的标准I/O函数使用 与 fopen,feof,fgets,fputs函数用法

原文链接:https://www.cnblogs.com/DOMLX/p/9614820.html

2114

扫码关注云+社区

领取腾讯云代金券