forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bitcoin): add bridge contract invoke
1. add bridge logic 2. add bitcoin ChainParams func parse bitcoin network 3. fix log error print 4. bitcoin config new way to read env
- Loading branch information
Showing
9 changed files
with
340 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package bitcoin | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/ecdsa" | ||
"fmt" | ||
"math/big" | ||
"net/url" | ||
"os" | ||
"path" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
) | ||
|
||
type Bridge struct { | ||
EthRpcUrl string | ||
EthPrivKey *ecdsa.PrivateKey | ||
ContractAddress common.Address | ||
ABI string | ||
GasLimit uint64 | ||
} | ||
|
||
// NewBridge new bridge | ||
func NewBridge(bridgeCfg BridgeConfig, abiFileDir string) (*Bridge, error) { | ||
rpcUrl, err := url.ParseRequestURI(bridgeCfg.EthRpcUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
privateKey, err := crypto.HexToECDSA(bridgeCfg.EthPrivKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
abi, err := os.ReadFile(path.Join(abiFileDir, bridgeCfg.ABI)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Bridge{ | ||
EthRpcUrl: rpcUrl.String(), | ||
ContractAddress: common.HexToAddress(bridgeCfg.ContractAddress), | ||
EthPrivKey: privateKey, | ||
ABI: string(abi), | ||
GasLimit: bridgeCfg.GasLimit, | ||
}, nil | ||
} | ||
|
||
// Deposit to ethereum | ||
// TODO: partition method and add test | ||
func (b *Bridge) Deposit(bitcoinAddress string, amount int64) error { | ||
if bitcoinAddress == "" { | ||
return fmt.Errorf("bitcoin address is empty") | ||
} | ||
|
||
ctx := context.Background() | ||
// dail ethereum rpc | ||
client, err := ethclient.Dial(b.EthRpcUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
publicKey := b.EthPrivKey.Public() | ||
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) | ||
if !ok { | ||
return fmt.Errorf("error casting public key to ECDSA") | ||
} | ||
nonce, err := client.PendingNonceAt(ctx, crypto.PubkeyToAddress(*publicKeyECDSA)) | ||
if err != nil { | ||
return err | ||
} | ||
gasPrice, err := client.SuggestGasPrice(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
contractAbi, err := abi.JSON(bytes.NewReader([]byte(b.ABI))) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
toAddress, err := b.BitcoinAddressToEthAddress(bitcoinAddress) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := contractAbi.Pack("deposit", common.HexToAddress(toAddress), new(big.Int).SetInt64(amount)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tx := types.NewTx(&types.LegacyTx{ | ||
Nonce: nonce, | ||
To: &b.ContractAddress, | ||
Value: big.NewInt(0), | ||
Gas: b.GasLimit, | ||
GasPrice: gasPrice, | ||
Data: data, | ||
}) | ||
|
||
chainID, err := client.ChainID(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
// sign tx | ||
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), b.EthPrivKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// send tx | ||
err = client.SendTransaction(ctx, signedTx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// wait tx confirm | ||
receipt, err := bind.WaitMined(ctx, client, signedTx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if receipt.Status != 1 { | ||
return fmt.Errorf("tx failed, receipt:%v", receipt) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// BitcoinAddressToEthAddress bitcoin address to eth address | ||
// TODO: implementation | ||
func (b *Bridge) BitcoinAddressToEthAddress(bitcoinAddress string) (string, error) { | ||
// TODO: wait aa finished | ||
return bitcoinAddress, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bitcoin_test | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/evmos/ethermint/bitcoin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewBridge(t *testing.T) { | ||
abiPath := path.Join("./testdata") | ||
|
||
abi, err := os.ReadFile(path.Join("./testdata", "abi.json")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
privateKey, err := crypto.HexToECDSA("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
bridgeCfg := bitcoin.BridgeConfig{ | ||
EthRpcUrl: "http://localhost:8545", | ||
ContractAddress: "0x123456789abcdef", | ||
EthPrivKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", | ||
ABI: "abi.json", | ||
GasLimit: 1000000, | ||
} | ||
|
||
bridge, err := bitcoin.NewBridge(bridgeCfg, abiPath) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, bridge) | ||
assert.Equal(t, bridgeCfg.EthRpcUrl, bridge.EthRpcUrl) | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.