-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
sender_request_test.go
218 lines (180 loc) · 5.69 KB
/
sender_request_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
132
133
134
135
136
137
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package paymail
import (
"fmt"
"testing"
"time"
"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/stretchr/testify/assert"
)
// TestSenderRequest_Sign will test the method Sign()
func TestSenderRequest_Sign(t *testing.T) {
// Create key
key, err := bitcoin.CreatePrivateKeyString()
assert.NoError(t, err)
assert.NotNil(t, key)
// Create the request / message
senderRequest := &SenderRequest{
Dt: time.Now().UTC().Format(time.RFC3339),
SenderHandle: testAlias + "@" + testDomain,
SenderName: testName,
Purpose: testMessage,
}
var signature string
t.Run("invalid key - empty", func(t *testing.T) {
signature, err = senderRequest.Sign("")
assert.Error(t, err)
assert.Equal(t, len(signature), 0)
})
t.Run("invalid key - 0", func(t *testing.T) {
signature, err = senderRequest.Sign("0")
assert.Error(t, err)
assert.Equal(t, len(signature), 0)
})
t.Run("invalid dt", func(t *testing.T) {
senderRequest.Dt = ""
signature, err = senderRequest.Sign(key)
assert.Error(t, err)
assert.Equal(t, len(signature), 0)
})
t.Run("invalid sender handle", func(t *testing.T) {
senderRequest.Dt = time.Now().UTC().Format(time.RFC3339)
senderRequest.SenderHandle = ""
signature, err = senderRequest.Sign(key)
assert.Error(t, err)
assert.Equal(t, len(signature), 0)
})
t.Run("valid signature", func(t *testing.T) {
senderRequest.SenderHandle = testAlias + "@" + testDomain
signature, err = senderRequest.Sign(key)
assert.NoError(t, err)
assert.NotEqual(t, len(signature), 0)
// Get address for verification
var address string
address, err = bitcoin.GetAddressFromPrivateKeyString(key, false)
assert.NoError(t, err)
// Verify the signature
err = senderRequest.Verify(address, signature)
assert.NoError(t, err)
})
}
// ExampleSenderRequest_Sign example using Sign()
//
// See more examples in /examples/
func ExampleSenderRequest_Sign() {
// Test private key
key := "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd"
// Create the request / message
senderRequest := &SenderRequest{
Dt: time.Now().UTC().Format(time.RFC3339),
SenderHandle: testAlias + "@" + testDomain,
SenderName: testName,
Purpose: testMessage,
}
// Sign the sender request
signature, err := senderRequest.Sign(key)
if err != nil {
fmt.Printf("error occurred in sign: %s", err.Error())
return
} else if len(signature) == 0 {
fmt.Printf("signature was empty")
return
}
// Cannot display signature as it changes because of the "dt" field
fmt.Printf("signature created!")
// Output:signature created!
}
// BenchmarkSenderRequest_Sign benchmarks the method Sign()
func BenchmarkSenderRequest_Sign(b *testing.B) {
// Create the request / message
senderRequest := &SenderRequest{
Dt: time.Now().UTC().Format(time.RFC3339),
SenderHandle: testAlias + "@" + testDomain,
SenderName: testName,
Purpose: testMessage,
}
for i := 0; i < b.N; i++ {
_, _ = senderRequest.Sign("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
}
}
// TestSenderRequest_Verify will test the method Verify()
func TestSenderRequest_Verify(t *testing.T) {
// Create key
key, err := bitcoin.CreatePrivateKeyString()
assert.NoError(t, err)
assert.NotNil(t, key)
// Create the request / message
senderRequest := &SenderRequest{
Dt: time.Now().UTC().Format(time.RFC3339),
SenderHandle: testAlias + "@" + testDomain,
SenderName: testName,
Purpose: testMessage,
}
// Sign
var signature string
signature, err = senderRequest.Sign(key)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(signature))
// Get address from private key
var address string
address, err = bitcoin.GetAddressFromPrivateKeyString(key, false)
assert.NoError(t, err)
assert.NotNil(t, address)
t.Run("valid verification", func(t *testing.T) {
err = senderRequest.Verify(address, signature)
assert.NoError(t, err)
})
t.Run("invalid - empty address", func(t *testing.T) {
err = senderRequest.Verify("", signature)
assert.Error(t, err)
})
t.Run("invalid - empty signature", func(t *testing.T) {
err = senderRequest.Verify(address, "")
assert.Error(t, err)
})
t.Run("invalid - wrong signature - hex short", func(t *testing.T) {
err = senderRequest.Verify(address, "0")
assert.Error(t, err)
})
t.Run("invalid - wrong signature", func(t *testing.T) {
err = senderRequest.Verify(address, "73646661736466736466617364667364666173646673646661736466")
assert.Error(t, err)
})
}
// ExampleSenderRequest_Verify example using Verify()
//
// See more examples in /examples/
func ExampleSenderRequest_Verify() {
// Example sender request
senderRequest := &SenderRequest{
Dt: "2020-10-02T16:43:39Z",
SenderHandle: testAlias + "@" + testDomain,
SenderName: testName,
Purpose: testMessage,
}
// Try verifying (valid) (using an address and a signature - previously generated for example)
if err := senderRequest.Verify(
"1LqWAxSaKdXRKATAj7ELk34ioyT1T8gXgU",
"G70DPE2p8xtCehUjRkQF2gI26kDu59JsQ6KKUmJyHi1XFGkeoIokgzN/kiMy+lujpXOi+C35sZUwgSMqOYRDXPQ=",
); err != nil {
fmt.Printf("error occurred in Verify: %s", err.Error())
return
}
fmt.Printf("signature verified!")
// Output:signature verified!
}
// BenchmarkSenderRequest_Verify benchmarks the method Verify()
func BenchmarkSenderRequest_Verify(b *testing.B) {
// Example sender request
senderRequest := &SenderRequest{
Dt: "2020-10-02T16:43:39Z",
SenderHandle: testAlias + "@" + testDomain,
SenderName: testName,
Purpose: testMessage,
}
for i := 0; i < b.N; i++ {
_ = senderRequest.Verify(
"1LqWAxSaKdXRKATAj7ELk34ioyT1T8gXgU",
"G70DPE2p8xtCehUjRkQF2gI26kDu59JsQ6KKUmJyHi1XFGkeoIokgzN/kiMy+lujpXOi+C35sZUwgSMqOYRDXPQ=",
)
}
}