OSDN Git Service

a little modify
[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 ```
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 ### Firstly build the transaction
86
87 ```java
88 Transaction.Template controlAddress = new Transaction.Builder()
89         .addAction(
90                 new Transaction.Action.SpendFromAccount()
91                         .setAccountId(senderAccount.id)
92                         .setAssetId(senderAsset.id)
93                         .setAmount(300000000)
94         )
95         .addAction(
96                 new Transaction.Action.ControlWithAddress()
97                         .setAddress(receiverAddress.address)
98                         .setAssetId(senderAsset.id)
99                         .setAmount(200000000)
100         ).build(client);
101 ```
102
103 ### Secondly sign the transaction
104
105 ```java
106 Transaction.Template singer = new Transaction.SignerBuilder().sign(client,
107         controlAddress, "123456");
108 ```
109
110 ### Finally submit the transaction
111
112 ```java
113 Transaction.SubmitResponse txs = Transaction.submit(client, singer);
114 ```
115
116 > For more details, see [API methods](https://github.com/Bytom/java-sdk/blob/master/doc/index.md#api-methods)