Skip to content

Commit

Permalink
crypto.Keypair to local interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Nov 9, 2022
1 parent d8f20e9 commit cc82734
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 86 deletions.
2 changes: 1 addition & 1 deletion cmd/gossamer/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func getKeystorePassword(ctx *cli.Context) []byte {

// KeypairInserter inserts a keypair.
type KeypairInserter interface {
Insert(kp crypto.Keypair) error
Insert(kp keystore.KeyPair) error
}

// unlockKeystore compares the length of passwords to the length of accounts,
Expand Down
9 changes: 9 additions & 0 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
Expand Down Expand Up @@ -98,3 +99,11 @@ type CodeSubstitutedState interface {
type Telemetry interface {
SendMessage(msg json.Marshaler)
}

// KeyPair is a key pair to sign messages and from which
// the public key and key type can be obtained.
type KeyPair interface {
Type() crypto.KeyType
Sign(msg []byte) ([]byte, error)
Public() crypto.PublicKey
}
3 changes: 1 addition & 2 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
Expand Down Expand Up @@ -452,7 +451,7 @@ func (s *Service) maintainTransactionPool(block *types.Block, bestBlockHash comm
}

// InsertKey inserts keypair into the account keystore
func (s *Service) InsertKey(kp crypto.Keypair, keystoreType string) error {
func (s *Service) InsertKey(kp KeyPair, keystoreType string) error {
ks, err := s.keys.GetKeystore([]byte(keystoreType))
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ func TestServiceInsertKey(t *testing.T) {
keyring, _ := keystore.NewSr25519Keyring()
aliceKeypair := keyring.Alice().(*sr25519.Keypair)
type args struct {
kp crypto.Keypair
kp KeyPair
keystoreType string
}
tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package rpc
import (
"encoding/json"

"github.com/ChainSafe/gossamer/dot/core"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/grandpa"
Expand Down Expand Up @@ -82,7 +82,7 @@ type TransactionStateAPI interface {

// CoreAPI is the interface for the core methods
type CoreAPI interface {
InsertKey(kp crypto.Keypair, keystoreType string) error
InsertKey(kp core.KeyPair, keystoreType string) error
HasKey(pubKeyStr string, keyType string) (bool, error)
GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error)
HandleSubmittedExtrinsic(types.Extrinsic) error
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package modules

import (
"github.com/ChainSafe/gossamer/dot/core"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/grandpa"
Expand Down Expand Up @@ -89,7 +89,7 @@ type TransactionStateAPI interface {

// CoreAPI is the interface for the core methods
type CoreAPI interface {
InsertKey(kp crypto.Keypair, keystoreType string) error
InsertKey(kp core.KeyPair, keystoreType string) error
HasKey(pubKeyStr string, keyType string) (bool, error)
GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error)
HandleSubmittedExtrinsic(types.Extrinsic) error
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/api_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewMockeryBlockAPI(t *testing.T) *modulesmocks.BlockAPI {
// NewMockCoreAPI creates and return an rpc CoreAPI interface mock
func NewMockCoreAPI(t *testing.T) *modulesmocks.CoreAPI {
m := modulesmocks.NewCoreAPI(t)
m.On("InsertKey", mock.AnythingOfType("crypto.Keypair"), mock.AnythingOfType("string")).Return(nil).Maybe()
m.On("InsertKey", mock.AnythingOfType("core.Keypair"), mock.AnythingOfType("string")).Return(nil).Maybe()
m.On("HasKey", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(false, nil).Maybe()
m.On("GetRuntimeVersion", mock.AnythingOfType("*common.Hash")).
Return(runtime.Version{SpecName: []byte(`mock-spec`)}, nil).Maybe()
Expand Down
8 changes: 6 additions & 2 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,14 @@ func TestAuthorModule_InsertKey_Integration(t *testing.T) {
t.Run(tname, func(t *testing.T) {
t.Parallel()

var expectedKp crypto.Keypair
type keyPair interface {
Public() crypto.PublicKey
}

var expectedKp keyPair
var pubkey string

if kp, ok := tt.kp.(crypto.Keypair); ok {
if kp, ok := tt.kp.(keyPair); ok {
expectedKp = kp
pubkey = kp.Public().Hex()
} else {
Expand Down
6 changes: 3 additions & 3 deletions dot/rpc/modules/mocks/core_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (nb nodeBuilder) createBABEService(cfg *Config, st *state.Service, ks KeySt
type KeyStore interface {
Name() keystore.Name
Type() string
Keypairs() []crypto.Keypair
Keypairs() []keystore.KeyPair
}

func (nodeBuilder) createBABEServiceWithBuilder(cfg *Config, st *state.Service, ks KeyStore,
Expand Down
8 changes: 0 additions & 8 deletions lib/crypto/keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ const Secp256k1Type KeyType = "secp256k1"
// UnknownType is used by the GenericKeystore
const UnknownType KeyType = "unknown"

// Keypair interface
type Keypair interface {
Type() KeyType
Sign(msg []byte) ([]byte, error)
Public() PublicKey
Private() PrivateKey
}

// PublicKey interface
type PublicKey interface {
Verify(msg, sig []byte) (bool, error)
Expand Down
13 changes: 6 additions & 7 deletions lib/keystore/basic_keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
type BasicKeystore struct {
name Name
typ crypto.KeyType
keys map[common.Address]crypto.Keypair // map of public key encodings to keypairs
keys map[common.Address]KeyPair // map of public key encodings to keypairs
lock sync.RWMutex
}

Expand All @@ -30,7 +30,7 @@ func NewBasicKeystore(name Name, typ crypto.KeyType) *BasicKeystore {
return &BasicKeystore{
name: name,
typ: typ,
keys: make(map[common.Address]crypto.Keypair),
keys: make(map[common.Address]KeyPair),
}
}

Expand All @@ -50,7 +50,7 @@ func (ks *BasicKeystore) Size() int {
}

// Insert adds a keypair to the keystore
func (ks *BasicKeystore) Insert(kp crypto.Keypair) error {
func (ks *BasicKeystore) Insert(kp KeyPair) error {
ks.lock.Lock()
defer ks.lock.Unlock()

Expand All @@ -65,7 +65,7 @@ func (ks *BasicKeystore) Insert(kp crypto.Keypair) error {
}

// GetKeypair returns a keypair corresponding to the given public key, or nil if it doesn't exist
func (ks *BasicKeystore) GetKeypair(pub crypto.PublicKey) crypto.Keypair {
func (ks *BasicKeystore) GetKeypair(pub crypto.PublicKey) KeyPair {
for _, key := range ks.keys {
if bytes.Equal(key.Public().Encode(), pub.Encode()) {
return key
Expand All @@ -75,7 +75,7 @@ func (ks *BasicKeystore) GetKeypair(pub crypto.PublicKey) crypto.Keypair {
}

// GetKeypairFromAddress returns a keypair corresponding to the given address, or nil if it doesn't exist
func (ks *BasicKeystore) GetKeypairFromAddress(pub common.Address) crypto.Keypair {
func (ks *BasicKeystore) GetKeypairFromAddress(pub common.Address) KeyPair {
ks.lock.RLock()
defer ks.lock.RUnlock()
return ks.keys[pub]
Expand All @@ -96,8 +96,7 @@ func (ks *BasicKeystore) PublicKeys() []crypto.PublicKey {
}

// Keypairs returns all keypairs in the keystore
func (ks *BasicKeystore) Keypairs() []crypto.Keypair {
srkeys := []crypto.Keypair{}
func (ks *BasicKeystore) Keypairs() (srkeys []KeyPair) {
if ks.keys == nil {
return srkeys
}
Expand Down
22 changes: 9 additions & 13 deletions lib/keystore/generic_keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import (
// GenericKeystore holds keys of any type
type GenericKeystore struct {
name Name
keys map[common.Address]crypto.Keypair // map of public key encodings to keypairs
keys map[common.Address]KeyPair // map of public key encodings to keypairs
lock sync.RWMutex
}

// NewGenericKeystore creates a new GenericKeystore
func NewGenericKeystore(name Name) *GenericKeystore {
return &GenericKeystore{
name: name,
keys: make(map[common.Address]crypto.Keypair),
keys: make(map[common.Address]KeyPair),
}
}

Expand All @@ -45,7 +45,7 @@ func (ks *GenericKeystore) Size() int {
}

// Insert adds a keypair to the keystore
func (ks *GenericKeystore) Insert(kp crypto.Keypair) error {
func (ks *GenericKeystore) Insert(kp KeyPair) error {
ks.lock.Lock()
defer ks.lock.Unlock()

Expand All @@ -56,7 +56,7 @@ func (ks *GenericKeystore) Insert(kp crypto.Keypair) error {
}

// GetKeypair returns a keypair corresponding to the given public key, or nil if it doesn't exist
func (ks *GenericKeystore) GetKeypair(pub crypto.PublicKey) crypto.Keypair {
func (ks *GenericKeystore) GetKeypair(pub crypto.PublicKey) KeyPair {
for _, key := range ks.keys {
if bytes.Equal(key.Public().Encode(), pub.Encode()) {
return key
Expand All @@ -66,7 +66,7 @@ func (ks *GenericKeystore) GetKeypair(pub crypto.PublicKey) crypto.Keypair {
}

// GetKeypairFromAddress returns a keypair corresponding to the given address, or nil if it doesn't exist
func (ks *GenericKeystore) GetKeypairFromAddress(pub common.Address) crypto.Keypair {
func (ks *GenericKeystore) GetKeypairFromAddress(pub common.Address) KeyPair {
ks.lock.RLock()
defer ks.lock.RUnlock()
return ks.keys[pub]
Expand All @@ -87,8 +87,7 @@ func (ks *GenericKeystore) PublicKeys() []crypto.PublicKey {
}

// Keypairs returns all keypairs in the keystore
func (ks *GenericKeystore) Keypairs() []crypto.Keypair {
srkeys := []crypto.Keypair{}
func (ks *GenericKeystore) Keypairs() (srkeys []KeyPair) {
if ks.keys == nil {
return srkeys
}
Expand Down Expand Up @@ -125,8 +124,7 @@ func (ks *GenericKeystore) Ed25519PublicKeys() []crypto.PublicKey {
}

// Ed25519Keypairs Keypair
func (ks *GenericKeystore) Ed25519Keypairs() []crypto.Keypair {
edkeys := []crypto.Keypair{}
func (ks *GenericKeystore) Ed25519Keypairs() (edkeys []KeyPair) {
if ks.keys == nil {
return edkeys
}
Expand Down Expand Up @@ -155,8 +153,7 @@ func (ks *GenericKeystore) Sr25519PublicKeys() []crypto.PublicKey {
}

// Sr25519Keypairs Keypair
func (ks *GenericKeystore) Sr25519Keypairs() []crypto.Keypair {
srkeys := []crypto.Keypair{}
func (ks *GenericKeystore) Sr25519Keypairs() (srkeys []KeyPair) {
if ks.keys == nil {
return srkeys
}
Expand Down Expand Up @@ -185,8 +182,7 @@ func (ks *GenericKeystore) Secp256k1PublicKeys() []crypto.PublicKey {
}

// Secp256k1Keypairs Keypair
func (ks *GenericKeystore) Secp256k1Keypairs() []crypto.Keypair {
sckeys := []crypto.Keypair{}
func (ks *GenericKeystore) Secp256k1Keypairs() (sckeys []KeyPair) {
if ks.keys == nil {
return sckeys
}
Expand Down
26 changes: 13 additions & 13 deletions lib/keystore/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// PrivateKeyToKeypair returns a public, private keypair given a private key
func PrivateKeyToKeypair(priv crypto.PrivateKey) (kp crypto.Keypair, err error) {
func PrivateKeyToKeypair(priv crypto.PrivateKey) (kp KeyPair, err error) {
if key, ok := priv.(*sr25519.PrivateKey); ok {
kp, err = sr25519.NewKeypairFromPrivate(key)
} else if key, ok := priv.(*ed25519.PrivateKey); ok {
Expand Down Expand Up @@ -51,7 +51,7 @@ func DecodePrivateKey(in []byte, keytype crypto.KeyType) (priv crypto.PrivateKey
}

// DecodeKeyPairFromHex turns an hex-encoded private key into a keypair
func DecodeKeyPairFromHex(keystr []byte, keytype crypto.KeyType) (kp crypto.Keypair, err error) {
func DecodeKeyPairFromHex(keystr []byte, keytype crypto.KeyType) (kp KeyPair, err error) {
switch keytype {
case crypto.Sr25519Type:
kp, err = sr25519.NewKeypairFromSeed(keystr)
Expand All @@ -67,7 +67,7 @@ func DecodeKeyPairFromHex(keystr []byte, keytype crypto.KeyType) (kp crypto.Keyp
// GenerateKeypair create a new keypair with the corresponding type and saves
// it to basepath/keystore/[public key].key in json format encrypted using the
// specified password and returns the resulting filepath of the new key
func GenerateKeypair(keytype string, kp crypto.Keypair, basepath string, password []byte) (string, error) {
func GenerateKeypair(keytype string, kp PublicPrivater, basepath string, password []byte) (string, error) {
if keytype == "" {
keytype = crypto.Sr25519Type
}
Expand Down Expand Up @@ -115,15 +115,15 @@ func GenerateKeypair(keytype string, kp crypto.Keypair, basepath string, passwor
func LoadKeystore(key string, ks TyperInserter) (err error) {
if key != "" {
var kr interface {
Alice() crypto.Keypair
Bob() crypto.Keypair
Charlie() crypto.Keypair
Dave() crypto.Keypair
Eve() crypto.Keypair
Ferdie() crypto.Keypair
George() crypto.Keypair
Heather() crypto.Keypair
Ian() crypto.Keypair
Alice() KeyPair
Bob() KeyPair
Charlie() KeyPair
Dave() KeyPair
Eve() KeyPair
Ferdie() KeyPair
George() KeyPair
Heather() KeyPair
Ian() KeyPair
}

switch ks.Type() {
Expand Down Expand Up @@ -204,7 +204,7 @@ func ImportKeypair(fp, dir string) (string, error) {

// ImportRawPrivateKey imports a raw private key and saves it to the keystore directory
func ImportRawPrivateKey(key, keytype, basepath string, password []byte) (string, error) {
var kp crypto.Keypair
var kp PublicPrivater
var err error

if keytype == "" {
Expand Down
Loading

0 comments on commit cc82734

Please sign in to comment.