Skip to content

Commit

Permalink
eth/client/core: Derive account from app seed
Browse files Browse the repository at this point in the history
asset.Driver is now updated with a Create function, which is
used to initialize a seeded wallet. The wallet seed is derived
from the app seed using blake256(appSeed| asssetID) and is passed
to the create function. The private key for the ETH wallet is
derived from this seed using the following BIP32 derivation path:
m/44'/60'/0'/0/0.
  • Loading branch information
martonp authored Oct 19, 2021
1 parent 9e1e0cb commit 7c2058f
Show file tree
Hide file tree
Showing 20 changed files with 571 additions and 315 deletions.
8 changes: 6 additions & 2 deletions client/asset/bch/bch.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ func init() {
// Driver implements asset.Driver.
type Driver struct{}

// Setup creates the BCH exchange wallet. Start the wallet with its Run method.
func (d *Driver) Setup(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
// Open opens the BCH exchange wallet. Start the wallet with its Run method.
func (d *Driver) Open(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
return NewWallet(cfg, logger, network)
}

func (d *Driver) Create(params *asset.CreateWalletParams) error {
return fmt.Errorf("no creatable wallet types")
}

// DecodeCoinID creates a human-readable representation of a coin ID for
// Bitcoin Cash.
func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
Expand Down
8 changes: 6 additions & 2 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,15 @@ func (r *swapReceipt) SignedRefund() dex.Bytes {
// Driver implements asset.Driver.
type Driver struct{}

// Setup creates the BTC exchange wallet. Start the wallet with its Run method.
func (d *Driver) Setup(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
// Open opens the BTC exchange wallet. Start the wallet with its Run method.
func (d *Driver) Open(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
return NewWallet(cfg, logger, network)
}

func (d *Driver) Create(*asset.CreateWalletParams) error {
return fmt.Errorf("no creatable wallet types")
}

// DecodeCoinID creates a human-readable representation of a coin ID for
// Bitcoin.
func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
Expand Down
8 changes: 6 additions & 2 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,15 @@ type fundingCoin struct {
// Driver implements asset.Driver.
type Driver struct{}

// Setup creates the DCR exchange wallet. Start the wallet with its Run method.
func (d *Driver) Setup(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
// Open opens the DCR exchange wallet. Start the wallet with its Run method.
func (d *Driver) Open(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
return NewWallet(cfg, logger, network)
}

func (d *Driver) Create(*asset.CreateWalletParams) error {
return fmt.Errorf("no creatable wallet types")
}

// DecodeCoinID creates a human-readable representation of a coin ID for Decred.
func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
txid, vout, err := decodeCoinID(coinID)
Expand Down
60 changes: 42 additions & 18 deletions client/asset/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@ var (
drivers = make(map[uint32]Driver)
)

// CreateWalletParams are the parameters for internal wallet creation. The
// Settings provided should be the same wallet configuration settings passed to
// Open.
type CreateWalletParams struct {
Seed []byte
Pass []byte
Settings map[string]string
DataDir string
Net dex.Network
}

// Driver is the interface required of all exchange wallets.
type Driver interface {
Setup(*WalletConfig, dex.Logger, dex.Network) (Wallet, error)
Create(*CreateWalletParams) error
Open(*WalletConfig, dex.Logger, dex.Network) (Wallet, error)
DecodeCoinID(coinID []byte) (string, error)
Info() *WalletInfo
}

func withDriver(assetID uint32, f func(Driver) error) error {
driversMtx.Lock()
defer driversMtx.Unlock()
drv, ok := drivers[assetID]
if !ok {
return fmt.Errorf("asset: unknown driver asset %d", assetID)
}
return f(drv)
}

// Register should be called by the init function of an asset's package.
func Register(assetID uint32, driver Driver) {
driversMtx.Lock()
Expand All @@ -39,27 +61,29 @@ func Register(assetID uint32, driver Driver) {
drivers[assetID] = driver
}

// Setup sets up the asset, returning the exchange wallet.
func Setup(assetID uint32, cfg *WalletConfig, logger dex.Logger, network dex.Network) (Wallet, error) {
driversMtx.Lock()
drv, ok := drivers[assetID]
driversMtx.Unlock()
if !ok {
return nil, fmt.Errorf("asset: unknown asset driver %d", assetID)
}
return drv.Setup(cfg, logger, network)
// CreateWallet creates a new wallet. This method should only be used once to create a
// seeded wallet, after which OpenWallet should be used to load and access the wallet.
func CreateWallet(assetID uint32, seedParams *CreateWalletParams) error {
return withDriver(assetID, func(drv Driver) error {
return drv.Create(seedParams)
})
}

// OpenWallet sets up the asset, returning the exchange wallet.
func OpenWallet(assetID uint32, cfg *WalletConfig, logger dex.Logger, net dex.Network) (w Wallet, err error) {
return w, withDriver(assetID, func(drv Driver) error {
w, err = drv.Open(cfg, logger, net)
return err
})
}

// DecodeCoinID creates a human-readable representation of a coin ID for a named
// asset with a corresponding driver registered with this package.
func DecodeCoinID(assetID uint32, coinID []byte) (string, error) {
driversMtx.Lock()
drv, ok := drivers[assetID]
driversMtx.Unlock()
if !ok {
return "", fmt.Errorf("asset: unknown asset driver %d", assetID)
}
return drv.DecodeCoinID(coinID)
func DecodeCoinID(assetID uint32, coinID []byte) (cid string, err error) {
return cid, withDriver(assetID, func(drv Driver) error {
cid, err = drv.DecodeCoinID(coinID)
return err
})
}

// A registered asset is information about a supported asset.
Expand Down
Loading

0 comments on commit 7c2058f

Please sign in to comment.