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