From b19d20145aeeb34ae940ac7cc919e56b6778ea21 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Sat, 9 Nov 2019 11:00:04 +0800 Subject: [PATCH 1/8] feat: add output and script size calculating --- .../main/java/org/nervos/ckb/type/Script.java | 13 +++++++++++++ .../org/nervos/ckb/type/cell/CellOutput.java | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/ckb/src/main/java/org/nervos/ckb/type/Script.java b/ckb/src/main/java/org/nervos/ckb/type/Script.java index 89275c0c7..c4e58d94e 100644 --- a/ckb/src/main/java/org/nervos/ckb/type/Script.java +++ b/ckb/src/main/java/org/nervos/ckb/type/Script.java @@ -3,7 +3,9 @@ import com.google.gson.annotations.SerializedName; import org.nervos.ckb.Encoder; import org.nervos.ckb.crypto.Blake2b; +import org.nervos.ckb.utils.Numeric; import org.nervos.ckb.utils.Serializer; +import org.nervos.ckb.utils.Strings; /** Copyright © 2019 Nervos Foundation. All rights reserved. */ public class Script { @@ -38,4 +40,15 @@ public String computeHash() { blake2b.update(Encoder.encode(Serializer.serializeScript(this))); return blake2b.doFinalString(); } + + public int calculateByteSize() { + int byteSize = 1; + if (!Strings.isEmpty(codeHash)) { + byteSize += Numeric.hexStringToByteArray(codeHash).length / 2; + } + if (!Strings.isEmpty(args)) { + byteSize += Numeric.hexStringToByteArray(args).length / 2; + } + return byteSize; + } } diff --git a/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java b/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java index bb4dc1dc0..ea587f84b 100644 --- a/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java +++ b/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java @@ -1,6 +1,8 @@ package org.nervos.ckb.type.cell; import org.nervos.ckb.type.Script; +import org.nervos.ckb.utils.Numeric; +import org.nervos.ckb.utils.Strings; /** Copyright © 2018 Nervos Foundation. All rights reserved. */ public class CellOutput { @@ -20,4 +22,18 @@ public CellOutput(String capacity, Script lock, Script type) { this.lock = lock; this.type = type; } + + public int calculateByteSize(String data) { + int byteSize = 8; + if (!Strings.isEmpty(data)) { + byteSize += Numeric.hexStringToByteArray(data).length / 2; + } + if (lock != null) { + byteSize += lock.calculateByteSize(); + } + if (type != null) { + byteSize += type.calculateByteSize(); + } + return byteSize; + } } From 4a084fac174c834a18a9abf84d6bbdcb02b68685 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Sat, 9 Nov 2019 11:01:15 +0800 Subject: [PATCH 2/8] fix: update multisig transaction estimating fee --- .../ckb/transaction/TransactionBuilder.java | 16 +++++++++------- .../nervos/ckb/MultiSignTransactionExample.java | 2 +- .../nervos/ckb/transaction/CellCollector.java | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java b/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java index 4f65bee8c..4ebc68430 100644 --- a/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java +++ b/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java @@ -25,18 +25,19 @@ public class TransactionBuilder { private List cellOutputs = new ArrayList<>(); private List cellOutputsData = new ArrayList<>(); private List witnesses = new ArrayList<>(); - private boolean containMultiSig = false; + private boolean isMultiSig = false; public TransactionBuilder(Api api) { this(api, false); } - public TransactionBuilder(Api api, boolean containMultiSig) { + public TransactionBuilder(Api api, boolean isMultiSig) { try { - this.containMultiSig = containMultiSig; - this.systemSecpCell = SystemContract.getSystemSecpCell(api); - if (containMultiSig) { + this.isMultiSig = isMultiSig; + if (isMultiSig) { this.systemMultiSigCell = SystemContract.getSystemMultiSigCell(api); + } else { + this.systemSecpCell = SystemContract.getSystemSecpCell(api); } } catch (Exception e) { e.printStackTrace(); @@ -83,9 +84,10 @@ public Transaction buildTx() throws IOException { } List cellDeps = new ArrayList<>(); - cellDeps.add(new CellDep(systemSecpCell.outPoint, CellDep.DEP_GROUP)); - if (containMultiSig) { + if (isMultiSig) { cellDeps.add(new CellDep(systemMultiSigCell.outPoint, CellDep.DEP_GROUP)); + } else { + cellDeps.add(new CellDep(systemSecpCell.outPoint, CellDep.DEP_GROUP)); } return new Transaction( "0", diff --git a/example/src/main/java/org/nervos/ckb/MultiSignTransactionExample.java b/example/src/main/java/org/nervos/ckb/MultiSignTransactionExample.java index 0a0ace5e1..982a7ff5f 100644 --- a/example/src/main/java/org/nervos/ckb/MultiSignTransactionExample.java +++ b/example/src/main/java/org/nervos/ckb/MultiSignTransactionExample.java @@ -120,7 +120,7 @@ public static Transaction generateTx( Collections.singletonList(configuration.address()), cellOutputs, feeRate, - configuration.serialize().length() + configuration.requireN * Sign.SIGN_LENGTH * 2); + configuration.serialize().length() + configuration.threshold * Sign.SIGN_LENGTH * 2); int startIndex = 0; for (CellsWithAddress cellsWithAddress : cellsWithAddresses) { txBuilder.addInputs(cellsWithAddress.inputs); diff --git a/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java b/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java index 31c965831..a58fbdc49 100644 --- a/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java +++ b/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java @@ -32,9 +32,9 @@ public Map> collectInputs( throws IOException { List cellOutputsData = new ArrayList<>(); for (int i = 0; i < cellOutputs.size() - 1; i++) { - int size = Serializer.serializeCellOutput(cellOutputs.get(i)).getLength(); + int size = cellOutputs.get(i).calculateByteSize("0x"); if (BigInteger.valueOf(size).compareTo(Numeric.toBigInt(cellOutputs.get(i).capacity)) > 0) { - throw new IOException("Cell output serialize size must not be bigger than capacity"); + throw new IOException("Cell output byte size must not be bigger than capacity"); } cellOutputsData.add("0x"); } From bcc1114ebdb5e388ad86e2b14e7b935fd0745ffe Mon Sep 17 00:00:00 2001 From: duanyytop Date: Sat, 9 Nov 2019 12:20:58 +0800 Subject: [PATCH 3/8] refactor: rename calculateByteSize to occupiedCapacity --- ckb/src/main/java/org/nervos/ckb/type/Script.java | 2 +- ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java | 6 +++--- .../main/java/org/nervos/ckb/transaction/CellCollector.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ckb/src/main/java/org/nervos/ckb/type/Script.java b/ckb/src/main/java/org/nervos/ckb/type/Script.java index c4e58d94e..b3bcb7ca2 100644 --- a/ckb/src/main/java/org/nervos/ckb/type/Script.java +++ b/ckb/src/main/java/org/nervos/ckb/type/Script.java @@ -41,7 +41,7 @@ public String computeHash() { return blake2b.doFinalString(); } - public int calculateByteSize() { + public int occupiedCapacity() { int byteSize = 1; if (!Strings.isEmpty(codeHash)) { byteSize += Numeric.hexStringToByteArray(codeHash).length / 2; diff --git a/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java b/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java index ea587f84b..b529a9231 100644 --- a/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java +++ b/ckb/src/main/java/org/nervos/ckb/type/cell/CellOutput.java @@ -23,16 +23,16 @@ public CellOutput(String capacity, Script lock, Script type) { this.type = type; } - public int calculateByteSize(String data) { + public int occupiedCapacity(String data) { int byteSize = 8; if (!Strings.isEmpty(data)) { byteSize += Numeric.hexStringToByteArray(data).length / 2; } if (lock != null) { - byteSize += lock.calculateByteSize(); + byteSize += lock.occupiedCapacity(); } if (type != null) { - byteSize += type.calculateByteSize(); + byteSize += type.occupiedCapacity(); } return byteSize; } diff --git a/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java b/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java index a58fbdc49..363300173 100644 --- a/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java +++ b/example/src/main/java/org/nervos/ckb/transaction/CellCollector.java @@ -32,7 +32,7 @@ public Map> collectInputs( throws IOException { List cellOutputsData = new ArrayList<>(); for (int i = 0; i < cellOutputs.size() - 1; i++) { - int size = cellOutputs.get(i).calculateByteSize("0x"); + int size = cellOutputs.get(i).occupiedCapacity("0x"); if (BigInteger.valueOf(size).compareTo(Numeric.toBigInt(cellOutputs.get(i).capacity)) > 0) { throw new IOException("Cell output byte size must not be bigger than capacity"); } From 5ba9cc112f5df6d23e123a4af3709645b7cfac55 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Mon, 11 Nov 2019 09:56:27 +0800 Subject: [PATCH 4/8] docs: update changelog and bump to v0.25.0 --- CHANGELOG.md | 7 +++++++ build.gradle | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6c75ec6f..e04e5c1fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [v0.25.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.24.6...v0.25.0) (2019-11-16) + +### Feature + +* Update cell output and script size calculating([b19d201](https://github.com/nervosnetwork/ckb-sdk-java/commit/b19d20145aeeb34ae940ac7cc919e56b6778ea21)) +* Update multisig transaction estimating fee([4a084fa](https://github.com/nervosnetwork/ckb-sdk-java/commit/4a084fac174c834a18a9abf84d6bbdcb02b68685)) + # [v0.24.6](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.24.5...v0.24.6) (2019-11-09) ### BugFix diff --git a/build.gradle b/build.gradle index 459d1a459..f3fceb8e1 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ allprojects { targetCompatibility = 1.8 group 'org.nervos.ckb' - version '0.24.6' + version '0.25.0' apply plugin: 'java' @@ -113,7 +113,7 @@ configure(subprojects.findAll { it.name != 'tests' }) { publications { mavenJava(MavenPublication) { groupId 'org.nervos.ckb' - version '0.24.6' + version '0.25.0' from components.java } } From 2709d570c61db110b37103cca00b60b84caaaf10 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 12 Nov 2019 16:42:05 +0800 Subject: [PATCH 5/8] chore: remove useless min capacity --- .../java/org/nervos/ckb/transaction/TransactionBuilder.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java b/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java index 4ebc68430..c029e4314 100644 --- a/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java +++ b/ckb/src/main/java/org/nervos/ckb/transaction/TransactionBuilder.java @@ -17,8 +17,6 @@ /** Copyright © 2019 Nervos Foundation. All rights reserved. */ public class TransactionBuilder { - private static final BigInteger MIN_CAPACITY = new BigInteger("6000000000"); - private SystemScriptCell systemSecpCell; private SystemScriptCell systemMultiSigCell; private List cellInputs = new ArrayList<>(); @@ -73,9 +71,6 @@ public Transaction buildTx() throws IOException { for (CellOutput output : cellOutputs) { needCapacity = needCapacity.add(Numeric.toBigInt(output.capacity)); } - if (needCapacity.compareTo(MIN_CAPACITY) < 0) { - throw new IOException("Less than min capacity"); - } if (cellInputs.size() == 0) { throw new IOException("Cell inputs could not empty"); } From 21b99d2f43a0fbe2da2f5d8237f5b2d8c8c9e5fd Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 12 Nov 2019 18:11:33 +0800 Subject: [PATCH 6/8] chore: remove useless signature --- .../org/nervos/ckb/transaction/Secp256k1SighashAllBuilder.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/ckb/src/main/java/org/nervos/ckb/transaction/Secp256k1SighashAllBuilder.java b/ckb/src/main/java/org/nervos/ckb/transaction/Secp256k1SighashAllBuilder.java index 73ce3819f..1578bab61 100644 --- a/ckb/src/main/java/org/nervos/ckb/transaction/Secp256k1SighashAllBuilder.java +++ b/ckb/src/main/java/org/nervos/ckb/transaction/Secp256k1SighashAllBuilder.java @@ -59,9 +59,6 @@ public void sign(ScriptGroup scriptGroup, String privateKey) throws IOException } String message = blake2b.doFinalString(); ECKeyPair ecKeyPair = ECKeyPair.createWithPrivateKey(privateKey, false); - ((Witness) groupWitnesses.get(0)).lock = - Numeric.toHexString( - Sign.signMessage(Numeric.hexStringToByteArray(message), ecKeyPair).getSignature()); Witness signedWitness = (Witness) groupWitnesses.get(0); signedWitness.lock = From e777ade0b973b6390e658082810321be2bbab6c2 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 12 Nov 2019 18:34:39 +0800 Subject: [PATCH 7/8] docs: update changelog --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e04e5c1fb..1aa71564b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,13 +1,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. -# [v0.25.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.24.6...v0.25.0) (2019-11-16) +# [v0.24.7](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.24.6...v0.24.7) (2019-11-12) ### Feature * Update cell output and script size calculating([b19d201](https://github.com/nervosnetwork/ckb-sdk-java/commit/b19d20145aeeb34ae940ac7cc919e56b6778ea21)) * Update multisig transaction estimating fee([4a084fa](https://github.com/nervosnetwork/ckb-sdk-java/commit/4a084fac174c834a18a9abf84d6bbdcb02b68685)) +### BugFix + +* Remove useless min capacity([2709d57](https://github.com/nervosnetwork/ckb-sdk-java/commit/2709d570c61db110b37103cca00b60b84caaaf10)) + # [v0.24.6](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.24.5...v0.24.6) (2019-11-09) ### BugFix From 5d536431f157985b15f25d483c120f043e0085bb Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 12 Nov 2019 18:35:37 +0800 Subject: [PATCH 8/8] chore: bump version to v0.24.7 --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index f3fceb8e1..26d3fad51 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ allprojects { targetCompatibility = 1.8 group 'org.nervos.ckb' - version '0.25.0' + version '0.24.7' apply plugin: 'java' @@ -113,7 +113,7 @@ configure(subprojects.findAll { it.name != 'tests' }) { publications { mavenJava(MavenPublication) { groupId 'org.nervos.ckb' - version '0.25.0' + version '0.24.7' from components.java } }