Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom address prefix #91

Merged
merged 9 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions contrib/launchtools/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -35,6 +36,9 @@ $ launchtools launch mahalo-3 --artifacts-dir ./ --with-config ./config.json
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
sdk.SetAddrCacheEnabled(false)
defer sdk.SetAddrCacheEnabled(true)

clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)

Expand All @@ -53,12 +57,9 @@ $ launchtools launch mahalo-3 --artifacts-dir ./ --with-config ./config.json
return errors.Wrap(err, "failed to get config flag")
}

config := &Config{}
if configPath != "" {
config, err = Config{}.FromFile(configPath)
if err != nil {
return err
}
config, err := NewConfig(configPath)
if err != nil {
return err
}

if err := config.Finalize(targetNetwork, bufio.NewReader(clientCtx.Input)); err != nil {
Expand Down
111 changes: 66 additions & 45 deletions contrib/launchtools/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"reflect"
"time"

"github.com/initia-labs/OPinit/contrib/launchtools/utils"
"github.com/pkg/errors"

"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/go-bip39"
)

Expand All @@ -26,7 +28,11 @@ type Config struct {
GenesisAccounts *GenesisAccounts `json:"genesis_accounts,omitempty"`
}

func (input Config) FromFile(path string) (*Config, error) {
func NewConfig(path string) (*Config, error) {
if path == "" {
return &Config{}, nil
}

bz, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to read file: %s", path))
Expand Down Expand Up @@ -167,29 +173,30 @@ func (l1config *L1Config) Finalize(targetNetwork string) error {
return nil
}

type Account struct {
Address string `json:"address,omitempty"`
Mnemonic string `json:"mnemonic,omitempty"`
type SystemAccount struct {
L1Address string `json:"l1_address,omitempty"`
L2Address string `json:"l2_address,omitempty"`
Mnemonic string `json:"mnemonic,omitempty"`
}

type AccountWithBalance struct {
Account
Coins string `json:"coins,omitempty"`
type GenesisAccount struct {
Address string `json:"address,omitempty"`
Coins string `json:"coins,omitempty"`
}

type GenesisAccounts []AccountWithBalance
type GenesisAccounts []GenesisAccount

func (gas *GenesisAccounts) Finalize(systemKeys SystemKeys) error {
keys := reflect.ValueOf(systemKeys)
for idx := 0; idx < keys.NumField(); idx++ {
k, ok := keys.Field(idx).Interface().(*Account)
k, ok := keys.Field(idx).Interface().(*SystemAccount)
if !ok {
return errors.New("systemKeys must be of type launcher.Account")
}

found := false
for _, ga := range *gas {
if ga.Address == k.Address {
if ga.Address == k.L2Address {
found = true
break
}
Expand All @@ -198,8 +205,8 @@ func (gas *GenesisAccounts) Finalize(systemKeys SystemKeys) error {
continue
}

*gas = append(*gas, AccountWithBalance{
Account: Account{Address: k.Address},
*gas = append(*gas, GenesisAccount{
Address: k.L2Address,
Coins: "",
})
}
Expand All @@ -219,13 +226,13 @@ func (gas *GenesisAccounts) Finalize(systemKeys SystemKeys) error {
}

type SystemKeys struct {
Validator *Account `json:"validator,omitempty"`
BridgeExecutor *Account `json:"bridge_executor,omitempty"`
OutputSubmitter *Account `json:"output_submitter,omitempty"`
BatchSubmitter *Account `json:"batch_submitter,omitempty"`
Validator *SystemAccount `json:"validator,omitempty"`
BridgeExecutor *SystemAccount `json:"bridge_executor,omitempty"`
OutputSubmitter *SystemAccount `json:"output_submitter,omitempty"`
BatchSubmitter *SystemAccount `json:"batch_submitter,omitempty"`

// Challenger does not require mnemonic
Challenger *Account `json:"challenger,omitempty"`
Challenger *SystemAccount `json:"challenger,omitempty"`
}

const mnemonicEntropySize = 256
Expand All @@ -243,19 +250,28 @@ func generateMnemonic() (string, error) {
return mnemonic, nil
}

func deriveAddress(mnemonic string) (string, error) {
func deriveAddress(mnemonic string) (string, string, error) {
algo := hd.Secp256k1
derivedPriv, err := algo.Derive()(
mnemonic,
keyring.DefaultBIP39Passphrase,
sdk.GetConfig().GetFullBIP44Path(),
)
if err != nil {
return "", errors.Wrap(err, "failed to derive private key")
return "", "", errors.Wrap(err, "failed to derive private key")
}

privKey := algo.Generate()(derivedPriv)
return sdk.AccAddress(privKey.PubKey().Address()).String(), nil
addrBz := privKey.PubKey().Address()

// use init Bech32 prefix for l1 address
l1Addr, err := utils.L1AddressCodec().BytesToString(addrBz)
if err != nil {
return "", "", errors.Wrap(err, "failed to convert address to bech32")
}

l2Addr, err := utils.L2AddressCodec().BytesToString(addrBz)
return l1Addr, l2Addr, err
}

func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader) error {
Expand All @@ -266,14 +282,15 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader) error {
}

// derive address
addr, err := deriveAddress(mnemonic)
l1Addr, l2Addr, err := deriveAddress(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}

systemKeys.Validator = &Account{
Address: addr,
Mnemonic: mnemonic,
systemKeys.Validator = &SystemAccount{
L1Address: l1Addr,
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.BatchSubmitter == nil {
Expand All @@ -283,14 +300,15 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader) error {
}

// derive address
addr, err := deriveAddress(mnemonic)
l1Addr, l2Addr, err := deriveAddress(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}

systemKeys.BatchSubmitter = &Account{
Address: addr,
Mnemonic: mnemonic,
systemKeys.BatchSubmitter = &SystemAccount{
L1Address: l1Addr,
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.BridgeExecutor == nil {
Expand All @@ -304,14 +322,15 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader) error {
}

// derive address
addr, err := deriveAddress(mnemonic)
l1Addr, l2Addr, err := deriveAddress(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}

systemKeys.BridgeExecutor = &Account{
Address: addr,
Mnemonic: mnemonic,
systemKeys.BridgeExecutor = &SystemAccount{
L1Address: l1Addr,
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.Challenger == nil {
Expand All @@ -321,14 +340,15 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader) error {
}

// derive address
addr, err := deriveAddress(mnemonic)
l1Addr, l2Addr, err := deriveAddress(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}

systemKeys.Challenger = &Account{
Address: addr,
Mnemonic: mnemonic,
systemKeys.Challenger = &SystemAccount{
L1Address: l1Addr,
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.OutputSubmitter == nil {
Expand All @@ -338,31 +358,32 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader) error {
}

// derive address
addr, err := deriveAddress(mnemonic)
l1Addr, l2Addr, err := deriveAddress(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}

systemKeys.OutputSubmitter = &Account{
Address: addr,
Mnemonic: mnemonic,
systemKeys.OutputSubmitter = &SystemAccount{
L1Address: l1Addr,
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}

// validate all accounts
if systemKeys.Validator.Address == "" || systemKeys.Validator.Mnemonic == "" {
if systemKeys.Validator.L2Address == "" || systemKeys.Validator.Mnemonic == "" {
return errors.New("validator account not initialized")
}
if systemKeys.BridgeExecutor.Address == "" || systemKeys.BridgeExecutor.Mnemonic == "" {
if systemKeys.BridgeExecutor.L1Address == "" || systemKeys.BridgeExecutor.L2Address == "" || systemKeys.BridgeExecutor.Mnemonic == "" {
return errors.New("bridge_executor account not initialized")
}
if systemKeys.BatchSubmitter.Address == "" {
if systemKeys.BatchSubmitter.L1Address == "" {
return errors.New("batch_submitter account not initialized")
}
if systemKeys.OutputSubmitter.Address == "" {
if systemKeys.OutputSubmitter.L1Address == "" {
return errors.New("output_submitter account not initialized")
}
if systemKeys.Challenger.Address == "" {
if systemKeys.Challenger.L1Address == "" {
return errors.New("challenger account not initialized")
}

Expand Down
6 changes: 3 additions & 3 deletions contrib/launchtools/steps/bridgeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ func SetBridgeInfo(

// create SetBridgeInfo message
setBridgeInfoMessage := setBridgeInfo(
config.SystemKeys.BridgeExecutor.Address,
config.SystemKeys.BridgeExecutor.L2Address,
bridgeId,
bridgeInfo.BridgeAddr,
config.L1Config.ChainID,
l1ClientID,
bridgeInfo.BridgeConfig,
)
// send createOpBridgeMessage to host (L1)
// send MsgSetBridgeInfo to host (L1)
txRes, err := ctx.GetRPCHelperL2().BroadcastTxAndWait(
config.SystemKeys.BridgeExecutor.Address,
config.SystemKeys.BridgeExecutor.L2Address,
config.SystemKeys.BridgeExecutor.Mnemonic,
200000,
sdk.NewCoins(),
Expand Down
29 changes: 15 additions & 14 deletions contrib/launchtools/steps/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/cosmos/go-bip39"
"github.com/initia-labs/OPinit/contrib/launchtools"
"github.com/initia-labs/OPinit/contrib/launchtools/utils"
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -107,7 +108,7 @@ func initializeGenesis(
"created node identity",
"node_id", nodeId,
"chain_id", config.L2Config.ChainID,
"validator_address", validatorKeySpec.Address,
"validator_address", validatorKeySpec.L2Address,
"moniker", cometConfig.Moniker,
)

Expand Down Expand Up @@ -144,7 +145,7 @@ func initializeGenesis(
// this call modifies appstate.opchild
log.Info("adding genesis validator",
"moniker", config.L2Config.Moniker,
"validator_address_acc", validatorKeySpec.Address,
"validator_address_acc", validatorKeySpec.L2Address,
"validator_address_val", sdk.ValAddress(valPubKey.Address()).String(),
)
opChildState, err := addGenesisValidator(
Expand All @@ -165,15 +166,15 @@ func initializeGenesis(
log.Info("adding fee whitelists",
"whitelist-len", 3,
"whitelists", strings.Join([]string{
config.SystemKeys.Validator.Address,
config.SystemKeys.BridgeExecutor.Address,
config.SystemKeys.Challenger.Address,
config.SystemKeys.Validator.L2Address,
config.SystemKeys.BridgeExecutor.L2Address,
config.SystemKeys.Challenger.L2Address,
}, ","),
)
opChildState, err = addFeeWhitelists(cdc, genesisAppState, []string{
config.SystemKeys.Validator.Address,
config.SystemKeys.BridgeExecutor.Address,
config.SystemKeys.Challenger.Address,
config.SystemKeys.Validator.L2Address,
config.SystemKeys.BridgeExecutor.L2Address,
config.SystemKeys.Challenger.L2Address,
})
if err != nil {
return nil, errors.Wrap(err, "failed to add fee whitelists")
Expand All @@ -184,10 +185,10 @@ func initializeGenesis(
// Step 4 -------------------------------------------------------------------------------------------
// Set bridge executor address in the genesis parameter
log.Info("setting bridge executor address",
"bridge-executor", config.SystemKeys.BridgeExecutor.Address,
"bridge-executor", config.SystemKeys.BridgeExecutor.L2Address,
)

opChildState, err = setOpChildBridgeExecutorAddress(cdc, genesisAppState, config.SystemKeys.BridgeExecutor.Address)
opChildState, err = setOpChildBridgeExecutorAddress(cdc, genesisAppState, config.SystemKeys.BridgeExecutor.L2Address)
if err != nil {
return nil, errors.Wrap(err, "failed to set bridge executor address")
}
Expand All @@ -197,10 +198,10 @@ func initializeGenesis(
// Step 5 -------------------------------------------------------------------------------------------
// Set admin address in the genesis parameter
log.Info("setting admin address",
"admin", config.SystemKeys.Validator.Address,
"admin", config.SystemKeys.Validator.L2Address,
)

opChildState, err = setOpChildAdminAddress(cdc, genesisAppState, config.SystemKeys.Validator.Address)
opChildState, err = setOpChildAdminAddress(cdc, genesisAppState, config.SystemKeys.Validator.L2Address)
if err != nil {
return nil, errors.Wrap(err, "failed to set bridge executor address")
}
Expand Down Expand Up @@ -236,7 +237,7 @@ func initializeGenesis(
return appGenesis, nil
}

func addGenesisAccounts(cdc codec.Codec, genesisAppState map[string]json.RawMessage, genAccsManifest []launchtools.AccountWithBalance) (
func addGenesisAccounts(cdc codec.Codec, genesisAppState map[string]json.RawMessage, genAccsManifest []launchtools.GenesisAccount) (
*authtypes.GenesisState,
*banktypes.GenesisState,
error,
Expand All @@ -253,7 +254,7 @@ func addGenesisAccounts(cdc codec.Codec, genesisAppState map[string]json.RawMess

for _, acc := range genAccsManifest {
// acc
addr, addrErr := sdk.AccAddressFromBech32(acc.Address)
addr, addrErr := utils.L2AddressCodec().StringToBytes(acc.Address)
if addrErr != nil {
return nil, nil, errors.Wrap(addrErr, fmt.Sprintf("failed to parse genesis account address %s", acc.Address))
}
Expand Down
Loading
Loading