Error validating generated transaction. BlockCypher

1.2k views Asked by At

Im using the blockcypher api from here https://github.com/blockcypher/java-client

And got an exception while trying send transaction:

    IntermediaryTransaction transaction = context.getTransactionService().
            newTransaction(
                    new ArrayList<>(Arrays.asList("mntg5ArRYYm9PirCaJYNxvHoMKbe2B4Q1b")),
                    new ArrayList<>(Arrays.asList("mzF8NP6rHYG9ehVPYSJBFygu6LSYVqZLZg")), 30000);

    String privateKey = "9acdde15b65eae424f06fd5b5e20572b20c38a25c7daf5b3244508940887dc68";
    SignUtils.signWithHexKeyWithPubKey(transaction, privateKey);

    context.getTransactionService().sendTransaction(transaction);

Here is log:

    3425   [           main] DEBUG ls.rest.jersey.JerseyRestUtils  - 
Posting to https://api.blockcypher.com/v1/btc/test3/txs/new?token=mytoken: {"double_spend":false,"inputs":[{"addresses":["mntg5ArRYYm9PirCaJYNxvHoMKbe2B4Q1b"]}],"outputs":[{"value":30000,"addresses":["mzF8NP6rHYG9ehVPYSJBFygu6LSYVqZLZg"]}]}
    5339   [           main] INFO  ockcypher.utils.sign.SignUtils  - Pushing Pub key for input
    5382   [           main] DEBUG ls.rest.jersey.JerseyRestUtils  - Posting to https://api.blockcypher.com/v1/btc/test3/txs/send?token=mytoken: {"tx":{"hash":"b37e8f5dfa909876fe636c07db13a65ea6671ea723847f2e58815e6357200dcb","block_height":-1,"addresses":["mzF8NP6rHYG9ehVPYSJBFygu6LSYVqZLZg","mntg5ArRYYm9PirCaJYNxvHoMKbe2B4Q1b"],"total":4219700,"fees":10300,"relayed_by":"78.128.153.1","received":"2018-09-18T14:37:52.357270154Z","ver":1,"vin_sz":1,"vout_sz":2,"confirmations":0,"preference":"high","double_spend":false,"inputs":[{"prev_hash":"68553b72dad64595bd2052547fcbe84a17e3966a01386b4caefb4518324cf2f9","output_index":0,"output_value":4230000,"addresses":["mntg5ArRYYm9PirCaJYNxvHoMKbe2B4Q1b"],"script_type":"pay-to-pubkey-hash"}],"outputs":[{"value":30000,"script":"76a914cd6c24f57b38917f2066b342147a3802d0a2f6f788ac","addresses":["mzF8NP6rHYG9ehVPYSJBFygu6LSYVqZLZg"],"script_type":"pay-to-pubkey-hash"},{"value":4189700,"script":"76a91450e47d76f6f5dfe1de7de1f19e40afaefbfc008c88ac","addresses":["mntg5ArRYYm9PirCaJYNxvHoMKbe2B4Q1b"],"script_type":"pay-to-pubkey-hash"}]},"tosign":["936678acb668bbe3055d98daf2bf43f7fd8c45e4948c9db01eca16759cbafc26"],"signatures":["3045022100c93d97a73c19c96a521c88d05ec1e11405611dd82407461a5a27dabf2d3d92540220574d453f707f3c40c862e8a1fd8a43c08a68e970fceaee21406aae75172ff137"],"pubkeys":["04a1cfb3e4c4088a4b8a30e09cd2e2ad0503948edab552dbe6550979fb77d3808fea0add24063d1249f2e54ec4dd01308961426f43b83393d367ac529fa8bbed9d"]}
    Exception in thread "main" BlockCypherException{message=Bad Request, status=400, blockCypherError=[Error{error='Error validating generated transaction: Error running script for input 0 referencing 68553b72dad64595bd2052547fcbe84a17e3966a01386b4caefb4518324cf2f9 at 0: Script was NOT verified successfully.'}], exception=null}
    at com.blockcypher.utils.rest.jersey.JerseyRestUtils.getBlockCypherException(JerseyRestUtils.java:90)
    at com.blockcypher.utils.rest.jersey.JerseyRestUtils.post(JerseyRestUtils.java:33)
    at com.blockcypher.utils.rest.jersey.JerseyRestUtils.post(JerseyRestUtils.java:50)
    at com.blockcypher.utils.rest.RestUtils.post(RestUtils.java:20)
    at com.blockcypher.service.TransactionService.postTransaction(TransactionService.java:59)
    at com.blockcypher.service.TransactionService.sendTransaction(TransactionService.java:133)
    at _test.sendBtc(_test.java:46)
    at _test.main(_test.java:27)

I am using bitcoin testnet.

1

There are 1 answers

0
sedni On

I found a solution. You must add a public key to IntermediaryTransaction, and use only method signWithHexKeyNoPubKey (wtf?) with privateKey in HEX format. Others variants are not work. I dont know why it works, but it works. Here is the correct, working code:

private static Transaction sendBtc(String addrFrom, String addrTo, String privateKey, String pubKey, long value) throws BlockCypherException {
    IntermediaryTransaction unsignedTX = context.getTransactionService().
            newTransaction(
                    new ArrayList<>(Arrays.asList(addrFrom)),
                    new ArrayList<>(Arrays.asList(addrTo)), value);

    unsignedTX.addPubKeys(pubKey);

    SignUtils.signWithHexKeyNoPubKey(unsignedTX, privateKey);

    return context.getTransactionService().sendTransaction(unsignedTX);
}