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 7 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
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
71 changes: 43 additions & 28 deletions bscript/script.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package bscript

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"strings"

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

// Sentinel errors raised by the package.
var(
ErrInvalidPKLen = errors.New("invalid public key length")
ErrInvalidOpCode = errors.New("invalid opcode data")
ErrEmptyScript = errors.New("script is empty")
ErrNotP2PKH = errors.New("not a P2PKH")
)
// Script type
type Script []byte

Expand Down Expand Up @@ -39,7 +46,7 @@ func NewFromASM(str string) (*Script, error) {
s.AppendOpCode(val)
} else {
if err := s.AppendPushDataHexString(section); err != nil {
return nil, errors.New("invalid opcode data")
return nil, ErrInvalidOpCode
}
}
}
Expand All @@ -49,10 +56,8 @@ func NewFromASM(str string) (*Script, error) {

// NewP2PKHFromPubKeyEC takes a public key hex string (in
// compressed format) and creates a P2PKH script from it.
func NewP2PKHFromPubKeyEC(pubKey *bsvec.PublicKey) (*Script, error) {

pubKeyBytes := pubKey.SerializeCompressed()

func NewP2PKHFromPubKeyEC(pubKey *bec.PublicKey) (*Script, error) {
pubKeyBytes := pubKey.SerialiseCompressed()
return NewP2PKHFromPubKeyBytes(pubKeyBytes)
}

Expand All @@ -63,18 +68,15 @@ func NewP2PKHFromPubKeyStr(pubKey string) (*Script, error) {
if err != nil {
return nil, err
}

return NewP2PKHFromPubKeyBytes(pubKeyBytes)
}

// NewP2PKHFromPubKeyBytes takes public key bytes (in
// compressed format) and creates a P2PKH script from it.
func NewP2PKHFromPubKeyBytes(pubKeyBytes []byte) (*Script, error) {

if len(pubKeyBytes) != 33 {
return nil, errors.New("invalid public key length")
return nil, ErrInvalidPKLen
}

return NewP2PKHFromPubKeyHash(crypto.Hash160(pubKeyBytes))
}

Expand Down Expand Up @@ -108,7 +110,6 @@ func NewP2PKHFromPubKeyHashStr(pubKeyHash string) (*Script, error) {
// NewP2PKHFromAddress takes an address
// and creates a P2PKH script from it.
func NewP2PKHFromAddress(addr string) (*Script, error) {

a, err := NewAddressFromString(addr)
if err != nil {
return nil, err
Expand All @@ -119,14 +120,14 @@ func NewP2PKHFromAddress(addr string) (*Script, error) {
return nil, err
}

s := &Script{}
s.AppendOpCode(OpDUP)
s.AppendOpCode(OpHASH160)
s := new(Script).
AppendOpCode(OpDUP).
AppendOpCode(OpHASH160)
if err = s.AppendPushData(publicKeyHashBytes); err != nil {
return nil, err
}
s.AppendOpCode(OpEQUALVERIFY)
s.AppendOpCode(OpCHECKSIG)
s.AppendOpCode(OpEQUALVERIFY).
AppendOpCode(OpCHECKSIG)

return s, nil
}
Expand Down Expand Up @@ -180,13 +181,13 @@ func (s *Script) AppendPushDataStrings(strs []string) error {
strBytes := []byte(str)
dataBytes = append(dataBytes, strBytes)
}

return s.AppendPushDataArray(dataBytes)
}

// AppendOpCode appends an opcode type to the script
func (s *Script) AppendOpCode(o uint8) {
func (s *Script) AppendOpCode(o uint8) *Script {
*s = append(*s, o)
return s
}

// ToString returns hex string of script.
Expand Down Expand Up @@ -233,10 +234,7 @@ func (s *Script) IsP2PK() bool {
return false
}

if len(parts) == 2 &&
len(parts[0]) > 0 &&
parts[1][0] == OpCHECKSIG {

if len(parts) == 2 && len(parts[0]) > 0 && parts[1][0] == OpCHECKSIG {
pubkey := parts[0]
version := pubkey[0]

Expand Down Expand Up @@ -269,8 +267,8 @@ func (s *Script) IsData() bool {
b[0] == 0x00 && b[1] == 0x6a
}

// IsMultisigOut returns true if this is a multisig output script.
func (s *Script) IsMultisigOut() bool {
// IsMultiSigOut returns true if this is a multisig output script.
func (s *Script) IsMultiSigOut() bool {
parts, err := DecodeParts(*s)
if err != nil {
return false
Expand Down Expand Up @@ -301,11 +299,11 @@ func isSmallIntOp(opcode byte) bool {
// PublicKeyHash returns a public key hash byte array if the script is a P2PKH script.
func (s *Script) PublicKeyHash() ([]byte, error) {
if s == nil || len(*s) == 0 {
return nil, fmt.Errorf("script is empty")
return nil, ErrEmptyScript
}

if (*s)[0] != 0x76 || (*s)[1] != 0xa9 {
return nil, fmt.Errorf("not a P2PKH")
return nil, ErrNotP2PKH
}

parts, err := DecodeParts((*s)[2:])
Expand All @@ -315,3 +313,20 @@ func (s *Script) PublicKeyHash() ([]byte, error) {

return parts[0], nil
}

// Equals will compare the script to b and return true if they match.
func (s *Script) Equals(b *Script) bool{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to add tests for these

return bytes.Equal(*s, *b)
}

// EqualsBytes will compare the script to a byte representation of a
// script, b, and return true if they match.
func (s *Script) EqualsBytes(b []byte) bool{
return bytes.Equal(*s, b)
}

// EqualsHex will compare the script to a hex string h,
// if they match then true is returned otherwise false.
func (s *Script) EqualsHex(h string) bool{
return s.ToString() == h
}
6 changes: 3 additions & 3 deletions bscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bt/bscript"
"github.com/stretchr/testify/assert"
)
Expand All @@ -29,7 +29,7 @@ func TestNewP2PKHFromPubKey(t *testing.T) {

pk, _ := hex.DecodeString("023717efaec6761e457f55c8417815505b695209d0bbfed8c3265be425b373c2d6")

pubkey, err := bsvec.ParsePubKey(pk, bsvec.S256())
pubkey, err := bec.ParsePubKey(pk, bec.S256())
assert.NoError(t, err)

scriptP2PKH, err := bscript.NewP2PKHFromPubKeyEC(pubkey)
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestScript_IsMultisigOut(t *testing.T) { // TODO: check this

scriptPub := bscript.NewFromBytes(b)
assert.NotNil(t, scriptPub)
assert.Equal(t, true, scriptPub.IsMultisigOut())
assert.Equal(t, true, scriptPub.IsMultiSigOut())
}

func TestScript_PublicKeyHash(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions bscript/unlockingscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package bscript
import (
"testing"

"github.com/bitcoinsv/bsvutil"
"github.com/libsv/go-bk/wif"
"github.com/stretchr/testify/assert"
)

func TestNewP2PKHUnlockingScript(t *testing.T) {

t.Run("unlock script with valid pubkey", func(t *testing.T) {

wif, err := bsvutil.DecodeWIF("KznvCNc6Yf4iztSThoMH6oHWzH9EgjfodKxmeuUGPq5DEX5maspS")
wif, err := wif.DecodeWIF("KznvCNc6Yf4iztSThoMH6oHWzH9EgjfodKxmeuUGPq5DEX5maspS")
assert.NoError(t, err)
assert.NotNil(t, wif)

var script *Script
script, err = NewP2PKHUnlockingScript(wif.SerializePubKey(), []byte("some-signature"), 0)
script, err = NewP2PKHUnlockingScript(wif.SerialisePubKey(), []byte("some-signature"), 0)
assert.NoError(t, err)
assert.NotNil(t, script)
assert.Equal(t, "0f736f6d652d7369676e6174757265002102798913bc057b344de675dac34faafe3dc2f312c758cd9068209f810877306d66", script.ToString())
Expand Down
2 changes: 1 addition & 1 deletion bytemanipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/hex"
"testing"

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

Expand Down
Loading