Documentation
ΒΆ
Overview ΒΆ
Package crec provides a unified SDK for interacting with the CREC system.
The SDK follows a resource-oriented design with sub-clients for each domain:
client, err := crec.NewClient("https://api.crec.example.com", "your-api-key")
if err != nil {
log.Fatal(err)
}
// Create a channel
channel, err := client.Channels.Create(ctx, channels.CreateInput{Name: "my-channel"})
// Sign and send an operation
op, err := client.Transact.ExecuteOperation(ctx, signer, operation, chainSelector)
// Poll for events
events, hasMore, err := client.Events.Poll(ctx, channelID, params)
Package crec provides a unified SDK for interacting with the CREC system.
Overview ΒΆ
The CREC SDK follows a resource-oriented design with a single top-level Client that provides access to sub-clients for each domain:
- github.com/smartcontractkit/crec-sdk/channels - Channel CRUD operations
- github.com/smartcontractkit/crec-sdk/events - Event polling and verification
- github.com/smartcontractkit/crec-sdk/transact - Operation signing and sending
- github.com/smartcontractkit/crec-sdk/watchers - Watcher CRUD operations
Use Client.ListNetworks to list available networks (GET /networks); no separate sub-client.
Getting Started ΒΆ
Create a new client with your API credentials:
import "github.com/smartcontractkit/crec-sdk"
client, err := crec.NewClient(
"https://api.crec.example.com",
"your-api-key",
)
if err != nil {
log.Fatal(err)
}
Working with Channels ΒΆ
Channels are logical groupings for organizing watchers, events, and operations:
// Create a channel
channel, err := client.Channels.Create(ctx, channels.CreateInput{
Name: "my-channel",
})
// List channels
channels, hasMore, err := client.Channels.List(ctx, channels.ListInput{})
// Delete a channel
err = client.Channels.Delete(ctx, channelID)
Working with Watchers ΒΆ
Watchers monitor blockchain events on specific smart contracts:
// Create a watcher with a known service
watcher, err := client.Watchers.CreateWithService(ctx, channelID, watchers.CreateWithServiceInput{
ChainSelector: "16015286601757825753",
Address: "0x...",
Service: "dvp",
Events: []string{"SettlementProposed"},
})
// Wait for watcher to become active
watcher, err = client.Watchers.WaitForActive(ctx, channelID, watcher.WatcherId, 30*time.Second)
Working with Events ΒΆ
Poll for events from a channel:
// Poll events
events, hasMore, err := client.Events.Poll(ctx, channelID, &apiClient.GetChannelsChannelIdEventsParams{
Limit: ptr(100),
})
// Verify an event's authenticity (requires crec.WithOrgID or use VerifyWithOrgID for multi-org)
valid, err := client.Events.Verify(&event)
Signing and Sending Operations ΒΆ
The Transact client provides full operation lifecycle management:
// Create an operation from transactions
operation := &types.Operation{
ID: big.NewInt(time.Now().Unix()),
Account: executorAccount,
Transactions: []types.Transaction{...},
}
// Sign and send in one step
result, err := client.Transact.ExecuteOperation(ctx, channelID, signer, operation, chainSelector)
// Or manually: sign, then send
hash, signature, err := client.Transact.SignOperation(ctx, operation, signer, chainSelector)
result, err := client.Transact.SendSignedOperation(ctx, channelID, operation, signature, chainSelector)
Configuration Options ΒΆ
Use functional options to customize the client:
client, err := crec.NewClient(
baseURL,
apiKey,
crec.WithLogger(logger),
crec.WithHTTPClient(customHTTPClient),
crec.WithOrgID("my-org-id"),
crec.WithEventVerification(2, []string{"0xSigner1", "0xSigner2", "0xSigner3"}),
crec.WithWatcherPolling(5*time.Second, 10*time.Second),
)
Using Individual Sub-Clients ΒΆ
If you only need a subset of the SDK's functionality, you can create individual sub-clients without instantiating the full Client. Use NewAPIClient to create an authenticated API client, then pass it to the sub-client you need:
import (
"github.com/smartcontractkit/crec-sdk"
"github.com/smartcontractkit/crec-sdk/channels"
"github.com/smartcontractkit/crec-sdk/watchers"
)
// Create an authenticated API client
api, err := crec.NewAPIClient("https://api.crec.example.com", "your-api-key")
if err != nil {
log.Fatal(err)
}
// Create only the sub-clients you need
channelsClient, err := channels.NewClient(&channels.Options{APIClient: api})
watchersClient, err := watchers.NewClient(&watchers.Options{APIClient: api})
This approach is useful when:
- You want to minimize dependencies in a specific package
- You're building a focused service that only uses one domain
- You need fine-grained control over sub-client configuration
Error Handling ΒΆ
All errors are wrapped with context and can be inspected using errors.Is:
if errors.Is(err, channels.ErrChannelNotFound) {
// Handle 404
}
if errors.Is(err, transact.ErrOperationNotFound) {
// Handle operation not found
}
Index ΒΆ
- Constants
- Variables
- type APIClient
- type Client
- type Option
- func WithCRETenantID(creTenantID string) Option
- func WithEventVerification(minRequiredSignatures int, validSigners []string) Option
- func WithHTTPClient(client *http.Client) Option
- func WithLogger(logger *slog.Logger) Option
- func WithOrgID(orgID string) Option
- func WithWatcherPolling(pollInterval, eventualConsistencyWindow time.Duration) Option
- func WithWorkflowOwner(workflowOwner string) Option
- func WithoutEventVerification() Option
Constants ΒΆ
const DefaultMinRequiredSignatures = 4
DefaultMinRequiredSignatures is the minimum number of valid signatures required to verify an event. Calculated as F + 1, where F is the maximum number of faulty nodes the DON can tolerate. With F+1 signatures, at least one must be from an honest node, guaranteeing the data passed consensus.
The DON transmits once this minimum is reached, so reports will never contain more than F+1 signatures. Do not increase this value unless you are certain about the implicationsβdoing so will cause signature verification to fail.
Variables ΒΆ
var ( // ErrBaseURLRequired is returned when the base URL is empty. ErrBaseURLRequired = errors.New("base URL is required") // ErrAPIKeyRequired is returned when the API key is empty. ErrAPIKeyRequired = errors.New("API key is required") // ErrInvalidEventVerificationConfig is returned when event verification is misconfigured. ErrInvalidEventVerificationConfig = errors.New("minRequiredSignatures must be > 0 when validSigners are provided") // ErrListNetworks is returned when listing networks fails. ErrListNetworks = errors.New("failed to list networks") )
Client initialization errors
var DefaultValidSigners = []string{
"0xff9b062fccb2f042311343048b9518068370f837",
"0xe55fcaf921e76c6bbcf9415bba12b1236f07b0c3",
"0x4d6cfd44f94408a39fb1af94a53c107a730ba161",
"0xde5cd1dd4300a0b4854f8223add60d20e1dfe21b",
"0xf3baa9a99b5ad64f50779f449bac83baac8bfdb6",
"0xd7f22fb5382ff477d2ff5c702cab0ef8abf18233",
"0xcdf20f8ffd41b02c680988b20e68735cc8c1ca17",
"0x4d7d71c7e584cfa1f5c06275e5d283b9d3176924",
"0x1a89c98e75983ec384ad8e83eaf7d0176eeaf155",
"0x4f99b550623e77b807df7cbed9c79d55e1163b48",
}
DefaultValidSigners contains the production Zone A workflow node public keys. These are the keys used by the DON to sign events on Ethereum Mainnet. Updated keys can be found in the chainlink-deployments repository.
These keys rarely change. When they do, update the SDK to get new defaults.
Functions ΒΆ
This section is empty.
Types ΒΆ
type APIClient ΒΆ
type APIClient = apiClient.ClientWithResponses
APIClient is the underlying HTTP client for the CREC API. This type is exported to allow users to create individual sub-clients without using the full Client.
func NewAPIClient ΒΆ
NewAPIClient creates an authenticated CREC API client that can be used to create individual sub-clients without instantiating the full Client.
This is useful when you only need a subset of the SDK's functionality.
Example:
api, err := crec.NewAPIClient("https://api.crec.example.com", "your-api-key")
if err != nil {
log.Fatal(err)
}
// Create only the channels sub-client
channelsClient, err := channels.NewClient(&channels.Options{APIClient: api})
type Client ΒΆ
type Client struct {
// Channels provides operations for managing CREC channels.
Channels *channels.Client
// Events provides operations for polling and verifying events from CREC.
Events *events.Client
// Transact provides operations for signing and sending operations to CREC.
Transact *transact.Client
// Wallets provides operations for managing CREC Smart Wallets.
Wallets *wallets.Client
// Watchers provides operations for managing CREC watchers.
Watchers *watchers.Client
// contains filtered or unexported fields
}
Client is the main entry point for the CREC SDK. It provides access to all sub-clients for interacting with different parts of the CREC system.
func NewClient ΒΆ
NewClient creates a new CREC SDK client with the provided base URL and API key.
Parameters:
- baseURL: The base URL of the CREC API (e.g., "https://api.crec.example.com")
- apiKey: The API key for authenticating with the CREC API
- opts: Optional configuration options (see Option for available options)
Returns a configured Client or an error if initialization fails.
func (*Client) ListNetworks ΒΆ
ListNetworks returns the list of available networks supported by the CREC platform. It delegates to the underlying API client (GET /networks) with no extra SDK logic.
Parameters:
- ctx: The context for the request.
Returns the list of networks, a boolean indicating if there are more results (HasMore), and an error if the request fails.
type Option ΒΆ
type Option func(*clientConfig)
Option is a functional option for configuring the Client.
func WithCRETenantID ΒΆ added in v0.6.1
WithCRETenantID sets the CRE tenant ID referring to different environments of CRE for workflow owner address derivation. Defaults to events.CreMainlineTenantID ("1") if not provided.
func WithEventVerification ΒΆ
WithEventVerification configures custom event verification settings. By default, the SDK uses DefaultValidSigners and DefaultMinRequiredSignatures. Use this option to override with custom keys or signature requirements.
Parameters:
- minRequiredSignatures: Minimum number of valid signatures required to verify an event
- validSigners: List of valid signer addresses (as hex strings)
func WithHTTPClient ΒΆ
WithHTTPClient sets a custom HTTP client for API requests. If not provided, http.DefaultClient is used.
func WithLogger ΒΆ
WithLogger sets a custom logger for the SDK. If not provided, slog.Default() is used.
func WithOrgID ΒΆ
WithOrgID sets the default organization ID for the events client. When set, [Client.Events].Verify and [Client.Events].VerifyOperationStatus can be called without passing an org ID. For multi-org use, omit this and use VerifyWithOrgID or VerifyOperationStatusWithOrgID with an explicit org ID per call.
func WithWatcherPolling ΒΆ
WithWatcherPolling configures the Watchers client polling behavior.
Parameters:
- pollInterval: Duration between polling attempts when waiting for watcher state changes
- eventualConsistencyWindow: Duration to tolerate 404 errors after creation due to eventual consistency
func WithWorkflowOwner ΒΆ
WithWorkflowOwner sets the default workflow owner address for the events client. When set, [Client.Events].VerifyWithWorkflowOwner and [Client.Events].VerifyOperationStatusWithWorkflowOwner can be called without passing the workflow owner. For multi-org or per-event workflow owner, pass it explicitly.
func WithoutEventVerification ΒΆ
func WithoutEventVerification() Option
WithoutEventVerification disables event verification entirely. Use this option if you don't need to verify event signatures.
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
Package channels provides operations for managing channels in the CREC platform.
|
Package channels provides operations for managing channels in the CREC platform. |
|
Package events provides operations for polling and verifying events from CREC.
|
Package events provides operations for polling and verifying events from CREC. |
|
extension
|
|
|
interfaces
|
|
|
mocks
|
|
|
Package parsing provides common utilities for parsing blockchain event data.
|
Package parsing provides common utilities for parsing blockchain event data. |
|
eip712
Package eip712 provides functionality for computing EIP-712 hashes and signatures for CREC operations without requiring network connectivity or API client dependencies.
|
Package eip712 provides functionality for computing EIP-712 hashes and signatures for CREC operations without requiring network connectivity or API client dependencies. |
|
signer
Package signer provides signing interfaces for the CREC SDK.
|
Package signer provides signing interfaces for the CREC SDK. |
|
signer/fireblocks
Package fireblocks provides a signer.Signer implementation using Fireblocks' custody infrastructure.
|
Package fireblocks provides a signer.Signer implementation using Fireblocks' custody infrastructure. |
|
signer/kms
Package kms provides a signer using AWS Key Management Service.
|
Package kms provides a signer using AWS Key Management Service. |
|
signer/local
Package local provides a signer using local ECDSA private keys.
|
Package local provides a signer using local ECDSA private keys. |
|
signer/privy
Package privy provides a signer using Privy's wallet-as-a-service platform.
|
Package privy provides a signer using Privy's wallet-as-a-service platform. |
|
signer/vault
Package vault provides a signer using HashiCorp Vault Transit secrets engine.
|
Package vault provides a signer using HashiCorp Vault Transit secrets engine. |
|
types
Package types provides data structures for CREC transact operations.
|
Package types provides data structures for CREC transact operations. |
|
Package wallets provides operations for managing Smart Wallets in the CREC platform.
|
Package wallets provides operations for managing Smart Wallets in the CREC platform. |
|
Package watchers provides operations for managing blockchain event watchers.
|
Package watchers provides operations for managing blockchain event watchers. |