# 交易所对接指南(Java)

# 安装包引入

  • 下载Bytom的java SDK包。 https://github.com/Bytom/bytom-java-sdk
  • 解压缩Bytom-sdk-java-{版本号}.zip包。
  • 将Bytom-sdk-{版本号}.jar及其依赖包引入到项目中。

# 生成client

public static Client generateClient() throws BytomException {
    String coreURL = Configuration.getValue("bytom.api.url");
    String accessToken = Configuration.getValue("client.access.token");
    if (coreURL == null || coreURL.isEmpty()) {
        coreURL = "http://127.0.0.1:9888/";
    }
    return new Client(coreURL, accessToken);
}

Client client = Client.generateClient();

# 生成充值地址

比原链采用 密钥-账户-地址模式,需要通过三步来生成地址

# 生成密钥

 @Test
public void testClientKeyCreate() throws Exception {
    String alias = "alice";
    String password = "123456";

    Key.Builder builder = new  Key.Builder().setAlias(alias).setPassword(password);
    Key key = Key.create(client, builder);
    System.out.println(key.toJson());
}

返回:

{
    "alias":"bob",
 "xpub":"10823b3b655d76c9122315bda4aded669b3087831c7e99379338d8d8d84296658cd866211a488dc234ec1ae6bbb11e6ad0e317311610e07cb2d0d098bd5f0c95",
    "file":"/Users/doraemon/bytom/testnet/keystore/UTC--2021-07-21T06-58-38.437857000Z--18a0283e-9928-4492-9d65-0dc6ab72da70" //密钥文件地址
}

# 生成账户

@Test
public void testAccountCreate() throws Exception {
    String alias = "bob";
    Integer quorum = 1; //单签
    List<String> root_xpubs = new ArrayList<>();
    root_xpubs.add("10823b3b655d76c9122315bda4aded669b3087831c7e99379338d8d8d84296658cd866211a488dc234ec1ae6bbb11e6ad0e317311610e07cb2d0d098bd5f0c95");

    Account.Builder builder = new Account.Builder().setAlias(alias).setQuorum(quorum).setRootXpub(root_xpubs);
    account = Account.create(client, builder);
    System.out.println(account.toJson());
}

返回:

{
    "id":"1SKBLDD100A02",
    "alias":"bob",
    "key_index":1,
    "quorum":1,
    "xpubs":[
 "10823b3b655d76c9122315bda4aded669b3087831c7e99379338d8d8d84296658cd866211a488dc234ec1ae6bbb11e6ad0e317311610e07cb2d0d098bd5f0c95"
    ]
}

# 生成账户地址

@Test
public void testReceiverCreate1() throws Exception {
    String alias = "bob";
    String id = "1SKBLDD100A02";

    Account.ReceiverBuilder receiverBuilder = new Account.ReceiverBuilder().setAccountAlias(alias).setAccountId(id);
    Receiver receiver = receiverBuilder.create(client);
    System.out.println(receiver.toJson());
}

返回:

{
    "address":"tm1qa38fp79x8vsh0vp8y58smam2shmsl76ah6nfkt",
    "control_program":"0014ec4e90f8a63b2177b027250f0df76a85f70ffb5d"
}

# 检测地址合法性

@Test
public void testAddressValidate() throws Exception {
    String alias = "bob";
    String id = "1SKBLDD100A02";

    Account.AddressBuilder addressBuilder = new Account.AddressBuilder().setAccountId(id).setAccountAlias(alias);

    String address = "tm1qa38fp79x8vsh0vp8y58smam2shmsl76ah6nfkt";
    Account.Address accountAddress = addressBuilder.validate(client, address);
    System.out.println(accountAddress.toJson());
}

返回true则是合法地址

{
    "valid":true,
    "is_local":true
}

# 资产提现(链上资产转账)

比原链资产链上转账需要Build-Sign-Submit 三个步骤

# 构造交易

@Test
public void testSimplePayment() throws BytomException {
    Transaction.Template payment = new Transaction.Builder()
            .addAction(new Transaction.Action.SpendFromAccount()
                .setAccountAlias("bob")
                .setAssetAlias("btm")
                .setAmount(220000000)
            ).addAction(new Transaction.Action.ControlWithAddress()
                .setAddress("tm1qqlwaqc6geh2jc0c4l99g4zlgc5hwawutuc7j6v")
                .setAssetAlias("btm")
                .setAmount(100000000)
            ).addAction(new Transaction.Action.ControlWithAddress()
                .setAddress("tm1qj66z7p576e73je3uc3k9whcermqz9mhzzhj47c")
                .setAssetAlias("btm")
                .setAmount(100000000)
            ).build(client);
    System.out.println(payment.toJson());
}

返回值:

{
    "raw_transaction":"070100010161015fcf9c0b3b1ccb8287fc89440a83f9bb2f965527f7e688e57589f0c23d30b8d9e7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c8afa0250101160014ec4e90f8a63b2177b027250f0df76a85f70ffb5d0001000301003effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80eabbb72401160014ec4e90f8a63b2177b027250f0df76a85f70ffb5d000001003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c2d72f0116001407ddd06348cdd52c3f15f94a8a8be8c52eeebb8b000001003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c2d72f0116001496b42f069ed67d19663cc46c575f191ec022eee20000",
    "signing_instructions":[
        {
            "position":0,
            "witness_components":[
                {
                    "type":"raw_tx_signature",
                    "quorum":1,
                    "keys":[
                        {
                            "xpub":"10823b3b655d76c9122315bda4aded669b3087831c7e99379338d8d8d84296658cd866211a488dc234ec1ae6bbb11e6ad0e317311610e07cb2d0d098bd5f0c95",
                            "derivation_path":[
                                "2c000000",
                                "99000000",
                                "01000000",
                                "00000000",
                                "01000000"
                            ]
                        }
                    ]
                },
                {
                    "type":"data",
                    "value":"0bca7cecfa25cfa751867083f9dcdbd32a7915018ec94675633d312c18f499a9",
                    "quorum":0
                }
            ]
        }
    ],
    "local":false,
    "allow_additional_actions":false
}

# 签名交易

@Test
public void testSignPayment() throws BytomException {
    Transaction.Template signedPayment = new Transaction.SignerBuilder().sign(client,payment, "123456");
    System.out.println(signedPayment.toJson());
}

返回值

{
    "raw_transaction":"070100010161015fcf9c0b3b1ccb8287fc89440a83f9bb2f965527f7e688e57589f0c23d30b8d9e7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c8afa0250101160014ec4e90f8a63b2177b027250f0df76a85f70ffb5d0063024071c211a8f9890e088169fd2b2b9a716df906d146d4aad03abb6b56e1fbbcc9342eb99632a5f576ee9985e0048cc358633e15cf85c21fe4a97254df4a9fc3b004200bca7cecfa25cfa751867083f9dcdbd32a7915018ec94675633d312c18f499a90301003effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80eabbb72401160014ec4e90f8a63b2177b027250f0df76a85f70ffb5d000001003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c2d72f0116001407ddd06348cdd52c3f15f94a8a8be8c52eeebb8b000001003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c2d72f0116001496b42f069ed67d19663cc46c575f191ec022eee20000",
    "signing_instructions":[
        {
            "position":0,
            "witness_components":[
                {
                    "type":"raw_tx_signature",
                    "quorum":1,
                    "keys":[
                        {
                            "xpub":"10823b3b655d76c9122315bda4aded669b3087831c7e99379338d8d8d84296658cd866211a488dc234ec1ae6bbb11e6ad0e317311610e07cb2d0d098bd5f0c95",
                            "derivation_path":[
                                "2c000000",
                                "99000000",
                                "01000000",
                                "00000000",
                                "01000000"
                            ]
                        }
                    ],
                    "signatures":[
                        "71c211a8f9890e088169fd2b2b9a716df906d146d4aad03abb6b56e1fbbcc9342eb99632a5f576ee9985e0048cc358633e15cf85c21fe4a97254df4a9fc3b004"
                    ]
                },
                {
                    "type":"data",
                    "value":"0bca7cecfa25cfa751867083f9dcdbd32a7915018ec94675633d312c18f499a9",
                    "quorum":0
                }
            ]
        }
    ],
    "local":false,
    "allow_additional_actions":false
}

# 提交交易

@Test
public void testSubmitPayment() throws BytomException {
    Transaction.SubmitResponse resp = Transaction.submit(client, signedPayment);
    System.out.println(resp.toJson());
}

返回:

{
    "tx_id":"a9d6cfb172a186040579ffd9a16cd1092e57468579faa71225d0ac76098524d1"
}

# 查询交易

@Test
public void testGetTransaction() throws Exception {
    String txID = "a9d6cfb172a186040579ffd9a16cd1092e57468579faa71225d0ac76098524d1";
    Transaction.QueryBuilder query = new Transaction.QueryBuilder();
    query.setTxId(txID);
    Transaction transaction = query.get(client);
    System.out.println(transaction.toJson());
}

返回:

如果有block_time则说明入块成功,如果需要确保不可回滚,需要和net-info接口的不可回滚高度做对比

{
    "tx_id":"a9d6cfb172a186040579ffd9a16cd1092e57468579faa71225d0ac76098524d1",
    "block_time":"1626852672000",
    "block_hash":"16a8262434b956e3d60f48c67c11449e453cb48eb98892dbd44f84c46dca2dff",
    "block_index":"1",
    "block_transactions_count":"2",
    "block_height":17209,
    "inputs":[
        {
            "account_alias":"bob",
            "account_id":"1SKBLDD100A02",
            "address":"tm1qa38fp79x8vsh0vp8y58smam2shmsl76ah6nfkt",
            "amount":10000000000,
            "asset_alias":"BTM",
            "asset_definition":{
                "decimals":8,
                "description":"Bytom Official Issue",
                "name":"BTM",
                "symbol":"BTM"
            },
            "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
            "spent_output_id":"3d84ae5c0092a0188c4e4b573442d94abe5dab0a50291c8d13ad682ef1860832",
            "type":"spend",
            "control_program":"0014ec4e90f8a63b2177b027250f0df76a85f70ffb5d"
        }
    ],
    "outputs":[
        {
            "id":"eaa906f3cb88f1de0167b92f9bb8c6a00b54c3c016a4e90c9de71945e21e352f",
            "type":"control",
            "position":0,
            "control_program":"0014ec4e90f8a63b2177b027250f0df76a85f70ffb5d",
            "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
            "asset_alias":"BTM",
            "asset_definition":{
                "decimals":8,
                "description":"Bytom Official Issue",
                "name":"BTM",
                "symbol":"BTM"
            },
            "amount":9780000000,
            "account_id":"1SKBLDD100A02",
            "account_alias":"bob",
            "address":"tm1qa38fp79x8vsh0vp8y58smam2shmsl76ah6nfkt"
        },
        {
            "id":"3dc12eeca931d8507a0c8b7a0ab62f8711ee3277a98f4e71727df70cdb17d3a2",
            "type":"control",
            "position":1,
            "control_program":"001407ddd06348cdd52c3f15f94a8a8be8c52eeebb8b",
            "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
            "asset_alias":"BTM",
            "asset_definition":{
                "decimals":8,
                "description":"Bytom Official Issue",
                "name":"BTM",
                "symbol":"BTM"
            },
            "amount":100000000,
            "account_id":"1SJ4C7TQG0A02",
            "account_alias":"alice",
            "address":"tm1qqlwaqc6geh2jc0c4l99g4zlgc5hwawutuc7j6v"
        },
        {
            "id":"4078fe7930a696a1a2704969b33c6187f074c7307065bc1b7d102c1dc7db9f23",
            "type":"control",
            "position":2,
            "control_program":"001496b42f069ed67d19663cc46c575f191ec022eee2",
            "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
            "asset_alias":"BTM",
            "asset_definition":{
                "decimals":8,
                "description":"Bytom Official Issue",
                "name":"BTM",
                "symbol":"BTM"
            },
            "amount":100000000,
            "address":"tm1qj66z7p576e73je3uc3k9whcermqz9mhzzhj47c"
        }
    ]
}
上次更新: 7/22/2021, 9:59:27 AM