Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bec v2 #20

Merged
merged 22 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ linters-settings:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
locale: UK
ignore-words:
- bsv
- bitcoin
Expand Down Expand Up @@ -347,7 +347,6 @@ linters:
- exhaustive
- sqlclosecheck
- nolintlint
- gci
- goconst
- lll
disable:
Expand All @@ -360,6 +359,7 @@ linters:
- testpackage
- nestif
- nlreturn
- gci
disable-all: false
presets:
- bugs
Expand Down
31 changes: 19 additions & 12 deletions bscript/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import (
"encoding/hex"
"fmt"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/bitcoinsv/bsvutil/base58"
"github.com/libsv/go-bt/crypto"
"github.com/libsv/go-bk/base58"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/crypto"
)

const (
hashP2PKH = 0x00
hashTestNetP2PKH = 0x6f
hashP2SH = 0x05 // TODO: remove deprecated p2sh stuff
hashTestNetP2SH = 0xc4
)

// An Address struct contains the address string as well as the hash160 hexstring of the public key.
Expand Down Expand Up @@ -38,17 +45,16 @@ func addressToPubKeyHashStr(address string) (string, error) {
}

switch decoded[0] {
case 0x00: // Pubkey hash (P2PKH address)
case hashP2PKH: // Pubkey hash (P2PKH address)
return hex.EncodeToString(decoded[1 : len(decoded)-4]), nil

case 0x6f: // Testnet pubkey hash (P2PKH address)
case hashTestNetP2PKH: // Testnet pubkey hash (P2PKH address)
return hex.EncodeToString(decoded[1 : len(decoded)-4]), nil

case 0x05: // Script hash (P2SH address)
case hashP2SH: // Script hash (P2SH address)
fallthrough
case 0xc4: // Testnet script hash (P2SH address)
case hashTestNetP2SH: // Testnet script hash (P2SH address)
fallthrough

default:
return "", fmt.Errorf("address %s is not supported", address)
}
Expand Down Expand Up @@ -78,6 +84,7 @@ func NewAddressFromPublicKeyHash(hash []byte, mainnet bool) (*Address, error) {
bb[0] = 111
}

// nolint: makezero // this needs init this way
bb = append(bb, hash...)

return &Address{
Expand All @@ -86,11 +93,11 @@ func NewAddressFromPublicKeyHash(hash []byte, mainnet bool) (*Address, error) {
}, nil
}

// NewAddressFromPublicKey takes a bsvec public key and returns an Address struct pointer.
// NewAddressFromPublicKey takes a bec public key and returns an Address struct pointer.
// If mainnet parameter is true it will return a mainnet address (starting with a 1).
// Otherwise (mainnet is false) it will return a testnet address (starting with an m or n).
func NewAddressFromPublicKey(pubKey *bsvec.PublicKey, mainnet bool) (*Address, error) {
hash := crypto.Hash160(pubKey.SerializeCompressed())
func NewAddressFromPublicKey(pubKey *bec.PublicKey, mainnet bool) (*Address, error) {
hash := crypto.Hash160(pubKey.SerialiseCompressed())

// regtest := 111
// mainnet: 0
Expand All @@ -99,7 +106,7 @@ func NewAddressFromPublicKey(pubKey *bsvec.PublicKey, mainnet bool) (*Address, e
if !mainnet {
bb[0] = 111
}

// nolint: makezero // this needs init this way
bb = append(bb, hash...)

return &Address{
Expand Down
6 changes: 3 additions & 3 deletions bscript/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bt/bscript"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -88,8 +88,8 @@ func TestNewAddressFromPublicKey(t *testing.T) {
pubKeyBytes, err := hex.DecodeString("026cf33373a9f3f6c676b75b543180703df225f7f8edbffedc417718a8ad4e89ce")
assert.NoError(t, err)

var pubKey *bsvec.PublicKey
pubKey, err = bsvec.ParsePubKey(pubKeyBytes, bsvec.S256())
var pubKey *bec.PublicKey
pubKey, err = bec.ParsePubKey(pubKeyBytes, bec.S256())
assert.NoError(t, err)
assert.NotNil(t, pubKey)

Expand Down
2 changes: 1 addition & 1 deletion bscript/addressvalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"strings"

"github.com/libsv/go-bt/crypto"
"github.com/libsv/go-bk/crypto"
)

type a25 [25]byte
Expand Down
2 changes: 1 addition & 1 deletion bscript/bip276.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"regexp"
"strconv"

"github.com/libsv/go-bt/crypto"
"github.com/libsv/go-bk/crypto"
)

// PrefixScript is the prefix in the BIP276 standard which
Expand Down
38 changes: 26 additions & 12 deletions bscript/oppushdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,51 @@ func DecodeParts(b []byte) ([][]byte, error) {
if len(b) < 2 {
return r, errors.New("not enough data")
}
l := uint64(b[1])
if len(b) < int(2+l) {

l := int(b[1])
b = b[2:]

if len(b) < l {
return r, errors.New("not enough data")
}
part := b[2 : 2+l]

part := b[:l]
r = append(r, part)
b = b[2+l:]
b = b[l:]

case OpPUSHDATA2:
if len(b) < 3 {
return r, errors.New("not enough data")
}
l := binary.LittleEndian.Uint16(b[1:])
if len(b) < int(3+l) {

l := int(binary.LittleEndian.Uint16(b[1:]))

b = b[3:]

if len(b) < l {
return r, errors.New("not enough data")
}
part := b[3 : 3+l]

part := b[:l]
r = append(r, part)
b = b[3+l:]
b = b[l:]

case OpPUSHDATA4:
if len(b) < 5 {
return r, errors.New("not enough data")
}
l := binary.LittleEndian.Uint32(b[1:])
if len(b) < int(5+l) {

l := int(binary.LittleEndian.Uint32(b[1:]))

b = b[5:]

if len(b) < l {
return r, errors.New("not enough data")
}
part := b[5 : 5+l]

part := b[:l]
r = append(r, part)
b = b[5+l:]
b = b[l:]

default:
if b[0] >= 0x01 && b[0] <= OpPUSHDATA4 {
Expand Down
25 changes: 25 additions & 0 deletions bscript/oppushdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ func TestDecodeParts(t *testing.T) {
assert.Equal(t, 0, len(decoded))
})

t.Run("invalid decode using OP_PUSHDATA2 - overflow", func(t *testing.T) {

b := make([]byte, 0)
b = append(b, bscript.OpPUSHDATA2)
b = append(b, 0xff)
b = append(b, 0xff)

bigScript := make([]byte, 0xffff)

b = append(b, bigScript...)

t.Logf("Script len is %d", len(b))

defer func() {
if r := recover(); r != nil {
t.Errorf("Panic detected: %v", r)
}
}()

_, err := bscript.DecodeParts(b)
if err != nil {
t.Error(err)
}
})

t.Run("invalid decode using OP_PUSHDATA4 - payload too small", func(t *testing.T) {

data := "testing the code OP_PUSHDATA4"
Expand Down
Loading