Skip to content

Commit

Permalink
refactor: use upgraded btcd library to handle Taproot address (#3039)
Browse files Browse the repository at this point in the history
* use upgraded btcd library to handle Taproot address; cleanup previous workaround code

* add changelog entry

* add address types as comments to function call btcutil.DecodeAddress()
  • Loading branch information
ws4charlie authored Oct 25, 2024
1 parent b334267 commit a6f1e44
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 337 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* [2890](https://github.com/zeta-chain/node/pull/2890) - refactor `MsgUpdateChainInfo` to accept a single chain, and add `MsgRemoveChainInfo` to remove a chain
* [2899](https://github.com/zeta-chain/node/pull/2899) - remove btc deposit fee v1 and improve unit tests
* [2952](https://github.com/zeta-chain/node/pull/2952) - add error_message to cctx.status
* [3039](https://github.com/zeta-chain/node/pull/3039) - use `btcd` native APIs to handle Bitcoin Taproot address

### Tests

Expand Down
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
17 changes: 7 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,15 @@ 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.
// this will decode all types of Bitcoin addresses: P2PKH, P2SH, P2WPKH, P2WSH, P2TR, etc.
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 +106,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 a6f1e44

Please sign in to comment.