资讯专栏INFORMATION COLUMN

Bytom Java版本离线签名

xumenger / 2323人阅读

摘要:比原项目仓库地址地址使用来构造对象参数单签使用来构造对象参数多签使用来构造对象参数多签多输入

比原项目仓库:

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

Gitee地址:https://gitee.com/BytomBlockc...

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

first get source code

git clone https://github.com/successli/tx_signer.git

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
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/24364.html

相关文章

  • Bytom Java版本离线签名

    摘要:比原项目仓库地址地址使用来构造对象参数单签使用来构造对象参数多签使用来构造对象参数多签多输入 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockc... tx_signer Java implementation of signing transaction offli...

    weapon 评论0 收藏0
  • 使用Java SDK实现离线签名

    摘要:当使用构建完成一笔交易并签名后,若没有全节点的帮助,也需要自己实现网络协议将交易广播到其他节点。其中,第一个依赖是的封装,可用于查询可用的以及提交交易第二个依赖用于构建交易以及对交易进行离线签名。 严格来说,tx-signer并不属于SDK,它是bytomd中构建交易、对交易签名两大模块的java实现版。因此,若想用tx-signer对交易进行离线签名,需要由你在本地保管好自己的私钥。...

    dreamGong 评论0 收藏0
  • Bytom国密网说明和指南

    摘要:在比原链主网中,在获取交易和区块头等摘要的过程中使用的哈希算法是算法,而在国密测试网中,使用算法替代。启动的是国密测试网。可以说,比原链的项目进展伴随着国密测试网的发布更上一层楼。 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockc... 国密算法是指国家密码管理局制...

    王岩威 评论0 收藏0
  • 比原链Bytom错误码一览

    摘要:错误编号内容注释非比原标准错误请求超时非法的请求体为网络错误编号内容注释区块链网络类型不匹配是签名相关的错误编号内容注释需要签名的个数超过实际需求签名的个数签名格式错误缺少主公钥主公钥重复为交易相关的错误构建交易错误编号内容注释资产余额不 0XX API错误 编号 内容 注释 BTM000 Bytom API Error 非比原标准错误 BTM001 Request t...

    Nosee 评论0 收藏0
  • Bytom Kit开发辅助工具介绍

    摘要:是一款为了帮助开发者更简单地理解的开发辅助工具,集合了校验标注解码测试水龙头等功能。这一功能可以创建新的私钥。链接比原水龙头工具接口,开发者可以使用该工具接口领取比原测试。 showImg(https://segmentfault.com/img/bVbrThp?w=1920&h=1280); Bytom Kit是一款为了帮助开发者更简单地理解Bytom的开发辅助工具,集合了校验、标注...

    cocopeak 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<