-
Notifications
You must be signed in to change notification settings - Fork 25
/
SendCkbByLightClientExample.java
49 lines (40 loc) · 2.22 KB
/
SendCkbByLightClientExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.nervos.ckb.example;
import com.nervos.lightclient.DefaultLightClientApi;
import com.nervos.lightclient.LightClientApi;
import com.nervos.lightclient.type.ScriptDetail;
import org.nervos.ckb.Network;
import org.nervos.ckb.sign.TransactionSigner;
import org.nervos.ckb.sign.TransactionWithScriptGroups;
import org.nervos.ckb.transaction.CkbTransactionBuilder;
import org.nervos.ckb.transaction.LightClientInputIterator;
import org.nervos.ckb.transaction.TransactionBuilderConfiguration;
import org.nervos.ckb.type.ScriptType;
import org.nervos.ckb.utils.Numeric;
import org.nervos.ckb.utils.address.Address;
import java.io.IOException;
import java.util.Collections;
public class SendCkbByLightClientExample {
public static void main(String[] args) throws IOException {
Network network = Network.TESTNET;
String sender = "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r";
TransactionBuilderConfiguration configuration = new TransactionBuilderConfiguration(network);
LightClientApi lightClientApi = new DefaultLightClientApi("http://localhost:9000");
ScriptDetail senderScriptDetail = new ScriptDetail();
senderScriptDetail.script = Address.decode(sender).getScript();
senderScriptDetail.scriptType = ScriptType.LOCK;
senderScriptDetail.blockNumber = 0;
// Set script to let light client sync information about this script on chain.
lightClientApi.setScripts(Collections.singletonList(senderScriptDetail));
LightClientInputIterator iterator = new LightClientInputIterator(lightClientApi);
iterator.addSearchKey(sender);
TransactionWithScriptGroups txWithGroups = new CkbTransactionBuilder(configuration, iterator)
.addOutput("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r",
50100000000L)
.setChangeOutput(sender)
.build();
TransactionSigner.getInstance(network)
.signTransaction(txWithGroups, "0x6c9ed03816e3111e49384b8d180174ad08e29feb1393ea1b51cef1c505d4e36a");
byte[] txHash = lightClientApi.sendTransaction(txWithGroups.getTxView());
System.out.println("Transaction hash: " + Numeric.toHexString(txHash));
}
}