-
Notifications
You must be signed in to change notification settings - Fork 28
Keystore signer impl #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
connorwstein
merged 6 commits into
ARCH-331-keystore-encryptor-2
from
ARCH-329-keystore-signer
Oct 10, 2025
Merged
Keystore signer impl #1598
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c71b449
Wip
connorwstein dc428a0
Basic signer test
connorwstein 07c9b1a
Errors new
connorwstein f61c918
Merge branch 'ARCH-331-keystore-encryptor-2' into ARCH-329-keystore-s…
connorwstein 0569eb1
Fix merge
connorwstein ca9606f
Remove unnecessary lock
connorwstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,17 @@ package keystore | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/ed25519" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| gethcrypto "github.com/ethereum/go-ethereum/crypto" | ||
| "github.com/smartcontractkit/chainlink-common/keystore/internal" | ||
| ) | ||
|
|
||
| var ( | ||
| ErrInvalidSignRequest = errors.New("invalid sign request") | ||
| ErrInvalidVerifyRequest = errors.New("invalid verify request") | ||
| ) | ||
|
|
||
| type SignRequest struct { | ||
|
|
@@ -15,7 +25,8 @@ type SignResponse struct { | |
| } | ||
|
|
||
| type VerifyRequest struct { | ||
| KeyName string | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General pattern I'm thinking about here:
|
||
| KeyType KeyType | ||
| PublicKey []byte | ||
| Data []byte | ||
| Signature []byte | ||
| } | ||
|
|
@@ -40,11 +51,74 @@ func (UnimplementedSigner) Verify(ctx context.Context, req VerifyRequest) (Verif | |
| return VerifyResponse{}, fmt.Errorf("Signer.Verify: %w", ErrUnimplemented) | ||
| } | ||
|
|
||
| // TODO: Signer implementation. | ||
| func (k *keystore) Sign(ctx context.Context, req SignRequest) (SignResponse, error) { | ||
| return SignResponse{}, nil | ||
| k.mu.RLock() | ||
| defer k.mu.RUnlock() | ||
|
|
||
| key, ok := k.keystore[req.KeyName] | ||
| if !ok { | ||
| return SignResponse{}, fmt.Errorf("%s: %w", req.KeyName, ErrKeyNotFound) | ||
| } | ||
| switch key.keyType { | ||
| case Ed25519: | ||
| privateKey := ed25519.PrivateKey(internal.Bytes(key.privateKey)) | ||
| signature := ed25519.Sign(privateKey, req.Data) | ||
| return SignResponse{ | ||
| Signature: signature, | ||
| }, nil | ||
| case ECDSA_S256: | ||
| if len(req.Data) != 32 { | ||
| return SignResponse{}, fmt.Errorf("data must be 32 bytes for ECDSA_S256, got %d: %w", len(req.Data), ErrInvalidSignRequest) | ||
| } | ||
| privateKey, err := gethcrypto.ToECDSA(internal.Bytes(key.privateKey)) | ||
| if err != nil { | ||
| return SignResponse{}, fmt.Errorf("failed to convert private key to ECDSA private key: %w", err) | ||
| } | ||
| signature, err := gethcrypto.Sign(req.Data, privateKey) | ||
| if err != nil { | ||
| return SignResponse{}, err | ||
| } | ||
| return SignResponse{ | ||
| Signature: signature, | ||
| }, nil | ||
| default: | ||
| return SignResponse{}, fmt.Errorf("unsupported key type: %s", key.keyType) | ||
| } | ||
| } | ||
|
|
||
| func (k *keystore) Verify(ctx context.Context, req VerifyRequest) (VerifyResponse, error) { | ||
| return VerifyResponse{}, nil | ||
| // Note don't need the lock since this is a pure function. | ||
| switch req.KeyType { | ||
| case Ed25519: | ||
| if len(req.Signature) != 64 { | ||
| return VerifyResponse{}, fmt.Errorf("signature must be 64 bytes for Ed25519, got %d: %w", len(req.Signature), ErrInvalidVerifyRequest) | ||
| } | ||
| if len(req.PublicKey) != 32 { | ||
| return VerifyResponse{}, fmt.Errorf("public key must be 32 bytes for Ed25519, got %d: %w", len(req.PublicKey), ErrInvalidVerifyRequest) | ||
| } | ||
| publicKey := ed25519.PublicKey(req.PublicKey) | ||
| signature := ed25519.Verify(publicKey, req.Data, req.Signature) | ||
| return VerifyResponse{ | ||
| Valid: signature, | ||
| }, nil | ||
| case ECDSA_S256: | ||
| if len(req.Data) != 32 { | ||
| return VerifyResponse{}, fmt.Errorf("data must be 32 bytes for ECDSA_S256, got %d: %w", len(req.Data), ErrInvalidVerifyRequest) | ||
| } | ||
| // ECDSA_S256 public keys are in SEC1 (uncompressed) format | ||
| if len(req.PublicKey) != 65 { | ||
| return VerifyResponse{}, fmt.Errorf("public key must be 65 bytes for ECDSA_S256, got %d: %w", len(req.PublicKey), ErrInvalidVerifyRequest) | ||
| } | ||
| if len(req.Signature) != 65 { | ||
| return VerifyResponse{}, fmt.Errorf("signature must be 65 bytes for ECDSA_S256, got %d: %w", len(req.Signature), ErrInvalidVerifyRequest) | ||
| } | ||
| // VerifySignature expects 64 bytes [R || S] without the V byte | ||
| // Strip the V byte (last byte) from the 65-byte signature | ||
| valid := gethcrypto.VerifySignature(req.PublicKey, req.Data, req.Signature[:64]) | ||
| return VerifyResponse{ | ||
| Valid: valid, | ||
| }, nil | ||
| default: | ||
| return VerifyResponse{}, fmt.Errorf("unsupported key type: %s", req.KeyType) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package keystore_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/keystore" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSigner(t *testing.T) { | ||
| ks := NewKeystoreTH(t) | ||
| ks.CreateTestKeys(t) | ||
| ctx := t.Context() | ||
|
|
||
| var tt = []struct { | ||
| name string | ||
| keyName string | ||
| data []byte | ||
| signature []byte | ||
| expectedError error | ||
| }{ | ||
| { | ||
| name: "ECDSA_S256 sign/verify", | ||
| keyName: ks.KeyName(keystore.ECDSA_S256, 0), | ||
| data: make([]byte, 32), // 32 byte digest | ||
| }, | ||
| { | ||
| name: "ECDSA_S256 sign/verify no such key", | ||
| keyName: "no-such-key", | ||
| data: make([]byte, 32), // 32 byte digest | ||
| expectedError: keystore.ErrKeyNotFound, | ||
| }, | ||
| { | ||
| name: "ECDSA_S256 sign/verify wrong data length", | ||
| keyName: ks.KeyName(keystore.ECDSA_S256, 0), | ||
| data: make([]byte, 31), | ||
| expectedError: keystore.ErrInvalidSignRequest, | ||
| }, | ||
| { | ||
| name: "Ed25519 sign/verify", | ||
| keyName: ks.KeyName(keystore.Ed25519, 0), | ||
| data: []byte("test_data"), | ||
| }, | ||
| { | ||
| name: "Ed25519 sign/verify no such key", | ||
| keyName: "no-such-key", | ||
| data: make([]byte, 2), | ||
| expectedError: keystore.ErrKeyNotFound, | ||
| }, | ||
| } | ||
| for _, tc := range tt { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| signature, err := ks.Keystore.Sign(ctx, keystore.SignRequest{KeyName: tc.keyName, Data: tc.data}) | ||
| if tc.expectedError != nil { | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, tc.expectedError) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| valid, err := ks.Keystore.Verify(ctx, keystore.VerifyRequest{ | ||
| KeyType: ks.KeysByName()[tc.keyName].KeyType, | ||
| PublicKey: ks.KeysByName()[tc.keyName].PublicKey, | ||
| Data: tc.data, | ||
| Signature: signature.Signature, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.True(t, valid.Valid) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.