Skip to main content

buyer

import "github.com/bsv8/go-bitfs/buyer"

Package buyer implements the buyer-side BitFS v3 workflow.

A Workflow verifies and stores seller quotes, coordinates pool opening, creates signed content requests, verifies deliveries, and produces cumulative payment updates. Wallet, storage, content, transaction, and node capabilities remain application-owned and are supplied through WorkflowConfig. Callers normally accept a 001 quote, complete 002 opening, request and accept 003/004, then exchange 005 updates before using the 006 close path.

Index

type ContentRequestInput

ContentRequestInput contains the quote, pool reference, content reference, and deadline for a request.

type ContentRequestInput struct {
QuoteTermsHash bitfs.Hash32
Pool pool.Reference
SelectedArbiterPubKey []byte
Content bitfs.ContentRef
ContentSize uint64
DeliveryDeadline bitfs.UnixSeconds
}

type ContentSink

ContentSink receives verified content bytes after request and delivery validation succeeds.

type ContentSink interface {
SaveVerifiedContent(context.Context, bitfs.Hash32, []byte) error
}

type QuoteStore

QuoteStore stores complete seller-signed 001 quote credentials. Implementations must return the credential addressed by its canonical terms hash.

type QuoteStore interface {
SaveQuote(context.Context, *bitfs.SignedFileQuote) error
LoadQuote(context.Context, bitfs.Hash32) (*bitfs.SignedFileQuote, error)
}

type SeedSource

SeedSource supplies a previously verified seed so block requests can be checked against the seed's committed block-hash list.

type SeedSource interface {
LoadSeed(context.Context, bitfs.Hash32) ([]byte, error)
}

type Workflow

Workflow implements the buyer side of 001–006. It validates seller credentials, persists accepted pool/payment state, signs requests and payment updates, and delegates storage, transaction construction, and node submission to its ports.

type Workflow struct {
// contains filtered or unexported fields
}

func NewWorkflow

func NewWorkflow(config WorkflowConfig) (*Workflow, error)

NewWorkflow validates the buyer dependencies and returns a workflow. Signer, verifiers, stores, opening, participant, and transaction ports are mandatory; an omitted Clock uses time.Now so expiry checks remain injectable in tests.

func (*Workflow) AcceptDelivery

func (workflow *Workflow) AcceptDelivery(ctx context.Context, request *bitfs.SignedContentRequest, delivery *bitfs.SignedContentDelivery) (*pool.PaymentUpdate, error)

AcceptDelivery verifies the request linkage, seller signature, content hash, and size in the 004 delivery. After optional ContentSink persistence succeeds, it builds and signs the next 005 cumulative payment update.

func (*Workflow) AcceptQuote

func (workflow *Workflow) AcceptQuote(ctx context.Context, quote *bitfs.SignedFileQuote) (*bitfs.FileQuoteTerms, error)

AcceptQuote verifies the seller signature, terms, and expiry before storing the quote.

func (*Workflow) AcceptRefundPresign

func (workflow *Workflow) AcceptRefundPresign(ctx context.Context, request *pool.RefundPresignRequest, response *pool.RefundPresignResponse, fundingTx []byte) (*pool.Reference, error)

AcceptRefundPresign verifies and durably records the complete pool proof, then records RefundTx as the initial accepted payment state (sequence 2, seller amount 0). The caller may reveal fundingTx only after this method succeeds, matching the 002 message ordering.

func (*Workflow) BuildFundingTxDelivery

func (workflow *Workflow) BuildFundingTxDelivery(fundingTx []byte) (*pool.FundingTxDelivery, error)

BuildFundingTxDelivery copies and validates fundingTx into the 002 delivery container. It does not submit or persist the transaction; the seller receives it only after AcceptRefundPresign has durably recorded the refund proof.

func (*Workflow) BuildImmediateClose

func (workflow *Workflow) BuildImmediateClose(ctx context.Context, input pool.CloseInput) (*pool.UnsignedPayment, []byte, error)

BuildImmediateClose constructs the unsigned immediate-close transaction and buyer detached signature from the accepted pool state. The caller passes the result to the seller, who adds the seller signature; then the caller invokes SubmitImmediateClose to submit the merged transaction. That method persists the state only after the node accepts the final transaction.

func (*Workflow) PreparePoolOpening

func (workflow *Workflow) PreparePoolOpening(ctx context.Context, input pool.OpeningInput) (*pool.RefundPresignRequest, error)

PreparePoolOpening asks the transaction engine to build the generic 002 refund evidence. FundingTx remains caller-owned and is not submitted here.

func (*Workflow) RefundAfterExpiry

func (workflow *Workflow) RefundAfterExpiry(ctx context.Context, spendTxID pool.Hash32) (pool.Hash32, error)

RefundAfterExpiry verifies expiry, assembles the separately stored opening signatures into a broadcastable refund, and submits it. If a higher accepted cumulative state exists, the non-final node remains authoritative and this method refuses to bypass it.

func (*Workflow) RequestContent

func (workflow *Workflow) RequestContent(ctx context.Context, input ContentRequestInput) (*bitfs.SignedContentRequest, error)

RequestContent validates the quote hash, selected arbiter, content reference, size, and deadline, then signs the 003 request. It does not read content or change pool state; the seller validates and fulfills the returned credential.

func (*Workflow) SubmitImmediateClose

func (workflow *Workflow) SubmitImmediateClose(ctx context.Context, close *pool.SignedPayment) (pool.Hash32, error)

SubmitImmediateClose verifies a fully signed final state, submits it, and records the state only after the node returns the expected transaction ID.

type WorkflowConfig

WorkflowConfig supplies the buyer workflow's signer, quote/content verifiers, quote and pool stores, opening and transaction ports, and optional content and seed adapters. All required ports must be non-nil; Clock defaults to time.Now.

type WorkflowConfig struct {
Signer pool.Signer
QuoteVerifier bitfs.QuoteTermsSignatureVerifier
SignatureVerifier bitfs.ContentTermsSignatureVerifier
Clock func() time.Time
Quotes QuoteStore
Pools pool.PoolStore
Opening pool.BuyerPoolOpeningHooks
Participants pool.ParticipantVerifier
Node pool.NonFinalPoolNode
Transactions pool.BuyerPoolPort
ContentSink ContentSink
SeedSource SeedSource
}

Generated by gomarkdoc