@@ -5,7 +5,10 @@ import (
55 "fmt"
66 "testing"
77
8+ "github.com/libsv/go-bk/bec"
9+ "github.com/libsv/go-bk/wif"
810 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
912)
1013
1114// TestCreatePrivateKey will test the method CreatePrivateKey()
@@ -371,3 +374,203 @@ func BenchmarkWifToPrivateKeyString(b *testing.B) {
371374 _ , _ = WifToPrivateKeyString ("5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei" )
372375 }
373376}
377+
378+ // TestCreateWif will test the method CreateWif()
379+ func TestCreateWif (t * testing.T ) {
380+ t .Run ("TestCreateWif" , func (t * testing.T ) {
381+ t .Parallel ()
382+
383+ // Create a WIF
384+ wifKey , err := CreateWif ()
385+ require .NoError (t , err )
386+ require .NotNil (t , wifKey )
387+ // t.Log("WIF:", wifKey.String())
388+ require .Equalf (t , 51 , len (wifKey .String ()), "WIF should be 51 characters long, got: %d" , len (wifKey .String ()))
389+ })
390+
391+ t .Run ("TestWifToPrivateKey" , func (t * testing.T ) {
392+ t .Parallel ()
393+
394+ // Create a WIF
395+ wifKey , err := CreateWif ()
396+ require .NoError (t , err )
397+ require .NotNil (t , wifKey )
398+ // t.Log("WIF:", wifKey.String())
399+ require .Equalf (t , 51 , len (wifKey .String ()), "WIF should be 51 characters long, got: %d" , len (wifKey .String ()))
400+
401+ // Convert WIF to Private Key
402+ var privateKey * bec.PrivateKey
403+ privateKey , err = WifToPrivateKey (wifKey .String ())
404+ require .NoError (t , err )
405+ require .NotNil (t , privateKey )
406+ privateKeyString := hex .EncodeToString (privateKey .Serialise ())
407+ // t.Log("Private Key:", privateKeyString)
408+ require .Equalf (t , 64 , len (privateKeyString ), "Private Key should be 64 characters long, got: %d" , len (privateKeyString ))
409+ })
410+ }
411+
412+ // ExampleCreateWif example using CreateWif()
413+ func ExampleCreateWif () {
414+ wifKey , err := CreateWif ()
415+ if err != nil {
416+ fmt .Println (err )
417+ return
418+ }
419+ fmt .Println ("WIF Key Generated Length:" , len (wifKey .String ()))
420+ // Output: WIF Key Generated Length: 51
421+ }
422+
423+ // BenchmarkCreateWif benchmarks the method CreateWif()
424+ func BenchmarkCreateWif (b * testing.B ) {
425+ for i := 0 ; i < b .N ; i ++ {
426+ _ , _ = CreateWif ()
427+ }
428+ }
429+
430+ // TestCreateWifString will test the method CreateWifString()
431+ func TestCreateWifString (t * testing.T ) {
432+ t .Run ("TestCreateWifString" , func (t * testing.T ) {
433+ t .Parallel ()
434+
435+ // Create a WIF
436+ wifKey , err := CreateWifString ()
437+ require .NoError (t , err )
438+ require .NotNil (t , wifKey )
439+ // t.Log("WIF:", wifKey)
440+ require .Equalf (t , 51 , len (wifKey ), "WIF should be 51 characters long, got: %d" , len (wifKey ))
441+ })
442+
443+ t .Run ("TestWifToPrivateKeyString" , func (t * testing.T ) {
444+ t .Parallel ()
445+
446+ // Create a WIF
447+ wifKey , err := CreateWifString ()
448+ require .NoError (t , err )
449+ require .NotNil (t , wifKey )
450+ // t.Log("WIF:", wifKey)
451+ require .Equalf (t , 51 , len (wifKey ), "WIF should be 51 characters long, got: %d" , len (wifKey ))
452+
453+ // Convert WIF to Private Key
454+ var privateKeyString string
455+ privateKeyString , err = WifToPrivateKeyString (wifKey )
456+ require .NoError (t , err )
457+ require .NotNil (t , privateKeyString )
458+ // t.Log("Private Key:", privateKeyString)
459+ require .Equalf (t , 64 , len (privateKeyString ), "Private Key should be 64 characters long, got: %d" , len (privateKeyString ))
460+
461+ })
462+ }
463+
464+ // ExampleCreateWifString example using CreateWifString()
465+ func ExampleCreateWifString () {
466+ wifKey , err := CreateWifString ()
467+ if err != nil {
468+ fmt .Println (err )
469+ return
470+ }
471+ fmt .Println ("WIF Key Generated Length:" , len (wifKey ))
472+ // Output: WIF Key Generated Length: 51
473+ }
474+
475+ // BenchmarkCreateWifString benchmarks the method CreateWifString()
476+ func BenchmarkCreateWifString (b * testing.B ) {
477+ for i := 0 ; i < b .N ; i ++ {
478+ _ , _ = CreateWifString ()
479+ }
480+ }
481+
482+ // TestWifFromString will test the method WifFromString()
483+ func TestWifFromString (t * testing.T ) {
484+ t .Run ("TestCreateWifFromPrivateKey" , func (t * testing.T ) {
485+ t .Parallel ()
486+
487+ // Create a Private Key
488+ privateKey , err := CreatePrivateKeyString ()
489+ require .NoError (t , err )
490+ require .NotNil (t , privateKey )
491+
492+ // Create a WIF
493+ var wifKey * wif.WIF
494+ wifKey , err = PrivateKeyToWif (privateKey )
495+ require .NoError (t , err )
496+ require .NotNil (t , wifKey )
497+ wifKeyString := wifKey .String ()
498+ t .Log ("WIF:" , wifKeyString )
499+ require .Equalf (t , 51 , len (wifKeyString ), "WIF should be 51 characters long, got: %d" , len (wifKeyString ))
500+
501+ // Convert WIF to Private Key
502+ var privateKeyString string
503+ privateKeyString , err = WifToPrivateKeyString (wifKeyString )
504+ require .NoError (t , err )
505+ require .NotNil (t , privateKeyString )
506+ t .Log ("Private Key:" , privateKeyString )
507+ require .Equalf (t , 64 , len (privateKeyString ), "Private Key should be 64 characters long, got: %d" , len (privateKeyString ))
508+
509+ // Compare Private Keys
510+ require .Equalf (t , privateKey , privateKeyString , "Private Key should be equal, got: %s" , privateKeyString )
511+
512+ // Decode WIF
513+ var decodedWif * wif.WIF
514+ decodedWif , err = WifFromString (wifKeyString )
515+ require .NoError (t , err )
516+ require .NotNil (t , decodedWif )
517+ require .Equalf (t , wifKeyString , decodedWif .String (), "WIF should be equal, got: %s" , decodedWif .String ())
518+ })
519+
520+ t .Run ("TestWifFromStringMissingWIF" , func (t * testing.T ) {
521+ t .Parallel ()
522+
523+ _ , err := WifFromString ("" )
524+ require .Error (t , err )
525+ require .Equal (t , ErrWifMissing , err )
526+ })
527+
528+ t .Run ("TestWifFromStringInvalidWIF" , func (t * testing.T ) {
529+ t .Parallel ()
530+
531+ _ , err := WifFromString ("invalid" )
532+ require .Error (t , err )
533+ require .Equal (t , "malformed private key" , err .Error ())
534+ })
535+ }
536+
537+ // ExampleWifFromString example using WifFromString()
538+ func ExampleWifFromString () {
539+ // Create a Private Key
540+ privateKey , err := CreatePrivateKeyString ()
541+ if err != nil {
542+ fmt .Println (err )
543+ return
544+ }
545+ fmt .Println ("Private Key Generated Length:" , len (privateKey ))
546+
547+ // Create a WIF
548+ var wifKey * wif.WIF
549+ wifKey , err = PrivateKeyToWif (privateKey )
550+ if err != nil {
551+ fmt .Println (err )
552+ return
553+ }
554+ fmt .Println ("WIF Key Generated Length:" , len (wifKey .String ()))
555+
556+ // Decode WIF
557+ var decodedWif * wif.WIF
558+ decodedWif , err = WifFromString (wifKey .String ())
559+ if err != nil {
560+ fmt .Println (err )
561+ return
562+ }
563+ fmt .Println ("WIF Key Decoded Length:" , len (decodedWif .String ()))
564+ // Output: Private Key Generated Length: 64
565+ // WIF Key Generated Length: 51
566+ // WIF Key Decoded Length: 51
567+ }
568+
569+ // BenchmarkWifFromString benchmarks the method WifFromString()
570+ func BenchmarkWifFromString (b * testing.B ) {
571+ wifKey , _ := CreateWif ()
572+ wifString := wifKey .String ()
573+ for i := 0 ; i < b .N ; i ++ {
574+ _ , _ = WifFromString (wifString )
575+ }
576+ }
0 commit comments