Skip to content

Commit

Permalink
skip mainnet addresses in cctxs
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Nov 2, 2023
1 parent 6e1fb4a commit 8e9db86
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
11 changes: 4 additions & 7 deletions common/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion zetaclient/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion zetaclient/btc_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8e9db86

Please sign in to comment.