Skip to content

Commit

Permalink
Merge pull request #66 from oxf71/aa
Browse files Browse the repository at this point in the history
btc address to eth address
  • Loading branch information
0x677261706562616261 authored Dec 7, 2023
2 parents 31b29e1 + a7ac9dd commit e7d3fe4
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 64 deletions.
19 changes: 16 additions & 3 deletions bitcoin/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path"

b2aa "github.com/b2network/b2-go-aa-utils"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -26,6 +27,9 @@ type Bridge struct {
ContractAddress common.Address
ABI string
GasLimit uint64
// AA contract address
AASCARegistry common.Address
AAKernelFactory common.Address
}

// NewBridge new bridge
Expand All @@ -51,6 +55,8 @@ func NewBridge(bridgeCfg BridgeConfig, abiFileDir string) (*Bridge, error) {
EthPrivKey: privateKey,
ABI: string(abi),
GasLimit: bridgeCfg.GasLimit,
AASCARegistry: common.HexToAddress(bridgeCfg.AASCARegistry),
AAKernelFactory: common.HexToAddress(bridgeCfg.AAKernelFactory),
}, nil
}

Expand Down Expand Up @@ -142,8 +148,15 @@ func (b *Bridge) ABIPack(abiData string, method string, args ...interface{}) ([]
}

// BitcoinAddressToEthAddress bitcoin address to eth address
// TODO: implementation
func (b *Bridge) BitcoinAddressToEthAddress(bitcoinAddress string) (string, error) {
// TODO: wait aa finished
return bitcoinAddress, nil
client, err := ethclient.Dial(b.EthRPCURL)
if err != nil {
return "", err
}

targetEthAddress, err := b2aa.GetSCAAddress(client, b.AASCARegistry, b.AAKernelFactory, bitcoinAddress)
if err != nil {
return "", err
}
return targetEthAddress.String(), nil
}
44 changes: 43 additions & 1 deletion bitcoin/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func TestNewBridge(t *testing.T) {
EthPrivKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
ABI: "abi.json",
GasLimit: 1000000,
AASCARegistry: "0x123456789abcdefgh",
AAKernelFactory: "0x123456789abcdefg",
}

bridge, err := bitcoin.NewBridge(bridgeCfg, abiPath)
Expand All @@ -42,7 +44,8 @@ func TestNewBridge(t *testing.T) {
assert.Equal(t, common.HexToAddress("0x123456789abcdef"), bridge.ContractAddress)
assert.Equal(t, privateKey, bridge.EthPrivKey)
assert.Equal(t, string(abi), bridge.ABI)
assert.Equal(t, bridgeCfg.GasLimit, bridge.GasLimit)
assert.Equal(t, common.HexToAddress("0x123456789abcdefgh"), bridge.AASCARegistry)
assert.Equal(t, common.HexToAddress("0x123456789abcdefg"), bridge.AAKernelFactory)
}

// TestLocalDeposit only test in local
Expand Down Expand Up @@ -75,6 +78,45 @@ func TestLocalDeposit(t *testing.T) {
}
}

// TestLocalBitcoinAddressToEthAddress only test in local
func TestLocalBitcoinAddressToEthAddress(t *testing.T) {
bridge := bridgeWithConfig(t)
testCase := []struct {
name string
bitcoinAddress string
ethAddress string
}{
{
name: "success: Segwit (bech32)",
bitcoinAddress: "tb1qjda2l5spwyv4ekwe9keddymzuxynea2m2kj0qy",
ethAddress: "0x2A9E233eE5d68fD70DE6C4b1d1Ffa29256e3ee9D",
},
{
name: "success: Segwit (bech32)",
bitcoinAddress: "bc1qf60zw2gec5qg2mk4nyjl0slnytu0s0p28k9her",
ethAddress: "0x1b98017D9d6A9B62a2CFb2764D8012e28606BD49",
},
{
name: "success: Legacy",
bitcoinAddress: "1KEFsFXrvuzMGd7Sdkwp7iTDcEcEv3GP1y",
ethAddress: "0x30f789e9C889A68180ef63F37cac923D89571394",
},
{
name: "success: Segwit",
bitcoinAddress: "3Q4g8hgbwZLZ7vA6U1Xp1UsBs7NBnC7zKS",
ethAddress: "0x0aB97EA8eDff3e28867EAe9e13C02e5aA6214f59",
},
}

for _, tc := range testCase {
t.Run(tc.name, func(t *testing.T) {
ethAddress, err := bridge.BitcoinAddressToEthAddress(tc.bitcoinAddress)
require.NoError(t, err)
assert.Equal(t, tc.ethAddress, ethAddress)
})
}
}

func TestABIPack(t *testing.T) {
t.Run("success", func(t *testing.T) {
abiData, err := os.ReadFile(path.Join("./testdata", "abi.json"))
Expand Down
17 changes: 13 additions & 4 deletions bitcoin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,20 @@ type BitconConfig struct {
}

type BridgeConfig struct {
EthRPCURL string `mapstructure:"eth-rpc-url"`
EthPrivKey string `mapstructure:"eth-priv-key"`
// EthRPCURL defines the ethereum rpc url
EthRPCURL string `mapstructure:"eth-rpc-url"`
// EthPrivKey defines the invoke ethereum private key
EthPrivKey string `mapstructure:"eth-priv-key"`
// ContractAddress defines the l1 -> l2 bridge contract address
ContractAddress string `mapstructure:"contract-address"`
ABI string `mapstructure:"abi"`
GasLimit uint64 `mapstructure:"gas-limit"`
// ABI defines the l1 -> l2 bridge contract abi
ABI string `mapstructure:"abi"`
// GasLimit defines the contract gas limit
GasLimit uint64 `mapstructure:"gas-limit"`
// AASCARegistry defines the contract AASCARegistry address
AASCARegistry string `mapstructure:"aa-sca-registry"`
// AAKernelFactory defines the contract AAKernelFactory address
AAKernelFactory string `mapstructure:"aa-kernel-factory"`
}

const (
Expand Down
8 changes: 8 additions & 0 deletions bitcoin/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestConfig(t *testing.T) {
os.Unsetenv("BITCOIN_BRIDGE_ETH_PRIV_KEY")
os.Unsetenv("BITCOIN_BRIDGE_ABI")
os.Unsetenv("BITCOIN_BRIDGE_GAS_LIMIT")
os.Unsetenv("BITCOIN_BRIDGE_AA_SCA_REGISTRY")
os.Unsetenv("BITCOIN_BRIDGE_AA_KERNEL_FACTORY")
config, err := bitcoin.LoadBitcoinConfig("./testdata")
require.NoError(t, err)
require.Equal(t, "signet", config.NetworkName)
Expand All @@ -42,6 +44,8 @@ func TestConfig(t *testing.T) {
require.Equal(t, "", config.Bridge.EthPrivKey)
require.Equal(t, "abi.json", config.Bridge.ABI)
require.Equal(t, uint64(3000), config.Bridge.GasLimit)
require.Equal(t, "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DFF3", config.Bridge.AASCARegistry)
require.Equal(t, "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DFF4", config.Bridge.AAKernelFactory)
}

func TestConfigEnv(t *testing.T) {
Expand All @@ -59,6 +63,8 @@ func TestConfigEnv(t *testing.T) {
os.Setenv("BITCOIN_BRIDGE_ETH_PRIV_KEY", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
os.Setenv("BITCOIN_BRIDGE_ABI", "aaa.abi")
os.Setenv("BITCOIN_BRIDGE_GAS_LIMIT", "23333")
os.Setenv("BITCOIN_BRIDGE_AA_SCA_REGISTRY", "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DF23")
os.Setenv("BITCOIN_BRIDGE_AA_KERNEL_FACTORY", "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DF24")
config, err := bitcoin.LoadBitcoinConfig("./testdata")
require.NoError(t, err)
require.Equal(t, "testnet", config.NetworkName)
Expand All @@ -75,6 +81,8 @@ func TestConfigEnv(t *testing.T) {
require.Equal(t, "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", config.Bridge.EthPrivKey)
require.Equal(t, "aaa.abi", config.Bridge.ABI)
require.Equal(t, uint64(23333), config.Bridge.GasLimit)
require.Equal(t, "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DF23", config.Bridge.AASCARegistry)
require.Equal(t, "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DF24", config.Bridge.AAKernelFactory)
}

func TestChainParams(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions bitcoin/testdata/bitcoin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ eth-priv-key = ""
contract-address = "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DFF2"
abi = "abi.json"
gas-limit = 3000
aa-sca-registry = "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DFF3"
aa-kernel-factory = "0xB457BF68D71a17Fa5030269Fb895e29e6cD2DFF4"

[evm]
enable-listener = true
rpc-host = "http://127.0.0.1"
rpc-port = 8545
contract-address = "0x47549F0f902F8E0DB96aBb16E06Da60751516F54"
start-height = 0
deposit = "0x01bee1bfa4116bd0440a1108ef6cb6a2f6eb9b611d8f53260aec20d39e84ee88"
deposit = "0x01bee1bfa4116bd0440a1108ef6cb6a2f6eb9b611d8f53260aec20d39e84ee88"
withdraw = "0xda335c6ae73006d1145bdcf9a98bc76d789b653b13fe6200e6fc4c5dd54add85"

23 changes: 12 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.0-rc.0
github.com/armon/go-metrics v0.4.1
github.com/b2network/b2-go-aa-utils v1.0.2
github.com/btcsuite/btcd v0.23.4
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.2
Expand All @@ -22,7 +23,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/holiman/uint256 v1.2.2
github.com/holiman/uint256 v1.2.3
github.com/improbable-eng/grpc-web v0.15.0
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.6
Expand All @@ -33,14 +34,14 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/status-im/keycard-go v0.2.0
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
github.com/tendermint/tendermint v0.34.27
github.com/tendermint/tm-db v0.6.7
github.com/tidwall/gjson v1.14.4
github.com/tidwall/sjson v1.2.5
github.com/tyler-smith/go-bip39 v1.1.0
golang.org/x/net v0.9.0
golang.org/x/text v0.9.0
golang.org/x/net v0.10.0
golang.org/x/text v0.13.0
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f
google.golang.org/grpc v1.54.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -57,7 +58,7 @@ require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/keyring v1.2.1 // indirect
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/Workiva/go-datastructures v1.0.53 // indirect
github.com/aead/siphash v1.0.1 // indirect
Expand Down Expand Up @@ -179,19 +180,19 @@ require (
github.com/tidwall/btree v1.5.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230131160201-f062dba9d201 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.107.0 // indirect
Expand Down
Loading

0 comments on commit e7d3fe4

Please sign in to comment.