From 57efcc9430c9f99dc1b1f38174138533f83922b4 Mon Sep 17 00:00:00 2001 From: revantark Date: Fri, 17 Jan 2025 18:21:18 +0530 Subject: [PATCH] add generateRedeemSACP --- btc/guardian/wallet.go | 4 ---- btc/htlc.go | 35 +++++++++++++++++++++++++++++++++++ btc/wallet_test.go | 4 ++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/btc/guardian/wallet.go b/btc/guardian/wallet.go index 946937b..d14ba56 100644 --- a/btc/guardian/wallet.go +++ b/btc/guardian/wallet.go @@ -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) diff --git a/btc/htlc.go b/btc/htlc.go index 0a03958..f832b25 100644 --- a/btc/htlc.go +++ b/btc/htlc.go @@ -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 @@ -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) diff --git a/btc/wallet_test.go b/btc/wallet_test.go index 7e1dc7d..fa7b7fa 100644 --- a/btc/wallet_test.go +++ b/btc/wallet_test.go @@ -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) @@ -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)