forked from cruzbit/cruzbit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction_test.go
More file actions
148 lines (127 loc) · 3.5 KB
/
Copy pathtransaction_test.go
File metadata and controls
148 lines (127 loc) · 3.5 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package cruzbit
import (
"encoding/base64"
"encoding/json"
"testing"
"golang.org/x/crypto/ed25519"
)
func TestTransaction(t *testing.T) {
// create a sender
pubKey, privKey, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
// create a recipient
pubKey2, privKey2, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
// create the unsigned transaction
tx := NewTransaction(pubKey, pubKey2, 50*CRUZBITS_PER_CRUZ, 0, 0, 0, 0, "for lunch")
// sign the transaction
if err := tx.Sign(privKey); err != nil {
t.Fatal(err)
}
// verify the transaction
ok, err := tx.Verify()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Errorf("Verification failed")
}
// re-sign the transaction with the wrong private key
if err := tx.Sign(privKey2); err != nil {
t.Fatal(err)
}
// verify the transaction (should fail)
ok, err = tx.Verify()
if err != nil {
t.Fatal(err)
}
if ok {
t.Errorf("Expected verification failure")
}
}
func TestTransactionTestVector1(t *testing.T) {
// create transaction for Test Vector 1
pubKeyBytes, err := base64.StdEncoding.DecodeString("80tvqyCax0UdXB+TPvAQwre7NxUHhISm/bsEOtbF+yI=")
if err != nil {
t.Fatal(err)
}
pubKey := ed25519.PublicKey(pubKeyBytes)
pubKeyBytes2, err := base64.StdEncoding.DecodeString("YkJHRtoQDa1TIKhN7gKCx54bavXouJy4orHwcRntcZY=")
if err != nil {
t.Fatal(err)
}
pubKey2 := ed25519.PublicKey(pubKeyBytes2)
tx := NewTransaction(pubKey, pubKey2, 50*CRUZBITS_PER_CRUZ, 2*CRUZBITS_PER_CRUZ, 0, 0, 0, "for lunch")
tx.Time = 1558565474
tx.Nonce = 2019727887
// check JSON matches test vector
txJson, err := json.Marshal(tx)
if err != nil {
t.Fatal(err)
}
if string(txJson) != `{"time":1558565474,"nonce":2019727887,"from":"80tvqyCax0UdXB+TPvAQwre7NxUHhISm/bsEOtbF+yI=",`+
`"to":"YkJHRtoQDa1TIKhN7gKCx54bavXouJy4orHwcRntcZY=","amount":5000000000,"fee":200000000,"memo":"for lunch","series":1}` {
t.Fatal("JSON differs from test vector: " + string(txJson))
}
// check ID matches test vector
id, err := tx.ID()
if err != nil {
t.Fatal(err)
}
if id.String() != "fc04870db147eb31823ce7c68ef366a7e94c2a719398322d746ddfd0f5c98776" {
t.Fatalf("ID %s differs from test vector", id)
}
// add signature from test vector
sigBytes, err := base64.StdEncoding.DecodeString("Fgb3q77evL5jZIXHMrpZ+wBOs2HZx07WYehi6EpHSlvnRv4wPvrP2sTTzAAmdvJZlkLrHXw1ensjXBiDosucCw==")
if err != nil {
t.Fatal(err)
}
tx.Signature = Signature(sigBytes)
// verify the transaction
ok, err := tx.Verify()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Errorf("Verification failed")
}
// re-sign the transaction with private key from test vector
privKeyBytes, err := base64.StdEncoding.DecodeString("EBQtXb3/Ht6KFh8/+Lxk9aDv2Zrag5G8r+dhElbCe07zS2+rIJrHRR1cH5M+8BDCt7s3FQeEhKb9uwQ61sX7Ig==")
if err != nil {
t.Fatal(err)
}
privKey := ed25519.PrivateKey(privKeyBytes)
if err := tx.Sign(privKey); err != nil {
t.Fatal(err)
}
// verify the transaction
ok, err = tx.Verify()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Errorf("Verification failed")
}
// re-sign the transaction with the wrong private key
_, privKey2, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
if err := tx.Sign(privKey2); err != nil {
t.Fatal(err)
}
// verify the transaction (should fail)
ok, err = tx.Verify()
if err != nil {
t.Fatal(err)
}
if ok {
t.Errorf("Expected verification failure")
}
}