Skip to content

Commit

Permalink
fix(cmd/wallet): Trim 0x when importing secp256k1
Browse files Browse the repository at this point in the history
  • Loading branch information
amela committed May 8, 2024
1 parent f4e49b2 commit 293e366
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cmd/wallet/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ var importCmd = &cobra.Command{
}
questions := []*survey.Question{
{
Name: "data",
Prompt: af.DataPrompt(kind, afCfg),
Validate: af.DataValidator(kind, afCfg),
Name: "data",
Prompt: af.DataPrompt(kind, afCfg),
Validate: af.DataValidator(kind, afCfg),
Transform: af.DataTransformer(kind, afCfg),
},
}
err = survey.Ask(questions, &answers)
Expand Down
1 change: 1 addition & 0 deletions examples/wallet/import-0x-secp256k1-raw.in.static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis wallet import emma
9 changes: 9 additions & 0 deletions examples/wallet/import-0x-secp256k1-raw.out.static
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
oasis wallet import emma
? Kind: private key
? Algorithm: secp256k1-raw
? Private key (hex-encoded): [Enter 2 empty lines to finish]0x4811ebbe4f29f32a758f6f7bad39deb97ea67f07350637e31c75795dc679262a

? Private key (hex-encoded):
4811ebbe4f29f32a758f6f7bad39deb97ea67f07350637e31c75795dc679262a
? Choose a new passphrase:
? Repeat passphrase:
24 changes: 24 additions & 0 deletions wallet/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/AlecAivazis/survey/v2"
ethCommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -279,6 +280,7 @@ func (af *fileAccountFactory) DataValidator(kind wallet.ImportKind, rawCfg map[s
}
case wallet.AlgorithmSecp256k1Raw:
// Ensure the private key is hex encoded.
ans = af.DataTransformer(kind, rawCfg)(ans)
_, err := hex.DecodeString(ans.(string))
if err != nil {
return fmt.Errorf("private key must be hex-encoded (without leading 0x): %w", err)
Expand All @@ -299,6 +301,28 @@ func (af *fileAccountFactory) DataValidator(kind wallet.ImportKind, rawCfg map[s
}
}

func (af *fileAccountFactory) DataTransformer(kind wallet.ImportKind, rawCfg map[string]interface{}) survey.Transformer {
return func(ans interface{}) (newAns interface{}) {
switch kind {
case wallet.ImportKindPrivateKey:
var cfg wallet.AccountConfig
if err := cfg.UnmarshalMap(rawCfg); err != nil {
return nil
}
switch cfg.Algorithm {
case wallet.AlgorithmSecp256k1Raw:
ansLowcase := strings.ToLower(ans.(string))
newAns, _ = strings.CutPrefix(ansLowcase, "0x")
return newAns
default:
return ans
}
default:
return ans
}
}
}

func (af *fileAccountFactory) RequiresPassphrase() bool {
// A file-backed account always requires a passphrase.
return true
Expand Down
4 changes: 4 additions & 0 deletions wallet/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (af *ledgerAccountFactory) DataValidator(_ wallet.ImportKind, _ map[string]
return nil
}

func (af *ledgerAccountFactory) DataTransformer(_ wallet.ImportKind, _ map[string]interface{}) survey.Transformer {
return nil
}

func (af *ledgerAccountFactory) RequiresPassphrase() bool {
return false
}
Expand Down
4 changes: 4 additions & 0 deletions wallet/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (af *testAccountFactory) DataValidator(_ wallet.ImportKind, _ map[string]in
return nil
}

func (af *testAccountFactory) DataTransformer(_ wallet.ImportKind, _ map[string]interface{}) survey.Transformer {
return nil
}

func (af *testAccountFactory) RequiresPassphrase() bool {
return false
}
Expand Down
3 changes: 3 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Factory interface {
// DataValidator returns a survey data input validator used when importing the account.
DataValidator(kind ImportKind, cfg map[string]interface{}) survey.Validator

// DataValidator returns a survey data input transformer used when importing the account.
DataTransformer(kind ImportKind, cfg map[string]interface{}) survey.Transformer

// RequiresPassphrase returns true if the account requires a passphrase.
RequiresPassphrase() bool

Expand Down

0 comments on commit 293e366

Please sign in to comment.