You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

  1. package crypto
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/btcsuite/btcutil/base58"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. type keyData struct {
  10. priv string
  11. pub string
  12. addr string
  13. }
  14. var secpDataTable = []keyData{
  15. {
  16. priv: "a96e62ed3955e65be32703f12d87b6b5cf26039ecfa948dc5107a495418e5330",
  17. pub: "02950e1cdfcb133d6024109fd489f734eeb4502418e538c28481f22bce276f248c",
  18. addr: "1CKZ9Nx4zgds8tU7nJHotKSDr4a9bYJCa3",
  19. },
  20. }
  21. func TestPubKeySecp256k1Address(t *testing.T) {
  22. for _, d := range secpDataTable {
  23. privB, _ := hex.DecodeString(d.priv)
  24. pubB, _ := hex.DecodeString(d.pub)
  25. addrBbz, _, _ := base58.CheckDecode(d.addr)
  26. addrB := Address(addrBbz)
  27. var priv PrivKeySecp256k1
  28. copy(priv[:], privB)
  29. pubT := priv.PubKey().(PubKeySecp256k1)
  30. pub := pubT[:]
  31. addr := priv.PubKey().Address()
  32. assert.Equal(t, pub, pubB, "Expected pub keys to match")
  33. assert.Equal(t, addr, addrB, "Expected addresses to match")
  34. }
  35. }
  36. func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
  37. pk, err := PubKeyFromBytes([]byte("foo"))
  38. require.NotNil(t, err, "expecting a non-nil error")
  39. require.Nil(t, pk, "expecting an empty public key on error")
  40. }