/**
* @url https://blog.csdn.net/weixin_34111790/article/details/91454965
* @param privateKey 私钥
* @param receiveAddress 收款地址
* @param formAddress 付款地址
* @param amount 数量
* @param fee 手续费
* @param unUtxos 找零模型集合
*/
@Deprecated(message = "该方法没有验证", replaceWith =ReplaceWith(expression = "signTransaction(arrayListOf(), rawTrx = \"\", address = \"\", privateKey = \"\", coinType = \"\")") )
private fun signTransaction(privateKey: String, receiveAddress: String, formAddress: String, amount: Long, fee: Long, unUtxos: List<UnSpentUtxo>): String {
if (unUtxos.isNullOrEmpty()) {
throw NullPointerException("unUtxos Can not be empty null")
}
val param = if (BuildConfig.DEBUG) RegTestParams.get() else MainNetParams.get()
val utxos = mutableListOf<UTXO>()
val dumpedPrivateKey = DumpedPrivateKey.fromBase58(param, privateKey)
val key: ECKey = dumpedPrivateKey.key
val tx = Transaction(param)
tx.addOutput(Coin.valueOf(amount), LegacyAddress.fromBase58(param, receiveAddress))
// 如果需要找零 消费列表总金额 - 已经转账的金额 - 手续费
val value = unUtxos.sumOf { it.value }
val payAddress = LegacyAddress.fromBase58(param, formAddress)
val leave = value - amount - fee
if (leave > 0) {
tx.addOutput(Coin.valueOf(leave), payAddress)
}
unUtxos.forEach {
utxos.add(UTXO(Sha256Hash.wrap(it.hash), it.txN, Coin.valueOf(it.value), it.height, false, Script(Utils.HEX.decode(it.script)), it.address))
}
utxos.forEach {
val outPoint = TransactionOutPoint(param, it.index, it.hash)
tx.addSignedInput(outPoint, it.script, key, Transaction.SigHash.ALL, true)
}
tx.confidence.source = TransactionConfidence.Source.NETWORK
tx.purpose = Transaction.Purpose.USER_PAYMENT
return tx.bitcoinSerialize().encodeHex()
}
BTC交易签名
最新推荐文章于 2022-08-21 17:45:45 发布