forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_client_mock.go
113 lines (97 loc) · 3.27 KB
/
key_client_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock
import (
"crypto/rand"
"encoding/hex"
"fmt"
. "github.com/hyperledger/burrow/keys"
// NOTE: prior to building out /crypto, use
// tendermint/go-crypto for the mock client
"github.com/tendermint/ed25519"
"golang.org/x/crypto/ripemd160"
)
//---------------------------------------------------------------------
// 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, error) {
key := &MockKey{
Address: make([]byte, 20),
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)
if err != nil {
return nil, err
}
copy(key.PrivateKey[:], privateKey[:])
copy(key.PublicKey[:], publicKey[:])
// prepend 0x01 for ed25519 public key
typedPublicKeyBytes := append([]byte{0x01}, key.PublicKey...)
hasher := ripemd160.New()
hasher.Write(typedPublicKeyBytes)
key.Address = hasher.Sum(nil)
return key, nil
}
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 monax-keys
// Implementation assertion
var _ KeyClient = (*MockKeyClient)(nil)
type MockKeyClient struct {
knownKeys map[string]*MockKey
}
func NewMockKeyClient() *MockKeyClient {
return &MockKeyClient{
knownKeys: make(map[string]*MockKey),
}
}
func (mock *MockKeyClient) NewKey() (address []byte) {
// Only tests ED25519 curve and ripemd160.
key, err := newMockKey()
if err != nil {
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) ([]byte, error) {
key := mock.knownKeys[fmt.Sprintf("%X", signAddress)]
if key == nil {
return nil, fmt.Errorf("Unknown address (%X)", signAddress)
}
signBytes, err := hex.DecodeString(signBytesString)
if err != nil {
return nil, fmt.Errorf("Sign bytes string is invalid hex string: %s", err.Error())
}
return key.Sign(signBytes)
}
func (mock *MockKeyClient) PublicKey(address []byte) (publicKey []byte, err error) {
key := mock.knownKeys[fmt.Sprintf("%X", address)]
if key == nil {
return nil, fmt.Errorf("Unknown address (%X)", address)
}
return key.PublicKey, nil
}