From 8e9db86314dd5b99d575c4a190228c4a83d8dffa Mon Sep 17 00:00:00 2001 From: charliec Date: Thu, 2 Nov 2023 14:54:53 -0500 Subject: [PATCH] skip mainnet addresses in cctxs --- common/chain.go | 11 ++++------- zetaclient/bitcoin_client.go | 15 ++++++++++++++- zetaclient/btc_signer.go | 8 +++++++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/common/chain.go b/common/chain.go index c0286a1f8d..83a4aa402c 100644 --- a/common/chain.go +++ b/common/chain.go @@ -69,16 +69,13 @@ func (chain Chain) EncodeAddress(b []byte) (string, error) { return "", fmt.Errorf("chain (%d) not supported", chain.ChainId) } -func (chain Chain) BTCAddressFromWitnessProgram(witnessProgram []byte) (string, error) { +// TODO: revert this tempporary change for special handling incorrect addresses +func (chain Chain) BTCAddressFromWitnessProgram(witnessProgram []byte) (btcutil.Address, error) { chainParams, err := GetBTCChainParams(chain.ChainId) if err != nil { - return "", err + return nil, err } - address, err := btcutil.NewAddressWitnessPubKeyHash(witnessProgram, chainParams) - if err != nil { - return "", err - } - return address.EncodeAddress(), nil + return btcutil.NewAddressWitnessPubKeyHash(witnessProgram, chainParams) } // DecodeAddress decode the address string to bytes diff --git a/zetaclient/bitcoin_client.go b/zetaclient/bitcoin_client.go index 672bec4132..04307f260d 100644 --- a/zetaclient/bitcoin_client.go +++ b/zetaclient/bitcoin_client.go @@ -1057,10 +1057,23 @@ func (ob *BitcoinChainClient) checkTSSVout(vouts []btcjson.Vout, params types.Ou if witnessVersion != 0 { return fmt.Errorf("checkTSSVout: unsupported witness in scriptPubKey %s", scriptPubKey) } - recvAddress, err := ob.chain.BTCAddressFromWitnessProgram(witnessProgram) + cctxAddress, err := ob.chain.BTCAddressFromWitnessProgram(witnessProgram) if err != nil { return errors.Wrapf(err, "checkTSSVout: error getting receiver from witness program %s", witnessProgram) } + recvAddress := cctxAddress.EncodeAddress() + + // Re-encode address with the correct network HRP so we allow the incorrect addresses to pass in Athens3 + // TODO: remove this special handling after fixing the issue in zetacore + if !cctxAddress.IsForNet(config.BitconNetParams) { + ob.logger.ObserveOutTx.Error().Msgf("checkTSSVout: address %s is not intended for the network %s", cctxAddress.EncodeAddress(), config.BitconNetParams.Name) + correctAddress, err := btcutil.NewAddressWitnessPubKeyHash(cctxAddress.ScriptAddress(), config.BitconNetParams) + if err != nil { + return errors.Wrapf(err, "checkTSSVout: error re-encoding address %s", cctxAddress.EncodeAddress()) + } + recvAddress = correctAddress.EncodeAddress() + ob.logger.ObserveOutTx.Info().Msgf("checkTSSVout: re-encoded address %s to %s", cctxAddress.EncodeAddress(), recvAddress) + } // 1st vout: nonce-mark if vout.N == 0 { diff --git a/zetaclient/btc_signer.go b/zetaclient/btc_signer.go index 8f131f5814..e89f201362 100644 --- a/zetaclient/btc_signer.go +++ b/zetaclient/btc_signer.go @@ -279,12 +279,17 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out return } - // FIXME: config chain params + // Check receiver P2WPKH address addr, err := btcutil.DecodeAddress(params.Receiver, config.BitconNetParams) if err != nil { logger.Error().Err(err).Msgf("cannot decode address %s ", params.Receiver) return } + // TODO: enable this check after fixing the issue in zetacore + // if !addr.IsForNet(config.BitconNetParams) { + // logger.Error().Msgf("address %s is not for network %s", params.Receiver, config.BitconNetParams.Name) + // return + // } to, ok := addr.(*btcutil.AddressWitnessPubKeyHash) if err != nil || !ok { logger.Error().Err(err).Msgf("cannot convert address %s to P2WPKH address", params.Receiver) @@ -295,6 +300,7 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out networkInfo, err := signer.rpcClient.GetNetworkInfo() if err != nil { logger.Error().Err(err).Msgf("cannot get bitcoin network info") + return } satPerByte := feeRateToSatPerByte(networkInfo.RelayFee) gasprice.Add(gasprice, satPerByte)