Skip to content

Commit

Permalink
core/services/keystore/keys/starkkey: move code from chainlink-starkn…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored May 31, 2023
1 parent 2e1e9a0 commit 105ba34
Show file tree
Hide file tree
Showing 27 changed files with 814 additions and 308 deletions.
16 changes: 13 additions & 3 deletions core/chains/starknet/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/txm"
"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/starknet"
"github.com/smartcontractkit/chainlink/v2/core/chains"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"

"github.com/smartcontractkit/chainlink/v2/core/chains/starknet/types"
"github.com/smartcontractkit/chainlink/v2/core/logger"
Expand All @@ -33,9 +34,9 @@ type chain struct {
txm txm.StarkTXM
}

func newChain(id string, cfg config.Config, ks keystore.StarkNet, cfgs types.Configs, lggr logger.Logger) (ch *chain, err error) {
func newChain(id string, cfg config.Config, ks keystore.StarkNet, cfgs types.Configs, lggr logger.Logger) (*chain, error) {
lggr = lggr.With("starknetChainID", id)
ch = &chain{
ch := &chain{
id: id,
cfg: cfg,
cfgs: cfgs,
Expand All @@ -46,7 +47,16 @@ func newChain(id string, cfg config.Config, ks keystore.StarkNet, cfgs types.Con
return ch.getClient()
}

ch.txm, err = txm.New(lggr, ks, cfg, getClient)
looppKs := starkkey.NewLooppKeystore(func(id string) (*big.Int, error) {
k, err := ks.Get(id)
if err != nil {
return nil, err
}
return k.ToPrivKey(), nil
})
ksAdapter := starkkey.NewKeystoreAdapter(looppKs)
var err error
ch.txm, err = txm.New(lggr, ksAdapter.Loopp(), cfg, getClient)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions core/cmd/starknet_keys_commands.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cmd

import (
starkkey "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"

"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
"github.com/smartcontractkit/chainlink/v2/core/utils"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
Expand Down
3 changes: 1 addition & 2 deletions core/cmd/starknet_keys_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (
"github.com/stretchr/testify/require"
"github.com/urfave/cli"

starkkey "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"

"github.com/smartcontractkit/chainlink/v2/core/cmd"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
"github.com/smartcontractkit/chainlink/v2/core/utils"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
Expand Down
3 changes: 1 addition & 2 deletions core/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (

pkgsolana "github.com/smartcontractkit/chainlink-solana/pkg/solana"

starkkey "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"

clienttypes "github.com/smartcontractkit/chainlink/v2/common/chains/client"
"github.com/smartcontractkit/chainlink/v2/core/assets"
"github.com/smartcontractkit/chainlink/v2/core/auth"
Expand Down Expand Up @@ -80,6 +78,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ocrkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/solkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/vrfkey"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
Expand Down
6 changes: 3 additions & 3 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shirou/gopsutil/v3 v3.22.12 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/smartcontractkit/caigo v0.0.0-20230508053235-41120ca1f9f3 // indirect
github.com/smartcontractkit/caigo v0.0.0-20230526231506-786d4587099a // indirect
github.com/smartcontractkit/chainlink-cosmos v0.4.0 // indirect
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20230526232037-00743d25b7ec // indirect
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20230530222821-30d787a10087 // indirect
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20230518143827-0b7a6e43719c // indirect
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.0-20230522061116-665c91c3d1fa // indirect
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.0-20230530233948-90c8af98011e // indirect
github.com/smartcontractkit/wsrpc v0.7.2 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1370,16 +1370,16 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartcontractkit/caigo v0.0.0-20230508053235-41120ca1f9f3 h1:6RsuA0LBbC/6DgTYzg659Pw7GKktubj1GzTcB/VNC44=
github.com/smartcontractkit/caigo v0.0.0-20230508053235-41120ca1f9f3/go.mod h1:2QuJdEouTWjh5BDy5o/vgGXQtR4Gz8yH1IYB5eT7u4M=
github.com/smartcontractkit/caigo v0.0.0-20230526231506-786d4587099a h1:B0WjTVtPW+GNY90rpmiwD/8tf3xr4jFC60sjuznNtRs=
github.com/smartcontractkit/caigo v0.0.0-20230526231506-786d4587099a/go.mod h1:2QuJdEouTWjh5BDy5o/vgGXQtR4Gz8yH1IYB5eT7u4M=
github.com/smartcontractkit/chainlink-cosmos v0.4.0 h1:xYLAcJJIm0cyMtYtMaosO45bykEwcwQOmZz3mz46Y2A=
github.com/smartcontractkit/chainlink-cosmos v0.4.0/go.mod h1:938jBqOrhdCq4A8enUiBliiDLBndAXebHIitKsDVqY0=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20230526232037-00743d25b7ec h1:ZG/N6BvjAKh6SkAHSYZ7el9WU6prCf0WsvcFjWJd0uU=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20230526232037-00743d25b7ec/go.mod h1:f7/HKcJWWFzINrkDDlpKsFaU6D+8D4B4OrQxkkCX4oc=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20230530222821-30d787a10087 h1:Xq3iwi8Vn+5uZb6RtKOn43GbSWLW7ij6S8Nn8JZ8vsg=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20230530222821-30d787a10087/go.mod h1:f7/HKcJWWFzINrkDDlpKsFaU6D+8D4B4OrQxkkCX4oc=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20230518143827-0b7a6e43719c h1:pM7FH+S92F7Xa/VSfbIs941FDKqSmo9pYeUBWHl1Pq0=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20230518143827-0b7a6e43719c/go.mod h1:CUmP50gxZsjwEYA7balCV3mhvX0CrR/a01X6jGBZR8I=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.0-20230522061116-665c91c3d1fa h1:WWCXWDUAiKiEuDpOTvSukEDzOpOF73x/9nFrnFyf8rA=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.0-20230522061116-665c91c3d1fa/go.mod h1:bwWxlWboUoG99segq2uS80r+q4VPBPE0I/CztPBCMuY=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.0-20230530233948-90c8af98011e h1:0g2/8OSVFxbGQ2L7FTDo29t90ItOf+1CI2zOn3DBwHM=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.0-20230530233948-90c8af98011e/go.mod h1:m43fLfOPrJK+cLDyhoCn9wY7hPWZzW1Ywu1rqZQ2Xhk=
github.com/smartcontractkit/libocr v0.0.0-20230503222226-29f534b2de1a h1:NFTlIAjSwx9vBYZeUerd0DDsvg+zZ5TSESw4dPNdwJk=
github.com/smartcontractkit/libocr v0.0.0-20230503222226-29f534b2de1a/go.mod h1:5JnCHuYgmIP9ZyXzgAfI5Iwu0WxBtBKp+ApeT5o1Cjw=
github.com/smartcontractkit/ocr2keepers v0.6.15 h1:dFhg+qT+tc6b3G8N4qnAHuHe8N0m8CSA8g4YGSeTGPo=
Expand Down
4 changes: 2 additions & 2 deletions core/services/keystore/keys/ocr2key/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/ethereum/go-ethereum/accounts/keystore"

starknet "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func FromEncryptedJSON(keyJSON []byte, password string) (KeyBundle, error) {
case chaintype.Solana:
kb = newKeyBundle(new(solanaKeyring))
case chaintype.StarkNet:
kb = newKeyBundle(new(starknet.OCR2Key))
kb = newKeyBundle(new(starkkey.OCR2Key))
default:
return nil, chaintype.NewErrInvalidChainType(export.ChainType)
}
Expand Down
11 changes: 5 additions & 6 deletions core/services/keystore/keys/ocr2key/key_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"github.com/ethereum/go-ethereum/crypto/secp256k1"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2/types"

starknet "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"

"github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
)

Expand All @@ -33,7 +32,7 @@ type KeyBundle interface {
var _ KeyBundle = &keyBundle[*evmKeyring]{}
var _ KeyBundle = &keyBundle[*cosmosKeyring]{}
var _ KeyBundle = &keyBundle[*solanaKeyring]{}
var _ KeyBundle = &keyBundle[*starknet.OCR2Key]{}
var _ KeyBundle = &keyBundle[*starkkey.OCR2Key]{}

var curve = secp256k1.S256()

Expand All @@ -47,7 +46,7 @@ func New(chainType chaintype.ChainType) (KeyBundle, error) {
case chaintype.Solana:
return newKeyBundleRand(chaintype.Solana, newSolanaKeyring)
case chaintype.StarkNet:
return newKeyBundleRand(chaintype.StarkNet, starknet.NewOCR2Key)
return newKeyBundleRand(chaintype.StarkNet, starkkey.NewOCR2Key)
}
return nil, chaintype.NewErrInvalidChainType(chainType)
}
Expand All @@ -62,7 +61,7 @@ func MustNewInsecure(reader io.Reader, chainType chaintype.ChainType) KeyBundle
case chaintype.Solana:
return mustNewKeyBundleInsecure(chaintype.Solana, newSolanaKeyring, reader)
case chaintype.StarkNet:
return mustNewKeyBundleInsecure(chaintype.StarkNet, starknet.NewOCR2Key, reader)
return mustNewKeyBundleInsecure(chaintype.StarkNet, starkkey.NewOCR2Key, reader)
}
panic(chaintype.NewErrInvalidChainType(chainType))
}
Expand Down Expand Up @@ -111,7 +110,7 @@ func (raw Raw) Key() (kb KeyBundle) {
case chaintype.Solana:
kb = newKeyBundle(new(solanaKeyring))
case chaintype.StarkNet:
kb = newKeyBundle(new(starknet.OCR2Key))
kb = newKeyBundle(new(starkkey.OCR2Key))
default:
return nil
}
Expand Down
11 changes: 5 additions & 6 deletions core/services/keystore/keys/starkkey/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@ package starkkey
import (
"github.com/ethereum/go-ethereum/accounts/keystore"

stark "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

const keyTypeIdentifier = "StarkNet"

// FromEncryptedJSON gets key from json and password
func FromEncryptedJSON(keyJSON []byte, password string) (stark.Key, error) {
func FromEncryptedJSON(keyJSON []byte, password string) (Key, error) {
return keys.FromEncryptedJSON(
keyTypeIdentifier,
keyJSON,
password,
adulteratedPassword,
func(_ keys.EncryptedKeyExport, rawPrivKey []byte) (stark.Key, error) {
return stark.Raw(rawPrivKey).Key(), nil
func(_ keys.EncryptedKeyExport, rawPrivKey []byte) (Key, error) {
return Raw(rawPrivKey).Key(), nil
},
)
}

// ToEncryptedJSON returns encrypted JSON representing key
func ToEncryptedJSON(key stark.Key, password string, scryptParams utils.ScryptParams) (export []byte, err error) {
func ToEncryptedJSON(key Key, password string, scryptParams utils.ScryptParams) (export []byte, err error) {
return keys.ToEncryptedJSON(
keyTypeIdentifier,
key.Raw(),
key,
password,
scryptParams,
adulteratedPassword,
func(id string, key stark.Key, cryptoJSON keystore.CryptoJSON) (keys.EncryptedKeyExport, error) {
func(id string, key Key, cryptoJSON keystore.CryptoJSON) (keys.EncryptedKeyExport, error) {
return keys.EncryptedKeyExport{
KeyType: id,
PublicKey: key.StarkKeyStr(),
Expand Down
5 changes: 2 additions & 3 deletions core/services/keystore/keys/starkkey/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package starkkey
import (
"testing"

starknet "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
Expand All @@ -13,7 +12,7 @@ func TestStarkNetKeys_ExportImport(t *testing.T) {
}

func createKey() (keys.KeyType, error) {
key, err := starknet.New()
key, err := New()
return TestWrapped{key}, err
}

Expand All @@ -24,7 +23,7 @@ func decryptKey(keyJSON []byte, password string) (keys.KeyType, error) {

// wrap key to conform to desired test interface
type TestWrapped struct {
starknet.Key
Key
}

func (w TestWrapped) ToEncryptedJSON(password string, scryptParams utils.ScryptParams) ([]byte, error) {
Expand Down
104 changes: 104 additions & 0 deletions core/services/keystore/keys/starkkey/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package starkkey

import (
crypto_rand "crypto/rand"
"encoding/hex"
"fmt"
"io"
"math/big"

"github.com/smartcontractkit/caigo"
)

// Raw represents the Stark private key
type Raw []byte

// Key gets the Key
func (raw Raw) Key() Key {
k := Key{}
var err error

k.priv = new(big.Int).SetBytes(raw)
k.pub.X, k.pub.Y, err = caigo.Curve.PrivateToPoint(k.priv)
if err != nil {
panic(err) // key not generated
}
return k
}

// String returns description
func (raw Raw) String() string {
return "<Starknet Raw Private Key>"
}

// GoString wraps String()
func (raw Raw) GoString() string {
return raw.String()
}

var _ fmt.GoStringer = &Key{}

type PublicKey struct {
X, Y *big.Int
}

// Key represents Starknet key
type Key struct {
priv *big.Int
pub PublicKey
}

// New creates new Key
func New() (Key, error) {
return newFrom(crypto_rand.Reader)
}

// MustNewInsecure return Key if no error
func MustNewInsecure(reader io.Reader) Key {
key, err := newFrom(reader)
if err != nil {
panic(err)
}
return key
}

func newFrom(reader io.Reader) (Key, error) {
return GenerateKey(reader)
}

// ID gets Key ID
func (key Key) ID() string {
return key.StarkKeyStr()
}

// StarkKeyStr is the starknet public key associated to the private key
// it is the X component of the ECDSA pubkey and used in the deployment of the account contract
// this func is used in exporting it via CLI and API
func (key Key) StarkKeyStr() string {
return "0x" + hex.EncodeToString(PubKeyToStarkKey(key.pub))
}

// Raw from private key
func (key Key) Raw() Raw {
return key.priv.Bytes()
}

// String is the print-friendly format of the Key
func (key Key) String() string {
return fmt.Sprintf("StarknetKey{PrivateKey: <redacted>, StarkKey: %s}", key.StarkKeyStr())
}

// GoString wraps String()
func (key Key) GoString() string {
return key.String()
}

// ToPrivKey returns the key usable for signing.
func (key Key) ToPrivKey() *big.Int {
return key.priv
}

// PublicKey copies public key object
func (key Key) PublicKey() PublicKey {
return key.pub
}
Loading

0 comments on commit 105ba34

Please sign in to comment.