-
Notifications
You must be signed in to change notification settings - Fork 13
EVM key store on top of common key store lib #270
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
connorwstein
wants to merge
5
commits into
develop
Choose a base branch
from
ARCH-334-evm-support
base: develop
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.
Draft
Changes from 2 commits
Commits
Show all changes
5 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
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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package keystore | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
gethtypes "github.com/ethereum/go-ethereum/core/types" | ||
gethcrypto "github.com/ethereum/go-ethereum/crypto" | ||
"github.com/smartcontractkit/chainlink-common/keystore" | ||
Check failure on line 13 in pkg/keystore/evm_tx.go
|
||
) | ||
|
||
const ( | ||
EVMPrefix = "evm" | ||
TxKeystorePrefix = "tx" | ||
) | ||
|
||
// JoinKeySegments joins path-like key name segments using "/" and avoids double slashes. | ||
// Empty segments are skipped so JoinKeySegments("EVM", "TX", "my-key") => "EVM/TX/my-key". | ||
func JoinKeySegments(segments ...string) string { | ||
cleaned := make([]string, 0, len(segments)) | ||
for _, s := range segments { | ||
s = strings.Trim(s, "/") | ||
if s == "" { | ||
continue | ||
} | ||
cleaned = append(cleaned, s) | ||
} | ||
return strings.Join(cleaned, "/") | ||
} | ||
|
||
func GetTxKeystoreName(localName string) string { | ||
return JoinKeySegments(EVMPrefix, TxKeystorePrefix, localName) | ||
} | ||
|
||
type TxKey struct { | ||
ks keystore.Keystore | ||
// Fully qualified name in keystore. Use for administration. | ||
fullName string | ||
name string | ||
addr common.Address | ||
} | ||
|
||
type SignTxRequest struct { | ||
ChainID *big.Int | ||
Tx *gethtypes.Transaction | ||
} | ||
|
||
type SignTxResponse struct { | ||
Tx *gethtypes.Transaction | ||
} | ||
|
||
func (k *TxKey) Name() string { | ||
return k.name | ||
} | ||
|
||
func (k *TxKey) FullName() string { | ||
return k.fullName | ||
} | ||
|
||
func (k *TxKey) Address() common.Address { | ||
return k.addr | ||
} | ||
|
||
func (k *TxKey) SignTx(ctx context.Context, req SignTxRequest) (SignTxResponse, error) { | ||
signer := gethtypes.LatestSignerForChainID(req.ChainID) | ||
h := signer.Hash(req.Tx) | ||
signReq := keystore.SignRequest{ | ||
KeyName: k.FullName(), | ||
Data: h[:], | ||
} | ||
signResp, err := k.ks.Sign(ctx, signReq) | ||
if err != nil { | ||
return SignTxResponse{}, err | ||
} | ||
req.Tx, err = req.Tx.WithSignature(signer, signResp.Signature) | ||
if err != nil { | ||
return SignTxResponse{}, err | ||
} | ||
return SignTxResponse{Tx: req.Tx}, nil | ||
} | ||
|
||
func CreateTxKey(ks keystore.Keystore, localName string) (*TxKey, error) { | ||
createReq := keystore.CreateKeysRequest{ | ||
Keys: []keystore.CreateKeyRequest{ | ||
{ | ||
KeyName: GetTxKeystoreName(localName), | ||
KeyType: keystore.ECDSA_S256, | ||
}, | ||
}, | ||
} | ||
resp, err := ks.CreateKeys(context.Background(), createReq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(resp.Keys) == 0 { | ||
return nil, fmt.Errorf("no keys created") | ||
} | ||
publicKey, err := gethcrypto.UnmarshalPubkey(resp.Keys[0].KeyInfo.PublicKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
addr := gethcrypto.PubkeyToAddress(*publicKey) | ||
return &TxKey{ | ||
ks: ks, | ||
name: localName, | ||
fullName: GetTxKeystoreName(localName), | ||
addr: addr, | ||
}, nil | ||
} | ||
|
||
func GetTxKeys(ctx context.Context, ks keystore.Keystore, names []string) ([]*TxKey, error) { | ||
var fullNames []string | ||
for _, name := range names { | ||
fullNames = append(fullNames, GetTxKeystoreName(name)) | ||
} | ||
resp, err := ks.GetKeys(ctx, keystore.GetKeysRequest{KeyNames: fullNames}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var keys []*TxKey | ||
for _, key := range resp.Keys { | ||
publicKey, err := gethcrypto.UnmarshalPubkey(key.KeyInfo.PublicKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
addr := gethcrypto.PubkeyToAddress(*publicKey) | ||
keys = append(keys, &TxKey{ | ||
ks: ks, | ||
fullName: key.KeyInfo.Name, | ||
name: key.KeyInfo.Name, | ||
addr: addr, | ||
}) | ||
} | ||
return keys, 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,75 @@ | ||
package keystore_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/ethclient/simulated" | ||
commonks "github.com/smartcontractkit/chainlink-common/keystore" | ||
ksstorage "github.com/smartcontractkit/chainlink-common/keystore/storage" | ||
evmks "github.com/smartcontractkit/chainlink-evm/pkg/keystore" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTxKey(t *testing.T) { | ||
storage := ksstorage.NewMemoryStorage() | ||
ctx := t.Context() | ||
ks, err := commonks.LoadKeystore(ctx, storage, commonks.EncryptionParams{ | ||
Password: "test-password", | ||
ScryptParams: commonks.FastScryptParams, | ||
}) | ||
require.NoError(t, err) | ||
testKey, err := evmks.CreateTxKey(ks, "test-tx-key") | ||
require.NoError(t, err) | ||
testKey2, err := evmks.CreateTxKey(ks, "test-tx-key-2") | ||
require.NoError(t, err) | ||
|
||
backend := simulated.NewBackend(types.GenesisAlloc{ | ||
testKey.Address(): { | ||
Balance: big.NewInt(0).Mul(big.NewInt(10), big.NewInt(1e18)), // 10 ETH | ||
}, | ||
}, simulated.WithBlockGasLimit(10e6)) | ||
defer backend.Close() | ||
testTransaction := types.NewTransaction( | ||
0, // Nonce | ||
testKey2.Address(), // To other key | ||
big.NewInt(1), // Value | ||
21000, // Gas Limit | ||
big.NewInt(20000000000), // Gas Price | ||
nil) | ||
resp, err := testKey.SignTx(ctx, evmks.SignTxRequest{ | ||
ChainID: big.NewInt(1337), // Use a test chain ID | ||
Tx: testTransaction, | ||
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp.Tx) | ||
require.NoError(t, backend.Client().SendTransaction(ctx, resp.Tx)) | ||
backend.Commit() | ||
receipt, err := backend.Client().TransactionReceipt(ctx, resp.Tx.Hash()) | ||
require.NoError(t, err) | ||
require.Equal(t, types.ReceiptStatusSuccessful, receipt.Status) | ||
|
||
endBalance, err := backend.Client().BalanceAt(ctx, testKey2.Address(), nil) | ||
require.NoError(t, err) | ||
require.Equal(t, endBalance, big.NewInt(1)) | ||
|
||
// Admin operation will invalidate the keys. | ||
_, err = ks.DeleteKeys(ctx, commonks.DeleteKeysRequest{ | ||
KeyNames: []string{testKey.FullName(), testKey2.FullName()}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Empty names will return all keys. | ||
keys, err := evmks.GetTxKeys(ctx, ks, []string{}) | ||
require.NoError(t, err) | ||
require.Equal(t, len(keys), 0) | ||
|
||
// Signing will now error. | ||
_, err = testKey.SignTx(ctx, evmks.SignTxRequest{ | ||
ChainID: big.NewInt(1337), // Use a test chain ID | ||
Tx: testTransaction, | ||
}) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, commonks.ErrKeyNotFound) | ||
} |
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.