Skip to content

Commit

Permalink
account: refactor expiry witness path
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jun 10, 2022
1 parent 95a0690 commit 17866e9
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions account/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1422,12 +1422,14 @@ func (m *manager) spendAccount(ctx context.Context, account *Account,
"accounts are not currently supported")
}

spendPkg, err = m.spendAccountExpiry(
ctx, account, packet, bestHeight,
spendPkg, err = m.signSpendTx(
ctx, account, packet, bestHeight, witnessType,
)

case multiSigWitness:
spendPkg, err = m.signSpendTx(ctx, account, packet, 0)
spendPkg, err = m.signSpendTx(
ctx, account, packet, 0, witnessType,
)

default:
err = fmt.Errorf("unhandled witness type: %v", witnessType)
Expand Down Expand Up @@ -1539,24 +1541,6 @@ func determineWitnessType(account *Account, bestHeight uint32) witnessType {
return multiSigWitness
}

// spendAccountExpiry creates the closing transaction of an account based on the
// expiration script path and signs it. bestHeight is used as the lock time of
// the transaction in order to satisfy the output's CHECKLOCKTIMEVERIFY.
func (m *manager) spendAccountExpiry(ctx context.Context, account *Account,
packet *psbt.Packet, bestHeight uint32) (*spendPackage, error) {

spendPkg, err := m.signSpendTx(ctx, account, packet, bestHeight)
if err != nil {
return nil, fmt.Errorf("error signing TX: %v", err)
}

spendPkg.tx.TxIn[0].Witness = poolscript.SpendExpiry(
spendPkg.witnessScript, spendPkg.ourSig,
)

return spendPkg, nil
}

// constructMultiSigWitness requests a signature from the auctioneer for the
// given spending transaction of an account and returns the fully constructed
// witness to spend the account input.
Expand Down Expand Up @@ -1634,9 +1618,14 @@ func (m *manager) createSpendTx(account *Account,
// the lock time of the transaction, otherwise it is 0. The transaction has its
// inputs and outputs sorted according to BIP-69.
func (m *manager) signSpendTx(ctx context.Context, account *Account,
packet *psbt.Packet, bestHeight uint32) (*spendPackage, error) {
packet *psbt.Packet, bestHeight uint32,
witnessType witnessType) (*spendPackage, error) {

tx := packet.UnsignedTx

// bestHeight is used as the lock time of the transaction in order to
// satisfy the output's CHECKLOCKTIMEVERIFY in case we're using the
// expiry witness.
tx.LockTime = bestHeight

// Ensure the transaction crafted passes some basic sanity checks before
Expand Down Expand Up @@ -1677,11 +1666,21 @@ func (m *manager) signSpendTx(ctx context.Context, account *Account,

ourSig := pIn.PartialSigs[0].Signature

// We temporarily set the final witness to the partial sig to allow
// extraction of the final TX.
pIn.FinalScriptWitness, err = serializeWitness([][]byte{ourSig})
// We temporarily set the final witness to the partial sig to allow the
// extraction of the final TX. Unless we're using the expiry path in
// which case we _can_ create the full and final witness.
switch witnessType {
case expiryWitness:
pIn.FinalScriptWitness, err = serializeWitness(
poolscript.SpendExpiry(witnessScript, ourSig),
)

default:
pIn.FinalScriptWitness, err = serializeWitness([][]byte{ourSig})
}
if err != nil {
return nil, fmt.Errorf("error serializing witness: %v", err)
return nil, fmt.Errorf("error serializing witness: %v",
err)
}

// We either have a single account input (renew, withdraw, close) that
Expand Down

0 comments on commit 17866e9

Please sign in to comment.