Skip to content

Commit

Permalink
feat: support bech32 addresses (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
joroshiba authored Jun 27, 2024
1 parent f59ca43 commit 082e84f
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 74 deletions.
5 changes: 3 additions & 2 deletions genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
"astriaRollupName": "astria",
"astriaOverrideGenesisExtraData": true,
"astriaSequencerInitialHeight": 2,
"astriaSequencerHrpPrefix": "astria",
"astriaCelestiaInitialHeight": 2,
"astriaCelestiaHeightVariance": 10,
"astriaBridgeAddresses": [
{
"bridgeAddress": "684ae50c49a434199199c9c698115391152d7b3f",
"bridgeAddress": "astria1dp9w2rzf5s6pnyvee8rfsy2njy2j67eln842y0",
"startHeight": 1,
"assetDenom": "nria",
"assetPrecision": 9
},
{
"bridgeAddress": "34fec43c7fcab9aef3b3cf8aba855e41ee69ca3a",
"bridgeAddress": "astria1xnlvg0rle2u6auane79t4p27g8hxnj36ja960z",
"startHeight": 1,
"assetDenom": "transfer/channel-1/usdc",
"assetPrecision": 6,
Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/ethereum/go-ethereum
go 1.21

require (
buf.build/gen/go/astria/execution-apis/grpc/go v1.3.0-20240601032207-1bcb86793146.3
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.34.1-20240601032207-1bcb86793146.1
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.34.1-20240529204957-2697e2110d78.1
buf.build/gen/go/astria/sequencerblock-apis/protocolbuffers/go v1.34.1-20240529204957-1b3cb2034833.1
buf.build/gen/go/astria/execution-apis/grpc/go v1.4.0-20240626163506-6267d841fc26.1
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.34.2-20240626163506-6267d841fc26.2
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.34.2-20240626163506-691883836b9e.2
buf.build/gen/go/astria/sequencerblock-apis/protocolbuffers/go v1.34.2-20240626163506-3df35f3e4d06.2
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
github.com/Microsoft/go-winio v0.6.1
github.com/VictoriaMetrics/fastcache v1.12.1
Expand All @@ -15,6 +15,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.13.43
github.com/aws/aws-sdk-go-v2/service/route53 v1.30.2
github.com/btcsuite/btcd/btcec/v2 v2.2.0
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/cespare/cp v0.1.0
github.com/cloudflare/cloudflare-go v0.79.0
github.com/cockroachdb/pebble v1.1.0
Expand All @@ -38,7 +39,7 @@ require (
github.com/golang/protobuf v1.5.4
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa
github.com/google/uuid v1.4.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.0
github.com/graph-gophers/graphql-go v1.3.0
github.com/hashicorp/go-bexpr v0.1.10
Expand Down Expand Up @@ -77,8 +78,8 @@ require (
golang.org/x/text v0.14.0
golang.org/x/time v0.5.0
golang.org/x/tools v0.20.0
google.golang.org/grpc v1.61.2
google.golang.org/protobuf v1.34.1
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -149,9 +150,8 @@ require (
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
80 changes: 57 additions & 23 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions grpc/execution/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ func NewExecutionServiceServerV1Alpha2(eth *eth.Ethereum) (*ExecutionServiceServ
} else {
nativeBridgeSeen := false
for _, cfg := range bc.Config().AstriaBridgeAddressConfigs {
err := cfg.Validate()
err := cfg.Validate(bc.Config().AstriaSequencerHrpPrefix)
if err != nil {
return nil, fmt.Errorf("invalid bridge address config: %w", err)
}

if cfg.Erc20Asset == nil {
if nativeBridgeSeen {
return nil, errors.New("only one native bridge address is allowed")
return nil, errors.New("only one native bridge address is allowed")
}
nativeBridgeSeen = true
}
Expand All @@ -117,7 +117,7 @@ func NewExecutionServiceServerV1Alpha2(eth *eth.Ethereum) (*ExecutionServiceServ
return nil, errors.New("astria bridge sender address must be set for bridged ERC20 assets")
}

bridgeAddresses[string(cfg.BridgeAddress)] = &cfg
bridgeAddresses[cfg.BridgeAddress] = &cfg
assetID := sha256.Sum256([]byte(cfg.AssetDenom))
bridgeAllowedAssetIDs[assetID] = struct{}{}
if cfg.Erc20Asset == nil {
Expand Down
4 changes: 2 additions & 2 deletions grpc/execution/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlock(t *testing.T) {

depositTx := &sequencerblockv1alpha1.RollupData{Value: &sequencerblockv1alpha1.RollupData_Deposit{Deposit: &sequencerblockv1alpha1.Deposit{
BridgeAddress: &primitivev1.Address{
Inner: bridgeAddress,
Bech32M: bridgeAddress,
},
AssetId: bridgeAssetDenom[:],
Amount: depositAmount,
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlockAndUpdateCommitment(t *testi

depositTx := &sequencerblockv1alpha1.RollupData{Value: &sequencerblockv1alpha1.RollupData_Deposit{Deposit: &sequencerblockv1alpha1.Deposit{
BridgeAddress: &primitivev1.Address{
Inner: bridgeAddress,
Bech32M: bridgeAddress,
},
AssetId: bridgeAssetDenom[:],
Amount: depositAmount,
Expand Down
29 changes: 20 additions & 9 deletions grpc/execution/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package execution
import (
"crypto/ecdsa"
"crypto/sha256"
"math/big"
"testing"
"time"

"github.com/btcsuite/btcd/btcutil/bech32"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
beaconConsensus "github.com/ethereum/go-ethereum/consensus/beacon"
Expand All @@ -17,9 +22,6 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
"math/big"
"testing"
"time"
)

var (
Expand All @@ -34,7 +36,7 @@ var (
testBalance = big.NewInt(2e18)
)

func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block, *ecdsa.PrivateKey, *ecdsa.PrivateKey) {
func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block, string, *ecdsa.PrivateKey) {
config := *params.AllEthashProtocolChanges
engine := consensus.Engine(beaconConsensus.New(ethash.NewFaker()))
if merged {
Expand All @@ -48,14 +50,24 @@ func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block, *ecd
panic(err)
}
bridgeAddress := crypto.PubkeyToAddress(bridgeAddressKey.PublicKey)
bridgeAddressBytes, err := bech32.ConvertBits(bridgeAddress.Bytes(), 8, 5, false)
if err != nil {
panic(err)
}

config.AstriaRollupName = "astria"
config.AstriaSequencerHrpPrefix = "astria"
config.AstriaSequencerInitialHeight = 10
config.AstriaCelestiaInitialHeight = 10
config.AstriaCelestiaHeightVariance = 10

bech32mBridgeAddress, err := bech32.EncodeM(config.AstriaSequencerHrpPrefix, bridgeAddressBytes)
if err != nil {
panic(err)
}
config.AstriaBridgeAddressConfigs = []params.AstriaBridgeAddressConfig{
{
BridgeAddress: bridgeAddress.Bytes(),
BridgeAddress: bech32mBridgeAddress,
StartHeight: 2,
AssetDenom: "nria",
AssetPrecision: 18,
Expand Down Expand Up @@ -101,7 +113,7 @@ func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block, *ecd
config.TerminalTotalDifficulty = totalDifficulty
}

return genesis, blocks, bridgeAddressKey, feeCollectorKey
return genesis, blocks, bech32mBridgeAddress, feeCollectorKey
}

// startEthService creates a full node instance for testing.
Expand All @@ -120,7 +132,7 @@ func startEthService(t *testing.T, genesis *core.Genesis) *eth.Ethereum {

func setupExecutionService(t *testing.T, noOfBlocksToGenerate int) (*eth.Ethereum, *ExecutionServiceServerV1Alpha2) {
t.Helper()
genesis, blocks, bridgeAddressKey, feeCollectorKey := generateMergeChain(noOfBlocksToGenerate, true)
genesis, blocks, bridgeAddress, feeCollectorKey := generateMergeChain(noOfBlocksToGenerate, true)
ethservice := startEthService(t, genesis)

serviceV1Alpha1, err := NewExecutionServiceServerV1Alpha2(ethservice)
Expand All @@ -133,8 +145,7 @@ func setupExecutionService(t *testing.T, noOfBlocksToGenerate int) (*eth.Ethereu
_, ok := serviceV1Alpha1.bridgeAllowedAssetIDs[bridgeAsset]
require.True(t, ok, "bridgeAllowedAssetIDs does not contain bridge asset id")

bridgeAddress := crypto.PubkeyToAddress(bridgeAddressKey.PublicKey)
_, ok = serviceV1Alpha1.bridgeAddresses[string(bridgeAddress.Bytes())]
_, ok = serviceV1Alpha1.bridgeAddresses[bridgeAddress]
require.True(t, ok, "bridgeAddress not set correctly")

_, err = ethservice.BlockChain().InsertChain(blocks)
Expand Down
2 changes: 1 addition & 1 deletion grpc/execution/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func validateAndUnmarshalSequencerTx(
bridgeSenderAddress common.Address,
) (*types.Transaction, error) {
if deposit := tx.GetDeposit(); deposit != nil {
bridgeAddress := string(deposit.BridgeAddress.GetInner())
bridgeAddress := deposit.BridgeAddress.GetBech32M()
bac, ok := bridgeAddresses[bridgeAddress]
if !ok {
return nil, fmt.Errorf("unknown bridge address: %s", bridgeAddress)
Expand Down
41 changes: 31 additions & 10 deletions grpc/execution/validation_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package execution

import (
"crypto/sha256"
"math/big"
"testing"

primitivev1 "buf.build/gen/go/astria/primitives/protocolbuffers/go/astria/primitive/v1"
sequencerblockv1alpha1 "buf.build/gen/go/astria/sequencerblock-apis/protocolbuffers/go/astria/sequencerblock/v1alpha1"
"crypto/sha256"
"github.com/btcsuite/btcd/btcutil/bech32"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
"math/big"
"testing"
)

func testBlobTx() *types.Transaction {
Expand All @@ -32,6 +34,25 @@ func testDepositTx() *types.Transaction {
})
}

func generateBech32MAddress() string {
addressKey, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
bridgeAddress := crypto.PubkeyToAddress(addressKey.PublicKey)
bridgeAddressBytes, err := bech32.ConvertBits(bridgeAddress.Bytes(), 8, 5, false)
if err != nil {
panic(err)
}

bech32m, err := bech32.EncodeM("astria", bridgeAddressBytes)
if err != nil {
panic(err)
}

return bech32m
}

func TestSequenceTxValidation(t *testing.T) {
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

Expand All @@ -57,8 +78,8 @@ func TestSequenceTxValidation(t *testing.T) {

invalidHeightBridgeAssetDenom := "invalid-height-asset-denom"
invalidHeightBridgeAssetDenomID := sha256.Sum256([]byte(invalidHeightBridgeAssetDenom))
invalidHeightBridgeAddress := "invalid-height-bridge-address"
serviceV1Alpha1.bridgeAddresses[invalidHeightBridgeAddress] = &params.AstriaBridgeAddressConfig{
invalidHeightBridgeAddressBech32m := generateBech32MAddress()
serviceV1Alpha1.bridgeAddresses[invalidHeightBridgeAddressBech32m] = &params.AstriaBridgeAddressConfig{
AssetDenom: invalidHeightBridgeAssetDenom,
StartHeight: 100,
}
Expand Down Expand Up @@ -102,7 +123,7 @@ func TestSequenceTxValidation(t *testing.T) {
description: "deposit tx with an unknown bridge address",
sequencerTx: &sequencerblockv1alpha1.RollupData{Value: &sequencerblockv1alpha1.RollupData_Deposit{Deposit: &sequencerblockv1alpha1.Deposit{
BridgeAddress: &primitivev1.Address{
Inner: []byte("unknown-bridge-address"),
Bech32M: generateBech32MAddress(),
},
AssetId: bridgeAssetDenom[:],
Amount: bigIntToProtoU128(big.NewInt(1000000000000000000)),
Expand All @@ -115,7 +136,7 @@ func TestSequenceTxValidation(t *testing.T) {
description: "deposit tx with an invalid asset id",
sequencerTx: &sequencerblockv1alpha1.RollupData{Value: &sequencerblockv1alpha1.RollupData_Deposit{Deposit: &sequencerblockv1alpha1.Deposit{
BridgeAddress: &primitivev1.Address{
Inner: bridgeAddress,
Bech32M: bridgeAddress,
},
AssetId: []byte("invalid-asset-id"),
Amount: bigIntToProtoU128(big.NewInt(1000000000000000000)),
Expand All @@ -128,7 +149,7 @@ func TestSequenceTxValidation(t *testing.T) {
description: "deposit tx with a disallowed asset id",
sequencerTx: &sequencerblockv1alpha1.RollupData{Value: &sequencerblockv1alpha1.RollupData_Deposit{Deposit: &sequencerblockv1alpha1.Deposit{
BridgeAddress: &primitivev1.Address{
Inner: bridgeAddress,
Bech32M: bridgeAddress,
},
AssetId: invalidBridgeAssetDenom[:],
Amount: bigIntToProtoU128(big.NewInt(1000000000000000000)),
Expand All @@ -141,7 +162,7 @@ func TestSequenceTxValidation(t *testing.T) {
description: "deposit tx with a height and asset below the bridge start height",
sequencerTx: &sequencerblockv1alpha1.RollupData{Value: &sequencerblockv1alpha1.RollupData_Deposit{Deposit: &sequencerblockv1alpha1.Deposit{
BridgeAddress: &primitivev1.Address{
Inner: []byte(invalidHeightBridgeAddress),
Bech32M: invalidHeightBridgeAddressBech32m,
},
AssetId: invalidHeightBridgeAssetDenomID[:],
Amount: bigIntToProtoU128(big.NewInt(1000000000000000000)),
Expand All @@ -154,7 +175,7 @@ func TestSequenceTxValidation(t *testing.T) {
description: "valid deposit tx",
sequencerTx: &sequencerblockv1alpha1.RollupData{Value: &sequencerblockv1alpha1.RollupData_Deposit{Deposit: &sequencerblockv1alpha1.Deposit{
BridgeAddress: &primitivev1.Address{
Inner: bridgeAddress,
Bech32M: bridgeAddress,
},
AssetId: bridgeAssetDenom[:],
Amount: bigIntToProtoU128(big.NewInt(1000000000000000000)),
Expand Down
Loading

0 comments on commit 082e84f

Please sign in to comment.