Skip to content

Commit

Permalink
use upgraded btcd library to handle Taproot address; cleanup previous…
Browse files Browse the repository at this point in the history
… workaround code
  • Loading branch information
ws4charlie committed Oct 24, 2024
1 parent b334267 commit 1b89911
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 337 deletions.
4 changes: 2 additions & 2 deletions e2e/e2etests/test_bitcoin_withdraw_taproot.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package e2etests

import (
"github.com/btcsuite/btcd/btcutil"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/node/e2e/runner"
"github.com/zeta-chain/node/pkg/chains"
)

func TestBitcoinWithdrawTaproot(r *runner.E2ERunner, args []string) {
Expand All @@ -15,7 +15,7 @@ func TestBitcoinWithdrawTaproot(r *runner.E2ERunner, args []string) {
// parse arguments and withdraw BTC
defaultReceiver := "bcrt1pqqqsyqcyq5rqwzqfpg9scrgwpugpzysnzs23v9ccrydpk8qarc0sj9hjuh"
receiver, amount := parseBitcoinWithdrawArgs(r, args, defaultReceiver)
_, ok := receiver.(*chains.AddressTaproot)
_, ok := receiver.(*btcutil.AddressTaproot)
require.True(r, ok, "Invalid receiver address specified for TestBitcoinWithdrawTaproot.")

withdrawBTCZRC20(r, receiver, amount)
Expand Down
16 changes: 6 additions & 10 deletions pkg/chains/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func ConvertRecoverToError(r interface{}) error {

// DecodeBtcAddress decodes a BTC address from a given string and chainID
func DecodeBtcAddress(inputAddress string, chainID int64) (address btcutil.Address, err error) {
// prevent potential panic from 'btcutil.DecodeAddress'
defer func() {
if r := recover(); r != nil {
err = ConvertRecoverToError(r)
Expand All @@ -68,19 +69,14 @@ func DecodeBtcAddress(inputAddress string, chainID int64) (address btcutil.Addre
if chainParams == nil {
return nil, fmt.Errorf("chain params not found")
}
// test taproot address type
address, err = DecodeTaprootAddress(inputAddress)
if err == nil {
if address.IsForNet(chainParams) {
return address, nil
}
return nil, fmt.Errorf("address %s is not for network %s", inputAddress, chainParams.Name)
}
// test taproot address failed; continue testing other types: P2WSH, P2WPKH, P2SH, P2PKH

// try decoding input address as a Bitcoin address
address, err = btcutil.DecodeAddress(inputAddress, chainParams)
if err != nil {
return nil, fmt.Errorf("decode address failed: %s, for input address %s", err.Error(), inputAddress)
}

// address must match the network
ok := address.IsForNet(chainParams)
if !ok {
return nil, fmt.Errorf("address %s is not for network %s", inputAddress, chainParams.Name)
Expand Down Expand Up @@ -109,7 +105,7 @@ func DecodeSolanaWalletAddress(inputAddress string) (pk solana.PublicKey, err er
func IsBtcAddressSupported(addr btcutil.Address) bool {
switch addr.(type) {
// P2TR address
case *AddressTaproot,
case *btcutil.AddressTaproot,
// P2WSH address
*btcutil.AddressWitnessScriptHash,
// P2WPKH address
Expand Down
217 changes: 0 additions & 217 deletions pkg/chains/address_taproot.go

This file was deleted.

79 changes: 0 additions & 79 deletions pkg/chains/address_taproot_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/chains/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func Test_IsBtcAddressSupported_P2TR(t *testing.T) {
// it should be a taproot address
addr, err := DecodeBtcAddress(tt.addr, tt.chainId)
require.NoError(t, err)
_, ok := addr.(*AddressTaproot)
_, ok := addr.(*btcutil.AddressTaproot)
require.True(t, ok)

// it should be supported
Expand Down
3 changes: 1 addition & 2 deletions zetaclient/chains/bitcoin/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/pkg/errors"

"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/zetaclient/chains/bitcoin/rpc"
"github.com/zeta-chain/node/zetaclient/chains/interfaces"
clientcommon "github.com/zeta-chain/node/zetaclient/common"
Expand Down Expand Up @@ -104,7 +103,7 @@ func EstimateOutboundSize(numInputs uint64, payees []btcutil.Address) (uint64, e
// GetOutputSizeByAddress returns the size of a tx output in bytes by the given address
func GetOutputSizeByAddress(to btcutil.Address) (uint64, error) {
switch addr := to.(type) {
case *chains.AddressTaproot:
case *btcutil.AddressTaproot:
if addr == nil {
return 0, nil
}
Expand Down
Loading

0 comments on commit 1b89911

Please sign in to comment.