aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorStephenButtolph <[email protected]>2020-09-08 10:37:28 -0400
committerStephenButtolph <[email protected]>2020-09-08 10:37:28 -0400
commit48ff7c5c79209c332ce1570d69f331edac665fec (patch)
treecea0e89239b0fa3c67340c44d2dd148a39260703 /plugin
parent891eb16f79fbc8552ede10d786bf70927f78367a (diff)
cleaned up keystore apis
Diffstat (limited to 'plugin')
-rw-r--r--plugin/evm/service.go62
1 files changed, 34 insertions, 28 deletions
diff --git a/plugin/evm/service.go b/plugin/evm/service.go
index 29ef35d..ade03e7 100644
--- a/plugin/evm/service.go
+++ b/plugin/evm/service.go
@@ -146,19 +146,25 @@ type ExportKeyReply struct {
// ExportKey returns a private key from the provided user
func (service *AvaAPI) ExportKey(r *http.Request, args *ExportKeyArgs, reply *ExportKeyReply) error {
service.vm.ctx.Log.Info("Platform: ExportKey called")
+
+ address, err := service.vm.ParseEthAddress(args.Address)
+ if err != nil {
+ return fmt.Errorf("couldn't parse %s to address: %s", args.Address, err)
+ }
+
db, err := service.vm.ctx.Keystore.GetDatabase(args.Username, args.Password)
if err != nil {
return fmt.Errorf("problem retrieving user '%s': %w", args.Username, err)
}
+ defer db.Close()
+
user := user{db: db}
- if address, err := service.vm.ParseEthAddress(args.Address); err != nil {
- return fmt.Errorf("couldn't parse %s to address: %s", args.Address, err)
- } else if sk, err := user.getKey(address); err != nil {
+ sk, err := user.getKey(address)
+ if err != nil {
return fmt.Errorf("problem retrieving private key: %w", err)
- } else {
- reply.PrivateKey = constants.SecretKeyPrefix + formatting.CB58{Bytes: sk.Bytes()}.String()
- return nil
}
+ reply.PrivateKey = constants.SecretKeyPrefix + formatting.CB58{Bytes: sk.Bytes()}.String()
+ return nil
}
// ImportKeyArgs are arguments for ImportKey
@@ -170,43 +176,40 @@ type ImportKeyArgs struct {
// ImportKey adds a private key to the provided user
func (service *AvaAPI) ImportKey(r *http.Request, args *ImportKeyArgs, reply *api.JsonAddress) error {
service.vm.ctx.Log.Info("Platform: ImportKey called for user '%s'", args.Username)
- if service.vm.ctx.Keystore == nil {
- return fmt.Errorf("oh no")
- }
- fmt.Sprintf("good")
- db, err := service.vm.ctx.Keystore.GetDatabase(args.Username, args.Password)
- if err != nil {
- return fmt.Errorf("problem retrieving data: %w", err)
- }
-
- user := user{db: db}
-
- factory := crypto.FactorySECP256K1R{}
if !strings.HasPrefix(args.PrivateKey, constants.SecretKeyPrefix) {
return fmt.Errorf("private key missing %s prefix", constants.SecretKeyPrefix)
}
+
trimmedPrivateKey := strings.TrimPrefix(args.PrivateKey, constants.SecretKeyPrefix)
formattedPrivateKey := formatting.CB58{}
if err := formattedPrivateKey.FromString(trimmedPrivateKey); err != nil {
return fmt.Errorf("problem parsing private key: %w", err)
}
+ factory := crypto.FactorySECP256K1R{}
skIntf, err := factory.ToPrivateKey(formattedPrivateKey.Bytes)
if err != nil {
return fmt.Errorf("problem parsing private key: %w", err)
}
sk := skIntf.(*crypto.PrivateKeySECP256K1R)
- if err := user.putAddress(sk); err != nil {
- return fmt.Errorf("problem saving key %w", err)
- }
-
// TODO: return eth address here
reply.Address, err = service.vm.FormatEthAddress(GetEthAddress(sk))
if err != nil {
return fmt.Errorf("problem formatting address: %w", err)
}
+
+ db, err := service.vm.ctx.Keystore.GetDatabase(args.Username, args.Password)
+ if err != nil {
+ return fmt.Errorf("problem retrieving data: %w", err)
+ }
+ defer db.Close()
+
+ user := user{db: db}
+ if err := user.putAddress(sk); err != nil {
+ return fmt.Errorf("problem saving key %w", err)
+ }
return nil
}
@@ -231,18 +234,19 @@ func (service *AvaAPI) ImportAVAX(_ *http.Request, args *ImportAVAXArgs, respons
return fmt.Errorf("problem parsing chainID %q: %w", args.SourceChain, err)
}
+ to, err := service.vm.ParseEthAddress(args.To)
+ if err != nil { // Parse address
+ return fmt.Errorf("couldn't parse argument 'to' to an address: %w", err)
+ }
+
// Get the user's info
db, err := service.vm.ctx.Keystore.GetDatabase(args.Username, args.Password)
if err != nil {
return fmt.Errorf("couldn't get user '%s': %w", args.Username, err)
}
- user := user{db: db}
-
- to, err := service.vm.ParseEthAddress(args.To)
- if err != nil { // Parse address
- return fmt.Errorf("couldn't parse argument 'to' to an address: %w", err)
- }
+ defer db.Close()
+ user := user{db: db}
privKeys, err := user.getKeys()
if err != nil { // Get keys
return fmt.Errorf("couldn't get keys controlled by the user: %w", err)
@@ -288,6 +292,8 @@ func (service *AvaAPI) ExportAVAX(_ *http.Request, args *ExportAVAXArgs, respons
if err != nil {
return fmt.Errorf("problem retrieving user '%s': %w", args.Username, err)
}
+ defer db.Close()
+
user := user{db: db}
privKeys, err := user.getKeys()
if err != nil {