Skip to content

Commit

Permalink
Refactor wallet cli features
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoe committed Feb 12, 2024
1 parent f119eea commit 5d4c631
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 78 deletions.
14 changes: 5 additions & 9 deletions tools/wasp-cli/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,9 @@ var keyChain keychain.KeyChain

func GetKeyChain() keychain.KeyChain {
if keyChain == nil {
fmt.Printf("\nKeyChain available: %v\n", keychain.IsKeyChainAvailable())

if keychain.IsKeyChainAvailable() {
fmt.Println("Using OS keychain")
keyChain = keychain.NewKeyChainZalando()
} else {
fmt.Println("Using encrypted file")
keyChain = keychain.NewKeyChainFile(BaseDir, cli.ReadPasswordFromStdin)
}
}
Expand Down Expand Up @@ -209,12 +205,12 @@ func GetUseLegacyDerivation() bool {
return viper.GetBool("wallet.useLegacyDerivation")
}

func GetWalletSchemeString() string {
return viper.GetString("wallet.scheme")
func GetWalletProviderString() string {
return viper.GetString("wallet.provider")
}

func SetWalletSchemeString(scheme string) {
Set("wallet.scheme", scheme)
func SetWalletProviderString(provider string) {
Set("wallet.provider", provider)
}

// GetSeedForMigration is used to migrate the seed of the config file to a certain wallet provider.
Expand All @@ -225,7 +221,7 @@ func GetSeedForMigration() string {
func GetWalletLogLevel() types.ILoggerConfigLevelFilter {
logLevel := viper.GetString("wallet.loglevel")
if logLevel == "" {
return types.LevelFilterTrace
return types.LevelFilterOff
}

return types.ILoggerConfigLevelFilter(logLevel)
Expand Down
2 changes: 1 addition & 1 deletion tools/wasp-cli/cli/wallet/providers/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func CreateKeyChain() {
err := config.GetKeyChain().SetSeed(seed)
log.Check(err)

log.Printf("Seed stored in the keychain.\n")
log.Printf("New seed stored in the keychain.\n")
}

func MigrateKeyChain(seed cryptolib.Seed) {
Expand Down
103 changes: 63 additions & 40 deletions tools/wasp-cli/cli/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ import (

var AddressIndex uint32

type WalletScheme string
type WalletProvider string

const (
SchemeKeyChain WalletScheme = "keychain"
SchemeLedger WalletScheme = "sdk_ledger"
SchemeStronghold WalletScheme = "sdk_stronghold"
ProviderKeyChain WalletProvider = "keychain"
ProviderLedger WalletProvider = "sdk_ledger"
ProviderStronghold WalletProvider = "sdk_stronghold"
)

func GetWalletScheme() WalletScheme {
scheme := WalletScheme(config.GetWalletSchemeString())
func GetWalletProvider() WalletProvider {
provider := WalletProvider(config.GetWalletProviderString())

switch scheme {
case SchemeLedger, SchemeKeyChain, SchemeStronghold:
return scheme
switch provider {
case ProviderLedger, ProviderKeyChain, ProviderStronghold:
return provider
}
return SchemeKeyChain
return ProviderKeyChain
}

func SetWalletScheme(scheme WalletScheme) error {
switch scheme {
case SchemeLedger, SchemeKeyChain, SchemeStronghold:
config.SetWalletSchemeString(string(scheme))
func SetWalletProvider(provider WalletProvider) error {
switch provider {
case ProviderLedger, ProviderKeyChain, ProviderStronghold:
config.SetWalletProviderString(string(provider))
return nil
}
return errors.New("invalid wallet scheme provided")
return errors.New("invalid wallet provider provided")
}

func getIotaSDKLibName() string {
Expand All @@ -60,14 +60,7 @@ func getIotaSDKLibName() string {
}
}

func getIotaSDK() *wasp_wallet_sdk.IOTASDK {
// LoadLibrary (windows) and dlLoad (linux) have different search path behaviors
// For now, use a relative path - as it will eventually be shipped with a release.
ex, err := os.Executable()
wd := filepath.Dir(ex)
log.Check(err)

libPath := path.Join(wd, getIotaSDKLibName())
func initIotaSDK(libPath string) *wasp_wallet_sdk.IOTASDK {
sdk, err := wasp_wallet_sdk.NewIotaSDK(libPath)
log.Check(err)

Expand All @@ -79,18 +72,48 @@ func getIotaSDK() *wasp_wallet_sdk.IOTASDK {
return sdk
}

func getIotaSDK() *wasp_wallet_sdk.IOTASDK {
// LoadLibrary (windows) and dlLoad (linux) have different search path behaviors
// For now, use a relative path - as it will eventually be shipped with a release.
// TODO: Revisit once proper release structure is set up.

ex, err := os.Executable()
log.Check(err)
executableDir := filepath.Dir(ex)

wd, err := os.Getwd()
log.Check(err)

searchPaths := []string{
path.Join(executableDir, getIotaSDKLibName()),
path.Join(wd, getIotaSDKLibName()),
}

for _, searchPath := range searchPaths {
if _, err := os.Stat(searchPath); err == nil {

return initIotaSDK(searchPath)
// file exists
}
}

log.Fatalf("Could not find %v", getIotaSDKLibName())

return nil
}

var loadedWallet wallets.Wallet

func Load() wallets.Wallet {
walletScheme := GetWalletScheme()
walletProvider := GetWalletProvider()

if loadedWallet == nil {
switch walletScheme {
case SchemeKeyChain:
switch walletProvider {
case ProviderKeyChain:
loadedWallet = providers.LoadKeyChain(AddressIndex)
case SchemeLedger:
case ProviderLedger:
loadedWallet = providers.LoadLedgerWallet(getIotaSDK(), AddressIndex)
case SchemeStronghold:
case ProviderStronghold:
loadedWallet = providers.LoadStrongholdWallet(getIotaSDK(), AddressIndex)
}
}
Expand All @@ -99,19 +122,19 @@ func Load() wallets.Wallet {
}

func InitWallet() {
walletScheme := GetWalletScheme()
walletProvider := GetWalletProvider()

switch walletScheme {
case SchemeKeyChain:
switch walletProvider {
case ProviderKeyChain:
providers.CreateKeyChain()
case SchemeLedger:
log.Printf("Ledger wallet scheme selected, no initialization required")
case SchemeStronghold:
case ProviderLedger:
log.Printf("Ledger wallet provider selected, no initialization required")
case ProviderStronghold:
providers.CreateNewStrongholdWallet(getIotaSDK())
}
}

func Migrate(scheme WalletScheme) {
func Migrate(provider WalletProvider) {
seedHex := config.GetSeedForMigration()
if seedHex == "" {
fmt.Println("No seed to migrate found.")
Expand All @@ -122,12 +145,12 @@ func Migrate(scheme WalletScheme) {
log.Check(err)
seed := cryptolib.SeedFromBytes(seedBytes)

switch scheme {
case SchemeKeyChain:
switch provider {
case ProviderKeyChain:
providers.MigrateKeyChain(seed)
case SchemeLedger:
log.Printf("Ledger wallet scheme selected, no migration available")
case SchemeStronghold:
case ProviderLedger:
log.Printf("Ledger wallet provider selected, no migration available")
case ProviderStronghold:
providers.MigrateToStrongholdWallet(getIotaSDK(), seed)
}
}
3 changes: 2 additions & 1 deletion tools/wasp-cli/wallet/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func Init(rootCmd *cobra.Command) {
rootCmd.AddCommand(initBalanceCmd())
rootCmd.AddCommand(initSendFundsCmd())
rootCmd.AddCommand(initRequestFundsCmd())
rootCmd.AddCommand(initSchemeCmd())
rootCmd.AddCommand(initWalletProviderCmd())
rootCmd.AddCommand(initMigrateCmd())

rootCmd.PersistentFlags().Uint32VarP(&wallet.AddressIndex, "address-index", "i", 0, "address index")

}
6 changes: 3 additions & 3 deletions tools/wasp-cli/wallet/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

func initMigrateCmd() *cobra.Command {
return &cobra.Command{
Use: "wallet-migrate (keychain, sdk_stronghold)",
Short: "Migrates a seed inside `wasp-cli.json` to a certain wallet provider (keychain, sdk_stronghold)",
Use: "wallet-migrate (keychain)",
Short: "Migrates a seed inside `wasp-cli.json` to the keychain provider",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
wallet.Migrate(wallet.WalletScheme(args[0]))
wallet.Migrate(wallet.WalletProvider(args[0]))
},
}
}
24 changes: 0 additions & 24 deletions tools/wasp-cli/wallet/scheme.go

This file was deleted.

24 changes: 24 additions & 0 deletions tools/wasp-cli/wallet/wallet_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wallet

import (
"github.com/spf13/cobra"

"github.com/iotaledger/wasp/tools/wasp-cli/cli/wallet"
"github.com/iotaledger/wasp/tools/wasp-cli/log"
)

func initWalletProviderCmd() *cobra.Command {
return &cobra.Command{
Use: "wallet-provider (keychain, sdk_ledger, sdk_stronghold)",
Short: "Get or set wallet provider (keychain, sdk_ledger, sdk_stronghold)",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Printf("Wallet provider: %s\n", string(wallet.GetWalletProvider()))
return
}

log.Check(wallet.SetWalletProvider(wallet.WalletProvider(args[0])))
},
}
}

0 comments on commit 5d4c631

Please sign in to comment.