forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identityKeys_test.go
131 lines (111 loc) · 3.38 KB
/
identityKeys_test.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package factom
import (
"crypto/rand"
"testing"
"bytes"
ed "github.com/FactomProject/ed25519"
"github.com/FactomProject/go-bip32"
)
func TestMarshalIdendityKey(t *testing.T) {
for i := 0; i < 100; i++ {
sec := make([]byte, 32)
_, err := rand.Read(sec)
if err != nil {
t.Error(err)
}
k1, err := MakeIdentityKey(sec)
if err != nil {
t.Error(err)
}
data1, err := k1.MarshalBinary()
if err != nil {
t.Error(err)
}
k2 := new(ECAddress)
data2, err := k2.UnmarshalBinaryData(data1)
if err != nil {
t.Error(err)
}
if len(data2) != 0 {
t.Errorf("UnmarshalBinary left %d bytes remaining", len(data2))
}
if bytes.Compare(k1.SecBytes(), k2.SecBytes()) != 0 {
t.Errorf("Unmarshaled object has different secret.")
}
if bytes.Compare(k1.PubBytes(), k2.PubBytes()) != 0 {
t.Errorf("Unmarshaled object has different public.")
}
}
}
func TestNewIdentityKey(t *testing.T) {
key := NewIdentityKey()
pub := "idpub1koTMq9h7FRCAdgZmDhjW85FBUsDJ8n1MBz94UaWf61JvLL1aa"
if key.PubString() != pub {
t.Errorf("new pubkey %s did not match %s", key.PubString(), pub)
}
}
func TestGetIdentityKey(t *testing.T) {
pub := "idpub1p4YkMzskVrtbK45nBHaikGda9w5SMvKvVsQtgVUfLK5Y8tByb"
sec := "idsec2wH72BNR9QZhTMGDbxwLWGrghZQexZvLTros2wCekkc62N9h7s"
k, err := GetIdentityKey(sec)
if err != nil {
t.Error(err)
}
if k.PubString() != pub {
t.Errorf("%s did not match %s", k.PubString(), pub)
}
msg := []byte("Hello Factom!")
sig := k.Sign(msg)
if !ed.Verify(k.PubFixed(), msg, sig) {
t.Errorf("Key signature did not match")
}
}
func TestMakeBIP44IdentityKey(t *testing.T) {
m := "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow"
pub := "idpub1p4YkMzskVrtbK45nBHaikGda9w5SMvKvVsQtgVUfLK5Y8tByb"
sec := "idsec2wH72BNR9QZhTMGDbxwLWGrghZQexZvLTros2wCekkc62N9h7s"
id, err := MakeBIP44IdentityKey(m, bip32.FirstHardenedChild, 0, 0)
if err != nil {
t.Error(err)
}
if id.String() != pub {
t.Errorf("incorrect public key from 12 words: got %s expecting %s", id.String(), pub)
}
if id.SecString() != sec {
t.Errorf("incorrect secret key from 12 words: got %s expecting %s", id.SecString(), sec)
}
}
func TestIsValidIdentityKey(t *testing.T) {
pub := "idpub1p4YkMzskVrtbK45nBHaikGda9w5SMvKvVsQtgVUfLK5Y8tByb"
sec := "idsec2wH72BNR9QZhTMGDbxwLWGrghZQexZvLTros2wCekkc62N9h7s"
badEmpty := ""
badLen := "idpub1p4YkMzskVrtbK45nBHaikGda9w5SMvKvVsQtgVUfLK5Y8tBybd"
badPrePub := "idpXb1p4YkMzskVrtbK45nBHaikGda9w5SMvKvVsQtgVUfLK5Y8tByb"
badPreSec := "idsXc2wH72BNR9QZhTMGDbxwLWGrghZQexZvLTros2wCekkc62N9h7s"
badCheckPub := "idpub1p4YkMzskVrtbK45nBHaikGda9w5SMvKvVsQtgVUfLK5Y8tBby"
badCheckSec := "idsec2wH72BNR9QZhTMGDbxwLWGrghZQexZvLTros2wCekkc62N9hs7"
if !IsValidIdentityKey(pub) {
t.Errorf("%s was not considered valid", pub)
}
if !IsValidIdentityKey(sec) {
t.Errorf("%s was not considered valid", sec)
}
if IsValidIdentityKey(badEmpty) {
t.Errorf("%s was considered valid", badEmpty)
}
if IsValidIdentityKey(badLen) {
t.Errorf("%s was considered valid", badLen)
}
if IsValidIdentityKey(badPrePub) {
t.Errorf("%s was considered valid", badPrePub)
}
if IsValidIdentityKey(badPreSec) {
t.Errorf("%s was considered valid", badPreSec)
}
if IsValidIdentityKey(badCheckPub) {
t.Errorf("%s was considered valid", badCheckPub)
}
if IsValidIdentityKey(badCheckSec) {
t.Errorf("%s was considered valid", badCheckSec)
}
}