Skip to content

Commit

Permalink
add randomness parameter to sample.BtcAddressP2WPKH
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Jan 21, 2025
1 parent 279335b commit 6b125bc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
10 changes: 5 additions & 5 deletions testutil/sample/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"
"testing"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -92,8 +92,8 @@ func EthAddressFromRand(r *rand.Rand) ethcommon.Address {
}

// BtcAddressP2WPKH returns a sample Bitcoin Pay-to-Witness-Public-Key-Hash (P2WPKH) address
func BtcAddressP2WPKH(t *testing.T, net *chaincfg.Params) *btcutil.AddressWitnessPubKeyHash {
privateKey, err := btcec.NewPrivateKey()
func BtcAddressP2WPKH(t *testing.T, r *rand.Rand, net *chaincfg.Params) *btcutil.AddressWitnessPubKeyHash {
privateKey, err := secp.GeneratePrivateKeyFromRand(r)
require.NoError(t, err)

pubKeyHash := btcutil.Hash160(privateKey.PubKey().SerializeCompressed())
Expand All @@ -104,8 +104,8 @@ func BtcAddressP2WPKH(t *testing.T, net *chaincfg.Params) *btcutil.AddressWitnes
}

// BtcAddressP2WPKH returns a pkscript for a sample btc P2WPKH address
func BtcAddressP2WPKHScript(t *testing.T, net *chaincfg.Params) []byte {
addr := BtcAddressP2WPKH(t, net)
func BtcAddressP2WPKHScript(t *testing.T, r *rand.Rand, net *chaincfg.Params) []byte {
addr := BtcAddressP2WPKH(t, r, net)
script, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
return script
Expand Down
3 changes: 2 additions & 1 deletion x/crosschain/types/cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ func Test_SetRevertOutboundValues(t *testing.T) {
})

t.Run("successfully set BTC revert address V1", func(t *testing.T) {
r := sample.Rand()
cctx := sample.CrossChainTx(t, "test")
cctx.InboundParams.SenderChainId = chains.BitcoinTestnet.ChainId
cctx.OutboundParams = cctx.OutboundParams[:1]
cctx.RevertOptions.RevertAddress = sample.BtcAddressP2WPKH(t, &chaincfg.TestNet3Params).String()
cctx.RevertOptions.RevertAddress = sample.BtcAddressP2WPKH(t, r, &chaincfg.TestNet3Params).String()

err := cctx.AddRevertOutbound(100)
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion x/crosschain/types/revert_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func TestRevertOptions_GetEVMRevertAddress(t *testing.T) {

func TestRevertOptions_GetBTCRevertAddress(t *testing.T) {
t.Run("valid Bitcoin revert address", func(t *testing.T) {
addr := sample.BtcAddressP2WPKH(t, &chaincfg.TestNet3Params).String()
r := sample.Rand()
addr := sample.BtcAddressP2WPKH(t, r, &chaincfg.TestNet3Params).String()
actualAddr, valid := types.RevertOptions{
RevertAddress: addr,
}.GetBTCRevertAddress(chains.BitcoinTestnet.ChainId)
Expand Down
9 changes: 6 additions & 3 deletions zetaclient/chains/bitcoin/observer/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func createTestBtcEvent(
memoStd *memo.InboundMemo,
) observer.BTCInboundEvent {
return observer.BTCInboundEvent{
FromAddress: sample.BtcAddressP2WPKH(t, net).String(),
FromAddress: sample.BtcAddressP2WPKH(t, sample.Rand(), net).String(),
ToAddress: sample.EthAddress().Hex(),
MemoBytes: memo,
MemoStd: memoStd,
Expand Down Expand Up @@ -235,6 +235,8 @@ func Test_DecodeEventMemoBytes(t *testing.T) {
}

func Test_ValidateStandardMemo(t *testing.T) {
r := sample.Rand()

// test cases
tests := []struct {
name string
Expand All @@ -249,7 +251,7 @@ func Test_ValidateStandardMemo(t *testing.T) {
},
FieldsV0: memo.FieldsV0{
RevertOptions: crosschaintypes.RevertOptions{
RevertAddress: sample.BtcAddressP2WPKH(t, &chaincfg.TestNet3Params).String(),
RevertAddress: sample.BtcAddressP2WPKH(t, r, &chaincfg.TestNet3Params).String(),
},
},
},
Expand Down Expand Up @@ -399,8 +401,9 @@ func Test_NewInboundVoteFromStdMemo(t *testing.T) {

t.Run("should create new inbound vote msg with standard memo", func(t *testing.T) {
// create revert options
r := sample.Rand()
revertOptions := crosschaintypes.NewEmptyRevertOptions()
revertOptions.RevertAddress = sample.BtcAddressP2WPKH(t, &chaincfg.MainNetParams).String()
revertOptions.RevertAddress = sample.BtcAddressP2WPKH(t, r, &chaincfg.MainNetParams).String()

// create test event
receiver := sample.EthAddress()
Expand Down
4 changes: 3 additions & 1 deletion zetaclient/chains/bitcoin/observer/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ func TestAvgFeeRateBlock828440Errors(t *testing.T) {
}

func Test_GetInboundVoteFromBtcEvent(t *testing.T) {
r := sample.Rand()

// can use any bitcoin chain for testing
chain := chains.BitcoinMainnet

Expand All @@ -167,7 +169,7 @@ func Test_GetInboundVoteFromBtcEvent(t *testing.T) {
{
name: "should return vote for standard memo",
event: &observer.BTCInboundEvent{
FromAddress: sample.BtcAddressP2WPKH(t, &chaincfg.MainNetParams).String(),
FromAddress: sample.BtcAddressP2WPKH(t, r, &chaincfg.MainNetParams).String(),
// a deposit and call
MemoBytes: testutil.HexToBytes(
t,
Expand Down

0 comments on commit 6b125bc

Please sign in to comment.