Skip to content

Commit 8242a5c

Browse files
committed
Restructure redeem script to reduce size of redirect & claim txs
Deduplicate the public key used in the claim path of the warning escrow output redeem script, of the v5 protocol, reducing the size of the redirect & claim txs by 8 vbytes (32 WUs) each. The claim public key doubles up as the trader's 2-of-2 multisig public key, used for both the regular escrow output of the deposit tx and the redirection spend path of the warning escrow output, so it may be reused through some manipulation of the bitcoin stack. Also reverse the order of the buyer & seller pubKeys & signatures, so that the buyer signature is always higher up in the stack, for consistency with the deposit tx redeem script. This leads to the following two warning escrow output redeem scripts, structured slightly differently: Buyer's warning escrow output redeem script: <buyerPubKey> OP_SWAP OP_IF 2 <sellerPubKey> OP_ROT 2 OP_CHECKMULTISIG OP_ELSE <claimDelay> OP_CHECKSEQUENCEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF Seller's warning escrow output redeem script: <sellerPubKey> OP_SWAP OP_IF 2 OP_SWAP <buyerPubKey> 2 OP_CHECKMULTISIG OP_ELSE <claimDelay> OP_CHECKSEQUENCEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF
1 parent 8b9ff7f commit 8242a5c

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

core/src/main/java/bisq/core/btc/wallet/RedirectionTransactionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ private static TransactionWitness redeemP2WSH(Script witnessScript,
140140
TransactionSignature sellerSignature) {
141141
var witness = new TransactionWitness(5);
142142
witness.setPush(0, new byte[]{});
143-
witness.setPush(1, buyerSignature.encodeToBitcoin());
144-
witness.setPush(2, sellerSignature.encodeToBitcoin());
143+
witness.setPush(1, sellerSignature.encodeToBitcoin());
144+
witness.setPush(2, buyerSignature.encodeToBitcoin());
145145
witness.setPush(3, new byte[]{1});
146146
witness.setPush(4, witnessScript.getProgram());
147147
return witness;

core/src/main/java/bisq/core/btc/wallet/WarningTransactionFactory.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,31 @@ public Transaction finalizeWarningTransaction(Transaction warningTx,
137137
return warningTx;
138138
}
139139

140-
// TODO: Should probably reverse order of pubKeys & signatures, for consistency with deposit tx redeem script.
141140
static Script createRedeemScript(boolean isBuyer, byte[] buyerPubKey, byte[] sellerPubKey, long claimDelay) {
142-
var scriptBuilder = new ScriptBuilder();
143-
scriptBuilder.op(OP_IF)
144-
.number(2)
145-
.data(buyerPubKey)
146-
.data(sellerPubKey)
147-
.number(2)
148-
.op(OP_CHECKMULTISIG);
141+
var scriptBuilder = new ScriptBuilder()
142+
.data(isBuyer ? buyerPubKey : sellerPubKey)
143+
.op(OP_SWAP);
144+
145+
if (isBuyer) {
146+
scriptBuilder.op(OP_IF)
147+
.number(2)
148+
.data(sellerPubKey)
149+
.op(OP_ROT)
150+
.number(2)
151+
.op(OP_CHECKMULTISIG);
152+
} else {
153+
scriptBuilder.op(OP_IF)
154+
.number(2)
155+
.op(OP_SWAP)
156+
.data(buyerPubKey)
157+
.number(2)
158+
.op(OP_CHECKMULTISIG);
159+
}
149160

150161
scriptBuilder.op(OP_ELSE)
151162
.number(claimDelay)
152163
.op(OP_CHECKSEQUENCEVERIFY)
153164
.op(OP_DROP)
154-
.data(isBuyer ? buyerPubKey : sellerPubKey)
155165
.op(OP_CHECKSIG);
156166

157167
return scriptBuilder.op(OP_ENDIF)

core/src/main/java/bisq/core/trade/protocol/bisq_v5/model/StagedPayoutTxParameters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class StagedPayoutTxParameters {
2727
public static final long REDIRECT_TX_FEE_BUMP_OUTPUT_VALUE = 2000;
2828

2929
private static final long WARNING_TX_EXPECTED_WEIGHT = 720; // 125 direct tx bytes, 220 witness bytes
30-
private static final long CLAIM_TX_EXPECTED_WEIGHT = 519; // 82 direct tx bytes, 191 witness bytes
31-
public static final long REDIRECT_TX_MIN_WEIGHT = 593; // 82 direct tx bytes, 265 witness bytes
30+
private static final long CLAIM_TX_EXPECTED_WEIGHT = 487; // 82 direct tx bytes, 159 witness bytes
31+
public static final long REDIRECT_TX_MIN_WEIGHT = 561; // 82 direct tx bytes, 233 witness bytes
3232

3333
// Min. fee rate for staged payout txs. If fee rate used at take offer time was higher we use that.
3434
// We prefer a rather high fee rate to not risk that the tx gets stuck if required fee rate would

0 commit comments

Comments
 (0)