Documentation
¶
Overview ¶
Package escrow provides buyer-protection for service payments.
Flow:
- Buyer calls service → funds moved: available → escrowed
- Service delivers result → seller marks delivered
- Buyer confirms → funds moved: buyer's escrowed → seller's available
- Buyer disputes → funds moved: buyer's escrowed → buyer's available
- Timeout → auto-released to seller
Index ¶
- Constants
- Variables
- type CreateRequest
- type DisputeRequest
- type Escrow
- type Handler
- func (h *Handler) ConfirmEscrow(c *gin.Context)
- func (h *Handler) CreateEscrow(c *gin.Context)
- func (h *Handler) DisputeEscrow(c *gin.Context)
- func (h *Handler) GetEscrow(c *gin.Context)
- func (h *Handler) ListEscrows(c *gin.Context)
- func (h *Handler) MarkDelivered(c *gin.Context)
- func (h *Handler) RegisterProtectedRoutes(r *gin.RouterGroup)
- func (h *Handler) RegisterRoutes(r *gin.RouterGroup)
- type LedgerService
- type MemoryStore
- func (m *MemoryStore) Create(ctx context.Context, escrow *Escrow) error
- func (m *MemoryStore) Get(ctx context.Context, id string) (*Escrow, error)
- func (m *MemoryStore) ListByAgent(ctx context.Context, agentAddr string, limit int) ([]*Escrow, error)
- func (m *MemoryStore) ListExpired(ctx context.Context, before time.Time, limit int) ([]*Escrow, error)
- func (m *MemoryStore) Update(ctx context.Context, escrow *Escrow) error
- type Service
- func (s *Service) AutoRelease(ctx context.Context, escrow *Escrow) error
- func (s *Service) Confirm(ctx context.Context, id, callerAddr string) (*Escrow, error)
- func (s *Service) Create(ctx context.Context, req CreateRequest) (*Escrow, error)
- func (s *Service) Dispute(ctx context.Context, id, callerAddr, reason string) (*Escrow, error)
- func (s *Service) Get(ctx context.Context, id string) (*Escrow, error)
- func (s *Service) ListByAgent(ctx context.Context, agentAddr string, limit int) ([]*Escrow, error)
- func (s *Service) MarkDelivered(ctx context.Context, id, callerAddr string) (*Escrow, error)
- func (s *Service) WithRecorder(r TransactionRecorder) *Service
- type Status
- type Store
- type Timer
- type TransactionRecorder
Constants ¶
const DefaultAutoRelease = 5 * time.Minute
DefaultAutoRelease is the default time before auto-releasing to seller.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type CreateRequest ¶
type CreateRequest struct {
BuyerAddr string `json:"buyerAddr" binding:"required"`
SellerAddr string `json:"sellerAddr" binding:"required"`
Amount string `json:"amount" binding:"required"`
ServiceID string `json:"serviceId"`
SessionKeyID string `json:"sessionKeyId"`
AutoRelease string `json:"autoRelease"` // Duration string, e.g. "5m", "1h"
}
CreateRequest contains the parameters for creating an escrow.
type DisputeRequest ¶
type DisputeRequest struct {
Reason string `json:"reason" binding:"required"`
}
DisputeRequest contains the parameters for disputing an escrow.
type Escrow ¶
type Escrow struct {
ID string `json:"id"`
BuyerAddr string `json:"buyerAddr"`
SellerAddr string `json:"sellerAddr"`
Amount string `json:"amount"`
ServiceID string `json:"serviceId,omitempty"`
SessionKeyID string `json:"sessionKeyId,omitempty"`
Status Status `json:"status"`
AutoReleaseAt time.Time `json:"autoReleaseAt"`
DeliveredAt *time.Time `json:"deliveredAt,omitempty"`
ResolvedAt *time.Time `json:"resolvedAt,omitempty"`
DisputeReason string `json:"disputeReason,omitempty"`
Resolution string `json:"resolution,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Escrow represents a buyer-protection escrow record.
func (*Escrow) IsTerminal ¶
IsTerminal returns true if the escrow is in a final state.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides HTTP endpoints for escrow operations.
func NewHandler ¶
NewHandler creates a new escrow handler.
func (*Handler) ConfirmEscrow ¶
ConfirmEscrow handles POST /v1/escrow/:id/confirm
func (*Handler) CreateEscrow ¶
CreateEscrow handles POST /v1/escrow
func (*Handler) DisputeEscrow ¶
DisputeEscrow handles POST /v1/escrow/:id/dispute
func (*Handler) ListEscrows ¶
ListEscrows handles GET /v1/agents/:address/escrows
func (*Handler) MarkDelivered ¶
MarkDelivered handles POST /v1/escrow/:id/deliver
func (*Handler) RegisterProtectedRoutes ¶
func (h *Handler) RegisterProtectedRoutes(r *gin.RouterGroup)
RegisterProtectedRoutes sets up protected (auth-required) escrow routes.
func (*Handler) RegisterRoutes ¶
func (h *Handler) RegisterRoutes(r *gin.RouterGroup)
RegisterRoutes sets up public (read-only) escrow routes.
type LedgerService ¶
type LedgerService interface {
EscrowLock(ctx context.Context, agentAddr, amount, reference string) error
ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
RefundEscrow(ctx context.Context, agentAddr, amount, reference string) error
}
LedgerService abstracts ledger operations so escrow doesn't import ledger.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is an in-memory escrow store for demo/development mode.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore creates a new in-memory escrow store.
func (*MemoryStore) Create ¶
func (m *MemoryStore) Create(ctx context.Context, escrow *Escrow) error
func (*MemoryStore) ListByAgent ¶
func (*MemoryStore) ListExpired ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements escrow business logic.
func NewService ¶
func NewService(store Store, ledger LedgerService) *Service
NewService creates a new escrow service.
func (*Service) AutoRelease ¶
AutoRelease releases expired escrows to the seller.
func (*Service) ListByAgent ¶
ListByAgent returns escrows involving an agent (as buyer or seller).
func (*Service) MarkDelivered ¶
MarkDelivered marks the escrow as delivered by the seller.
func (*Service) WithRecorder ¶
func (s *Service) WithRecorder(r TransactionRecorder) *Service
WithRecorder adds a transaction recorder for reputation integration.
type Status ¶
type Status string
Status represents the state of an escrow.
const ( StatusPending Status = "pending" // Created, funds locked StatusDelivered Status = "delivered" // Seller marked service as delivered StatusReleased Status = "released" // Buyer confirmed, funds sent to seller StatusDisputed Status = "disputed" // Buyer disputed, funds returned StatusRefunded Status = "refunded" // Dispute resolved with refund StatusExpired Status = "expired" // Auto-released after timeout )
type Store ¶
type Store interface {
Create(ctx context.Context, escrow *Escrow) error
Get(ctx context.Context, id string) (*Escrow, error)
Update(ctx context.Context, escrow *Escrow) error
ListByAgent(ctx context.Context, agentAddr string, limit int) ([]*Escrow, error)
ListExpired(ctx context.Context, before time.Time, limit int) ([]*Escrow, error)
}
Store persists escrow data.