-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from nervosnetwork/rc/v0.35.1
Rc/v0.35.1
- Loading branch information
Showing
11 changed files
with
399 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
example/src/main/java/org/nervos/ckb/SingleSigWithCkbIndexerTxExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.nervos.ckb; | ||
|
||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.nervos.ckb.crypto.secp256k1.Sign; | ||
import org.nervos.ckb.service.Api; | ||
import org.nervos.ckb.service.CkbIndexerApi; | ||
import org.nervos.ckb.transaction.*; | ||
import org.nervos.ckb.type.Witness; | ||
import org.nervos.ckb.type.cell.CellOutput; | ||
import org.nervos.ckb.type.transaction.Transaction; | ||
import org.nervos.ckb.utils.Utils; | ||
|
||
/** Copyright © 2019 Nervos Foundation. All rights reserved. */ | ||
public class SingleSigWithCkbIndexerTxExample { | ||
|
||
private static final String NODE_URL = "http://localhost:8118"; | ||
private static final String CKB_INDEXER_NODE_URL = "http://localhost:8116"; | ||
private static final BigInteger UnitCKB = new BigInteger("100000000"); | ||
private static Api api; | ||
private static CkbIndexerApi ckbIndexerApi; | ||
private static List<String> SendPrivateKeys; | ||
private static List<String> SendAddresses; | ||
private static List<String> ReceiveAddresses; | ||
|
||
static { | ||
api = new Api(NODE_URL, false); | ||
ckbIndexerApi = new CkbIndexerApi(CKB_INDEXER_NODE_URL, false); | ||
SendPrivateKeys = | ||
Arrays.asList( | ||
"08730a367dfabcadb805d69e0e613558d5160eb8bab9d6e326980c2c46a05db2", | ||
"d00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc"); | ||
SendAddresses = | ||
Arrays.asList( | ||
"ckt1qyqxgp7za7dajm5wzjkye52asc8fxvvqy9eqlhp82g", | ||
"ckt1qyqvsv5240xeh85wvnau2eky8pwrhh4jr8ts8vyj37"); | ||
ReceiveAddresses = | ||
Arrays.asList( | ||
"ckt1qyqxvnycu7tdtyuejn3mmcnl4y09muxz8c3s2ewjd4", | ||
"ckt1qyqtnz38fht9nvmrfdeunrhdtp29n0gagkps4duhek"); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
System.out.println("Wait some time for ckb-indexer running"); | ||
|
||
List<Receiver> receivers = | ||
Arrays.asList( | ||
new Receiver(ReceiveAddresses.get(0), Utils.ckbToShannon(800)), | ||
new Receiver(ReceiveAddresses.get(1), Utils.ckbToShannon(900))); | ||
|
||
System.out.println( | ||
"Before transferring, first sender's balance: " | ||
+ getBalance(SendAddresses.get(0)).divide(UnitCKB).toString(10) | ||
+ " CKB"); | ||
|
||
String hash = sendCapacity(receivers, SendAddresses.get(0)); | ||
System.out.println("Transaction hash: " + hash); | ||
|
||
// waiting transaction into block, sometimes you should wait more seconds | ||
Thread.sleep(30000); | ||
|
||
System.out.println( | ||
"After transferring, first sender's balance: " | ||
+ getBalance(SendAddresses.get(0)).divide(UnitCKB).toString(10) | ||
+ " CKB"); | ||
} | ||
|
||
private static BigInteger getBalance(String address) throws IOException { | ||
return new CollectUtils(ckbIndexerApi).getCapacityWithAddressByCkbIndexer(address); | ||
} | ||
|
||
private static String sendCapacity(List<Receiver> receivers, String changeAddress) | ||
throws IOException { | ||
List<ScriptGroupWithPrivateKeys> scriptGroupWithPrivateKeysList = new ArrayList<>(); | ||
|
||
TransactionBuilder txBuilder = new TransactionBuilder(api); | ||
CollectUtils txUtils = new CollectUtils(ckbIndexerApi); | ||
|
||
List<CellOutput> cellOutputs = txUtils.generateOutputs(receivers, changeAddress); | ||
txBuilder.addOutputs(cellOutputs); | ||
|
||
// You can get fee rate by rpc or set a simple number | ||
BigInteger feeRate = BigInteger.valueOf(1024); | ||
|
||
// initial_length = 2 * secp256k1_signature_byte.length | ||
// collectInputsWithIndexer method uses indexer rpc to collect cells quickly | ||
CollectResult collectResult = | ||
txUtils.collectInputsWithCkbIndexer( | ||
SendAddresses, txBuilder.buildTx(), feeRate, Sign.SIGN_LENGTH * 2, true); | ||
|
||
// update change cell output capacity after collecting cells | ||
cellOutputs.get(cellOutputs.size() - 1).capacity = collectResult.changeCapacity; | ||
txBuilder.setOutputs(cellOutputs); | ||
|
||
int startIndex = 0; | ||
for (CellsWithAddress cellsWithAddress : collectResult.cellsWithAddresses) { | ||
txBuilder.addInputs(cellsWithAddress.inputs); | ||
for (int i = 0; i < cellsWithAddress.inputs.size(); i++) { | ||
txBuilder.addWitness(i == 0 ? new Witness(Witness.SIGNATURE_PLACEHOLDER) : "0x"); | ||
} | ||
if (cellsWithAddress.inputs.size() > 0) { | ||
scriptGroupWithPrivateKeysList.add( | ||
new ScriptGroupWithPrivateKeys( | ||
new ScriptGroup( | ||
NumberUtils.regionToList(startIndex, cellsWithAddress.inputs.size())), | ||
Collections.singletonList( | ||
SendPrivateKeys.get(SendAddresses.indexOf(cellsWithAddress.address))))); | ||
startIndex += cellsWithAddress.inputs.size(); | ||
} | ||
} | ||
|
||
Secp256k1SighashAllBuilder signBuilder = new Secp256k1SighashAllBuilder(txBuilder.buildTx()); | ||
|
||
for (ScriptGroupWithPrivateKeys scriptGroupWithPrivateKeys : scriptGroupWithPrivateKeysList) { | ||
signBuilder.sign( | ||
scriptGroupWithPrivateKeys.scriptGroup, scriptGroupWithPrivateKeys.privateKeys.get(0)); | ||
} | ||
Transaction tx = signBuilder.buildTx(); | ||
return api.sendTransaction(tx); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
example/src/main/java/org/nervos/ckb/indexer/CkbIndexerCell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.nervos.ckb.indexer; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import org.nervos.ckb.type.OutPoint; | ||
import org.nervos.ckb.type.cell.CellOutput; | ||
|
||
public class CkbIndexerCell { | ||
@SerializedName("block_number") | ||
public String blockNumber; | ||
|
||
@SerializedName("out_point") | ||
public OutPoint outPoint; | ||
|
||
public CellOutput output; | ||
|
||
@SerializedName("output_data") | ||
public String outputData; | ||
|
||
@SerializedName("tx_index") | ||
public String txIndex; | ||
} |
11 changes: 11 additions & 0 deletions
11
example/src/main/java/org/nervos/ckb/indexer/CkbIndexerCellResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.nervos.ckb.indexer; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import java.util.List; | ||
|
||
public class CkbIndexerCellResponse { | ||
public List<CkbIndexerCell> objects; | ||
|
||
@SerializedName("last_cursor") | ||
public String lastCursor; | ||
} |
13 changes: 13 additions & 0 deletions
13
example/src/main/java/org/nervos/ckb/indexer/CkbIndexerCellsCapacityResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.nervos.ckb.indexer; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class CkbIndexerCellsCapacityResponse { | ||
@SerializedName("block_hash") | ||
public String blockHash; | ||
|
||
@SerializedName("block_number") | ||
public String blockNumber; | ||
|
||
public String capacity; | ||
} |
21 changes: 21 additions & 0 deletions
21
example/src/main/java/org/nervos/ckb/indexer/SearchKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.nervos.ckb.indexer; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import org.nervos.ckb.type.Script; | ||
|
||
public class SearchKey { | ||
public Script script; | ||
|
||
@SerializedName("script_type") | ||
public String scriptType; | ||
|
||
public SearchKey(Script script, String scriptType) { | ||
this.script = script; | ||
this.scriptType = scriptType; | ||
} | ||
|
||
public SearchKey(Script script) { | ||
this.script = script; | ||
this.scriptType = "lock"; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
example/src/main/java/org/nervos/ckb/service/CkbIndexerApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.nervos.ckb.service; | ||
|
||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import org.nervos.ckb.indexer.CkbIndexerCellResponse; | ||
import org.nervos.ckb.indexer.CkbIndexerCellsCapacityResponse; | ||
import org.nervos.ckb.indexer.SearchKey; | ||
import org.nervos.ckb.utils.Numeric; | ||
|
||
/** Copyright © 2020 Nervos Foundation. All rights reserved. */ | ||
public class CkbIndexerApi { | ||
|
||
private RpcService rpcService; | ||
|
||
public CkbIndexerApi(String nodeUrl) { | ||
this(nodeUrl, false); | ||
} | ||
|
||
public CkbIndexerApi(String nodeUrl, boolean isDebug) { | ||
rpcService = new RpcService(nodeUrl, isDebug); | ||
} | ||
|
||
public CkbIndexerCellResponse getCells( | ||
SearchKey searchKey, String order, BigInteger limit, String afterCursor) throws IOException { | ||
if ("0x".equals(afterCursor)) { | ||
return rpcService.post( | ||
"get_cells", | ||
Arrays.asList(searchKey, order, Numeric.toHexStringWithPrefix(limit)), | ||
CkbIndexerCellResponse.class); | ||
} else { | ||
return rpcService.post( | ||
"get_cells", | ||
Arrays.asList(searchKey, order, Numeric.toHexStringWithPrefix(limit), afterCursor), | ||
CkbIndexerCellResponse.class); | ||
} | ||
} | ||
|
||
public CkbIndexerCellsCapacityResponse getCellsCapacity(SearchKey searchKey) throws IOException { | ||
return rpcService.post( | ||
"get_cells_capacity", | ||
Collections.singletonList(searchKey), | ||
CkbIndexerCellsCapacityResponse.class); | ||
} | ||
} |
Oops, something went wrong.