diff --git a/CHANGELOG.md b/CHANGELOG.md index 51e8281..acaa914 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,14 +24,23 @@ All notable changes to this project will be documented in this file. The format - `pubKey.ToDER()` now returns bytes - `pubKey.ToHash()` is now `pubKey.Hash()` - `pubKey.SerializeCompressed()` is now `pubKey.Compressed()` + - `pubKey.SerializeUncompressed()` is now `pubKey.Uncompressed()` - `pubKey.SerializeHybrid()` is now `pubKey.Hybrid()` + - updated `merklepath.go` to use new helper functions from `transaction.merkletreeparent.go` ### Added + - `transaction.Verify` which uses `merklepath.Verify` and `` - `publickey.ToDERHex()` returns a hex encoded public key - `script.Chunks` helper method for `DecodeScript(scriptBytes)` - `script.PubKey` returns a `*ec.PublicKey` - `script.PubKeyHex` - `script.Address` + - file `transaction.merkletreeparent.go` which contains helper functions + - `transaction.MerkleTreeParentStr` + - `transaction.MerkleTreeParentBytes` + - `transaction.MerkleTreeParents` + - file `transaction/chaintracker/whatsonchain.go` - chain tracker for whatsonchain.com + - Example - using the whatsonchain chain tracker - Example - get address and p2pkh pubkey from script ## [1.1.9] - 2024-10-01 diff --git a/primitives/ec/publickey_test.go b/primitives/ec/publickey_test.go index 6dcd375..add3044 100644 --- a/primitives/ec/publickey_test.go +++ b/primitives/ec/publickey_test.go @@ -348,3 +348,39 @@ func TestBRC42PublicVectors(t *testing.T) { }) } } + +func TestToDERHex(t *testing.T) { + for _, test := range pubKeyTests { + pk, err := ParsePubKey(test.key) + if err != nil { + if test.isValid { + t.Errorf("%s pubkey failed when shouldn't %v", + test.name, err) + } + continue + } + derHex := pk.ToDERHex() + if derHex != hex.EncodeToString(pk.ToDER()) { + t.Errorf("%s pubkey: ToDERHex does not match ToDER.", + test.name) + } + } +} + +func TestToDER(t *testing.T) { + for _, test := range pubKeyTests { + pk, err := ParsePubKey(test.key) + if err != nil { + if test.isValid { + t.Errorf("%s pubkey failed when shouldn't %v", + test.name, err) + } + continue + } + der := pk.ToDER() + if !bytes.Equal(der, pk.ToDER()) { + t.Errorf("%s pubkey: ToDER does not match itself.", + test.name) + } + } +} diff --git a/primitives/ec/signature_test.go b/primitives/ec/signature_test.go index 062816b..e94bbe2 100644 --- a/primitives/ec/signature_test.go +++ b/primitives/ec/signature_test.go @@ -14,6 +14,7 @@ import ( "testing" crypto "github.com/bitcoin-sv/go-sdk/primitives/hash" + "github.com/stretchr/testify/require" ) type signatureTest struct { @@ -702,6 +703,8 @@ func TestRFC6979(t *testing.T) { wantSigBytes) continue } + + require.True(t, privKey.PubKey().Verify([]byte(test.msg), gotSig)) } } @@ -724,4 +727,5 @@ func TestSignatureIsEqual(t *testing.T) { t.Fatalf("value of IsEqual is incorrect, %v is not "+ "equal to %v", sig1, sig2) } + } diff --git a/util/bytemanipulation_test.go b/util/bytemanipulation_test.go index e27bc12..d39a62a 100644 --- a/util/bytemanipulation_test.go +++ b/util/bytemanipulation_test.go @@ -119,3 +119,33 @@ func TestReverseBytes(t *testing.T) { ) }) } + +func TestReverseBytesInPlace(t *testing.T) { + t.Parallel() + + t.Run("Empty slice", func(t *testing.T) { + input := []byte{} + util.ReverseBytesInPlace(input) + require.Equal(t, []byte{}, input) + }) + + t.Run("Single byte", func(t *testing.T) { + input := []byte{0x01} + util.ReverseBytesInPlace(input) + require.Equal(t, []byte{0x01}, input) + }) + + t.Run("Multiple bytes", func(t *testing.T) { + input := []byte{0x01, 0x02, 0x03, 0x04} + expected := []byte{0x04, 0x03, 0x02, 0x01} + util.ReverseBytesInPlace(input) + require.Equal(t, expected, input) + }) + + t.Run("Odd number of bytes", func(t *testing.T) { + input := []byte{0x01, 0x02, 0x03, 0x04, 0x05} + expected := []byte{0x05, 0x04, 0x03, 0x02, 0x01} + util.ReverseBytesInPlace(input) + require.Equal(t, expected, input) + }) +}