From 394f525b75b9c8fa12a6764c8ee5f1b79a7a8872 Mon Sep 17 00:00:00 2001 From: shenao78 Date: Thu, 21 Mar 2019 16:50:22 +0800 Subject: [PATCH] add readme --- README.md | 8 + tx-signer/README.md | 278 +++++++-------------- .../src/main/java/io/bytom/offline/api/.DS_Store | Bin 0 -> 6148 bytes .../java/io/bytom/offline/api/IssuanceInput.java | 2 +- .../java/io/bytom/offline/api/Transaction.java | 1 + tx-signer/src/test/java/io/bytom/AppTest.java | 5 +- 6 files changed, 108 insertions(+), 186 deletions(-) create mode 100644 tx-signer/src/main/java/io/bytom/offline/api/.DS_Store diff --git a/README.md b/README.md index e69de29..7260f58 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,8 @@ +## Bytom Java SDK + +Bytom java sdk contains two modules: + +1. java-sdk encapsulates of the request data and return value of the bytom api,The signature of the transaction needs to be completed online.See the [java-sdk documentation](java-sdk/README.md) for details. + +2. tx-signer implementation offline signature of transaction, see the [tx-signer documentation](tx-signer/README.md) for details. + diff --git a/tx-signer/README.md b/tx-signer/README.md index ae213ae..b5afb72 100755 --- a/tx-signer/README.md +++ b/tx-signer/README.md @@ -2,207 +2,117 @@ # tx_signer -Java implementation of signing transaction offline to bytomd and serializing transaction to submit. +Java implementation of signing transaction offline. ## 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 +#### Add dependency to your project 1. first get source code - ``` + ``` git clone https://github.com/Bytom/bytom-java-sdk.git ``` -2. get jar package +2. install to maven repository ``` - $ mvn assembly:assembly -Dmaven.test.skip=true + $ mvn clean install -DskipTests ``` - You can get a jar with dependencies, and you can use it in your project. - -#### Test cases - -Need 3 Parameters: +3. add dependency + ```xml + + io.bytom + tx-signer + 1.0.0 + + ``` -- Private Keys Array -- UTXO list. -- Submit Transaction - - call submit-transaction api. [submit-transaction api](https://github.com/Bytom/bytom/wiki/API-Reference#submit-transaction) - - +## Example -```java +#### build transaction with spend input +``` + String btmAssetID = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; + + // create spend input + SpendInput input = new SpendInput(); + input.setAssetId(btmAssetID); + input.setAmount(9800000000L); + + // control program of spend utxo + input.setProgram("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"); + + // source position of spend utxo + input.setSourcePosition(2); + + // source id of spend utxo + input.setSourceID("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"); + + // is the spend utxo used for change + input.setChange(true); + + // BIP protocol for derived paths, default BIP44 + input.setBipProtocol(BIPProtocol.BIP32); + + // contorl program index of spend utxo + input.setControlProgramIndex(457); + + // account index + input.setKeyIndex(1); + + // provide a root private key for signing + input.setRootPrivateKey("4864bae85cf38bfbb347684abdbc01e311a24f99e2c7fe94f3c071d9c83d8a5a349722316972e382c339b79b7e1d83a565c6b3e7cf46847733a47044ae493257"); -AnnotatedBaseInput - String utxoJson = "{\n" + - " \"id\": \"cf2f5c7340490d33d535a680dc8d95bb66fcccbf1045706484621cc067b982ae\",\n" + - " \"amount\": 70000000,\n" + - " \"address\": \"tm1qf4g97wae3fsz973huwrjqnd68v530nmd7n2pc43zfpw9tvd30qfsd7jl8p\",\n" + - " \"program\": \"00204d505f3bb98a6022fa37e387204dba3b2917cf6df4d41c5622485c55b1b17813\",\n" + - " \"change\": false,\n" + - " \"highest\": 140925,\n" + - " \"account_alias\": \"mutiaccout\",\n" + - " \"asset_id\": \"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\",\n" + - " \"asset_alias\": \"BTM\",\n" + - " \"account_id\": \"0PCB0S1GG0A02\",\n" + - " \"control_program_index\": 2,\n" + - " \"source_id\": \"9b29c72a653f986d5c5a7bf16c0fe63a9f639a0d15f3faeabeb4c14df70bbd91\",\n" + - " \"source_pos\": 0,\n" + - " \"valid_height\": 0,\n" + - " \"derive_rule\": 0\n" + - "}"; - UTXO utxo = UTXO.fromJson(utxoJson); - Transaction.AnnotatedInput input = UTXO.utxoToAnnotatedInput(utxo); - return input; -} + // build transaction with signature + Transaction tx = new Transaction.Builder() + .addInput(input) + .addOutput(new Output(btmAssetID, 8800000000L, "0014a82f02bc37bc5ed87d5f9fca02f8a6a7d89cdd5c")) + .addOutput(new Output(btmAssetID, 900000000L, "00200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66")) + .setTimeRange(0) + .build(); -``` - -Single-key Example : - -```java -String rootKey = "38d2c44314c401b3ea7c23c54e12c36a527aee46a7f26b82443a46bf40583e439dea25de09b0018b35a741d8cd9f6ec06bc11db49762617485f5309ab72a12d4"; -String btmAssetID = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; - - - @Test - public void testSpend() throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { - Transaction.AnnotatedInput input = btmUtxoToInput(); - Transaction Transaction = new Transaction.Builder() -// .addInput(new Transaction.AnnotatedInput().setType(1).setAssetId(btmAssetID).setAmount(880000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b"). -// setChange(false).setControlProgramIndex(2).setSourceId("fc43933d1c601b2503b033e31d3bacfa5c40ccb2ff0be6e94d8332462e0928a3").setSourcePosition(0)) - .addInput(input.setType(1)) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(btmAssetID).setAmount(170000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b")) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(btmAssetID).setAmount(10000000).setControlProgram("0020fa56ca7d47f8528e68e120d0e052885faeb9d090d238fa4266bdde21b137513c")) - .build(200000); - Transaction transaction = MapTransaction.mapTx(Transaction); - SignTransaction signTransaction = new SignTransaction(); - String rawTransaction = signTransaction.rawTransaction(rootKey, transaction); - System.out.println(rawTransaction); - } - - //issue asset - @Test - public void testIssue() throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { - - String issueAssetId = "a680606d49daae62ef9cb03263ca82a0b1e3184bb6311ea52a5189207f718789"; - String program = "ae204cae24c2cec15491e70fc554026469496e373df9b9970b23acac8b782da0822d5151ad"; - String assetDefine = "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"; - Transaction Transaction = new Transaction.Builder() - .addInput(new Transaction.AnnotatedInput().setType(0).setAssetId(issueAssetId).setControlProgram(program).setAmount(100000000).setAssetDefinition(assetDefine).setChange(false).setKeyIndex(13)) - .addInput(new Transaction.AnnotatedInput().setType(1).setAssetId(btmAssetID).setAmount(880000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b"). - setChange(false).setControlProgramIndex(2).setSourceId("fc43933d1c601b2503b033e31d3bacfa5c40ccb2ff0be6e94d8332462e0928a3").setSourcePosition(0)) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(issueAssetId).setAmount(100000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b")) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(btmAssetID).setAmount(870000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b")) - .build(2000000); - MapTransaction.mapTx(Transaction); - SignTransaction sign = new SignTransaction(); - String rawTransaction = sign.rawTransaction(rootKey, Transaction); - System.out.println(rawTransaction); - } - - - //retire asset - @Test - public void testRetire() throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { - String arbitrary = "77656c636f6d65efbc8ce6aca2e8bf8ee69da5e588b0e58e9fe5ad90e4b896e7958c"; - String retireControlProgram = "6a"+Integer.toString(Hex.decode(arbitrary).length,16)+arbitrary; - String assetId1 = "207265909236260b30942a6b00e30ceb769e0e58156b6482bac64117619c9dcf"; - - Transaction transaction = new Transaction.Builder() - .addInput(new Transaction.AnnotatedInput().setType(1).setAssetId(btmAssetID).setAmount(890000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b"). - setChange(false).setControlProgramIndex(2).setSourceId("6026b1acb11f3cbfb5280865ea857117a76d41ba7d60509dd358648424d8496a").setSourcePosition(0)) - .addInput(new Transaction.AnnotatedInput().setType(1).setAssetId(assetId1).setAmount(100000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b"). - setChange(false).setControlProgramIndex(2).setSourceId("d53e7ccfa06d3b27933a44e5d6e3e288edfc7e38f5396b8552ef44cf58a20347").setSourcePosition(0)) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(btmAssetID).setAmount(880000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b")) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(assetId1).setAmount(100000000).setControlProgram(retireControlProgram )) - .build(2000000); - MapTransaction.mapTx(transaction); - SignTransaction sign = new SignTransaction(); - String rawTransaction = sign.rawTransaction(rootKey, transaction); - System.out.println(rawTransaction); - } + String rawTransaction = tx.rawTransaction(); +``` -``` - -Multi-keys Example: - -> Need an account has two or more keys. - -```java - //single input and multi-sign - @Test - public void testMutiSpend(){ - Transaction.AnnotatedInput input = btmUtxoToInput(); - Transaction Transaction = new Transaction.Builder() -// .addInput(new Transaction.AnnotatedInput().setType(1).setAssetId(btmAssetID).setAmount(880000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b"). -// setChange(false).setControlProgramIndex(2).setSourceId("fc43933d1c601b2503b033e31d3bacfa5c40ccb2ff0be6e94d8332462e0928a3").setSourcePosition(0)) - .addInput(input.setType(1)) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(btmAssetID).setAmount(50000000).setControlProgram("00204d505f3bb98a6022fa37e387204dba3b2917cf6df4d41c5622485c55b1b17813")) - .addOutput(new Transaction.AnnotatedOutput().setAssetId(btmAssetID).setAmount(10000000).setControlProgram("001414d362694eacfa110dc20dec77d610d22340f95b")) - .build(200000); - Transaction transaction = MapTransaction.mapTx(Transaction); - SignTransaction signTransaction = new SignTransaction(); - String[] rootKeys = new String[3]; - rootKeys[0] = "38d2c44314c401b3ea7c23c54e12c36a527aee46a7f26b82443a46bf40583e439dea25de09b0018b35a741d8cd9f6ec06bc11db49762617485f5309ab72a12d4"; - rootKeys[1] = "50a23bf6200b8a98afc049a7d0296a619e2ee27fa0d6d4d271ca244b280b324347627e543cc079614642c7b88c78ce38092430b01d124663e8b84026aefefde1"; - rootKeys[2] = "00e4bf1251fb5aa37aa2a11dec6c0db5cec3f17aa312dbddb30e06957a32ae503ebcdfd4ad5e29be21ee9ec336e939eb72439cf6d99c785268c8f3d71c1be877"; - signTransaction.buildWitness(transaction, 0, rootKeys); - String raw = signTransaction.serializeTransaction(transaction); - System.out.println(raw); - } +#### build transaction with issuance input ``` + IssuanceInput issuanceInput = new IssuanceInput(); + issuanceInput.setAssetId("7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad14"); + issuanceInput.setAmount(100000000000L); + + // issuance program + issuanceInput.setProgram("ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad"); + issuanceInput.setNonce("ac9d5a527f5ab00a"); + + // asset index + issuanceInput.setKeyIndex(5); + + // raw asset definition + issuanceInput.setRawAssetDefinition("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"); + + // provide a root private key for signing + issuanceInput.setRootPrivateKey("4864bae85cf38bfbb347684abdbc01e311a24f99e2c7fe94f3c071d9c83d8a5a349722316972e382c339b79b7e1d83a565c6b3e7cf46847733a47044ae493257"); + + SpendInput spendInput = new SpendInput(); + spendInput.setAssetId(btmAssetID); + spendInput.setAmount(9800000000L); + spendInput.setProgram("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"); + spendInput.setKeyIndex(1); + spendInput.setChange(true); + spendInput.setSourceID("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"); + spendInput.setSourcePosition(2); + spendInput.setControlProgramIndex(457); + spendInput.setRootPrivateKey("4864bae85cf38bfbb347684abdbc01e311a24f99e2c7fe94f3c071d9c83d8a5a349722316972e382c339b79b7e1d83a565c6b3e7cf46847733a47044ae493257"); + + Transaction tx = new Transaction.Builder() + .addInput(issuanceInput) + .addInput(spendInput) + .addOutput(new Output("7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad14", 100000000000L, "001437e1aec83a4e6587ca9609e4e5aa728db7007449")) + .addOutput(new Output(btmAssetID, 9700000000L, "00148be1104e04734e5edaba5eea2e85793896b77c56")) + .setTimeRange(0) + .build(); + + String rawTx = tx.rawTransaction(); -Submit-transaction: -```java -// submit rawTransaction -@Test -public void SubmitTransaction() throws BytomException { - Client client = TestUtil.generateClient(); - String raw = "0701c09a0c01016b01699b29c72a653f986d5c5a7bf16c0fe63a9f639a0d15f3faeabeb4c14df70bbd91ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80bbb02100012200204d505f3bb98a6022fa37e387204dba3b2917cf6df4d41c5622485c55b1b17813ac020440290bc8593d429d5c7d02f96232cf8035d3776eebf2a7e855c906cdbcf35281f98575624e326fbf1f9e8de70a7047b5af2f43c1d102fcf632d1544cff46aa970540ff237b6d2c5760bf633b640e120600d06e57ab3119d192b33894967a74293531507df473235233033f12c493bc005c8d1e2f858524a431edc7d522891dfada04406ebebe5d380e129fb212a777aca0f971cfe38a2e54cd9b822459609664303aa2e10caa10f5cb8eae4a53688f895a9cbe13e5be085dd289251c63a7c86718730f67ae204e4fcad6d69dfbad1fa83c37e6fd2031476940b44a9165fa79084e5a4d9acaed20415a96ffead835222ee96eda7c16e99a4c87e8e5e43e54d2d493855f707baf78209613d3c8a4aa730bb24e0324a920b2e3c8db7260ded4527f6d6d7d14d86b64385353ad020148ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80e1eb17012200204d505f3bb98a6022fa37e387204dba3b2917cf6df4d41c5622485c55b1b1781300013cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80ade2040116001414d362694eacfa110dc20dec77d610d22340f95b00"; - SubmitTransaction.SubmitResponse submitResponse = SubmitTransaction.submitRawTransaction(client, raw); - System.out.println(submitResponse.tx_id); -} +``` \ No newline at end of file diff --git a/tx-signer/src/main/java/io/bytom/offline/api/.DS_Store b/tx-signer/src/main/java/io/bytom/offline/api/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0