经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 区块链 » 查看文章
Bytom Java版本离线签名
来源:cnblogs  作者:比原链Bytom  时间:2018/11/8 9:33:02  对本文有异议

比原项目仓库:

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

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

git checkout

  1. $ 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

  1. $ cd $GOPATH/src/github.com/bytom
  2. $ make bytomd # build bytomd
  3. $ 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:

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

launch

  1. $ ./bytomd node --mining

Usage

Build jar

  1. first get source code

    1. git clone https://github.com/successli/tx_signer.git
  2. get jar package

    1. $ 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:

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

Single-key Example:

  1. @Test
  2. // 使用 SDK 来构造 Template 对象参数, 单签
  3. public void testSignSingleKey() throws BytomException {
  4. Client client = Client.generateClient();
  5. String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
  6. String address = "sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
  7. // build transaction obtain a Template object
  8. Template template = new Transaction.Builder()
  9. .addAction(
  10. new Transaction.Action.SpendFromAccount()
  11. .setAccountId("0G0NLBNU00A02")
  12. .setAssetId(asset_id)
  13. .setAmount(40000000)
  14. )
  15. .addAction(
  16. new Transaction.Action.SpendFromAccount()
  17. .setAccountId("0G0NLBNU00A02")
  18. .setAssetId(asset_id)
  19. .setAmount(300000000)
  20. )
  21. .addAction(
  22. new Transaction.Action.ControlWithAddress()
  23. .setAddress(address)
  24. .setAssetId(asset_id)
  25. .setAmount(30000000)
  26. ).build(client);
  27. logger.info("template: " + template.toJson());
  28. // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object
  29. RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);
  30. logger.info("decodeTx: " + decodedTx.toJson());
  31. // need a private key array
  32. String[] privateKeys = new String[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};
  33. logger.info("private key:" + privateKeys[0]);
  34. // call offline sign method to obtain a basic offline signed template
  35. Signatures signatures = new SignaturesImpl();
  36. Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);
  37. logger.info("basic signed raw: " + basicSigned.toJson());
  38. // call sign transaction api to calculate whole raw_transaction id
  39. // sign password is None or another random String
  40. Template result = new Transaction.SignerBuilder().sign(client,
  41. basicSigned, "");
  42. logger.info("result raw_transaction: " + result.toJson());
  43. // success to submit transaction
  44. }

Multi-keys Example:

Need an account has two or more keys.

  1. @Test
  2. // 使用 SDK 来构造 Template 对象参数, 多签
  3. public void testSignMultiKeys() throws BytomException {
  4. Client client = Client.generateClient();
  5. String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
  6. String address = "sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
  7. // build transaction obtain a Template object
  8. // account 0G1RPP6OG0A06 has two keys
  9. Template template = new Transaction.Builder()
  10. .setTtl(10)
  11. .addAction(
  12. new Transaction.Action.SpendFromAccount()
  13. .setAccountId("0G1RPP6OG0A06")
  14. .setAssetId(asset_id)
  15. .setAmount(40000000)
  16. )
  17. .addAction(
  18. new Transaction.Action.SpendFromAccount()
  19. .setAccountId("0G1RPP6OG0A06")
  20. .setAssetId(asset_id)
  21. .setAmount(300000000)
  22. )
  23. .addAction(
  24. new Transaction.Action.ControlWithAddress()
  25. .setAddress(address)
  26. .setAssetId(asset_id)
  27. .setAmount(30000000)
  28. ).build(client);
  29. logger.info("template: " + template.toJson());
  30. // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object
  31. RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);
  32. logger.info("decodeTx: " + decodedTx.toJson());
  33. // need a private key array
  34. String[] privateKeys = new String[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",
  35. "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};
  36. logger.info("private key 1:" + privateKeys[0]);
  37. logger.info("private key 2:" + privateKeys[1]);
  38. // call offline sign method to obtain a basic offline signed template
  39. Signatures signatures = new SignaturesImpl();
  40. Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);
  41. logger.info("basic signed raw: " + basicSigned.toJson());
  42. // call sign transaction api to calculate whole raw_transaction id
  43. // sign password is None or another random String
  44. Template result = new Transaction.SignerBuilder().sign(client,
  45. basicSigned, "");
  46. logger.info("result raw_transaction: " + result.toJson());
  47. // success to submit transaction
  48. }

Multi-keys and Multi-inputs Example:

  1. @Test
  2. // 使用 SDK 来构造 Template 对象参数, 多签, 多输入
  3. public void testSignMultiKeysMultiInputs() throws BytomException {
  4. Client client = Client.generateClient();
  5. String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
  6. String address = "sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
  7. // build transaction obtain a Template object
  8. Template template = new Transaction.Builder()
  9. .setTtl(10)
  10. // 1 input
  11. .addAction(
  12. new Transaction.Action.SpendFromAccount()
  13. .setAccountId("0G1RPP6OG0A06") // Multi-keys account
  14. .setAssetId(asset_id)
  15. .setAmount(40000000)
  16. )
  17. .addAction(
  18. new Transaction.Action.SpendFromAccount()
  19. .setAccountId("0G1RPP6OG0A06")
  20. .setAssetId(asset_id)
  21. .setAmount(300000000)
  22. ) // 2 input
  23. .addAction(
  24. new Transaction.Action.SpendFromAccount()
  25. .setAccountId("0G1Q6V1P00A02") // Multi-keys account
  26. .setAssetId(asset_id)
  27. .setAmount(40000000)
  28. )
  29. .addAction(
  30. new Transaction.Action.SpendFromAccount()
  31. .setAccountId("0G1Q6V1P00A02")
  32. .setAssetId(asset_id)
  33. .setAmount(300000000)
  34. )
  35. .addAction(
  36. new Transaction.Action.ControlWithAddress()
  37. .setAddress(address)
  38. .setAssetId(asset_id)
  39. .setAmount(60000000)
  40. ).build(client);
  41. logger.info("template: " + template.toJson());
  42. // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object
  43. RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);
  44. logger.info("decodeTx: " + decodedTx.toJson());
  45. // need a private key array
  46. String[] privateKeys = new String[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",
  47. "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",
  48. "08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};
  49. logger.info("private key 1:" + privateKeys[0]);
  50. logger.info("private key 2:" + privateKeys[1]);
  51. // call offline sign method to obtain a basic offline signed template
  52. Signatures signatures = new SignaturesImpl();
  53. Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);
  54. logger.info("basic signed raw: " + basicSigned.toJson());
  55. // call sign transaction api to calculate whole raw_transaction id
  56. // sign password is None or another random String
  57. Template result = new Transaction.SignerBuilder().sign(client,
  58. basicSigned, "");
  59. logger.info("result raw_transaction: " + result.toJson());
  60. // success to submit transaction
  61. }
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号