@@ -126,3 +126,59 @@ func BenchmarkPrivateKeyFromString(b *testing.B) {
126
126
_ , _ = PrivateKeyFromString (key )
127
127
}
128
128
}
129
+
130
+ // TestPrivateAndPublicKeys will test the method PrivateAndPublicKeys()
131
+ func TestPrivateAndPublicKeys (t * testing.T ) {
132
+
133
+ t .Parallel ()
134
+
135
+ // Create the list of tests
136
+ var tests = []struct {
137
+ input string
138
+ expectedPrivateKey string
139
+ expectedNil bool
140
+ expectedError bool
141
+ }{
142
+ {"" , "" , true , true },
143
+ {"0" , "" , true , true },
144
+ {"00000" , "" , true , true },
145
+ {"0-0-0-0-0" , "" , true , true },
146
+ {"z4035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abz" , "" , true , true },
147
+ {"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd" , "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd" , false , false },
148
+ }
149
+
150
+ // Run tests
151
+ for _ , test := range tests {
152
+ if privateKey , publicKey , err := PrivateAndPublicKeys (test .input ); err != nil && ! test .expectedError {
153
+ t .Errorf ("%s Failed: [%s] inputted and error not expected but got: %s" , t .Name (), test .input , err .Error ())
154
+ } else if err == nil && test .expectedError {
155
+ t .Errorf ("%s Failed: [%s] inputted and error was expected" , t .Name (), test .input )
156
+ } else if (privateKey == nil || publicKey == nil ) && ! test .expectedNil {
157
+ t .Errorf ("%s Failed: [%s] inputted and was nil but not expected" , t .Name (), test .input )
158
+ } else if (privateKey != nil || publicKey != nil ) && test .expectedNil {
159
+ t .Errorf ("%s Failed: [%s] inputted and was NOT nil but expected to be nil" , t .Name (), test .input )
160
+ } else if privateKey != nil && hex .EncodeToString (privateKey .Serialize ()) != test .expectedPrivateKey {
161
+ t .Errorf ("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s" , t .Name (), test .input , test .expectedPrivateKey , hex .EncodeToString (privateKey .Serialize ()))
162
+ }
163
+ }
164
+ }
165
+
166
+ // ExamplePrivateAndPublicKeys example using PrivateAndPublicKeys()
167
+ func ExamplePrivateAndPublicKeys () {
168
+ privateKey , publicKey , err := PrivateAndPublicKeys ("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd" )
169
+ if err != nil {
170
+ fmt .Printf ("error occurred: %s" , err .Error ())
171
+ return
172
+ }
173
+ fmt .Printf ("private key: %s public key: %s" , hex .EncodeToString (privateKey .Serialize ()), hex .EncodeToString (publicKey .SerializeCompressed ()))
174
+
175
+ // Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd public key: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f
176
+ }
177
+
178
+ // BenchmarkPrivateAndPublicKeys benchmarks the method PrivateAndPublicKeys()
179
+ func BenchmarkPrivateAndPublicKeys (b * testing.B ) {
180
+ key , _ := CreatePrivateKeyString ()
181
+ for i := 0 ; i < b .N ; i ++ {
182
+ _ , _ , _ = PrivateAndPublicKeys (key )
183
+ }
184
+ }
0 commit comments