Skip to content

Commit

Permalink
Allow selection of address on sign path (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianNEMO committed Jan 8, 2024
1 parent 9b1f6f8 commit 6335e2a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
24 changes: 23 additions & 1 deletion internal/usecase/path_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package usecase

import (
"context"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -32,6 +33,10 @@ func pathSign(b *Backend) *framework.Path {
Description: "Hex string of the hash that should be signed.",
Default: "",
},
"address": {
Type: framework.TypeString,
Description: "The address that belongs to a private key in the key-manager.",
},
},
}
}
Expand All @@ -51,6 +56,11 @@ func (b *Backend) sign(
return nil, errInvalidType
}

address, ok := data.Get("address").(string)
if !ok {
return nil, errInvalidType
}

keyManager, err := b.retrieveKeyManager(ctx, req, serviceNameInput)
if err != nil {
b.Logger().Error("Failed to retrieve the signing keyManager",
Expand All @@ -66,7 +76,19 @@ func (b *Backend) sign(
return nil, fmt.Errorf("signing keyManager %s does not have a key pair", serviceNameInput)
}

privateKey, err := crypto.HexToECDSA(keyManager.KeyPairs[0].PrivateKey)
var privateKeyStr string
for _, keyPairs := range keyManager.KeyPairs {
if keyPairs.Address == address {
privateKeyStr = keyPairs.PrivateKey
break
}
}

if privateKeyStr == "" {
return nil, errors.New("no private key for the input address")
}

privateKey, err := crypto.HexToECDSA(privateKeyStr)
if err != nil {
b.Logger().Error("Error reconstructing private key from retrieved hex", "error", err)
return nil, fmt.Errorf("error reconstructing private key from retrieved hex")
Expand Down
15 changes: 11 additions & 4 deletions internal/usecase/path_sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ func TestBackend_sign(t *testing.T) {
b, _ := newTestBackend(t)

const (
testSvc = "test-service"
testSvc = "test-service"
privateKeyString = "3ee65159f7aa057c482b1041f18f37ce90ef5e460cb46fd3fa0c40fbae41c7e1"
)
privateKey, err := crypto.HexToECDSA(privateKeyString)
if err != nil {
t.Fatalf("err: %v", err)
}
address := crypto.PubkeyToAddress(privateKey.PublicKey)

req := logical.TestRequest(t, logical.UpdateOperation, "key-managers")
storage := req.Storage
req.Data = map[string]interface{}{
"serviceName": testSvc,
"privateKey": "3ee65159f7aa057c482b1041f18f37ce90ef5e460cb46fd3fa0c40fbae41c7e1",
"privateKey": privateKeyString,
}
_, err := b.HandleRequest(context.Background(), req)
_, err = b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -34,7 +40,8 @@ func TestBackend_sign(t *testing.T) {
req = logical.TestRequest(t, logical.CreateOperation, "key-managers/"+testSvc+"/sign")
req.Storage = storage
data := map[string]interface{}{
"hash": hash.Hex(),
"hash": hash.Hex(),
"address": address.String(),
}
req.Data = data
resp, err := b.HandleRequest(context.Background(), req)
Expand Down

0 comments on commit 6335e2a

Please sign in to comment.