Trying to send a raw transaction bitoinj

440 views Asked by At
Transaction transaction = new Transaction(params);
// 遍历未花费列表,组装合适的item

double sum = 0;
String address = null;
List<Unspent> unspents = new ArrayList<>();
Map<String, AddrDTO> keysMap = new HashMap<>();
for (Unspent utxo : unSpentBTCList) {
    /*
     * if(!script.isSentToRawPubKey() && !script.isSentToAddress()) {
     * logger.info("格式不對:" + utxo.address()); continue; }
     */
    AddrDTO addrDto = this.getAddrDTO(utxo.address());
    if (addrDto == null) {
        logger.info("address 找不到:" + utxo.address());
        continue;
    }
    keysMap.put(utxo.address(), addrDto);
    unspents.add(utxo);
    sum += utxo.amount();
    address = utxo.address();
    if (sum >= amount) {
        break;// 停止。
    }
}
if (sum < amount) {
    logger.error("余额不足");
    throw new RuntimeException("余额不足!");
}

long value = btc2Satoshi(amount);
transaction.addOutput(Coin.valueOf(value), Address.fromBase58(params, to));
// transaction.

// 消费列表总金额 - 已经转账的金额 - 手续费 就等于需要返回给自己的金额了
long longFee = btc2Satoshi(fee);
long balance = btc2Satoshi(sum) - value - longFee;
// 输出-转给自己
if (balance > 0) {
    transaction.addOutput(Coin.valueOf(balance), Address.fromBase58(params, address));
}
int i = 0;
for (Unspent utxo : unspents) {
    AddrDTO addrDto = keysMap.get(utxo.address());
    logger.info("xxxxxxxxxx:" + utxo.txid() + ":" + addrDto.getAddress());
    DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, addrDto.getPrivateKey());
    Script s = new Script(Hex.decode(utxo.scriptPubKey()));
    TransactionOutPoint outPoint = new TransactionOutPoint(params, i++, Sha256Hash.wrap(utxo.txid()));
    ECKey ecKey = dumpedPrivateKey.getKey();
    transaction.addSignedInput(outPoint, s, ecKey, Transaction.SigHash.ALL, true);
    logger.info("xxxxxxxxxx:" + utxo.amount());
}
String hex = Hex.toHexString(transaction.bitcoinSerialize());
logger.info("bitcoinj hex = " + hex);

Exception in thread "main" org.bitcoinj.core.ScriptException: Don't know how to sign for this kind of scriptPubKey: HASH160 PUSHDATA(20)[1a0a82f0669c14c6739e4cf1a5a3f221f657e28f] EQUAL at org.bitcoinj.core.Transaction.addSignedInput(Transaction.java:823) at com.idasex.bitcoin.BitcoinClient.signBTCTransactionData(BitcoinClient.java:337) at com.idasex.bitcoin.BitcoinClient.sendRawTx(BitcoinClient.java:274) at com.idasex.bitcoin.BitcoinClient.main(BitcoinClient.java:409)

0

There are 0 answers