Newer
Older
package keys
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"os"
"testing"
"time"
"golang.org/x/crypto/ripemd160"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/execution/evm/sha3"
"github.com/hyperledger/burrow/keys/pbkeys"
"github.com/hyperledger/burrow/logging"
tm_crypto "github.com/tendermint/go-crypto"
"google.golang.org/grpc"
)
type hashInfo struct {
data string
expected string
}
var hashData = map[string]hashInfo{
"sha256": {"hi", "8F434346648F6B96DF89DDA901C5176B10A6D83961DD3C1AC88B59B2DC327AA4"},
"ripemd160": {"hi", "242485AB6BFD3502BCB3442EA2E211687B8E4D89"},
}
var (
KEY_TYPES = []string{"ed25519", "secp256k1"}
HASH_TYPES = []string{"sha256", "ripemd160"}
)
// start the server
func init() {
failedCh := make(chan error)
testDir := "test_scratch/" + DefaultKeysDir
os.RemoveAll(testDir)
err := StartStandAloneServer(testDir, DefaultHost, TestPort, false, logging.NewNoopLogger())
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
failedCh <- err
}()
tick := time.NewTicker(time.Second)
select {
case err := <-failedCh:
fmt.Println(err)
os.Exit(1)
case <-tick.C:
}
}
func grpcKeysClient() pbkeys.KeysClient {
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
conn, err := grpc.Dial(DefaultHost+":"+TestPort, opts...)
if err != nil {
fmt.Printf("Failed to connect to grpc server: %v\n", err)
os.Exit(1)
}
return pbkeys.NewKeysClient(conn)
}
func checkAddrFromPub(typ string, pub, addr []byte) error {
var addr2 []byte
switch typ {
case "ed25519":
tmPubKey := new(tm_crypto.PubKeyEd25519)
copy(tmPubKey[:], pub)
addr2 = tmPubKey.Address()
case "secp256k1":
sha := sha256.New()
sha.Write(pub)
hash := ripemd160.New()
hash.Write(sha.Sum(nil))
addr2 = hash.Sum(nil)
default:
return fmt.Errorf("Unknown or incomplete typ %s", typ)
}
if bytes.Compare(addr, addr2) != 0 {
return fmt.Errorf("Keygen addr doesn't match pub. Got %X, expected %X", addr2, addr)
}
return nil
}
func testServerKeygenAndPub(t *testing.T, typ string) {
c := grpcKeysClient()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
genresp, err := c.GenerateKey(ctx, &pbkeys.GenRequest{Curvetype: typ})
if err != nil {
t.Fatal(err)
}
addr := genresp.Address
resp, err := c.PublicKey(ctx, &pbkeys.PubRequest{Address: addr})
if err != nil {
t.Fatal(err)
}
addrB, err := crypto.AddressFromHexString(addr)
if err != nil {
t.Fatal(err)
}
if err = checkAddrFromPub(typ, resp.GetPub(), addrB[:]); err != nil {
t.Fatal(err)
}
}
func TestServerKeygenAndPub(t *testing.T) {
for _, typ := range KEY_TYPES {
testServerKeygenAndPub(t, typ)
}
}
func testServerSignAndVerify(t *testing.T, typ string) {
c := grpcKeysClient()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
genresp, err := c.GenerateKey(ctx, &pbkeys.GenRequest{Curvetype: typ})
if err != nil {
t.Fatal(err)
}
addr := genresp.Address
resp, err := c.PublicKey(ctx, &pbkeys.PubRequest{Address: addr})
if err != nil {
t.Fatal(err)
}
hash := sha3.Sha3([]byte("the hash of something!"))
sig, err := c.Sign(ctx, &pbkeys.SignRequest{Address: addr, Message: hash})
if err != nil {
t.Fatal(err)
}
_, err = c.Verify(ctx, &pbkeys.VerifyRequest{Signature: sig.GetSignature(), Pub: resp.GetPub(), Message: hash, Curvetype: typ})
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
if err != nil {
t.Fatal(err)
}
}
func TestServerSignAndVerify(t *testing.T) {
for _, typ := range KEY_TYPES {
testServerSignAndVerify(t, typ)
}
}
func testServerHash(t *testing.T, typ string) {
hData := hashData[typ]
data, expected := hData.data, hData.expected
c := grpcKeysClient()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := c.Hash(ctx, &pbkeys.HashRequest{Hashtype: typ, Message: []byte(data)})
if err != nil {
t.Fatal(err)
}
hash := resp.GetHash()
if hash != expected {
t.Fatalf("Hash error for %s. Got %s, expected %s", typ, hash, expected)
}
}
func TestServerHash(t *testing.T) {
for _, typ := range HASH_TYPES {
testServerHash(t, typ)
}
}
//---------------------------------------------------------------------------------
func checkErrs(t *testing.T, errS string, err error) {
if err != nil {
t.Fatal(err)
}
if errS != "" {
t.Fatal(errS)
}
}