diff --git a/keys/mock/key_client_mock.go b/keys/mock/key_client_mock.go index e8c737a59c8600d8ccad02b847d8abdc17d3bd7a..0272d12d561932eac6d0e6ebf099b2852a1d887e 100644 --- a/keys/mock/key_client_mock.go +++ b/keys/mock/key_client_mock.go @@ -15,47 +15,75 @@ package mock import ( + "crypto/rand" "encoding/hex" "fmt" - // for the mock of key server we explicitly import - // the keys server to ensure the core components are - // compatible with eris-db. - "github.com/monax/eris-keys/crypto" + // NOTE: prior to building out /crypto, use + // tendermint/go-crypto for the mock client + "github.com/tendermint/ed25519" . "github.com/monax/eris-db/keys" ) +//--------------------------------------------------------------------- +// Mock ed25510 key for mock keys client + +// Simple ed25519 key structure for mock purposes with ripemd160 address +type MockKey struct { + Address []byte + PrivateKey [ed25519.PrivateKeySize]byte + PublicKey []byte +} + +func newMockKey() *MockKey { + key := &MockKey{ + Address: make([]byte, 20), + PrivateKey: new([ed25519.PrivateKeySize]byte), + Publickey: make([]byte, ed25519.PublicKeySize), + } + // this is a mock key, so the entropy of the source is purely + // for testing + publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader) + copy(key.PrivateKey[:], privateKey[:]) + copy(key.Publickey[:], publicKey[:]) + return key +} + +func (mockKey *MockKey) Sign(message []byte) ([]byte, error) { + signatureBytes := make([]byte, ed25519.SignatureSize) + signature = ed25519.Sign(&mockKey.PrivateKey, message) + copy(signatureBytes[:], signature[:]) + return signatureBytes, nil +} + //--------------------------------------------------------------------- // Mock client for replacing signing done by eris-keys -// NOTE [ben] Compiler check to ensure MockKeyClient successfully implements -// eris-db/keys.KeyClient +// Implementation assertion var _ KeyClient = (*MockKeyClient)(nil) type MockKeyClient struct { - knownKeys map[string]*crypto.Key + knownKeys map[string]*MockKey } func NewMockKeyClient() *MockKeyClient { return &MockKeyClient{ - knownKeys: make(map[string]*crypto.Key), + knownKeys: make(map[string]*MockKey), } } func (mock *MockKeyClient) NewKey() (address []byte) { // Only tests ED25519 curve and ripemd160. - keyType := crypto.KeyType{crypto.CurveTypeEd25519, - crypto.AddrTypeRipemd160} - key, err := crypto.NewKey(keyType) + key, err := newMockKey() if err != nil { - panic(fmt.Sprintf("Mocked key client failed on key generation (%s): %s", keyType.String(), err)) + panic(fmt.Sprintf("Mocked key client failed on key generation: %s", err)) } mock.knownKeys[fmt.Sprintf("%X", key.Address)] = key return key.Address } -func (mock *MockKeyClient) Sign(signBytesString string, signAddress []byte) (signature []byte, err error) { +func (mock *MockKeyClient) Sign(signBytesString string, signAddress []byte) ([]byte, error) { key := mock.knownKeys[fmt.Sprintf("%X", signAddress)] if key == nil { return nil, fmt.Errorf("Unknown address (%X)", signAddress) @@ -72,5 +100,5 @@ func (mock *MockKeyClient) PublicKey(address []byte) (publicKey []byte, err erro if key == nil { return nil, fmt.Errorf("Unknown address (%X)", address) } - return key.Pubkey() + return key.PublicKey, nil }