Skip to content

Commit

Permalink
add tests, update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Oct 18, 2024
1 parent cb7bd65 commit 62f3122
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions primitives/ec/publickey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
4 changes: 4 additions & 0 deletions primitives/ec/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

crypto "github.com/bitcoin-sv/go-sdk/primitives/hash"
"github.com/stretchr/testify/require"
)

type signatureTest struct {
Expand Down Expand Up @@ -702,6 +703,8 @@ func TestRFC6979(t *testing.T) {
wantSigBytes)
continue
}

require.True(t, privKey.PubKey().Verify([]byte(test.msg), gotSig))
}
}

Expand All @@ -724,4 +727,5 @@ func TestSignatureIsEqual(t *testing.T) {
t.Fatalf("value of IsEqual is incorrect, %v is not "+
"equal to %v", sig1, sig2)
}

}
30 changes: 30 additions & 0 deletions util/bytemanipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 62f3122

Please sign in to comment.