Skip to content

Commit

Permalink
Have wallet shuffle inputs and outputs (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Aug 5, 2020
1 parent 4323cd7 commit d9024b1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,25 @@ case class ShufflingNonInteractiveFinalizer(
ec: ExecutionContext): Future[Transaction] = {
val addChange = ChangeFinalizer(inputInfos, feeRate, changeSPK)

val sanityCheck = SanityCheckFinalizer(
inputInfos = inputInfos,
expectedOutputSPKs = txBuilderResult.outputs.map(_.scriptPubKey),
expectedFeeRate = feeRate,
changeSPKs = Vector(changeSPK))
val shuffled = addChange.andThen(ShuffleFinalizer)

val addWitnessData = AddWitnessDataFinalizer(inputInfos)
shuffled.buildTx(txBuilderResult).flatMap { tempTx =>
val tempTxBuilderResult = RawTxBuilderResult.fromTransaction(tempTx)

addChange
.andThen(ShuffleFinalizer)
.andThen(sanityCheck)
.andThen(addWitnessData)
.buildTx(txBuilderResult)
val shuffledInputInfos = tempTxBuilderResult.inputs
.map(input => inputInfos.find(_.outPoint == input.previousOutput).get)

val sanityCheck =
SanityCheckFinalizer(inputInfos = shuffledInputInfos,
expectedOutputSPKs =
tempTxBuilderResult.outputs.map(_.scriptPubKey),
expectedFeeRate = feeRate,
changeSPKs = Vector(changeSPK))

sanityCheck
.andThen(AddWitnessDataFinalizer(shuffledInputInfos))
.buildTx(tempTxBuilderResult)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class WalletSendingTest extends BitcoinSWalletTest {
tx <- wallet.sendToAddress(testAddress, amountToSend, feeRateOpt)
} yield {
assert(
tx.outputs.head == TransactionOutput(amountToSend,
testAddress.scriptPubKey))
tx.outputs.contains(
TransactionOutput(amountToSend, testAddress.scriptPubKey)))
}
}

Expand Down
6 changes: 3 additions & 3 deletions wallet/src/main/scala/org/bitcoins/wallet/Wallet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.bitcoins.core.util.{BitcoinScriptUtil, FutureUtil}
import org.bitcoins.core.wallet.builder.{
RawTxBuilderWithFinalizer,
RawTxSigner,
StandardNonInteractiveFinalizer
ShufflingNonInteractiveFinalizer
}
import org.bitcoins.core.wallet.fee.FeeUnit
import org.bitcoins.core.wallet.utxo.TxoState.{
Expand Down Expand Up @@ -337,7 +337,7 @@ abstract class Wallet
* finalizing and signing the transaction, then correctly processing and logging it
*/
private def finishSend(
txBuilder: RawTxBuilderWithFinalizer[StandardNonInteractiveFinalizer],
txBuilder: RawTxBuilderWithFinalizer[ShufflingNonInteractiveFinalizer],
utxoInfos: Vector[ScriptSignatureParams[InputInfo]],
sentAmount: CurrencyUnit,
feeRate: FeeUnit,
Expand Down Expand Up @@ -395,7 +395,7 @@ abstract class Wallet
changeAddr <- getNewChangeAddress(fromAccount.hdAccount)

output = TransactionOutput(amount, address.scriptPubKey)
txBuilder = StandardNonInteractiveFinalizer.txBuilderFrom(
txBuilder = ShufflingNonInteractiveFinalizer.txBuilderFrom(
Vector(output),
utxos,
feeRate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.bitcoins.core.protocol.transaction._
import org.bitcoins.core.wallet.builder.{
RawTxBuilder,
RawTxBuilderWithFinalizer,
StandardNonInteractiveFinalizer
ShufflingNonInteractiveFinalizer
}
import org.bitcoins.core.wallet.fee.FeeUnit
import org.bitcoins.core.wallet.utxo.{
Expand Down Expand Up @@ -74,7 +74,7 @@ trait FundTransactionHandling extends WalletLogger { self: Wallet =>
CoinSelectionAlgo.AccumulateLargest,
fromTagOpt: Option[AddressTag],
markAsReserved: Boolean = false): Future[(
RawTxBuilderWithFinalizer[StandardNonInteractiveFinalizer],
RawTxBuilderWithFinalizer[ShufflingNonInteractiveFinalizer],
Vector[ScriptSignatureParams[InputInfo]])] = {
val utxosF = for {
utxos <- fromTagOpt match {
Expand Down Expand Up @@ -170,7 +170,7 @@ trait FundTransactionHandling extends WalletLogger { self: Wallet =>
val txBuilder =
RawTxBuilder().setLockTime(lockTime) ++= destinations ++= inputs

val finalizer = StandardNonInteractiveFinalizer(
val finalizer = ShufflingNonInteractiveFinalizer(
utxoSpendingInfos.map(_.inputInfo),
feeRate,
change.scriptPubKey)
Expand Down

0 comments on commit d9024b1

Please sign in to comment.