OSDN Git Service

add a transaction.md file in doc
[bytom/bytom-java-sdk.git] / README.md
1 # Bytom java-sdk
2
3 This page will document the API classes and ways to properly use the API.
4 Subsequent new releases also maintain backward compatibility with this class
5 approach. For more information, please see Bytom API reference documentation
6 at [Bytom wiki](https://github.com/Bytom/bytom/wiki/API-Reference)
7
8 ## Basic Usage
9
10 ```java
11 public static Client generateClient() throws BytomException {
12     String coreURL = Configuration.getValue("bytom.api.url");
13     String accessToken = Configuration.getValue("client.access.token");
14     if (coreURL == null || coreURL.isEmpty()) {
15         coreURL = "http://127.0.0.1:9888/";
16     }
17     return new Client(coreURL, accessToken);
18 }
19
20 Client client = TestUtils.generateClient();
21 ```
22
23 ## Usage
24
25 * [`Step 1: Create a key`](#create-a-key)
26 * [`Step 2: Create an account`](#create-an-account)
27 * [`Step 3: Create an receiver`](#create-an-receiver)
28 * [`Step 4: Create an asset`](#create-an-asset)
29 * [`Step 5: Issue asset`](#issue-asset)
30     * [`Firstly build the transaction`](#firstly-build-the-transaction)
31     * [`Secondly sign the transaction`](#secondly-sign-the-transaction)
32     * [`Finally submit the transaction`](#finally-submit-the-transaction)
33
34 > For more details, see [API methods](https://github.com/Bytom/java-sdk/blob/master/doc/index.md#api-methods)
35
36 ## Create a key
37
38 ```java
39 String alias = "test";
40 String password = "123456";
41
42 Key.Builder builder = new Key.Builder().setAlias(alias).setPassword(password);
43 Key key = Key.create(client, builder);
44 ```
45
46 ## Create an account
47
48 ```java
49 String alias = "sender-account";
50 Integer quorum = 1;
51 List<String> root_xpubs = new ArrayList<String>();
52 root_xpubs.add(senderKey.xpub);
53
54 Account.Builder builder = new Account.Builder().setAlias(alias).setQuorum(quorum).setRootXpub(root_xpubs);
55
56 Account account = Account.create(client, builder);
57 ```
58
59 ## Create an receiver
60
61 ```java
62 String alias = receiverAccount.alias;
63 String id = receiverAccount.id;
64
65 Account.ReceiverBuilder receiverBuilder = new Account.ReceiverBuilder().setAccountAlias(alias).setAccountId(id);
66 Receiver receiver = receiverBuilder.create(client);
67 ```
68
69 ## Create an asset
70
71 ```java
72  String alias = "receiver-asset";
73
74 List<String> xpubs = receiverAccount.xpubs;
75
76 Asset.Builder builder = new Asset.Builder()
77                         .setAlias(alias)
78                         .setQuorum(1)
79                         .setRootXpubs(xpubs);
80 receiverAsset = builder.create(client);
81 ```
82
83 ## Issue asset
84
85 For more transaction details, see [transactions](https://github.com/Bytom/java-sdk/blob/master/doc/transactions.md)
86
87 ### Firstly build the transaction
88
89 ```java
90 Transaction.Template controlAddress = new Transaction.Builder()
91         .addAction(
92                 new Transaction.Action.SpendFromAccount()
93                         .setAccountId(senderAccount.id)
94                         .setAssetId(senderAsset.id)
95                         .setAmount(300000000)
96         )
97         .addAction(
98                 new Transaction.Action.ControlWithAddress()
99                         .setAddress(receiverAddress.address)
100                         .setAssetId(senderAsset.id)
101                         .setAmount(200000000)
102         ).build(client);
103 ```
104
105 ### Secondly sign the transaction
106
107 ```java
108 Transaction.Template singer = new Transaction.SignerBuilder().sign(client,
109         controlAddress, "123456");
110 ```
111
112 ### Finally submit the transaction
113
114 ```java
115 Transaction.SubmitResponse txs = Transaction.submit(client, singer);
116 ```
117
118 > For more details, see [API methods](https://github.com/Bytom/java-sdk/blob/master/doc/index.md#api-methods)