1 # Java SDK documentation
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.
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/";
15 return new Client(coreURL, accessToken);
18 Client client = TestUtils.generateClient();
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)
32 > For more details, see [`API methods`](#api-methods)
37 String alias = "test";
38 String password = "123456";
40 Key.Builder builder = new Key.Builder().setAlias(alias).setPassword(password);
41 Key key = Key.create(client, builder);
47 String alias = "sender-account";
49 List<String> root_xpubs = new ArrayList<String>();
50 root_xpubs.add(senderKey.xpub);
52 Account.Builder builder = new Account.Builder().setAlias(alias).setQuorum(quorum).setRootXpub(root_xpubs);
54 Account account = Account.create(client, builder);
60 String alias = receiverAccount.alias;
61 String id = receiverAccount.id;
63 Account.ReceiverBuilder receiverBuilder = new Account.ReceiverBuilder().setAccountAlias(alias).setAccountId(id);
64 Receiver receiver = receiverBuilder.create(client);
70 String alias = "receiver-asset";
72 List<String> xpubs = receiverAccount.xpubs;
74 Asset.Builder builder = new Asset.Builder()
78 receiverAsset = builder.create(client);
83 ### Firstly build the transaction
86 Transaction.Template controlAddress = new Transaction.Builder()
88 new Transaction.Action.SpendFromAccount()
89 .setAccountId(senderAccount.id)
90 .setAssetId(senderAsset.id)
94 new Transaction.Action.ControlWithAddress()
95 .setAddress(receiverAddress.address)
96 .setAssetId(senderAsset.id)
101 ### Secondly sign the transaction
104 Transaction.Template singer = new Transaction.SignerBuilder().sign(client,
105 controlAddress, "123456");
108 ### Finally submit the transaction
111 Transaction.SubmitResponse txs = Transaction.submit(client, singer);
118 * [`Key API`](#key-api)
119 * [`Account API`](#account-api)
120 * [`Asset API`](#asset-api)
121 * [`Transaction API`](#transaction-api)
122 * [`Wallet API`](#wallet-api)
123 * [`Access Token API`](#access-token-api)
124 * [`Block API`](#block-api)
125 * [`Mining API`](#mining-api)
126 * [`Other API`](#other-api)
134 Key create(Client client, Builder builder);
139 - `Client` - *client*, Client object that makes requests to the core.
140 - `Key.Builder` - *builder*, Builder object that builds request parameters.
144 - `Key` - *key*, Key object.
151 List<Key> list(Client client);
156 - `Client` - *client*, Client object that makes requests to the core.
160 - `List of Key`, *List<Key>*, an ArrayList object contains Key objects.
167 void delete(Client client, String xpub, String password);
172 - `Client` - *client*, Client object that makes requests to the core.
173 - `String` - *xpub*, pubkey of the key.
174 - `String` - *password*, password of the key.
178 none if the key is deleted successfully.
182 #### resetKeyPassword
185 void resetPwd(Client client, String xpub, String oldPwd, String newPwd);
190 - `Client` - *client*, Client object that makes requests to the core.
191 - `String` - *xpub*, pubkey of the key.
192 - `String` - *oldPwd*, old password of the key.
193 - `String` - *newPwd*, new password of the key.
197 none if the key password is reset successfully.
206 Account create(Client client, Builder builder);
210 - `Client` - *client*, Client object that makes requests to the core.
211 - `Account.Builder` - *builder*, Builder object that builds request parameters.
215 - `Account` - *account*, Account object.
222 List<Account> list(Client client);
227 - `Client` - *client*, Client object that makes requests to the core.
231 - `List of Account`, *List<Account>*, an ArrayList object contains Account objects.
238 void delete(Client client, String account_info);
243 - `Client` - *client*, Client object that makes requests to the core.
244 - `String` - *account_info*, alias or ID of account.
248 none if the account is deleted successfully.
252 #### createAccountReceiver
255 Receiver create(Client client);
260 - `Client` - *client*, Client object that makes requests to the core.
264 - `Receiver` - *receiver*, Receiver object.
272 List<Address> list(Client client);
277 - `Client` - *client*, Client object that makes requests to the core.
281 - `List of Address`, *List<Address>*, an ArrayList object contains Address objects.
288 Address validate(Client client, String address);
293 - `Client` - *Client*, Client object that makes requests to the core.
294 - `String` - *address*, address of account.
298 - `Address` - *address*, an Address object.
307 Asset create(Client client);
312 - `Client` - *Client*, Client object that makes requests to the core.
316 - `Asset` - *asset*, an Asset object.
323 Asset get(Client client);
328 - `Client` - *Client*, Client object that makes requests to the core.
332 - `Asset` - *asset*, an Asset object.
339 List<Asset> list(Client client);
344 - `Client` - *Client*, Client object that makes requests to the core.
348 - `List of Asset`, *List<Asset>*, an ArrayList object contains Asset objects.
352 #### updateAssetAlias
355 void update(Client client);
360 - `Client` - *Client*, Client object that makes requests to the core.
364 none if the asset alias is updated success.
371 List<Balance> list(Client client);
376 - `Client` - *Client*, Client object that makes requests to the core.
380 - `List of Balance`, an ArrayList object contains Balance objects.
384 #### listUnspentOutPuts
387 List<UnspentOutput> list(Client client);
392 - `Client` - *Client*, Client object that makes requests to the core.
396 - `List of UnspentOutput`, an ArrayList object contains UnspentOutput objects.
402 #### buildTransaction
405 Template build(Client client);
410 - `Client` - *Client*, Client object that makes requests to the core.
414 - `Template` - *template*, a template object.
421 Template sign(Client client, Template template, String password);
428 - `Client` - *Client*, Client object that makes requests to the core.
429 - `Template` - *template*, a template object.
430 - `String` - *password*, signature of the password.
434 - `Template` - *template*, a template object.
438 #### submitTransaction
441 SubmitResponse submit(Client client, Template template);
446 - `Client` - *Client*, Client object that makes requests to the core.
447 - `Template` - *template*, a template object.
451 - `SubmitResponse` - *submitResponse*, a SubmitResponse object
455 #### estimateTransactionGas
458 TransactionGas estimateGas(Client client, Template template);
463 - `Client` - *Client*, Client object that makes requests to the core.
464 - `Template` - *template*, a template object.
468 - `TransactionGas` - *transactionGas*, a TransactionGas object
475 Transaction get(Client client);
480 - `Client` - *Client*, Client object that makes requests to the core.
484 - `Transaction` - *transaction*, a Transaction object
488 #### listTransactions
491 List<Transaction> list(Client client);
496 - `Client` - *Client*, Client object that makes requests to the core.
500 - `List of Transaction`, *List<Transaction>*, an ArrayList object contains Transaction objects.
505 //list all transactions
506 List<Transaction> transactionList = new Transaction.QueryBuilder().list(client);
511 #### listTransactionsById
514 List<Transaction> listById(Client client);
519 - `Client` - *Client*, Client object that makes requests to the core.
523 - `List of Transaction`, *List<Transaction>*, an ArrayList object contains Transaction objects.
528 String tx_id = "f04d4d9b2580ff6496f9f08d903de5a2365975fb8d65b66ca4259f152c5dd134";
529 //list all transactions by tx_id
530 List<Transaction> transactionList = new Transaction.QueryBuilder().setTxId(tx_id).list(client);
535 #### listTransactionsByAccountId
538 List<Transaction> listByAccountId(Client client);
543 - `Client` - *Client*, Client object that makes requests to the core.
547 - `List of Transaction`, *List<Transaction>*, an ArrayList object contains Transaction objects.
552 String account_id = "0E6KP8C100A02";
553 //list all transactions by account_id
554 List<Transaction> transactionList = new Transaction.QueryBuilder().setAccountId(account_id).list(client);
563 Wallet backupWallet(Client client);
568 - `Client` - *Client*, Client object that makes requests to the core.
572 - `Wallet` - *wallet*, a Wallet object
579 void restoreWallet(Client client ,Object accountImage, Object assetImage , Object keyImages);
585 - `Client` - *Client*, Client object that makes requests to the core.
586 - `Object` - *account_image*, account image.
587 - `Object` - *asset_image*, asset image.
588 - `Object` - *key_images*, key image.
592 none if restore wallet success.
599 AccessToken accessToken = new AccessToken.Builder().setId("sheng").create(client);
601 List<AccessToken> tokenList = AccessToken.list(client);
603 String secret = "5e37378eb59de6b10e60f2247ebf71c4955002e75e0cd31ede3bf48813a0a799";
604 AccessToken.check(client, "sheng", secret);
607 #### createAccessToken
610 AccessToken create(Client client);
615 - `Client` - *Client*, Client object that makes requests to the core.
619 - `AccessToken` - *accessToken*, an AccessToken object.
623 #### listAccessTokens
626 List<AccessToken> list(Client client);
631 - `Client` - *Client*, Client object that makes requests to the core.
635 - `Array of Object`, access token array.
639 #### deleteAccessToken
642 void delete(Client client, String id);
647 - `Client` - *Client*, Client object that makes requests to the core.
648 - `String` - *id*, token ID.
652 none if the access token is deleted successfully.
656 #### checkAccessToken
659 void check(Client client, String id, String secret);
664 - `Client` - *Client*, Client object that makes requests to the core.
665 - `String` - *id*, token ID.
666 - `String` - *secret*, secret of token, the second part of the colon division for token.
670 none if the access token is checked valid.
679 Integer getBlockCount(Client client);
688 - `Integer` - *block_count*, recent block height of the blockchain.
695 String getBlockHash(Client client);
704 - `String` - *block_hash*, recent block hash of the blockchain.
710 Block getBlock(Client client);
715 - `Client` - *Client*, Client object that makes requests to the core.
719 - `Block` - *block*, a Block object.
726 BlockHeader getBlockHeader(Client client);
731 - `Client` - *Client*, Client object that makes requests to the core.
735 - `BlockHeader` - *blockHeader*, header of block.
742 BlockDifficulty getBlockDifficulty(Client client);
747 - `Client` - *Client*, Client object that makes requests to the core.
751 - `BlockDifficulty` - *blockDifficulty*, a BlockDifficulty object
758 BlockHashRate getHashRate(Client client);
763 - `Client` - *Client*, Client object that makes requests to the core.
767 - `BlockHashRate` - *blockHashRate*, a BlockHashRate object
775 Boolean isMining(Client client);
780 - `Client` - *Client*, Client object that makes requests to the core.
784 - `Boolean` - *is_mining*, whether the node is mining.
791 void setMining(Client client, Boolean isMining);
796 - `Client` - *Client*, Client object that makes requests to the core.
797 - `Boolean` - *is_mining*, whether the node is mining.
806 NetInfo getNetInfo(Client client);
811 - `Client` - *Client*, Client object that makes requests to the core.
815 - `NetInfo` - *netInfo*, a NetInfo object.
822 Gas gasRate(Client client);
827 - `Client` - *Client*, Client object that makes requests to the core.
831 - `Gas` - *gas*, a Gas object.
838 Boolean verifyMessage(Client client);
843 - `Client` - *Client*, Client object that makes requests to the core.
847 - `Boolean` - *result*, verify result.
854 MinerWork getWork(Client client);
859 - `Client` - *Client*, Client object that makes requests to the core.
863 - `MinerWork` - *minerWork*, a MinerWork object.
870 void submiWork(Client client, String blockHeader);
875 - `Client` - *Client*, Client object that makes requests to the core.
876 - `String` - *block_header*, raw block header.
880 none if the work is submitted successfully.