Help us shape the future of Go! Take the 2026 Go Developer Survey

synapse

package module
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2026 License: Apache-2.0 Imports: 34 Imported by: 1

README

synapse-go

CI Release Go Reference Go Report Card License Go Version

Go SDK for Filecoin Onchain Cloud (FOC).

Status: Beta - API may change.

Docs: getting started | API reference | examples

Install

go get github.com/strahe/synapse-go

Requires Go 1.26.3+.

Quick Start

client, err := synapse.New(ctx,
    synapse.WithPrivateKeyHex("0x..."),
    synapse.WithRPCURL("https://api.calibration.node.glif.io/rpc/v1"),
    synapse.WithSource("my-app"),
)
if err != nil { return err }
defer client.Close()

// file is an io.Reader with 127 bytes to about 1 GiB of data.
upload, err := client.Storage().Upload(ctx, file, &storage.UploadOptions{Copies: 2})
if err != nil { return err }

fmt.Println("piece:", upload.PieceCID)
fmt.Printf("copies: %d/%d\n", upload.SuccessCount(), upload.RequestedCopies)
fmt.Println("retrieve:", upload.Copies[0].RetrievalURL)

Load the key from your config or secret manager. Mainnet and Calibration are detected from the RPC endpoint. Uploads wait about 3 seconds before committing so concurrent uploads can share a transaction; see Commit Batching to change this.

Package Map

Package Purpose
synapse Root client that initializes chain config, contract addresses, and services
storage Multi-provider upload/download orchestration, dataset discovery, and prepare flows
payments USDFC balances, deposits, withdrawals, approvals, and Filecoin Pay rails
costs Storage pricing, lockup, runway, and funding cost calculations
warmstorage FWSS datasets, pricing, approved-provider discovery, and termination
spregistry Storage provider registry discovery and provider/product management
sessionkey Delegated session key authorization for FWSS EIP-712 operations
chain Filecoin chain IDs, contract addresses, epochs, and token units
signer Secp256k1 and BLS signing abstractions
piece PieceCID v1/v2 calculation, parsing, and validation
filbeam FilBeam egress quota, usage stats, and CDN retrieval for FWSS datasets
pdp Low-level Curio-compatible PDP provider HTTP client

Testing

CI covers build, vet, lint, tests, and govulncheck.

Integration tests require INTEGRATION_PRIVATE_KEY in .env (needs tFIL for gas + 5 USDFC).

INTEGRATION_RPC_URL is optional.

Approximate local runtimes on Calibration:

make test                      # seconds; normal development loop
make test-integration-readonly # 30-60s; read-only Calibration checks
make test-integration-fast     # 5-10m; upload/download smoke with cleanup
make test-integration-cross    # 15-20m; full cross-package flow
make test-integration          # ~30m; final validation before merge

Development

make check   # build + vet + lint + test

License

Apache-2.0

Documentation

Overview

Package synapse provides the root Client for the Filecoin Onchain Cloud (FOC) Go SDK.

Create a client with New, passing a private key and an RPC endpoint (or an existing ethclient.Client). The client auto-detects the chain, resolves contract addresses, and eagerly initialises all sub-services before returning.

client, err := synapse.New(ctx,
    synapse.WithPrivateKeyHex("0x..."),
    synapse.WithRPCURL("https://api.calibration.node.glif.io/rpc/v1"),
    synapse.WithSource("my-app"),
)
if err != nil {
    // handle error
}
defer client.Close()

// data must contain uploadable content; PieceCIDv2 requires at least
// 127 raw bytes.
result, err := client.Storage().Upload(ctx, data, &storage.UploadOptions{Copies: 2})

WithStorageSigner can delegate Storage EIP-712 authorization signatures to an authorized key while the root key continues to determine Client.Address and remains the payer and transaction signer. Authorize the delegated address through Client.SessionKey before the first storage write; New does not check that authorization during client construction.

Sub-services are accessed via getters: Client.Storage, Client.Payments, Client.WarmStorage, Client.SPRegistry, Client.Costs, Client.FilBeam, and Client.SessionKey. Each getter returns the service instance created by New. Client.ResolvedAddresses returns the address snapshot used by those services. Read-only callers can use ResolveAddresses directly without a private key.

HTTP security

HTTP clients assembled by New reject private and reserved network destinations by default. Match rejected requests with ErrPrivateNetwork. Use WithAllowPrivateNetworks only for trusted private infrastructure. Supplying WithHTTPClient replaces these safeguards with the caller's transport policy. These root-client defaults do not change standalone pdp.New or filbeam.New clients.

Lower-level packages (chain, signer, piece, storage, payments, etc.) can still be used independently without the root client.

Example

Example shows the minimal setup for creating a synapse.Client connected to an RPC endpoint with a signing key.

package main

import (
	"context"
	"fmt"
	"log"

	synapse "github.com/strahe/synapse-go"
)

func main() {
	ctx := context.Background()

	client, err := synapse.New(ctx,
		synapse.WithRPCURL("https://api.calibration.node.glif.io/rpc/v1"),
		synapse.WithPrivateKeyHex("0x..."),
	)
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = client.Close() }()

	fmt.Println(client.Address())
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClosed = lifecycle.ErrClosed

ErrClosed is returned by service methods invoked after the owning Client has been closed via Client.Close. It is an alias of the shared closed-client sentinel and matches the per-package ErrClosed sentinels (for example, payments.ErrClosed) re-exported by every sub-service.

View Source
var ErrInvalidArgument = errors.New("synapse: invalid argument")

ErrInvalidArgument is returned when a public root-package function receives a nil, zero, or otherwise invalid caller-supplied argument.

View Source
var ErrPrivateNetwork = safehttp.ErrPrivateNetwork

ErrPrivateNetwork is returned when a root-managed HTTP client refuses to dial a private, local, multicast, unspecified, or reserved address. The Storage service exports the same sentinel as storage.ErrPrivateNetwork.

Functions

This section is empty.

Types

type Client

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

Client is the root entry point for the Filecoin Onchain Cloud SDK. It composes all sub-services, all of which are initialised eagerly by New. Create with New; release resources with Client.Close.

All methods are safe for concurrent use.

func New

func New(ctx context.Context, opts ...ClientOption) (*Client, error)

New creates a Client, connecting to the given RPC endpoint and resolving the chain and contract addresses.

Required options: a private key (WithPrivateKey or WithPrivateKeyHex). If no RPC source is provided, the client uses the selected chain's default RPC endpoint, defaulting to Calibration when no chain is selected.

func (*Client) Address

func (c *Client) Address() common.Address

Address returns the root EVM address derived from the private key.

func (*Client) Chain

func (c *Client) Chain() chain.Chain

Chain returns the resolved chain.Chain.

func (*Client) Close

func (c *Client) Close() error

Close releases resources held by the Client. If the ethclient was created internally (via WithRPCURL), it is closed. User-provided clients (via WithEthClient or WithHTTPClient) are left open. Idle connections belonging to the root-managed HTTP transport are closed. Safe to call concurrently or multiple times.

Close aborts pending upload batches rather than flushing them. Call Client.Storage().Flush before Close when accepted uploads must reach final confirmation. Close cannot retract a provider submission that already succeeded. After Close returns, the Client and all services obtained from it must not be used.

func (*Client) Costs

func (c *Client) Costs() *costs.Service

Costs returns the costs.Service.

func (*Client) FilBeam

func (c *Client) FilBeam() *filbeam.Service

FilBeam returns the filbeam.Service.

func (*Client) GetProviderInfoByAddress

func (c *Client) GetProviderInfoByAddress(ctx context.Context, addr common.Address) (*spregistry.ProviderInfo, error)

GetProviderInfoByAddress looks up a storage provider on Client.SPRegistry by its service-provider common.Address.

func (*Client) GetProviderInfoByID

func (c *Client) GetProviderInfoByID(ctx context.Context, id types.BigInt) (*spregistry.ProviderInfo, error)

GetProviderInfoByID looks up a storage provider on Client.SPRegistry by its numeric types.BigInt id.

func (*Client) Payments

func (c *Client) Payments() *payments.Service

Payments returns the payments.Service.

func (*Client) ResolvedAddresses added in v0.4.0

func (c *Client) ResolvedAddresses() ResolvedAddresses

ResolvedAddresses returns the contract-address snapshot used to initialise the Client and its services.

func (*Client) SPRegistry

func (c *Client) SPRegistry() *spregistry.Service

SPRegistry returns the spregistry.Service.

func (*Client) SessionKey

func (c *Client) SessionKey() *sessionkey.Service

SessionKey returns the sessionkey.Service.

func (*Client) Storage

func (c *Client) Storage() *storage.Service

Storage returns the storage.Service.

The service is wired with a storage.ServiceResolver that uses Client.WarmStorage and Client.SPRegistry. A per-provider PDP client is created inside the storage.ContextFactory closure whenever an immutable storage context is resolved.

func (*Client) WarmStorage

func (c *Client) WarmStorage() *warmstorage.Service

WarmStorage returns the warmstorage.Service.

type ClientOption

type ClientOption func(*clientConfig)

ClientOption configures a Client via New. Nil options are ignored.

func WithAllowPrivateNetworks

func WithAllowPrivateNetworks(allow bool) ClientOption

WithAllowPrivateNetworks disables the built-in SSRF guard for root-managed provider PDP requests, FilBeam requests, and URL-based storage.Service.Download calls. When false (the default), these clients refuse to dial local, private, multicast, unspecified, or otherwise reserved address ranges, returning ErrPrivateNetwork.

Set to true only when you knowingly need to fetch content from a private network (e.g. in-cluster storage nodes). This is an explicit SSRF opt-in for trusted private infrastructure; do not enable it for untrusted user-supplied URLs.

Root-managed clients continue to ignore environment-variable proxies when this option is true. This option has no effect when WithHTTPClient is also provided: the custom client's transport is responsible for any SSRF safeguards.

func WithCDN

func WithCDN(enabled bool) ClientOption

WithCDN sets the Client-wide default for CDN-first context downloads and the withCDN dataset-metadata flag used during provider selection.

This is a default only: storage.UploadOptions and the context-construction and selection option types carry their own *bool WithCDN override. Leaving the per-operation value nil inherits this Client default.

Example — override per upload:

b := false
_, err := client.Storage().Upload(ctx, r, &storage.UploadOptions{WithCDN: &b})

func WithChain

func WithChain(c chain.Chain) ClientOption

WithChain selects the Filecoin network. When omitted, New detects the chain from the RPC endpoint, except when both chain and RPC source are omitted; that path defaults to Calibration.

func WithEthClient

func WithEthClient(c *ethclient.Client) ClientOption

WithEthClient provides a pre-created ethclient.Client. When set, WithRPCURL is ignored and the client is NOT closed by Client.Close.

func WithFilBeamRetrievalDomain added in v0.2.0

func WithFilBeamRetrievalDomain(domain string) ClientOption

WithFilBeamRetrievalDomain overrides the chain default FilBeam retrieval domain. Leave unset for the built-in Mainnet / Calibration defaults.

func WithHTTPClient

func WithHTTPClient(c *http.Client) ClientOption

WithHTTPClient sets the HTTP client used by every service that makes HTTP calls:

  • filbeam.Service (stats API and CDN downloads)
  • storage.Service (URL-based downloads via Service.HTTPClient)
  • provider HTTP clients used for upload, pull, provider RPC calls, and manager-level relayed termination

Services communicating over Ethereum JSON-RPC (payments, sessionkey, warmstorage, spregistry, costs) reuse the chain client instead and are not affected by this option.

If nil, the root client installs HTTP clients that reject private and reserved network destinations with ErrPrivateNetwork and do not use environment-variable proxies. PDP control requests keep their 30-second timeout, FilBeam keeps no client-wide timeout, and Storage URL downloads keep their 24-hour timeout.

A non-nil client is passed through unchanged. Its transport is responsible for SSRF protection, proxy policy, redirects, and timeouts. Client.Close does not close a caller-provided client.

func WithLogger

func WithLogger(l *slog.Logger) ClientOption

WithLogger sets the structured logger passed to sub-services. Nil disables logging (the default).

func WithMaxMulticallCalls added in v0.5.0

func WithMaxMulticallCalls(max int) ClientOption

WithMaxMulticallCalls limits the number of actual contract calls in each dynamic Multicall3 request made by Payments, Storage, WarmStorage, SPRegistry, and SessionKey methods. Zero uses the default of 64. Negative values cause New to return ErrInvalidArgument.

Batches execute serially. Payments account summaries pin all reads to one block; other methods may observe different blocks. The call-count limit does not bound request or response bytes, gas, or execution time. Choose a value based on the RPC node's request and response limits, eth_call gas cap, timeout, and rate limits. Lower it when requests are rejected or time out; raise it for a self-hosted node only after testing those limits.

func WithPrivateKey

func WithPrivateKey(key *ecdsa.PrivateKey) ClientOption

WithPrivateKey sets the ECDSA private key used for transaction signing. If both WithPrivateKey and WithPrivateKeyHex are provided, WithPrivateKey takes precedence.

func WithPrivateKeyHex

func WithPrivateKeyHex(hex string) ClientOption

WithPrivateKeyHex sets the private key from a hex-encoded string. Both "0x"-prefixed and raw hex are accepted. Ignored when WithPrivateKey is also provided.

func WithRPCURL

func WithRPCURL(url string) ClientOption

WithRPCURL sets the JSON-RPC endpoint URL. An ethclient is dialed during New and closed by Client.Close.

func WithSource

func WithSource(s string) ClientOption

WithSource sets the application-level source identifier used for dataset namespace isolation. Datasets with different source values are treated as distinct namespaces; reuse only occurs within the same source.

func WithStorageSigner added in v0.5.0

func WithStorageSigner(storageSigner signer.StorageSigner) ClientOption

WithStorageSigner sets the signer used for Storage EIP-712 authorizations. The root private key remains the payer and continues to sign transactions, including payments, operator approvals, and direct storage termination via Storage().TerminateService with SkipProvider or WarmStorage().TerminateDataSet.

Before the first storage write, authorize the signer's address by calling Login or LoginWithOptions on Client.SessionKey. New does not query or validate on-chain authorization. signer.Secp256k1Signer, KMS/HSM-backed implementations, remote signers, and decorators are supported when they implement signer.StorageSigner. A nil or typed-nil signer uses the root signer.

func WithUploadBatching added in v0.6.0

func WithUploadBatching(opts ...storage.UploadBatcherOption) ClientOption

WithUploadBatching enables root-managed batching for high-level storage uploads and applies the supplied batching options. Batching is enabled by default with a three-second idle wait, no maximum wait, and four concurrent submissions. Call WithoutUploadBatching to retain immediate per-upload commits.

func WithUploadPullConcurrency added in v0.6.2

func WithUploadPullConcurrency(concurrency int) ClientOption

WithUploadPullConcurrency caps concurrent secondary copy workflows within each upload. Values <= 0 select the default of 4. A value of 1 serializes secondary pulls while still allowing commits to overlap with them.

func WithoutUploadBatching added in v0.6.0

func WithoutUploadBatching() ClientOption

WithoutUploadBatching disables root-managed upload batching. High-level storage uploads submit their commit as soon as the piece is ready.

type ContractCaller added in v0.4.0

type ContractCaller interface {
	CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
}

ContractCaller provides the contract read needed by ResolveAddresses. The root Client uses its configured Ethereum client.

type ResolvedAddresses added in v0.4.0

type ResolvedAddresses struct {
	FWSS               common.Address
	PDPVerifier        common.Address
	SPRegistry         common.Address
	USDFC              common.Address
	Payments           common.Address
	ViewContract       common.Address
	FilBeamBeneficiary common.Address
	SessionKeyRegistry common.Address
}

ResolvedAddresses is the contract topology read from FWSS. FWSS is the bootstrap address supplied to ResolveAddresses. PDPVerifier and FilBeamBeneficiary may be zero when those optional services are not configured.

func ResolveAddresses added in v0.4.0

func ResolveAddresses(ctx context.Context, caller ContractCaller, fwss common.Address) (ResolvedAddresses, error)

ResolveAddresses reads the current contract topology from FWSS. It uses the SDK's Multicall3 deployment for supported Filecoin networks and does not fall back to compiled child-contract addresses.

Directories

Path Synopsis
Package chain defines chain identifiers, contract addresses, and epoch utilities for supported Filecoin networks (Mainnet and Calibration).
Package chain defines chain identifiers, contract addresses, and epoch utilities for supported Filecoin networks (Mainnet and Calibration).
Package costs provides cost calculation for storage operations.
Package costs provides cost calculation for storage operations.
examples
download-piece command
Download-piece downloads a retrieval URL and verifies it against a PieceCID.
Download-piece downloads a retrieval URL and verifies it against a PieceCID.
list-datasets command
List-datasets shows the storage state for the current wallet.
List-datasets shows the storage state for the current wallet.
list-providers command
List-providers shows active PDP storage providers and their capabilities.
List-providers shows active PDP storage providers and their capabilities.
piece-info command
Piece-info calculates Filecoin PieceCID information for a local file.
Piece-info calculates Filecoin PieceCID information for a local file.
quickstart command
Quickstart runs the shortest complete FOC flow on Calibration: prepare funds when needed, upload data, download the first copy, and verify the bytes match.
Quickstart runs the shortest complete FOC flow on Calibration: prepare funds when needed, upload data, download the first copy, and verify the bytes match.
upload-file command
Upload-file uploads a local file to FOC storage.
Upload-file uploads a local file to FOC storage.
Package filbeam provides a client for FilBeam stats and retrieval APIs.
Package filbeam provides a client for FilBeam stats and retrieval APIs.
internal
abi
Package abi provides helpers for working with contract ABIs beyond what generated bindings offer: address auto-discovery via the FWSS root of trust, generic event extraction from receipts, and PieceCID <-> contract struct conversions.
Package abi provides helpers for working with contract ABIs beyond what generated bindings offer: address auto-discovery via the FWSS root of trust, generic event extraction from receipts, and PieceCID <-> contract struct conversions.
adapters
Package adapters contains the private glue that composes the per-service packages (warmstorage, spregistry, payments, internal/contracts/ pdpverifier) into the narrow interfaces consumed by the storage package.
Package adapters contains the private glue that composes the per-service packages (warmstorage, spregistry, payments, internal/contracts/ pdpverifier) into the narrow interfaces consumed by the storage package.
contracts
Package contracts contains go:generate directives for regenerating smart contract bindings from ABI JSON files using abigen.
Package contracts contains go:generate directives for regenerating smart contract bindings from ABI JSON files using abigen.
contracts/cmd/syncabi command
Command syncabi fetches pinned contract ABIs from the upstream FilOzone/filecoin-services repository and regenerates Go bindings under internal/contracts/.
Command syncabi fetches pinned contract ABIs from the upstream FilOzone/filecoin-services repository and regenerates Go bindings under internal/contracts/.
contracts/erc20
Package erc20 contains the generated Go bindings for the standard ERC20 token interface, used for USDFC token operations.
Package erc20 contains the generated Go bindings for the standard ERC20 token interface, used for USDFC token operations.
contracts/filpay
Package filpay contains the generated Go bindings for the Filecoin Pay smart contract.
Package filpay contains the generated Go bindings for the Filecoin Pay smart contract.
contracts/fwss
Package fwss contains the generated Go bindings for the FWSS (Filecoin Warm Storage Service) smart contract.
Package fwss contains the generated Go bindings for the FWSS (Filecoin Warm Storage Service) smart contract.
contracts/fwssview
Package fwssview contains the generated Go bindings for the FWSS StateView contract (read-only surface of FilecoinWarmStorageService).
Package fwssview contains the generated Go bindings for the FWSS StateView contract (read-only surface of FilecoinWarmStorageService).
contracts/pdpverifier
Package pdpverifier contains generated Go bindings and internal error helpers for the PDPVerifier smart contract.
Package pdpverifier contains generated Go bindings and internal error helpers for the PDPVerifier smart contract.
contracts/provideridset
Package provideridset contains the generated Go bindings for the ProviderIdSet smart contract.
Package provideridset contains the generated Go bindings for the ProviderIdSet smart contract.
contracts/sessionkeyregistry
Package sessionkeyregistry contains the generated Go bindings for the SessionKeyRegistry smart contract.
Package sessionkeyregistry contains the generated Go bindings for the SessionKeyRegistry smart contract.
contracts/spregistry
Package spregistry contains the generated Go bindings for the ServiceProviderRegistry smart contract.
Package spregistry contains the generated Go bindings for the ServiceProviderRegistry smart contract.
contracts/syncabi
Package syncabi fetches pinned contract ABIs from the upstream FilOzone/filecoin-services repository and regenerates the Go bindings under internal/contracts/.
Package syncabi fetches pinned contract ABIs from the upstream FilOzone/filecoin-services repository and regenerates the Go bindings under internal/contracts/.
idconv
Package idconv converts between SDK uint256 identifiers and generated contract binding *big.Int values.
Package idconv converts between SDK uint256 identifiers and generated contract binding *big.Int values.
integrationtest
Package integrationtest provides small helpers shared by the SDK's integration tests (files built under `//go:build integration`).
Package integrationtest provides small helpers shared by the SDK's integration tests (files built under `//go:build integration`).
lifecycle
Package lifecycle tracks whether a [synapse.Client] has been closed so that services derived from it can refuse new work with a clear error rather than making RPC calls on a closed ethclient.
Package lifecycle tracks whether a [synapse.Client] has been closed so that services derived from it can refuse new work with a clear error rather than making RPC calls on a closed ethclient.
redact
Package redact removes credentials from URLs before they are placed in errors or logs.
Package redact removes credentials from URLs before they are placed in errors or logs.
retry
Package retry provides exponential backoff retry logic for transient failures in RPC calls, HTTP requests, and transaction submissions.
Package retry provides exponential backoff retry logic for transient failures in RPC calls, HTTP requests, and transaction submissions.
safehttp
Package safehttp builds HTTP clients that reject private and reserved network destinations.
Package safehttp builds HTTP clients that reject private and reserved network destinations.
testutil
Package testutil provides internal test helpers, mock implementations, and fixture data for testing synapse-go packages.
Package testutil provides internal test helpers, mock implementations, and fixture data for testing synapse-go packages.
txutil
Package txutil provides transaction lifecycle utilities for Ethereum/FEVM transactions.
Package txutil provides transaction lifecycle utilities for Ethereum/FEVM transactions.
typeddata
Package typeddata implements EIP-712 typed data signing for storage provider authentication.
Package typeddata implements EIP-712 typed data signing for storage provider authentication.
upstream
Package upstream records pinned upstream references.
Package upstream records pinned upstream references.
Package payments provides the payment management service for Filecoin Onchain Cloud.
Package payments provides the payment management service for Filecoin Onchain Cloud.
Package pdp provides a Curio-compatible PDP provider HTTP client.
Package pdp provides a Curio-compatible PDP provider HTTP client.
Package piece provides PieceCID utilities for Filecoin content addressing.
Package piece provides PieceCID utilities for Filecoin content addressing.
Package sessionkey provides SessionKeyRegistry authorization management on Filecoin EVM.
Package sessionkey provides SessionKeyRegistry authorization management on Filecoin EVM.
Package signer provides signing abstractions for Filecoin and Ethereum transactions.
Package signer provides signing abstractions for Filecoin and Ethereum transactions.
Package spregistry provides the Storage Provider Registry service.
Package spregistry provides the Storage Provider Registry service.
Package storage provides provider selection, immutable storage targets, and single- or multi-copy upload orchestration.
Package storage provides provider selection, immutable storage targets, and single- or multi-copy upload orchestration.
Package types holds shared SDK domain types used across service packages.
Package types holds shared SDK domain types used across service packages.
Package warmstorage provides the WarmStorage (FWSS) service for managing storage contracts, data sets, and pricing.
Package warmstorage provides the WarmStorage (FWSS) service for managing storage contracts, data sets, and pricing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL