0
Im using bitcoinj library and blockcypher api for creating wallet and transactions on Bitcoin Testnet. I get this error when sending signed transaction.
"error": "Error validating generated transaction: Error running script for input 0 referencing 1eaf42445d037370ed5b37dd2e5a4a4f672aabb659e200d81a5f6fe9545e52d7 at 0: Script was NOT verified successfully."
So, first i create new transaction, then in reponse i get this
{
"tx": {
"block_height": -1,
"block_index": -1,
"hash": "a3d9881f64e987bf700436118da676745629b29aaec00ceee658f03a61330987",
"addresses": [
"mje2F5tdP5t2R5sWyPGdTD2EycbBJqKGTC",
"mgKGQMT9TB9XjhpVZCUsSNNQV5pWF6yqSb"
],
"total": 11320617,
"fees": 4700,
"size": 119,
"preference": "high",
"relayed_by": "82.117.202.34",
"received": "2019-01-31T13:30:07.624499085Z",
"ver": 1,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 2,
"confirmations": 0,
"inputs": [
{
"prev_hash": "1eaf42445d037370ed5b37dd2e5a4a4f672aabb659e200d81a5f6fe9545e52d7",
"output_index": 0,
"output_value": 11325317,
"sequence": 4294967295,
"addresses": [
"mje2F5tdP5t2R5sWyPGdTD2EycbBJqKGTC"
],
"script_type": "pay-to-pubkey-hash",
"age": 1453583
}
],
"outputs": [
{
"value": 5000,
"script": "76a91408c24023f9172ab0042ea2bef2b077517e0b826288ac",
"addresses": [
"mgKGQMT9TB9XjhpVZCUsSNNQV5pWF6yqSb"
],
"script_type": "pay-to-pubkey-hash"
},
{
"value": 11315617,
"script": "76a9142d36b60f3dd37f3cca06876846a597bfdf2cf63f88ac",
"addresses": [
"mje2F5tdP5t2R5sWyPGdTD2EycbBJqKGTC"
],
"script_type": "pay-to-pubkey-hash"
}
]
},
"tosign": [
"389a2e15c404b2c2a85ce4d68101fc568f55e42df3a4c1c97fdeaa7816cee29c"
]
}
I take this "toSign" hash and then sign with my private key from wallet.
private fun getSignedTransactionHex(transactionToSign: String): String {
val wallet = walletService.loadWalletFromFile("dsadasd")
var privateKey: BigInteger? = null
wallet.importedKeys.forEach {
privateKey = it.privKey
publicKey = it.publicKeyAsHex
System.out.println("Public key: $publicKey")
}
val ecKeyWif = ECKey.fromPrivate(privateKey)
val wifAsString = ecKeyWif.getPrivateKeyAsWiF(walletService.getTestNetwork())
val dpk = DumpedPrivateKey.fromBase58(null, wifAsString)
val ecKey = dpk.key
val testNet = walletService.getTestNetwork()
val check = ecKey.getPrivateKeyAsWiF(testNet)
val hash = Sha256Hash.wrap(transactionToSign)
val signature = ecKey.sign(hash)
val res = signature.encodeToDER()
return res.toHexString()
}
Then, ill add this signed hash, my public key and "toSign" hash to transaction that i got from response earlier.
{
"tx": {
"block_height": -1,
"block_index": -1,
"hash": "a3d9881f64e987bf700436118da676745629b29aaec00ceee658f03a61330987",
"addresses": [
"mje2F5tdP5t2R5sWyPGdTD2EycbBJqKGTC",
"mgKGQMT9TB9XjhpVZCUsSNNQV5pWF6yqSb"
],
"total": 11320617,
"fees": 4700,
"size": 119,
"preference": "high",
"relayed_by": "82.117.202.34",
"received": "2019-01-31T09:39:33.07827321Z",
"ver": 1,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 2,
"confirmations": 0,
"inputs": [
{
"pre1v_hash": "1eaf42445d037370ed5b37dd2e5a4a4f672aabb659e200d81a5f6fe9545e52d7",
"output_index": 0,
"output_value": 11325317,
"sequence": 4294967295,
"addresses": [
"mje2F5tdP5t2R5sWyPGdTD2EycbBJqKGTC"
],
"script_type": "pay-to-pubkey-hash",
"age": 1453583
}
],
"outputs": [
{
"value": 5000,
"script": "76a91408c24023f9172ab0042ea2bef2b077517e0b826288ac",
"addresses": [
"mgKGQMT9TB9XjhpVZCUsSNNQV5pWF6yqSb"
],
"script_type": "pay-to-pubkey-hash"
},
{
"value": 11315617,
"script": "76a9142d36b60f3dd37f3cca06876846a597bfdf2cf63f88ac",
"addresses": [
"mje2F5tdP5t2R5sWyPGdTD2EycbBJqKGTC"
],
"script_type": "pay-to-pubkey-hash"
}
]
},
"tosign": [
"389a2e15c404b2c2a85ce4d68101fc568f55e42df3a4c1c97fdeaa7816cee29c"
],
"signatures": [
"30440220236ab8fddb66f9ae008e17e05795ed37394e07f0f1df4325d30d03aac4fac56a02200f45f4eb9174c58d419df3d98ad045a4f21cc35b1a5912c3df63e336e7ce59c4"
],
"pubkeys": [
"0268768af434d44d700e0578bbbd89ad7100ee83f86430310fec3b7931468c1b9e"
]
}
Im doing something wrong probably, do you have any suggestion?