wif_test.go raw

   1  package util_test
   2  
   3  import (
   4  	"github.com/p9c/p9/pkg/chaincfg"
   5  	"testing"
   6  
   7  	"github.com/p9c/p9/pkg/ecc"
   8  	"github.com/p9c/p9/pkg/util"
   9  )
  10  
  11  func TestEncodeDecodeWIF(t *testing.T) {
  12  	priv1, _ := ecc.PrivKeyFromBytes(
  13  		ecc.S256(), []byte{
  14  			0x0c, 0x28, 0xfc, 0xa3, 0x86, 0xc7, 0xa2, 0x27,
  15  			0x60, 0x0b, 0x2f, 0xe5, 0x0b, 0x7c, 0xae, 0x11,
  16  			0xec, 0x86, 0xd3, 0xbf, 0x1f, 0xbe, 0x47, 0x1b,
  17  			0xe8, 0x98, 0x27, 0xe1, 0x9d, 0x72, 0xaa, 0x1d,
  18  		},
  19  	)
  20  	priv2, _ := ecc.PrivKeyFromBytes(
  21  		ecc.S256(), []byte{
  22  			0xdd, 0xa3, 0x5a, 0x14, 0x88, 0xfb, 0x97, 0xb6,
  23  			0xeb, 0x3f, 0xe6, 0xe9, 0xef, 0x2a, 0x25, 0x81,
  24  			0x4e, 0x39, 0x6f, 0xb5, 0xdc, 0x29, 0x5f, 0xe9,
  25  			0x94, 0xb9, 0x67, 0x89, 0xb2, 0x1a, 0x03, 0x98,
  26  		},
  27  	)
  28  	wif1, e := util.NewWIF(priv1, &chaincfg.MainNetParams, false)
  29  	if e != nil {
  30  		t.Fatal(e)
  31  	}
  32  	wif2, e := util.NewWIF(priv2, &chaincfg.TestNet3Params, true)
  33  	if e != nil {
  34  		t.Fatal(e)
  35  	}
  36  	tests := []struct {
  37  		wif     *util.WIF
  38  		encoded string
  39  	}{
  40  		{
  41  			wif1,
  42  			"6y6s1rujTdZGyZ1yitF2sFjcr4YALrhky2AwDEKXAGBKGbtc8HA",
  43  		},
  44  		{
  45  			wif2,
  46  			"cV1Y7ARUr9Yx7BR55nTdnR7ZXNJphZtCCMBTEZBJe1hXt2kB684q",
  47  		},
  48  	}
  49  	for _, test := range tests {
  50  		// Test that encoding the WIF structure matches the expected string.
  51  		s := test.wif.String()
  52  		if s != test.encoded {
  53  			t.Errorf(
  54  				"TestEncodeDecodePrivateKey failed: want '%s', got '%s'",
  55  				test.encoded, s,
  56  			)
  57  			continue
  58  		}
  59  		// Test that decoding the expected string results in the original WIF structure.
  60  		w, e := util.DecodeWIF(test.encoded)
  61  		if e != nil {
  62  			continue
  63  		}
  64  		if got := w.String(); got != test.encoded {
  65  			t.Errorf("NewWIF failed: want '%v', got '%v'", test.wif, got)
  66  		}
  67  	}
  68  }
  69