Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion cmd/common/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ func GetNPASelection(cfg *cliConfig.Config) *NPASelection {
s.AccountSetExplicitly = true
}
if s.AccountName != "" {
accCfg, err := LoadAccountConfig(cfg, s.AccountName)
accountName, accCfg, err := LoadAccountConfig(cfg, s.AccountName)
cobra.CheckErr(err)
s.AccountName = accountName
s.Account = accCfg
}

Expand Down
26 changes: 20 additions & 6 deletions cmd/common/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func LoadAccount(cfg *config.Config, name string) wallet.Account {
return acc
}

acfg, err := LoadAccountConfig(cfg, name)
name, acfg, err := LoadAccountConfig(cfg, name)
cobra.CheckErr(err)

af, err := acfg.LoadFactory()
Expand Down Expand Up @@ -156,18 +156,32 @@ func ParseTestAccountAddress(name string) string {
return ""
}

// LoadAccountConfig loads the config instance of the given named account.
func LoadAccountConfig(cfg *config.Config, name string) (*config.Account, error) {
// LoadAccountConfig loads the name and config instance of the given account name or address.
func LoadAccountConfig(cfg *config.Config, name string) (string, *config.Account, error) {
if testName := ParseTestAccountAddress(name); testName != "" {
return LoadTestAccountConfig(testName)
acfg, err := LoadTestAccountConfig(testName)
return name, acfg, err
}

// Early check for whether the account exists so that we don't ask for passphrase first.
if acfg, exists := cfg.Wallet.All[name]; exists {
return acfg, nil
return name, acfg, nil
}

// Name may be a native or Ethereum address instead; resolve it to the matching configured
// wallet account, if any.
if addr, ethAddr, err := helpers.ResolveEthOrOasisAddress(name); err == nil && addr != nil {
for accName, acfg := range cfg.Wallet.All {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that multiple accounts can resolve to the same address in which case this would be ambiguous. Currently this takes a random account while the other possibility would be to detect and reject such cases.

switch {
case acfg.GetAddress() == *addr:
return accName, acfg, nil
case ethAddr != nil && acfg.GetEthAddress() != nil && *acfg.GetEthAddress() == *ethAddr:
return accName, acfg, nil
}
}
}

return nil, fmt.Errorf("account '%s' does not exist in the wallet", name)
return "", nil, fmt.Errorf("account '%s' does not exist in the wallet", name)
}

// LoadTestAccount loads the given named test account.
Expand Down
4 changes: 2 additions & 2 deletions cmd/rofl/common/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ func MaybeLoadManifestAndSetNPA(cfg *cliConfig.Config, npa *common.NPASelection,
case npa.AccountSetExplicitly, common.TxUnsigned:
// Account explicitly overridden on the command line, leave unchanged.
default:
accCfg, err := common.LoadAccountConfig(cfg, d.Admin)
accountName, accCfg, err := common.LoadAccountConfig(cfg, d.Admin)
switch {
case err == nil:
npa.Account = accCfg
npa.AccountName = d.Admin
npa.AccountName = accountName
case opts != nil && opts.NeedAdmin:
return nil, "", nil, err
default:
Expand Down
4 changes: 2 additions & 2 deletions cmd/rofl/provider/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,12 @@ func maybeLoadManifestAndSetNPA(cfg *cliConfig.Config, npa *common.NPASelection)
switch manifest.Provider {
case "":
default:
accCfg, err := common.LoadAccountConfig(cfg, manifest.Provider)
accountName, accCfg, err := common.LoadAccountConfig(cfg, manifest.Provider)
if err != nil {
return nil, err
}
npa.Account = accCfg
npa.AccountName = manifest.Provider
npa.AccountName = accountName
}
return manifest, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var exportCmd = &cobra.Command{

common.Warn("WARNING: Exporting the account will expose secret key material!")
acc := common.LoadAccount(config.Global(), name)
accCfg, _ := common.LoadAccountConfig(config.Global(), name)
_, accCfg, _ := common.LoadAccountConfig(config.Global(), name)

showPublicWalletInfo(name, acc, accCfg)

Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var showCmd = &cobra.Command{
name := args[0]

acc := common.LoadAccount(config.Global(), name)
accCfg, _ := common.LoadAccountConfig(config.Global(), name)
_, accCfg, _ := common.LoadAccountConfig(config.Global(), name)
showPublicWalletInfo(name, acc, accCfg)
},
}
Expand Down
Loading