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.

50 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. pubKey := priv.PubKey()
  30. pubT, _ := pubKey.(PubKeySecp256k1)
  31. pub := pubT[:]
  32. addr := pubKey.Address()
  33. assert.Equal(t, pub, pubB, "Expected pub keys to match")
  34. assert.Equal(t, addr, addrB, "Expected addresses to match")
  35. }
  36. }
  37. func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
  38. pk, err := PubKeyFromBytes([]byte("foo"))
  39. require.NotNil(t, err, "expecting a non-nil error")
  40. require.Nil(t, pk, "expecting an empty public key on error")
  41. }