離線交易生成 hex交易值屈藐、手動(dòng)廣播出去即可。
廣播交易
// 1、私鑰
ECKey privKey = ECKey.fromPrivate(Utils.HEX.decode(unspentBean.getPrivKey()));
// 2、計(jì)算金額
long amount = (long) (unspentBean.getAmout() * LongMath.pow(10, netParams.getUnitExponent()));// 輸入金額
long fee = (long) (unspentBean.getFeeD() * LongMath.pow(10, netParams.getUnitExponent())); // 曠工費(fèi)
long inputSum = 0L; // 總輸入金額
long needed = amount + fee; // 最終轉(zhuǎn)賬金額:輸入金額+曠工費(fèi)
long change; // 找零 總輸入-總消費(fèi)
// 3梗掰、符合的utxo列表
List<UnspentBean.UtxoBean> utxoList = unspentBean.getUtxos();
for (UnspentBean.UtxoBean utxo : utxoList) {
inputSum += utxo.getValue();
if (inputSum >= needed) {
break;
}
}
// 4、計(jì)算找零
change = inputSum - needed;
// 5嗅回、構(gòu)建交易
Transaction tx = new Transaction(netParams);
tx.addOutput(Coin.valueOf(amount), Address.fromBase58(netParams, unspentBean.getToA())); // 轉(zhuǎn)出
tx.addOutput(Coin.valueOf(change), Address.fromBase58(netParams, unspentBean.getFromA()));// 找零
// 6及穗、簽名輸出腳本
for (UnspentBean.UtxoBean utxosBean : utxoList) {
UTXO sUtxo = new UTXO(Sha256Hash.wrap(utxosBean.getTx_hash()),
utxosBean.getTx_pos(),
Coin.valueOf(utxosBean.getValue()),
utxosBean.getHeight(),
false,
new Script(Utils.HEX.decode(utxosBean.getScriptPubKey())));
TransactionOutPoint outPoint = new TransactionOutPoint(netParams, sUtxo.getIndex(), sUtxo.getHash());
tx.addSignedInput(outPoint, sUtxo.getScript(), privKey, Transaction.SigHash.ALL, true);
}
// 7、構(gòu)建交易簽名
tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
tx.setPurpose(Transaction.Purpose.USER_PAYMENT);
String transCode = Utils.HEX.encode(tx.bitcoinSerialize());
Logger.e("交易簽名:"+transCode);