Documentation
¶
Overview ¶
Package chargeserver contains the Tempo charge server method and verifier. It is usually imported as charge alongside the generic HTTP 402 flow in package server.
Package chargeserver verifies Tempo charge Credentials and builds Tempo charge Challenges for MPP HTTP servers.
Index ¶
- type Config
- type FeePayerPolicy
- type Intent
- func (i *Intent) Broadcast(ctx context.Context, credential *mpp.Credential, requestMap map[string]any) (*mpp.Receipt, error)
- func (i *Intent) Name() string
- func (i *Intent) Validate(ctx context.Context, credential *mpp.Credential, requestMap map[string]any) (*mppserver.Validation, error)
- func (i *Intent) Verify(ctx context.Context, credential *mpp.Credential, requestMap map[string]any) (*mpp.Receipt, error)
- type IntentConfig
- type Method
- type MethodConfig
- type RelayConfig
- type RelayErrorCode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Intent verifies Tempo charge credentials for this method.
Intent *Intent
// Currency restricts issued challenges to exactly this token contract
// address. It cannot be combined with Currencies.
//
// Deprecated: Use Currencies with a single element.
Currency string
// Currencies lists the accepted token contract addresses in presentation
// order; see MethodConfig.Currencies.
Currencies []string
// Recipient is the default payee address for issued challenges.
Recipient string
// Decimals controls how human-readable amounts are normalized.
Decimals int
// ChainID binds issued challenges to a specific Tempo chain when set.
ChainID int64
// FeePayer enables sponsored transaction flows by default.
FeePayer bool
// FeePayerURL points at a remote co-signer when the server does not sign locally.
FeePayerURL string
// SupportedModes limits the credential submission modes advertised to clients.
SupportedModes []tempo.ChargeMode
// RPC overrides the Tempo JSON-RPC client used for verification.
RPC tempo.RPCClient
// RPCURL is used to build an RPC client when RPC is nil.
RPCURL string
// FeePayerSigner co-signs sponsored transactions locally when provided.
FeePayerSigner *temposigner.Signer
// FeePayerPrivateKey constructs FeePayerSigner when FeePayerSigner is nil.
FeePayerPrivateKey string
// FeePayerPrivateKeyEnv loads the fee-payer key from an environment variable when FeePayerPrivateKey is empty.
FeePayerPrivateKeyEnv string
// FeePayerPolicies allowlists the fee tokens this verifier will sponsor.
FeePayerPolicies map[string]FeePayerPolicy
// FeeToken fixes the fee token a local fee payer pays gas in; see IntentConfig.FeeToken.
FeeToken string
// Store is required to verify payments. Use a persistent store shared by all
// replicas, with atomic PutIfAbsent and no eviction of replay keys.
// An explicit tempo.NewMemoryStore() is only suitable for single-process development.
Store tempo.Store
// ExpireReplayKeys lets replay keys expire after their challenge; see
// IntentConfig.ExpireReplayKeys for when that is safe.
ExpireReplayKeys bool
// Relay delegates credential validation and finalization to Tempo API or a compatible MPP relay.
Relay *RelayConfig
}
Config combines the method defaults and intent verification settings used by the high-level Tempo server constructor.
type FeePayerPolicy ¶
type FeePayerPolicy struct {
MaxFeePerGas *big.Int
MaxPriorityFeePerGas *big.Int
MaxTotalFee *big.Int
// RejectKeyAuthorization rejects sponsored transactions that install a new
// access key (carry a keyAuthorization). The default allows them.
RejectKeyAuthorization bool
}
FeePayerPolicy configures sponsored transaction limits for one TIP-20 fee token.
type Intent ¶
type Intent struct {
// contains filtered or unexported fields
}
Intent verifies Tempo charge Credentials and returns Receipts.
func NewIntent ¶
func NewIntent(config IntentConfig) (*Intent, error)
NewIntent constructs a Tempo charge verifier.
func (*Intent) Broadcast ¶ added in v0.4.0
func (i *Intent) Broadcast( ctx context.Context, credential *mpp.Credential, requestMap map[string]any, ) (*mpp.Receipt, error)
Broadcast revalidates and settles a Tempo charge Credential.
func (*Intent) Validate ¶ added in v0.4.0
func (i *Intent) Validate( ctx context.Context, credential *mpp.Credential, requestMap map[string]any, ) (*mppserver.Validation, error)
Validate checks a Tempo charge Credential without consuming replay state, signing a sponsored transaction, or broadcasting.
type IntentConfig ¶
type IntentConfig struct {
// RPC overrides the Tempo JSON-RPC client used for verification.
RPC tempo.RPCClient
// RPCURL is used to build an RPC client when RPC is nil.
RPCURL string
// FeePayerSigner co-signs sponsored transactions locally when provided.
FeePayerSigner *temposigner.Signer
// FeePayerPrivateKey constructs FeePayerSigner when FeePayerSigner is nil.
FeePayerPrivateKey string
// FeePayerPrivateKeyEnv loads the fee-payer key from an environment variable when FeePayerPrivateKey is empty.
FeePayerPrivateKeyEnv string
// FeePayerPolicies allowlists the fee tokens this verifier will sponsor.
// With a local fee payer, the configured tokens are the fee tokens it may
// pay gas in (tried in address order), independent of the charge currency.
// When empty, a local fee payer may use pathUSD and, on mainnet, USDC.e.
// Remote fee payers (FeePayerURL) pay in the charge currency, which needs
// a policy here.
FeePayerPolicies map[string]FeePayerPolicy
// FeeToken fixes the fee token a local fee payer pays gas in. When empty,
// the local fee payer uses the first allowed fee token it holds a balance
// of, or the first allowed fee token. It must be an allowed fee token and
// is ignored for remote fee payers (FeePayerURL).
FeeToken string
// Store is required to verify payments. Use a persistent store shared by all
// replicas, with atomic PutIfAbsent and no eviction of replay keys.
// An explicit tempo.NewMemoryStore() is only suitable for single-process development.
Store tempo.Store
// ExpireReplayKeys lets replay keys expire ten minutes after their
// challenge's expiry instead of keeping them forever. Store must implement
// tempo.ExpiringStore, whose PutIfAbsentUntil then replaces PutIfAbsent
// for replay claims. It is only safe when every credential reaches this
// Intent through server.Mpp, which verifies the challenge ID's HMAC,
// including its expiry, before settlement: Verify and Broadcast called
// directly trust the credential's echoed expiry, so an expired key would
// let the same hash, proof or transaction settle again.
ExpireReplayKeys bool
}
IntentConfig configures Tempo charge verification.
type Method ¶
type Method struct {
// contains filtered or unexported fields
}
Method adapts Tempo charge configuration to the generic server interfaces.
func MethodFromConfig ¶
MethodFromConfig constructs a Tempo charge method from one config struct. It returns an error when the currency configuration is invalid.
func NewMethod ¶
func NewMethod(config MethodConfig) *Method
NewMethod builds a Tempo server method with request defaults.
Without Currency or Currencies, the method accepts tempo.DefaultCurrenciesForChain for the configured chain, or for the chain inferred from the intent's RPC URL when ChainID is zero. With no custom RPC, it defaults to mainnet. NewMethod panics when Intent lacks replay storage or the currency configuration is invalid (see MethodConfig.Currencies); use MethodFromConfig to receive the error instead.
func (*Method) BuildChargeRequest ¶
BuildChargeRequest normalizes server charge parameters into Tempo request data for a single currency: ChargeParams.Currency when set, otherwise the first accepted currency.
func (*Method) BuildChargeRequests ¶ added in v0.4.0
BuildChargeRequests returns one normalized Tempo charge request per accepted currency, in presentation order. A per-request ChargeParams.Currency overrides the configured currencies and yields a single request.
type MethodConfig ¶
type MethodConfig struct {
// Intent verifies Tempo charge credentials for this method.
Intent *Intent
// Currency restricts issued challenges to exactly this token contract
// address. It cannot be combined with Currencies.
//
// Deprecated: Use Currencies with a single element.
Currency string
// Currencies lists the accepted token contract addresses in presentation
// order. The server issues one charge Challenge per currency, and a
// Credential for any of them verifies. An explicit list replaces the chain
// defaults from tempo.DefaultCurrenciesForChain; it must contain at least
// one valid 0x-prefixed address, and case-insensitive duplicates are
// dropped. It cannot be combined with Currency.
Currencies []string
// Recipient is the default payee address for issued challenges.
Recipient string
// Decimals controls how human-readable amounts are normalized.
Decimals int
// ChainID binds issued challenges to a specific Tempo chain when set.
ChainID int64
// FeePayer enables sponsored transaction flows by default.
FeePayer bool
// FeePayerURL points at a remote co-signer when the server does not sign locally.
FeePayerURL string
// SupportedModes limits the credential submission modes advertised to clients.
SupportedModes []tempo.ChargeMode
}
MethodConfig configures a Tempo payment method for server-side charging.
type RelayConfig ¶ added in v0.4.0
type RelayConfig struct {
// APIKey is a Tempo API key with the mpp:write scope.
APIKey string
// APIBaseURL is the relay base URL, including an optional path prefix.
// It defaults to https://api.tempo.xyz.
APIBaseURL string
// HTTPClient overrides the client used for relay calls. When nil, relay
// calls use a client with a 30 second timeout.
HTTPClient *http.Client
}
RelayConfig configures Tempo API or a compatible MPP relay.
type RelayErrorCode ¶ added in v0.4.0
type RelayErrorCode string
RelayErrorCode is a stable machine-readable relay failure code.
const ( // RelayErrorAlreadyUsed means the credential or transaction was already used. RelayErrorAlreadyUsed RelayErrorCode = "already_used" // RelayErrorBroadcastFailed means transaction broadcast or confirmation failed. RelayErrorBroadcastFailed RelayErrorCode = "broadcast_failed" // RelayErrorExpired means the challenge or payment authorization expired. RelayErrorExpired RelayErrorCode = "expired" // RelayErrorInvalidPayment means the payment payload or requirements are invalid. RelayErrorInvalidPayment RelayErrorCode = "invalid_payment" // RelayErrorInsufficientFunds means the sender lacks funds. RelayErrorInsufficientFunds RelayErrorCode = "insufficient_funds" // RelayErrorPolicyDenied means the payment violates relay or account policy. RelayErrorPolicyDenied RelayErrorCode = "policy_denied" // RelayErrorScreenRejected means an originator or recipient failed screening. RelayErrorScreenRejected RelayErrorCode = "screen_rejected" // RelayErrorSimulationFailed means transaction simulation failed. RelayErrorSimulationFailed RelayErrorCode = "simulation_failed" RelayErrorTemporarilyUnavailable RelayErrorCode = "temporarily_unavailable" // RelayErrorUnsupported means the method, intent, network, or asset is unsupported. RelayErrorUnsupported RelayErrorCode = "unsupported" // RelayErrorUnknown means the relay encountered an unexpected failure. RelayErrorUnknown RelayErrorCode = "unknown" )