-
Notifications
You must be signed in to change notification settings - Fork 15
Txm compatibility with keystore lib #318
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 2 commits into
develop
from
CCIP-9004-txm-keystore-lib-compatibility
Jan 8, 2026
+160
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package keys | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/keystore" | ||
| "github.com/smartcontractkit/chainlink-common/pkg/types/core" | ||
| ) | ||
|
|
||
| var _ core.Keystore = &TxKeyCoreKeystore{} | ||
|
|
||
| type TxKeyCoreKeystore struct { | ||
| ks keystore.Keystore | ||
| cache map[string]string | ||
| } | ||
|
|
||
| // NewTxKeyCoreKeystore creates a new CoreKeystore for transaction keys. | ||
| // This wrapper is required for using TxKeys with the txm | ||
| // which requires address based lookups. | ||
| func NewTxKeyCoreKeystore(ks keystore.Keystore) *TxKeyCoreKeystore { | ||
| return &TxKeyCoreKeystore{ | ||
| ks: ks, | ||
| cache: make(map[string]string), | ||
| } | ||
| } | ||
|
|
||
| func (s *TxKeyCoreKeystore) GetCache() map[string]string { | ||
| return s.cache | ||
| } | ||
|
|
||
| func (s *TxKeyCoreKeystore) Accounts(ctx context.Context) ([]string, error) { | ||
| keys, err := GetTxKeys(ctx, s.ks, []string{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| accounts := make([]string, 0, len(keys)) | ||
| for _, key := range keys { | ||
| accounts = append(accounts, key.Address().String()) | ||
| } | ||
| return accounts, nil | ||
| } | ||
|
|
||
| func (s *TxKeyCoreKeystore) Sign(ctx context.Context, account string, data []byte) ([]byte, error) { | ||
| if addr, ok := s.cache[account]; ok { | ||
| resp, err := s.ks.Sign(ctx, keystore.SignRequest{ | ||
| KeyName: addr, | ||
| Data: data, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return resp.Signature, nil | ||
| } | ||
| // Otherwise do the first time lookup to find the key by address. | ||
| keys, err := GetTxKeys(ctx, s.ks, []string{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(keys) == 0 { | ||
| return nil, errors.New("no keys found") | ||
| } | ||
| for _, key := range keys { | ||
| if key.Address().String() == account { | ||
| s.cache[account] = key.KeyPath().String() | ||
| resp, err := key.SignRaw(ctx, SignRawDataRequest{Data: data}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return resp.Signature, nil | ||
| } | ||
| } | ||
| return nil, errors.New("key not found") | ||
| } | ||
|
|
||
| func (s *TxKeyCoreKeystore) Decrypt(ctx context.Context, account string, data []byte) ([]byte, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
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,65 @@ | ||
| package keys | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/ethereum/go-ethereum/crypto" | ||
| "github.com/smartcontractkit/chainlink-common/keystore" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCoreKeystore(t *testing.T) { | ||
| ctx := context.Background() | ||
| tmpfile, err := os.CreateTemp("", "keystore.json") | ||
| require.NoError(t, err) | ||
| defer os.Remove(tmpfile.Name()) | ||
| ks, err := keystore.LoadKeystore(ctx, keystore.NewFileStorage(tmpfile.Name()), "password") | ||
| require.NoError(t, err) | ||
|
|
||
| coreKs := NewTxKeyCoreKeystore(ks) | ||
| accounts, err := coreKs.Accounts(ctx) | ||
| require.NoError(t, err) | ||
| require.Len(t, accounts, 0) | ||
|
|
||
| txKey, err := CreateTxKey(ks, "key1") | ||
| require.NoError(t, err) | ||
|
|
||
| keys, err := ks.GetKeys(ctx, keystore.GetKeysRequest{ | ||
| KeyNames: []string{txKey.KeyPath().String()}, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Len(t, keys.Keys, 1) | ||
|
|
||
| data := crypto.Keccak256([]byte("data")) | ||
| signature, err := coreKs.Sign(ctx, txKey.Address().String(), data) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, signature) | ||
|
|
||
| resp, err := ks.Verify(ctx, keystore.VerifyRequest{ | ||
| KeyType: keystore.ECDSA_S256, | ||
| PublicKey: keys.Keys[0].KeyInfo.PublicKey, | ||
| Data: data, | ||
| Signature: signature, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.True(t, resp.Valid) | ||
|
|
||
| // Make sure the cache populated. | ||
| cache := coreKs.GetCache() | ||
| require.Equal(t, txKey.KeyPath().String(), cache[txKey.Address().String()]) | ||
|
|
||
| // Sign again with cached. | ||
| signature, err = coreKs.Sign(ctx, txKey.Address().String(), data) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, signature) | ||
| resp, err = ks.Verify(ctx, keystore.VerifyRequest{ | ||
| KeyType: keystore.ECDSA_S256, | ||
| PublicKey: keys.Keys[0].KeyInfo.PublicKey, | ||
| Data: data, | ||
| Signature: signature, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.True(t, resp.Valid) | ||
| } |
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
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.