-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from libsv/enhancement/header_client
- Loading branch information
Showing
7 changed files
with
98 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
package bc | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
var ( | ||
// ErrHeaderNotFound can be returned if the blockHash isn't found on the network. | ||
ErrHeaderNotFound = errors.New("header with not found") | ||
// ErrNotOnLongestChain indicates the blockhash is present but isn't on the longest current chain. | ||
ErrNotOnLongestChain = errors.New("header exists but is not on the longest chain") | ||
) | ||
|
||
// A BlockHeaderChain is a generic interface used to map things in the block header chain | ||
// (chain of block headers). For example, it is used to get a block Header from a bitcoin | ||
// block hash if it exists in the longest block header chain. | ||
// | ||
// Errors can be returned if the header isn't found or is on a stale chain, you may also use the | ||
// ErrHeaderNotFound & ErrNotOnLongestChain sentinel errors when implementing the interface. | ||
type BlockHeaderChain interface { | ||
BlockHeader(ctx context.Context, blockHash string) (blockHeader string, err error) | ||
} | ||
BlockHeader(ctx context.Context, blockHash string) (*BlockHeader, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
package bc | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// An SPVClient is a struct used to specify interfaces | ||
// used to complete Simple Payment Verification (SPV) | ||
// in conjunction with a Merkle Proof. | ||
// | ||
// The implementation of BlockHeaderChain which is supplied will depend on the client | ||
// you are using, some may return a HeaderJSON response others may return the blockhash. | ||
type SPVClient struct { | ||
// BlockHeaderChain will be set when an implementation returning a bc.BlockHeader type is provided. | ||
bhc BlockHeaderChain | ||
} | ||
|
||
// NewSPVClient creates a new SPVClient based on params | ||
// passed or will use defaults if nil is passed. | ||
func NewSPVClient(bhc BlockHeaderChain) *SPVClient { | ||
if bhc == nil { | ||
return &SPVClient{} | ||
} | ||
// SPVOpts can be implemented to provided functional options for an SPVClient. | ||
type SPVOpts func(*SPVClient) | ||
|
||
return &SPVClient{ | ||
bhc: bhc, | ||
// WithBlockHeaderChain will inject the provided BlockHeaderChain into the SPVClient. | ||
func WithBlockHeaderChain(bhc BlockHeaderChain) SPVOpts { | ||
return func(s *SPVClient) { | ||
s.bhc = bhc | ||
} | ||
} | ||
|
||
// NewSPVClient creates a new SPVClient based on the options provided. | ||
// If no BlockHeaderChain implementation is provided, the setup will return an error. | ||
func NewSPVClient(opts ...SPVOpts) (*SPVClient, error) { | ||
cli := &SPVClient{} | ||
for _, opt := range opts { | ||
opt(cli) | ||
} | ||
if cli.bhc == nil { | ||
return nil, errors.New("at least one blockchain header implementation should be returned") | ||
} | ||
return cli, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters