-
Notifications
You must be signed in to change notification settings - Fork 25
Coreshim WIP #1786
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
Draft
cedric-cordenier
wants to merge
2
commits into
main
Choose a base branch
from
coreshim-add-csa
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+491
−0
Draft
Coreshim WIP #1786
Changes from all commits
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,139 @@ | ||
| // `coreshim` provides utilities to generate keys that are compatible with the core node | ||
| // and can be imported by it. | ||
| package coreshim | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| gethkeystore "github.com/ethereum/go-ethereum/accounts/keystore" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/keystore" | ||
| "github.com/smartcontractkit/chainlink-common/keystore/serialization" | ||
| ) | ||
|
|
||
| var ( | ||
| ErrInvalidExportFormat = errors.New("invalid export format") | ||
| ) | ||
|
|
||
| const ( | ||
| KeyTypeCSA = "csa" | ||
| keyNameDefault = "default" | ||
| exportFormat = "github.com/smartcontractkit/chainlink-common/keystore/coreshim" | ||
| ) | ||
|
|
||
| type Keystore struct { | ||
| keystore.Keystore | ||
| } | ||
|
|
||
| type Key struct { | ||
| KeyName string | ||
| Data json.RawMessage | ||
| } | ||
|
|
||
| type Envelope struct { | ||
| Type string | ||
| Keys []keystore.ExportKeyResponse | ||
| ExportFormat string | ||
| } | ||
|
|
||
| func NewKeystore(ks keystore.Keystore) *Keystore { | ||
| return &Keystore{ | ||
| Keystore: ks, | ||
| } | ||
| } | ||
|
|
||
| // decryptKey decrypts an encrypted key using the provided password and returns the deserialized key. | ||
| func decryptKey(encryptedData []byte, password string) (*serialization.Key, error) { | ||
| encData := gethkeystore.CryptoJSON{} | ||
| err := json.Unmarshal(encryptedData, &encData) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal key material into CryptoJSON: %w", err) | ||
| } | ||
|
|
||
| decData, err := gethkeystore.DecryptDataV3(encData, password) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not decrypt data: %w", err) | ||
| } | ||
|
|
||
| keypb := &serialization.Key{} | ||
| err = proto.Unmarshal(decData, keypb) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal key into serialization.Key: %w", err) | ||
| } | ||
|
|
||
| return keypb, nil | ||
| } | ||
|
|
||
| func (ks *Keystore) GenerateEncryptedCSAKey(ctx context.Context, password string) ([]byte, error) { | ||
| path := keystore.NewKeyPath(KeyTypeCSA, keyNameDefault) | ||
| _, err := ks.CreateKeys(ctx, keystore.CreateKeysRequest{ | ||
| Keys: []keystore.CreateKeyRequest{ | ||
| { | ||
| KeyName: path.String(), | ||
| KeyType: keystore.Ed25519, | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to generate exportable key: %w", err) | ||
| } | ||
|
|
||
| er, err := ks.ExportKeys(ctx, keystore.ExportKeysRequest{ | ||
| Keys: []keystore.ExportKeyParam{ | ||
| { | ||
| KeyName: path.String(), | ||
| Enc: keystore.EncryptionParams{ | ||
| Password: password, | ||
| ScryptParams: keystore.DefaultScryptParams, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to export key: %w", err) | ||
| } | ||
|
|
||
| envelope := Envelope{ | ||
| Type: KeyTypeCSA, | ||
| Keys: er.Keys, | ||
| ExportFormat: exportFormat, | ||
| } | ||
|
|
||
| data, err := json.Marshal(&envelope) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal envelope: %w", err) | ||
| } | ||
|
|
||
| return data, nil | ||
| } | ||
|
|
||
| func FromEncryptedCSAKey(data []byte, password string) ([]byte, error) { | ||
| envelope := Envelope{} | ||
| err := json.Unmarshal(data, &envelope) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal import data into envelope: %w", err) | ||
| } | ||
|
|
||
| if envelope.ExportFormat != exportFormat { | ||
| return nil, fmt.Errorf("invalid export format: %w", ErrInvalidExportFormat) | ||
| } | ||
|
|
||
| if envelope.Type != KeyTypeCSA { | ||
| return nil, fmt.Errorf("invalid key type: expected %s, got %s", KeyTypeCSA, envelope.Type) | ||
| } | ||
|
|
||
| if len(envelope.Keys) != 1 { | ||
| return nil, fmt.Errorf("expected exactly one key in envelope, got %d", len(envelope.Keys)) | ||
| } | ||
|
|
||
| keypb, err := decryptKey(envelope.Keys[0].Data, password) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return keypb.PrivateKey, nil | ||
| } | ||
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 coreshim | ||
|
|
||
| import ( | ||
| "crypto/ed25519" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/keystore" | ||
| ) | ||
|
|
||
| func TestCSAKeyRoundTrip(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := t.Context() | ||
| password := "test-password" | ||
|
|
||
| st := keystore.NewMemoryStorage() | ||
| ks, err := keystore.LoadKeystore(ctx, st, "test", | ||
| keystore.WithScryptParams(keystore.FastScryptParams), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| coreshimKs := NewKeystore(ks) | ||
|
|
||
| encryptedKey, err := coreshimKs.GenerateEncryptedCSAKey(ctx, password) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, encryptedKey) | ||
|
|
||
| csaKeyPath := keystore.NewKeyPath(KeyTypeCSA, keyNameDefault) | ||
| getKeysResp, err := ks.GetKeys(ctx, keystore.GetKeysRequest{ | ||
| KeyNames: []string{csaKeyPath.String()}, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Len(t, getKeysResp.Keys, 1) | ||
|
|
||
| storedPublicKey := getKeysResp.Keys[0].KeyInfo.PublicKey | ||
| require.NotEmpty(t, storedPublicKey) | ||
|
|
||
| privateKey, err := FromEncryptedCSAKey(encryptedKey, password) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, privateKey) | ||
|
|
||
| require.Equal(t, 64, len(privateKey)) | ||
|
|
||
| derivedPublicKey := ed25519.PrivateKey(privateKey).Public().(ed25519.PublicKey) | ||
| require.Equal(t, storedPublicKey, []byte(derivedPublicKey)) | ||
| } | ||
|
|
||
| func TestCSAKeyImportWithWrongPassword(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := t.Context() | ||
| password := "test-password" | ||
| wrongPassword := "wrong-password" | ||
|
|
||
| st := keystore.NewMemoryStorage() | ||
| ks, err := keystore.LoadKeystore(ctx, st, "test", | ||
| keystore.WithScryptParams(keystore.FastScryptParams), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| coreshimKs := NewKeystore(ks) | ||
|
|
||
| encryptedKey, err := coreshimKs.GenerateEncryptedCSAKey(ctx, password) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, encryptedKey) | ||
|
|
||
| _, err = FromEncryptedCSAKey(encryptedKey, wrongPassword) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "could not decrypt data") | ||
| } | ||
|
|
||
| func TestCSAKeyImportInvalidFormat(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := FromEncryptedCSAKey([]byte("invalid json"), "password") | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "could not unmarshal import data") | ||
| } | ||
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,142 @@ | ||
| package coreshim | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/keystore" | ||
| "github.com/smartcontractkit/chainlink-common/keystore/ocr2offchain" | ||
| ) | ||
|
|
||
| type ChainType string | ||
|
Contributor
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. Any chance that we can reuse one of the existing chain family types? |
||
|
|
||
| const ( | ||
| // Must match ChainType in core. | ||
| chainTypeEVM ChainType = "evm" | ||
| ) | ||
|
|
||
| const ( | ||
| KeyTypeOCR = "OCR" | ||
| PrefixOCR2Onchain = "ocr2_onchain" | ||
| ) | ||
|
|
||
| type OCRKeyBundle struct { | ||
| ChainType ChainType | ||
| OffchainSigningKey []byte | ||
| OffchainEncryptionKey []byte | ||
| OnchainSigningKey []byte | ||
| } | ||
|
|
||
| func (ks *Keystore) GenerateEncryptedOCRKeyBundle(ctx context.Context, chainType ChainType, password string) ([]byte, error) { | ||
| _, err := ocr2offchain.CreateOCR2OffchainKeyring(ctx, ks.Keystore, keyNameDefault) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var onchainKeyPath keystore.KeyPath | ||
| switch chainType { | ||
| case chainTypeEVM: | ||
| path := keystore.NewKeyPath(PrefixOCR2Onchain, keyNameDefault, string(chainType)) | ||
| _, err := ks.CreateKeys(ctx, keystore.CreateKeysRequest{ | ||
| Keys: []keystore.CreateKeyRequest{ | ||
| { | ||
| KeyName: path.String(), | ||
| KeyType: keystore.ECDSA_S256, | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to generate exportable key: %w", err) | ||
| } | ||
|
|
||
| onchainKeyPath = path | ||
| default: | ||
| return nil, fmt.Errorf("unsupported chain type: %s", chainType) | ||
| } | ||
|
|
||
| er, err := ks.ExportKeys(ctx, keystore.ExportKeysRequest{ | ||
| Keys: []keystore.ExportKeyParam{ | ||
| { | ||
| KeyName: keystore.NewKeyPath(ocr2offchain.PrefixOCR2Offchain, keyNameDefault, ocr2offchain.OCR2OffchainSigning).String(), | ||
| Enc: keystore.EncryptionParams{ | ||
| Password: password, | ||
| ScryptParams: keystore.DefaultScryptParams, | ||
| }, | ||
| }, | ||
| { | ||
| KeyName: keystore.NewKeyPath(ocr2offchain.PrefixOCR2Offchain, keyNameDefault, ocr2offchain.OCR2OffchainEncryption).String(), | ||
| Enc: keystore.EncryptionParams{ | ||
| Password: password, | ||
| ScryptParams: keystore.DefaultScryptParams, | ||
| }, | ||
| }, | ||
| { | ||
| KeyName: onchainKeyPath.String(), | ||
| Enc: keystore.EncryptionParams{ | ||
| Password: password, | ||
| ScryptParams: keystore.DefaultScryptParams, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to export OCR key bundle: %w", err) | ||
| } | ||
|
|
||
| envelope := Envelope{ | ||
| Type: KeyTypeOCR, | ||
| Keys: er.Keys, | ||
| ExportFormat: exportFormat, | ||
| } | ||
|
|
||
| data, err := json.Marshal(&envelope) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal OCR key bundle envelope: %w", err) | ||
| } | ||
|
|
||
| return data, nil | ||
| } | ||
|
|
||
| func FromEncryptedOCRKeyBundle(data []byte, password string) (*OCRKeyBundle, error) { | ||
| envelope := Envelope{} | ||
| err := json.Unmarshal(data, &envelope) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal import data into envelope: %w", err) | ||
| } | ||
|
|
||
| if envelope.ExportFormat != exportFormat { | ||
| return nil, fmt.Errorf("invalid export format: %w", ErrInvalidExportFormat) | ||
| } | ||
|
|
||
| if envelope.Type != KeyTypeOCR { | ||
| return nil, fmt.Errorf("invalid key type: expected %s, got %s", KeyTypeOCR, envelope.Type) | ||
| } | ||
|
|
||
| if len(envelope.Keys) != 3 { | ||
| return nil, fmt.Errorf("expected exactly three keys in envelope, got %d", len(envelope.Keys)) | ||
| } | ||
|
|
||
| bundle := &OCRKeyBundle{} | ||
|
|
||
| for _, key := range envelope.Keys { | ||
| keypb, err := decryptKey(key.Data, password) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if strings.Contains(key.KeyName, ocr2offchain.OCR2OffchainSigning) { | ||
| bundle.OffchainSigningKey = keypb.PrivateKey | ||
| } else if strings.Contains(key.KeyName, ocr2offchain.OCR2OffchainEncryption) { | ||
| bundle.OffchainEncryptionKey = keypb.PrivateKey | ||
| } else if strings.Contains(key.KeyName, PrefixOCR2Onchain) { | ||
| bundle.OnchainSigningKey = keypb.PrivateKey | ||
| // Extract chain type from the key path | ||
| keyPath := keystore.NewKeyPathFromString(key.KeyName) | ||
| bundle.ChainType = ChainType(strings.ToLower(keyPath.Base())) | ||
| } | ||
| } | ||
|
|
||
| return bundle, nil | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT about using a more descriptive name?
Could then also consider dropping
Key(s)prefixes/suffixes from various identifiers: