摘要:比原項目倉庫地址地址使用來構造對象參數單簽使用來構造對象參數多簽使用來構造對象參數多簽多輸入
比原項目倉庫:
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockc...
tx_signerJava implementation of signing transaction offline to bytomd.
Pre Get the source code$ git clone https://github.com/Bytom/bytom.git $GOPATH/src/github.com/bytomgit 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.
InitializeFirst of all, initialize the node:
$ cd ./cmd/bytomd $ ./bytomd init --chain_id solonetlaunch
$ ./bytomd node --miningUsage 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 casesNeed 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 }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/24364.html
摘要:比原項目倉庫地址地址使用來構造對象參數單簽使用來構造對象參數多簽使用來構造對象參數多簽多輸入 比原項目倉庫: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockc... tx_signer Java implementation of signing transaction offli...
摘要:當使用構建完成一筆交易并簽名后,若沒有全節點的幫助,也需要自己實現網絡協議將交易廣播到其他節點。其中,第一個依賴是的封裝,可用于查詢可用的以及提交交易第二個依賴用于構建交易以及對交易進行離線簽名。 嚴格來說,tx-signer并不屬于SDK,它是bytomd中構建交易、對交易簽名兩大模塊的java實現版。因此,若想用tx-signer對交易進行離線簽名,需要由你在本地保管好自己的私鑰。...
摘要:在比原鏈主網中,在獲取交易和區塊頭等摘要的過程中使用的哈希算法是算法,而在國密測試網中,使用算法替代。啟動的是國密測試網。可以說,比原鏈的項目進展伴隨著國密測試網的發布更上一層樓。 比原項目倉庫: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockc... 國密算法是指國家密碼管理局制...
摘要:錯誤編號內容注釋非比原標準錯誤請求超時非法的請求體為網絡錯誤編號內容注釋區塊鏈網絡類型不匹配是簽名相關的錯誤編號內容注釋需要簽名的個數超過實際需求簽名的個數簽名格式錯誤缺少主公鑰主公鑰重復為交易相關的錯誤構建交易錯誤編號內容注釋資產余額不 0XX API錯誤 編號 內容 注釋 BTM000 Bytom API Error 非比原標準錯誤 BTM001 Request t...
摘要:是一款為了幫助開發者更簡單地理解的開發輔助工具,集合了校驗標注解碼測試水龍頭等功能。這一功能可以創建新的私鑰。鏈接比原水龍頭工具接口,開發者可以使用該工具接口領取比原測試。 showImg(https://segmentfault.com/img/bVbrThp?w=1920&h=1280); Bytom Kit是一款為了幫助開發者更簡單地理解Bytom的開發輔助工具,集合了校驗、標注...
閱讀 3976·2021-11-18 13:22
閱讀 1813·2021-11-17 09:33
閱讀 2877·2021-09-26 09:46
閱讀 1208·2021-08-21 14:11
閱讀 2884·2019-08-30 15:53
閱讀 2707·2019-08-30 15:52
閱讀 1885·2019-08-30 10:52
閱讀 1517·2019-08-29 15:30