Skip to content

Commit

Permalink
add generateRedeemSACP
Browse files Browse the repository at this point in the history
  • Loading branch information
Revantark committed Jan 17, 2025
1 parent b0b53b2 commit 57efcc9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 0 additions & 4 deletions btc/guardian/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,6 @@ func (w *Wallet) batchAndBroadcast(ctx context.Context, req []btc.SendRequest, p
return chainhash.Hash{}, fmt.Errorf("failed to adjust fee: %w", err)
}

for _, in := range tx.TxIn {
w.logger.Info("Witness size", zap.Int("size", in.Witness.SerializeSize()))
}

tx, err = w.signTx(tx, inValues)
if err != nil {
return chainhash.Hash{}, fmt.Errorf("failed to sign tx: %w", err)
Expand Down
35 changes: 35 additions & 0 deletions btc/htlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type HTLCWallet interface {
//
// Signature is added at the first index of the witness of the transaction inputs.
GenerateInstantRefundSACP(ctx context.Context, htlc *HTLC, recipient btcutil.Address) ([]byte, error)

// GenerateRedeemSACP generates the SACP tx needed for the redeem action.
GenerateRedeemSACP(ctx context.Context, secret []byte, htlc *HTLC, to btcutil.Address) ([]byte, error)

// Address returns the tapscript address of the HTLC
Address(htlc *HTLC) (btcutil.Address, error)
// Status returns the transaction if submitted and bool indicating whether the transaction
Expand Down Expand Up @@ -157,6 +161,37 @@ func (hw *htlcWallet) Address(htlc *HTLC) (btcutil.Address, error) {
return addr, nil
}

func (hw *htlcWallet) GenerateRedeemSACP(ctx context.Context, secret []byte, htlc *HTLC, to btcutil.Address) ([]byte, error) {
redeemTapLeaf, cbBytes, err := getControlBlock(hw.internalKey, htlc, LeafRedeem)
if err != nil {
return nil, err
}

witness := [][]byte{
AddSignatureSchnorrOp,
secret,
redeemTapLeaf.Script,
cbBytes,
}

scriptAddr, err := hw.Address(htlc)
if err != nil {
return nil, err
}

txBytes, err := hw.wallet.GenerateSACP(ctx, SpendRequest{
Witness: witness,
Leaf: redeemTapLeaf,
ScriptAddress: scriptAddr,
HashType: SigHashSingleAnyoneCanPay,
Recipient: to,
}, to)
if err != nil {
return nil, err
}
return txBytes, nil
}

// GenerateInstantRefundSACP generates the SACP tx needed for the instant refunds
func (hw *htlcWallet) GenerateInstantRefundSACP(ctx context.Context, htlc *HTLC, recipient btcutil.Address) ([]byte, error) {
instantRefundLeaf, cbBytes, err := getControlBlock(hw.internalKey, htlc, LeafInstantRefund)
Expand Down
4 changes: 2 additions & 2 deletions btc/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ var _ = Describe("Wallets", Ordered, func() {
fmt.Println("charlieAddr", charlieAddr.EncodeAddress())

req = []btc.SendRequest{
btc.NewSendRequestWithInvalidateID("3", 100000, charlieAddr, "2"),
btc.NewSendRequestWithInvalidateID("3", 100000, charlieAddr, "2", ""),
}

txid, err = wallet.Send(context.Background(), req, nil, nil)
Expand All @@ -210,7 +210,7 @@ var _ = Describe("Wallets", Ordered, func() {

// but change the id to 1
req = []btc.SendRequest{
btc.NewSendRequestWithInvalidateID("4", 100000, daveAddr, "1"),
btc.NewSendRequestWithInvalidateID("4", 100000, daveAddr, "1", ""),
}

txid, err = wallet.Send(context.Background(), req, nil, nil)
Expand Down

0 comments on commit 57efcc9

Please sign in to comment.