buyer
import "github.com/bsv8/go-bitfs/buyer"
buyer 包实现 BitFS v3 的买方工作流。
Workflow 负责验证并保存卖方报价、协调费用池开池、创建已签名内容请求、验证交付内容以及生成累计支付更新。钱包、存储、内容、交易和节点能力由应用自行拥有,并通过 WorkflowConfig 注入。调用方通常先接受 001 报价并完成 002 开池,再请求和接受 003/004 消息,交换 005 更新,最后进入 006 关闭流程。
索引
- 类型 ContentRequestInput
- 类型 ContentSink
- 类型 QuoteStore
- 类型 SeedSource
- 类型 Workflow
- 函数 NewWorkflow(config WorkflowConfig) (*Workflow, error)
- 函数 (workflow *Workflow) AcceptDelivery(ctx context.Context, request *bitfs.SignedContentRequest, delivery *bitfs.SignedContentDelivery) (*pool.PaymentUpdate, error)
- 函数 (workflow *Workflow) AcceptQuote(ctx context.Context, quote *bitfs.SignedFileQuote) (*bitfs.FileQuoteTerms, error)
- 函数 (workflow *Workflow) AcceptRefundPresign(ctx context.Context, request *pool.RefundPresignRequest, response *pool.RefundPresignResponse, fundingTx []byte) (*pool.Reference, error)
- 函数 (workflow *Workflow) BuildFundingTxDelivery(fundingTx []byte) (*pool.FundingTxDelivery, error)
- 函数 (workflow *Workflow) BuildImmediateClose(ctx context.Context, input pool.CloseInput) (*pool.UnsignedPayment, []byte, error)
- 函数 (workflow *Workflow) PreparePoolOpening(ctx context.Context, input pool.OpeningInput) (*pool.RefundPresignRequest, error)
- 函数 (workflow *Workflow) RefundAfterExpiry(ctx context.Context, spendTxID pool.Hash32) (pool.Hash32, error)
- 函数 (workflow *Workflow) RequestContent(ctx context.Context, input ContentRequestInput) (*bitfs.SignedContentRequest, error)
- 函数 (workflow *Workflow) SubmitImmediateClose(ctx context.Context, close *pool.SignedPayment) (pool.Hash32, error)
- 类型 WorkflowConfig
类型 ContentRequestInput
ContentRequestInput 包含请求所使用的报价、费用池引用、内容引用和截止时间。
type ContentRequestInput struct {
QuoteTermsHash bitfs.Hash32
Pool pool.Reference
SelectedArbiterPubKey []byte
Content bitfs.ContentRef
ContentSize uint64
DeliveryDeadline bitfs.UnixSeconds
}
类型 ContentSink
ContentSink 在请求和交付验证成功后接收已验证的内容字节。
type ContentSink interface {
SaveVerifiedContent(context.Context, bitfs.Hash32, []byte) error
}
类型 QuoteStore
QuoteStore 保存完整的卖方签名 001 报价凭证。实现必须能够按规范条款哈希返回对应凭证。
type QuoteStore interface {
SaveQuote(context.Context, *bitfs.SignedFileQuote) error
LoadQuote(context.Context, bitfs.Hash32) (*bitfs.SignedFileQuote, error)
}
类型 SeedSource
SeedSource 提供此前已验证的种子,使区块请求能够依据种子承诺的区块哈希列表进行检查。
type SeedSource interface {
LoadSeed(context.Context, bitfs.Hash32) ([]byte, error)
}
类型 Workflow
Workflow 实现 001–006 的买方部分:验证卖方凭证,持久化已接受的费用池和支付状态,为请求及支付更新签名,并通过端口委托存储、交易构造和节点提交。
type Workflow struct {
// contains filtered or unexported fields
}
函数 NewWorkflow
func NewWorkflow(config WorkflowConfig) (*Workflow, error)
NewWorkflow 验证买方依赖并返回工作流。Signer、各类验证器、存储、开池端口、参与者校验器和交易端口均为必需项;未提供 Clock 时使用 time.Now,因此测试仍可注入过期时间。
函数 (*Workflow) AcceptDelivery
func (workflow *Workflow) AcceptDelivery(ctx context.Context, request *bitfs.SignedContentRequest, delivery *bitfs.SignedContentDelivery) (*pool.PaymentUpdate, error)
AcceptDelivery 验证 004 交付中的请求关联、卖方签名、内容哈希和大小。可选的 ContentSink 持久化成功后,它构造并签名下一条 005 累计支付更新。
函数 (*Workflow) AcceptQuote
func (workflow *Workflow) AcceptQuote(ctx context.Context, quote *bitfs.SignedFileQuote) (*bitfs.FileQuoteTerms, error)
AcceptQuote 在保存报价前验证卖方签名、条款和有效期。
函数 (*Workflow) AcceptRefundPresign
func (workflow *Workflow) AcceptRefundPresign(ctx context.Context, request *pool.RefundPresignRequest, response *pool.RefundPresignResponse, fundingTx []byte) (*pool.Reference, error)
AcceptRefundPresign 验证并持久化完整费用池证明,然后将 RefundTx 记录为初始已接受支付状态(序列号为 2,卖方金额为 0)。调用方只有在该方法成功后才能揭示 fundingTx,这符合 002 的消息顺序。
函数 (*Workflow) BuildFundingTxDelivery
func (workflow *Workflow) BuildFundingTxDelivery(fundingTx []byte) (*pool.FundingTxDelivery, error)
BuildFundingTxDelivery 将 fundingTx 复制并验证后放入 002 交付容器。它不会提交或持久化交易;只有 AcceptRefundPresign 已持久化退款证明后,卖方才会收到该交易。
函数 (*Workflow) BuildImmediateClose
func (workflow *Workflow) BuildImmediateClose(ctx context.Context, input pool.CloseInput) (*pool.UnsignedPayment, []byte, error)
BuildImmediateClose 根据已接受的费用池状态构造未签名的立即关闭交易和买方分离签名。调用方将结果交给卖方,由卖方添加卖方签名;随后调用方调用 SubmitImmediateClose 提交合并后的交易。该方法仅在节点接受最终交易后持久化状态。
函数 (*Workflow) PreparePoolOpening
func (workflow *Workflow) PreparePoolOpening(ctx context.Context, input pool.OpeningInput) (*pool.RefundPresignRequest, error)
PreparePoolOpening 请求交易引擎构造通用的 002 退款证据。FundingTx 仍由调用方拥有,此处不会提交该交易。
函数 (*Workflow) RefundAfterExpiry
func (workflow *Workflow) RefundAfterExpiry(ctx context.Context, spendTxID pool.Hash32) (pool.Hash32, error)
RefundAfterExpiry 验证是否到期,将分别保存的开池签名组装成可广播的退款交易并提交。如果存在更高序列的已接受累计状态,非最终状态节点仍是权威来源,该方法会拒绝绕过它。
函数 (*Workflow) RequestContent
func (workflow *Workflow) RequestContent(ctx context.Context, input ContentRequestInput) (*bitfs.SignedContentRequest, error)
RequestContent 验证报价哈希、选定仲裁人、内容引用、大小和截止时间,然后为 003 请求签名。它不会读取内容或改变费用池状态;卖方负责验证并履行返回的凭证。
函数 (*Workflow) SubmitImmediateClose
func (workflow *Workflow) SubmitImmediateClose(ctx context.Context, close *pool.SignedPayment) (pool.Hash32, error)
SubmitImmediateClose 验证已完整签名的最终状态并提交;只有节点返回预期交易 ID 后才记录该状态。
类型 WorkflowConfig
WorkflowConfig 为买方工作流提供签名器、报价/内容验证器、报价和费用池存储、开池与交易端口,以及可选的内容和种子适配器。所有必需端口都不能为 nil;Clock 默认使用 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
}
由 gomarkdoc 生成