Skip to content

Commit

Permalink
cleanup to use da_address
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Oct 22, 2024
1 parent 6581f3f commit e6ea82d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# OPinit CosmosSDK Modules

The OPinit Stack is a versatile framework designed for CosmosSDK, facilitating the development of vm-agnostic Optimistic Rollups. This framework aligns closely with the interface of Optimism's Bedrock, promoting a straightforward user experience. Leveraging the Initia L1 Governance model, it addresses fraud-proof disputes efficiently.

## Optimistic Rollup Architecture

![image](https://github.com/initia-labs/OPinit/assets/160459432/77103140-73ef-41f2-95ba-682bee616f4e)


### L1 Components

#### [Bridge Module](./specs/l1_bridge.md)
Expand Down
2 changes: 1 addition & 1 deletion contrib/launchtools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ minitiad launch $TARGET_NETWORK --with-config [path-to-config]
"l1_address": "init1f4lu0ze9c7zegrrjfpymjvztucqz48z3cy8p5f"
},
"batch_submitter": {
"l1_address": "init1hqv5xqt7lckdj9p5kfp2q5auc5z37p2vyt4d72"
"da_address": "init1hqv5xqt7lckdj9p5kfp2q5auc5z37p2vyt4d72"
},
"challenger": {
"l1_address": "init1gn0yjtcma92y27c0z84ratxf6juy69lpln6u88",
Expand Down
73 changes: 16 additions & 57 deletions contrib/launchtools/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ import (

"github.com/pkg/errors"

"cosmossdk.io/core/address"
"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"

cmtcrypto "github.com/cometbft/cometbft/crypto"

"github.com/initia-labs/OPinit/contrib/launchtools/utils"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
)
Expand Down Expand Up @@ -289,6 +284,7 @@ func (l1config *L1Config) Finalize(buf *bufio.Reader) error {
type SystemAccount struct {
L1Address string `json:"l1_address,omitempty"`
L2Address string `json:"l2_address,omitempty"`
DAAddress string `json:"da_address,omitempty"`
Mnemonic string `json:"mnemonic,omitempty"`
}

Expand Down Expand Up @@ -367,51 +363,6 @@ func generateMnemonic() (string, error) {
return mnemonic, nil
}

func deriveAddress(mnemonic string, codec address.Codec) (string, error) {
addrBz, err := deriveAddressBz(mnemonic)
if err != nil {
return "", errors.Wrap(err, "failed to convert address to bech32")
}
return codec.BytesToString(addrBz)
}

func deriveL1L2Addresses(mnemonic string) (string, string, error) {
l1Addr, err := deriveAddress(mnemonic, utils.L1AddressCodec())
if err != nil {
return "", "", err
}
l2Addr, err := deriveAddress(mnemonic, utils.L2AddressCodec())
return l1Addr, l2Addr, err
}

func deriveAddressWithChainType(mnemonic string, chainType ophosttypes.BatchInfo_ChainType) (string, error) {
var codec address.Codec
switch chainType {
case ophosttypes.BatchInfo_CHAIN_TYPE_INITIA:
codec = utils.L1AddressCodec()
case ophosttypes.BatchInfo_CHAIN_TYPE_CELESTIA:
codec = utils.CelestiaAddressCodec()
default:
return "", errors.New("unsupported chain type")
}
return deriveAddress(mnemonic, codec)
}

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

privKey := algo.Generate()(derivedPriv)
return privKey.PubKey().Address(), nil
}

func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget ophosttypes.BatchInfo_ChainType) error {
if systemKeys.Validator == nil {
mnemonic, err := generateMnemonic()
Expand All @@ -420,7 +371,7 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget
}

// derive address
_, l2Addr, err := deriveL1L2Addresses(mnemonic)
l2Addr, err := utils.DeriveL2Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
Expand All @@ -437,13 +388,13 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget
}

// derive address
daAddr, err := deriveAddressWithChainType(mnemonic, batchSubmissionTarget)
daAddr, err := utils.DeriveDAAddress(mnemonic, batchSubmissionTarget)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}

systemKeys.BatchSubmitter = &SystemAccount{
L1Address: daAddr,
DAAddress: daAddr,
Mnemonic: mnemonic,
}
}
Expand All @@ -458,7 +409,11 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget
}

// derive address
l1Addr, l2Addr, err := deriveL1L2Addresses(mnemonic)
l1Addr, err := utils.DeriveL1Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
l2Addr, err := utils.DeriveL2Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
Expand All @@ -476,7 +431,11 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget
}

// derive address
l1Addr, l2Addr, err := deriveL1L2Addresses(mnemonic)
l1Addr, err := utils.DeriveL1Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
l2Addr, err := utils.DeriveL2Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
Expand All @@ -494,7 +453,7 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget
}

// derive address
l1Addr, _, err := deriveL1L2Addresses(mnemonic)
l1Addr, err := utils.DeriveL1Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
Expand All @@ -512,7 +471,7 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget
if systemKeys.BridgeExecutor.L1Address == "" || systemKeys.BridgeExecutor.L2Address == "" || systemKeys.BridgeExecutor.Mnemonic == "" {
return errors.New("bridge_executor account not initialized")
}
if systemKeys.BatchSubmitter.L1Address == "" {
if systemKeys.BatchSubmitter.DAAddress == "" {
return errors.New("batch_submitter account not initialized")
}
if systemKeys.OutputSubmitter.L1Address == "" {
Expand Down
2 changes: 1 addition & 1 deletion contrib/launchtools/steps/opbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func InitializeOpBridge(
config.SystemKeys.BridgeExecutor.L1Address,
config.SystemKeys.Challenger.L1Address,
config.SystemKeys.OutputSubmitter.L1Address,
config.SystemKeys.BatchSubmitter.L1Address,
config.SystemKeys.BatchSubmitter.DAAddress,
config.OpBridge.BatchSubmissionTarget,
*config.OpBridge.OutputSubmissionInterval,
*config.OpBridge.OutputFinalizationPeriod,
Expand Down
52 changes: 52 additions & 0 deletions contrib/launchtools/utils/address.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package utils

import (
"github.com/pkg/errors"

cmtcrypto "github.com/cometbft/cometbft/crypto"

"cosmossdk.io/core/address"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
)

func L1AddressCodec() address.Codec {
Expand All @@ -27,3 +35,47 @@ func HackBech32Prefix(prefix string) func() {
sdk.GetConfig().SetBech32PrefixForAccount(originPrefix, originPubPrefix)
}
}

func DeriveL1Address(mnemonic string) (string, error) {
return deriveAddress(mnemonic, L1AddressCodec())
}

func DeriveL2Address(mnemonic string) (string, error) {
return deriveAddress(mnemonic, L2AddressCodec())
}

func DeriveDAAddress(mnemonic string, chainType ophosttypes.BatchInfo_ChainType) (string, error) {
var codec address.Codec
switch chainType {
case ophosttypes.BatchInfo_CHAIN_TYPE_INITIA:
codec = L1AddressCodec()
case ophosttypes.BatchInfo_CHAIN_TYPE_CELESTIA:
codec = CelestiaAddressCodec()
default:
return "", errors.New("unsupported chain type")
}
return deriveAddress(mnemonic, codec)
}

func deriveAddress(mnemonic string, codec address.Codec) (string, error) {
addrBz, err := deriveAddressBz(mnemonic)
if err != nil {
return "", errors.Wrap(err, "failed to convert address to bech32")
}
return codec.BytesToString(addrBz)
}

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

privKey := algo.Generate()(derivedPriv)
return privKey.PubKey().Address(), nil
}

0 comments on commit e6ea82d

Please sign in to comment.