Skip to content

Commit

Permalink
Merge pull request #136 from nervosnetwork/rc/0.19.0
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.19.0
  • Loading branch information
duanyytop authored Aug 28, 2019
2 parents 5de38d0 + 3ae94f5 commit 401d2e3
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 160 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [v0.19.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.18.0...v0.19.0) (2019-08-28)

### Feature

* Update transaction related data type([e36173d](https://github.com/nervosnetwork/ckb-sdk-java/commit/e36173dc0936ee838fc0ff29740c394c477b897d))
* Update transaction related data type([e5bbf46](https://github.com/nervosnetwork/ckb-sdk-java/commit/e5bbf46ac7cc80fb276e86fd91cf3540cf934899))
* Update transaction related data type([1087071](https://github.com/nervosnetwork/ckb-sdk-java/commit/108707191a69f1a49a4b505db032c53d6071481a))
* Remove epoch_reward of Epoch([ce2cdbb](https://github.com/nervosnetwork/ckb-sdk-java/commit/ce2cdbba4e3341f555f278f5103d05d5471a7336))
* Update genesis block system cell and code hash([4a81810](https://github.com/nervosnetwork/ckb-sdk-java/commit/4a8181066f1a9b1662b5f98d53d5e1af09dd8a95))
* Update genesis block system cell and code hash([3cb10bb](https://github.com/nervosnetwork/ckb-sdk-java/commit/3cb10bb9956ecd6b347dd9dfcdab08349621e903))
* Add compute lock hash rpc([3cb10bb](https://github.com/nervosnetwork/ckb-sdk-java/commit/5548addd05cc5aaff7d5bfab9865050884ac862c))

### Bugfix

* Fix transaction witness bug([77b7f67](https://github.com/nervosnetwork/ckb-sdk-java/commit/77b7f674eb95cafb126ccfabf9f9f41fa70797e3))

### Test

* Refactor wallet and sendCapacity test cases([05428e0](https://github.com/nervosnetwork/ckb-sdk-java/commit/05428e0cb5b953f196fd8c244457ffc2f5cb58a4))

### Breaking Changes

* Remove header seal and add nonce to header
* Update script hash generator
* Update genesis block system cell and code hash
* Remove cell_output data and add outputs_data to transaction
* Remove epoch_reward of epoch
* Update cell dep(cell_dep and header_dep)

# [v0.18.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.17.0...v0.18.0) (2019-08-10)

* Update to support CKB v0.18.0.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ gradle shadowJar // ./gradlew shadowJar
```
A `console-{version}-all.jar` package will be generated, which you can put into your project to develop with it.

If you don't want to generate the jar by yourself, you can download a build from [releases](https://github.com/nervosnetwork/ckb-sdk-java/releases).

#### Import Jar to Project

When you need to import `ckb-java-sdk` dependency to your project, you can add the `console-{version}-all.jar` to your project `libs` package.

If you use Java IDE (eg. IntelliJ IDEA or Eclipse or other Editors), you can import jar according to IDE option help documents.

### Usage

#### JSON-RPC
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ buildscript {
ext.rxjavaVersion = '2.2.11'
ext.gsonVersion = '2.8.5'
ext.jacksonVersion = '2.10.0.pr1'
ext.okhttpVersion = '4.0.1'
ext.loggingOkhttpVersion = '4.0.1'
ext.slf4jVersion = '1.7.27'
ext.okhttpVersion = '4.1.0'
ext.loggingOkhttpVersion = '4.1.0'
ext.slf4jVersion = '1.7.28'
ext.guavaVersion = '28.0-jre'

ext.junitVersion = '5.5.1'
Expand Down Expand Up @@ -36,7 +36,7 @@ allprojects {
targetCompatibility = 1.8

group 'org.nervos.ckb'
version '0.18.0'
version '0.19.0'

apply plugin: 'java'

Expand Down
2 changes: 1 addition & 1 deletion console/src/main/java/org/nervos/ckb/RpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) throws IOException {
System.out.println("Current block number: " + currentBlockNumber.toString());
System.out.println(
"Current block information: "
+ new Gson().toJson(client.getBlockByNumber(currentBlockNumber.toString(16))));
+ new Gson().toJson(client.getBlockByNumber(currentBlockNumber.toString())));
}

public Block getBlockByNumber(String blockNumber) throws IOException {
Expand Down
72 changes: 54 additions & 18 deletions console/src/main/java/org/nervos/ckb/Wallet.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.nervos.ckb;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.nervos.ckb.address.AddressUtils;
import org.nervos.ckb.crypto.Hash;
import org.nervos.ckb.crypto.secp256k1.Sign;
import org.nervos.ckb.methods.response.CkbTransactionHash;
import org.nervos.ckb.methods.type.OutPoint;
import org.nervos.ckb.methods.type.Script;
import org.nervos.ckb.methods.type.Witness;
import org.nervos.ckb.methods.type.cell.CellDep;
import org.nervos.ckb.methods.type.cell.CellInput;
import org.nervos.ckb.methods.type.cell.CellOutput;
import org.nervos.ckb.methods.type.cell.CellOutputWithOutPoint;
Expand All @@ -25,21 +28,29 @@ public class Wallet {

private static final String MIN_CAPACITY = "6000000000";

private Script lockScript;
private String privateKey;
private Script lockScript;
private SystemScriptCell systemScriptCell;
private CKBService ckbService;

public Wallet(String privateKey, Script lockScript, String nodeUrl) {
public Wallet(String privateKey, String nodeUrl) {
this.privateKey = privateKey;
this.lockScript = lockScript;
HttpService.setDebug(false);
ckbService = CKBService.build(new HttpService(nodeUrl));

try {
systemScriptCell = getSystemScriptCell(ckbService);
} catch (IOException e) {
e.printStackTrace();
}
lockScript = generateLockScript(privateKey, systemScriptCell.cellHash);
}

public String sendCapacity(List<Receiver> receivers) throws Exception {
Transaction transaction = generateTx(receivers);
CkbTransactionHash ckbTransactionHash = ckbService.sendTransaction(transaction).send();
if (ckbTransactionHash.result == null) {
System.out.println(ckbTransactionHash.error.message);
if (ckbTransactionHash.error != null) {
throw new IOException(ckbTransactionHash.error.message);
}
return ckbTransactionHash.getTransactionHash();
}
Expand All @@ -53,37 +64,47 @@ private Transaction generateTx(List<Receiver> receivers) throws Exception {
throw new Exception("Less than min capacity");
}

CellInputs cellInputs = getCellInputs(lockScript.scriptHash(), needCapacities);
CellInputs cellInputs = getCellInputs(getLockHash(lockScript), needCapacities);
if (cellInputs.capacity.compareTo(needCapacities) < 0) {
throw new Exception("No enough Capacities");
}

SystemScriptCell systemScriptCell =
SystemContract.getSystemScriptCell(ckbService, Network.TESTNET);
List<CellOutput> cellOutputs = new ArrayList<>();
AddressUtils addressUtils = new AddressUtils(Network.TESTNET);
for (Receiver receiver : receivers) {
String blake2b = addressUtils.getBlake160FromAddress(receiver.address);
cellOutputs.add(
new CellOutput(
receiver.capacity.toString(),
"0x",
new Script(systemScriptCell.cellHash, Arrays.asList(blake2b))));
new Script(
systemScriptCell.cellHash, Collections.singletonList(blake2b), Script.TYPE)));
}

if (cellInputs.capacity.compareTo(needCapacities) > 0) {
cellOutputs.add(
new CellOutput(
cellInputs.capacity.subtract(needCapacities).toString(10), "0x", lockScript));
new CellOutput(cellInputs.capacity.subtract(needCapacities).toString(10), lockScript));
}

List<Witness> witnesses = new ArrayList<>();
int len = cellInputs.inputs.size();
for (int i = 0; i < len; i++) {
witnesses.add(new Witness());
}

List<String> cellOutputsData = new ArrayList<>();
for (int i = 0; i < cellOutputs.size(); i++) {
cellOutputsData.add("0x");
}

Transaction transaction =
new Transaction(
"0",
Collections.singletonList(new OutPoint(null, systemScriptCell.outPoint)),
Collections.singletonList(new CellDep(systemScriptCell.outPoint, CellDep.DEP_GROUP)),
Collections.emptyList(),
cellInputs.inputs,
cellOutputs,
new ArrayList<>());
cellOutputsData,
witnesses);

String txHash = ckbService.computeTransactionHash(transaction).send().getTransactionHash();
return transaction.sign(Numeric.toBigInt(privateKey), txHash);
Expand Down Expand Up @@ -120,11 +141,26 @@ private CellInputs getCellInputs(String lockHash, BigInteger needCapacities) thr
return new CellInputs(cellInputs, new BigDecimal(inputsCapacities).toBigInteger());
}

class CellInputs {
private Script generateLockScript(String privateKey, String codeHash) {
String publicKey = Sign.publicKeyFromPrivate(Numeric.toBigInt(privateKey), true).toString(16);
String blake160 =
Numeric.prependHexPrefix(Numeric.cleanHexPrefix(Hash.blake2b(publicKey)).substring(0, 40));
return new Script(codeHash, Collections.singletonList(blake160), Script.TYPE);
}

private String getLockHash(Script script) throws IOException {
return ckbService.computeScriptHash(script).send().getScriptHash();
}

private SystemScriptCell getSystemScriptCell(CKBService ckbService) throws IOException {
return SystemContract.getSystemScriptCell(ckbService);
}

static class CellInputs {
List<CellInput> inputs;
BigInteger capacity;

public CellInputs(List<CellInput> inputs, BigInteger capacity) {
CellInputs(List<CellInput> inputs, BigInteger capacity) {
this.inputs = inputs;
this.capacity = capacity;
}
Expand Down
33 changes: 8 additions & 25 deletions console/src/test/java/org/nervos/ckb/WalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,22 @@
import java.math.BigInteger;
import java.util.Collections;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.nervos.ckb.crypto.Hash;
import org.nervos.ckb.crypto.secp256k1.Sign;
import org.nervos.ckb.methods.type.Script;
import org.nervos.ckb.utils.Numeric;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class WalletTest {
class WalletTest {

private Wallet transaction;
private static final String PRIVATE_KEY =
"e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e4";

private static final String RECEIVE_ADDRESS =
"ckt1q9gry5zgaqtljzuuptt987pncdu9txh5570myf9amk0q3v";
private static final String CODE_HASH =
"0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08";

@BeforeAll
public void init() {
String publicKey = Sign.publicKeyFromPrivate(Numeric.toBigInt(PRIVATE_KEY), true).toString(16);
String blake160 =
Numeric.prependHexPrefix(Numeric.cleanHexPrefix(Hash.blake2b(publicKey)).substring(0, 40));
Script inputLockScript = new Script(CODE_HASH, Collections.singletonList(blake160));
transaction = new Wallet(PRIVATE_KEY, inputLockScript, "http://localhost:8114");
}
"e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3";
private static final String RECEIVE_ADDRESS = "ckt1qyqqtdpzfjwq7e667ktjwnv3hngrqkmwyhhqpa8dav";
private static final String NODE_URL = "http://localhost:8114";

@Test
public void testSendCapacity() throws Exception {
Wallet.Receiver receiver = new Wallet.Receiver(RECEIVE_ADDRESS, new BigInteger("6000000000"));
String hash = transaction.sendCapacity(Collections.singletonList(receiver));
void testSendCapacity() throws Exception {
Wallet wallet = new Wallet(PRIVATE_KEY, NODE_URL);
Wallet.Receiver receiver = new Wallet.Receiver(RECEIVE_ADDRESS, new BigInteger("10000000000"));
String hash = wallet.sendCapacity(Collections.singletonList(receiver));
Assertions.assertNotNull(hash);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.nervos.ckb.methods.response;

import org.nervos.ckb.methods.Response;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class CkbScriptHash extends Response<String> {

public String getScriptHash() {
return result;
}
}
3 changes: 0 additions & 3 deletions core/src/main/java/org/nervos/ckb/methods/type/Epoch.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
public class Epoch {
public String number;

@JsonProperty("epoch_reward")
public String epochReward;

@JsonProperty("start_number")
public String startNumber;

Expand Down
9 changes: 1 addition & 8 deletions core/src/main/java/org/nervos/ckb/methods/type/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Header {
public String dao;
public String difficulty;
public String hash;
public String nonce;
public String number;
public String epoch;

Expand All @@ -32,12 +33,4 @@ public class Header {
public String unclesHash;

public int version;

public Seal seal;

public static class Seal {
public String nonce;

public String proof;
}
}
19 changes: 7 additions & 12 deletions core/src/main/java/org/nervos/ckb/methods/type/OutPoint.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package org.nervos.ckb.methods.type;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.nervos.ckb.methods.type.cell.CellOutPoint;

/** Copyright © 2018 Nervos Foundation. All rights reserved. */
/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class OutPoint {

@JsonProperty("block_hash")
public String blockHash;
@JsonProperty("tx_hash")
public String txHash;

public CellOutPoint cell;
public String index;

public OutPoint() {}

public OutPoint(CellOutPoint cellOutPoint) {
this.cell = cellOutPoint;
}

public OutPoint(String blockHash, CellOutPoint cellOutPoint) {
this.blockHash = blockHash;
this.cell = cellOutPoint;
public OutPoint(String txHash, String index) {
this.txHash = txHash;
this.index = index;
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/org/nervos/ckb/methods/type/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class Script {

public static final String DATA = "Data";
public static final String TYPE = "Type";
public static final String DATA = "data";
public static final String TYPE = "type";

@JsonProperty("code_hash")
public String codeHash;
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/org/nervos/ckb/methods/type/cell/CellDep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.nervos.ckb.methods.type.cell;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.nervos.ckb.methods.type.OutPoint;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class CellDep {

public static final String CODE = "code";
public static final String DEP_GROUP = "dep_group";

@JsonProperty("out_point")
public OutPoint outPoint;

@JsonProperty("dep_type")
public String depType;

public CellDep() {}

public CellDep(OutPoint outPoint, String depType) {
this.outPoint = outPoint;
this.depType = depType;
}
}
Loading

0 comments on commit 401d2e3

Please sign in to comment.