Skip to content

Commit

Permalink
Merge pull request #17 from libsv/feat/optional_verification
Browse files Browse the repository at this point in the history
Feat/optional verification
  • Loading branch information
theflyingcodr authored Sep 30, 2021
2 parents c46106d + d8ae780 commit 80f2df6
Show file tree
Hide file tree
Showing 129 changed files with 29,765 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor linguist-generated=true
7 changes: 4 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ linters:
- govet
- gosec
- bodyclose
- golint
- revive
- unconvert
- dupl
- maligned
Expand All @@ -349,15 +349,16 @@ linters:
- nolintlint
- goconst
- lll
- godot
disable:
- gocritic # use this for very opinionated linting
- gochecknoglobals
- whitespace
- golint
- wsl
- goerr113
- godot
- testpackage
- nestif
- goerr113
- nlreturn
- gci
disable-all: false
Expand Down
2 changes: 1 addition & 1 deletion .make/go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ install-go: ## Install the application (Using Native Go)

lint: ## Run the golangci-lint application (install if not found)
@echo "downloading golangci-lint..."
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- v1.33.0
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- v1.42.0
@echo "running golangci-lint..."
@GOGC=20 ./bin/golangci-lint run

Expand Down
6 changes: 3 additions & 3 deletions blockheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (bh *BlockHeader) Valid() bool {
}

digest := bt.ReverseBytes(crypto.Sha256d(bh.Bytes()))
var bn *big.Int = big.NewInt(0)
var bn = big.NewInt(0)
bn.SetBytes(digest)

return bn.Cmp(target) < 0
Expand Down Expand Up @@ -138,7 +138,7 @@ func ExtractMerkleRootFromBlockHeader(header string) (string, error) {
return hex.EncodeToString(bh.HashMerkleRoot), nil
}

// MarshalJSON marshals the receiving bc.BlockHeader into a JSON []byte
// MarshalJSON marshals the receiving bc.BlockHeader into a JSON []byte.
func (bh *BlockHeader) MarshalJSON() ([]byte, error) {
return json.Marshal(bhJSON{
Version: bh.Version,
Expand All @@ -150,7 +150,7 @@ func (bh *BlockHeader) MarshalJSON() ([]byte, error) {
})
}

// UnmarshalJSON unmarshals a JSON []byte into the receiving bc.BlockHeader
// UnmarshalJSON unmarshals a JSON []byte into the receiving bc.BlockHeader.
func (bh *BlockHeader) UnmarshalJSON(b []byte) error {
var bhj bhJSON
if err := json.Unmarshal(b, &bhj); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion bytemanipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (b sortByteArrays) Swap(i, j int) {
b[j], b[i] = b[i], b[j]
}

// SortByteArrays comment TODO:
// SortByteArrays comment.
func SortByteArrays(src [][]byte) [][]byte {
sorted := sortByteArrays(src)
sort.Sort(sorted)
Expand Down
4 changes: 2 additions & 2 deletions difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {
regDiff = math.Pow(2, 256) / regDiff
}

// ExpandTargetFrom comment
// ExpandTargetFrom comment.
func ExpandTargetFrom(bits string) (string, error) {
bn, err := ExpandTargetFromAsInt(bits)
if err != nil {
Expand All @@ -49,7 +49,7 @@ func ExpandTargetFrom(bits string) (string, error) {
return hex.EncodeToString(dst), nil
}

// ExpandTargetFromAsInt comment
// ExpandTargetFromAsInt comment.
func ExpandTargetFromAsInt(bits string) (*big.Int, error) {
binaryBits, err := hex.DecodeString(bits)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.15

require (
github.com/kr/pretty v0.1.0 // indirect
github.com/libsv/go-bk v0.0.0-20210430094342-ff08e691962b
github.com/libsv/go-bt/v2 v2.0.0-20210730150403-97fd3b293e05
github.com/libsv/go-bk v0.1.4
github.com/libsv/go-bt/v2 v2.0.0-beta.7
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/libsv/go-bk v0.0.0-20210430094342-ff08e691962b h1:NJkme7OR8Mz8emNPiG0Ix2gSBqZyQRasE1LVCp5SvhE=
github.com/libsv/go-bk v0.0.0-20210430094342-ff08e691962b/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI=
github.com/libsv/go-bk v0.1.4 h1:bTxlerGeibh8RRmyhFK03wSAEp6EAJxGR4vXuRT0LCE=
github.com/libsv/go-bk v0.1.4/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI=
github.com/libsv/go-bt/v2 v2.0.0-20210730150403-97fd3b293e05 h1:rRANK7Nc+bp0odO2zvDdHZBloGuwopGIx54yXypuR9k=
github.com/libsv/go-bt/v2 v2.0.0-20210730150403-97fd3b293e05/go.mod h1:62PATaSIMQ3omXVr9D4S7uMKaQByCJjNkrNzFpYThco=
github.com/libsv/go-bt/v2 v2.0.0-beta.7 h1:ccJXfmO2i+79oP/mDYYqvqa5DH1QV+2a1eh8+HUKhic=
github.com/libsv/go-bt/v2 v2.0.0-beta.7/go.mod h1:6Vk1qMlMoFJFNm7rR7NmFx4VwHNMCi4+V9WBqTd37rQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -23,7 +27,10 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
2 changes: 1 addition & 1 deletion mapicallback.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bc

// MapiCallback is the body contents posted to the provided callback url from Merchant API
// MapiCallback is the body contents posted to the provided callback url from Merchant API.
type MapiCallback struct {
CallbackPayload string `json:"callbackPayload"`
APIVersion string `json:"apiVersion"`
Expand Down
2 changes: 1 addition & 1 deletion merklebranches.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func getHashes(txHashes []string) []string {
return hashes
}

// GetMerkleBranches comment TODO:
// GetMerkleBranches comment.
func GetMerkleBranches(template []string) []string {
hashes := getHashes(template)

Expand Down
4 changes: 2 additions & 2 deletions spv/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type EnvelopeCreator interface {
CreateEnvelope(context.Context, *bt.Tx) (*Envelope, error)
}

// TxStore interfaces the a tx store
// TxStore interfaces the a tx store.
type TxStore interface {
Tx(ctx context.Context, txID string) (*bt.Tx, error)
}

// MerkleProofStore interfaces a Merkle Proof store
// MerkleProofStore interfaces a Merkle Proof store.
type MerkleProofStore interface {
MerkleProof(ctx context.Context, txID string) (*bc.MerkleProof, error)
}
Expand Down
17 changes: 17 additions & 0 deletions spv/envelope.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package spv

import (
"github.com/libsv/go-bt/v2"
"github.com/pkg/errors"

"github.com/libsv/go-bc"
)

Expand All @@ -19,3 +22,17 @@ type Envelope struct {
func (e *Envelope) IsAnchored() bool {
return e.Proof != nil
}

// HasParents returns true if this envelope has immediate parents.
func (e *Envelope) HasParents() bool {
return e.Parents == nil || len(e.Parents) == 0
}

// ParentTX will return a parent if found and convert the rawTx to a bt.TX, otherwise a ErrNotAllInputsSupplied error is returned.
func (e *Envelope) ParentTX(txID string) (*bt.Tx, error) {
env, ok := e.Parents[txID]
if !ok {
return nil, errors.Wrapf(ErrNotAllInputsSupplied, "expected parent tx %s is missing", txID)
}
return bt.NewTxFromString(env.RawTx)
}
34 changes: 25 additions & 9 deletions spv/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@ package spv
import "github.com/pkg/errors"

var (
// ErrNoTxInputs returns if an envelope is attempted to be created from a transaction that has no inputs
// ErrNoTxInputs returns if an envelope is attempted to be created from a transaction that has no inputs.
ErrNoTxInputs = errors.New("provided tx has no inputs to build envelope from")

// ErrPaymentNotVerified returns if a transaction in the tree provided was missed during verification
// ErrPaymentNotVerified returns if a transaction in the tree provided was missed during verification.
ErrPaymentNotVerified = errors.New("a tx was missed during validation")

// ErrTipTxConfirmed returns if the tip transaction is already confirmed
// ErrTipTxConfirmed returns if the tip transaction is already confirmed.
ErrTipTxConfirmed = errors.New("tip transaction must be unconfirmed")

// ErrNoConfirmedTransaction returns if a path from tip to beginning/anchor contains no confirmed transcation
// ErrNoConfirmedTransaction returns if a path from tip to beginning/anchor contains no confirmed transaction.
ErrNoConfirmedTransaction = errors.New("not confirmed/anchored tx(s) provided")

// ErrTxIDMismatch returns if they key value pair of a transactions input has a mismatch in txID
// ErrTxIDMismatch returns if they key value pair of a transactions input has a mismatch in txID.
ErrTxIDMismatch = errors.New("input and proof ID mismatch")

// ErrNotAllInputsSupplied returns if an unconfirmed transaction in envelope contains inputs which are not
// present in the parent envelope
// present in the parent envelope.
ErrNotAllInputsSupplied = errors.New("a tx input missing in parent envelope")

// ErrNoTxInputsToVerify returns if a transaction has no inputs
// ErrNoTxInputsToVerify returns if a transaction has no inputs.
ErrNoTxInputsToVerify = errors.New("a tx has no inputs to verify")

// ErrNilInitialPayment returns if a transaction has no inputs
// ErrNilInitialPayment returns if a transaction has no inputs.
ErrNilInitialPayment = errors.New("initial payment cannot be nil")

// ErrInputRefsOutOfBoundsOutput returns if a transaction has no inputs
// ErrInputRefsOutOfBoundsOutput returns if a transaction has no inputs.
ErrInputRefsOutOfBoundsOutput = errors.New("tx input index into output is out of bounds")

// ErrNoFeeQuoteSupplied is returned when VerifyFees is enabled but no bt.FeeQuote has been supplied.
ErrNoFeeQuoteSupplied = errors.New("no bt.FeeQuote supplied for fee validation, supply the bt.FeeQuote using VerifyFees opt")

// ErrFeePaidNotEnough returned when not enough fees have been paid.
ErrFeePaidNotEnough = errors.New("not enough fees paid")

// ErrCannotCalculateFeePaid returned when fee check is enabled but the tx has no parents.
ErrCannotCalculateFeePaid = errors.New("no parents supplied in envelope which means we cannot valdiate " +
"fees, either ensure parents are supplied or remove fee check")

// ErrInvalidProof is returned if the merkle proof validation fails.
ErrInvalidProof = errors.New("invalid merkle proof, payment invalid")

// ErrMissingOutput is returned when checking fees if an output in a parent tx is missing.
ErrMissingOutput = errors.New("expected output used in payment tx missing")
)
Loading

0 comments on commit 80f2df6

Please sign in to comment.