channel_v1

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMockSigner

func NewMockSigner() sign.Signer

Types

type ActionGateway

type ActionGateway interface {
	// AllowAction checks if a user is allowed to perform a specific gated action based on their past activity and allowances.
	AllowAction(tx action_gateway.Store, userAddress string, gatedAction core.GatedAction) error
}

type Handler

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

Handler manages channel state transitions and provides RPC endpoints for state submission.

func NewHandler

func NewHandler(
	useStoreInTx StoreTxProvider,
	memoryStore MemoryStore,
	actionGateway ActionGateway,
	nodeSigner *core.ChannelDefaultSigner,
	stateAdvancer core.StateAdvancer,
	statePacker core.StatePacker,
	nodeAddress string,
	minChallenge uint32,
	m metrics.RuntimeMetricExporter,
	maxSessionKeyIDs int,
) *Handler

NewHandler creates a new Handler instance with the provided dependencies.

func (*Handler) GetChannels

func (h *Handler) GetChannels(c *rpc.Context)

GetChannels retrieves all channels for a user with optional status/asset/type filtering and pagination.

func (*Handler) GetEscrowChannel

func (h *Handler) GetEscrowChannel(c *rpc.Context)

GetEscrowChannel retrieves current on-chain escrow channel information.

func (*Handler) GetHomeChannel

func (h *Handler) GetHomeChannel(c *rpc.Context)

GetHomeChannel retrieves current on-chain home channel information.

func (*Handler) GetLastKeyStates

func (h *Handler) GetLastKeyStates(c *rpc.Context)

GetLastKeyStates retrieves the latest channel session key states for a user with optional filtering by session key.

func (*Handler) GetLatestState

func (h *Handler) GetLatestState(c *rpc.Context)

GetLatestState retrieves the current state of the user stored on the Node.

func (*Handler) RequestCreation

func (h *Handler) RequestCreation(c *rpc.Context)

RequestCreation processes channel creation requests from users. It validates the channel definition and initial state, checks for existing channels, signs the state with the node's key, and persists the new pending state and channel.

func (*Handler) SubmitSessionKeyState

func (h *Handler) SubmitSessionKeyState(c *rpc.Context)

SubmitSessionKeyState processes channel session key state submissions for registration and updates.

func (*Handler) SubmitState

func (h *Handler) SubmitState(c *rpc.Context)

SubmitState processes user-submitted state transitions, validates them against the current state, verifies user signatures, signs the new state with the node's key, and persists changes. For transfer transitions, it automatically creates corresponding receiver states. For certain transitions (escrow lock, etc.), it schedules blockchain actions.

type MemoryStore

type MemoryStore interface {
	// IsAssetSupported checks if a given asset (token) is supported on the specified blockchain.
	IsAssetSupported(asset, tokenAddress string, blockchainID uint64) (bool, error)

	// GetAssetDecimals checks if an asset exists and returns its decimals in YN
	GetAssetDecimals(asset string) (uint8, error)

	// GetTokenDecimals returns the decimals for a token on a specific blockchain
	GetTokenDecimals(blockchainID uint64, tokenAddress string) (uint8, error)
}

type MockActionGateway

type MockActionGateway struct {
	Err error
}

func (*MockActionGateway) AllowAction

type MockAssetStore

type MockAssetStore struct {
	mock.Mock
}

MockAssetStore is a mock implementation of the core.AssetStore interface

func (*MockAssetStore) GetAssetDecimals

func (m *MockAssetStore) GetAssetDecimals(asset string) (uint8, error)

func (*MockAssetStore) GetTokenDecimals

func (m *MockAssetStore) GetTokenDecimals(blockchainID uint64, tokenAddress string) (uint8, error)

type MockMemoryStore

type MockMemoryStore struct {
	mock.Mock
}

MockMemoryStore is a mock implementation of the MemoryStore interface

func (*MockMemoryStore) GetAssetDecimals

func (m *MockMemoryStore) GetAssetDecimals(asset string) (uint8, error)

func (*MockMemoryStore) GetTokenDecimals

func (m *MockMemoryStore) GetTokenDecimals(blockchainID uint64, tokenAddress string) (uint8, error)

func (*MockMemoryStore) IsAssetSupported

func (m *MockMemoryStore) IsAssetSupported(asset, tokenAddress string, blockchainID uint64) (bool, error)

type MockSigValidator

type MockSigValidator struct {
	mock.Mock
}

MockSigValidator is a mock implementation of the SigValidator interface

func (*MockSigValidator) Verify

func (m *MockSigValidator) Verify(wallet string, data, sig []byte) error

type MockStatePacker

type MockStatePacker struct {
	mock.Mock
}

func (*MockStatePacker) PackState

func (m *MockStatePacker) PackState(state core.State) ([]byte, error)

type MockStore

type MockStore struct {
	mock.Mock
}

MockStore is a mock implementation of the Store interface

func (*MockStore) BeginTx

func (m *MockStore) BeginTx() (Store, func() error, func() error)

func (*MockStore) CheckOpenChannel

func (m *MockStore) CheckOpenChannel(wallet, asset string) (string, bool, error)

func (*MockStore) CreateChannel

func (m *MockStore) CreateChannel(channel core.Channel) error

func (*MockStore) EnsureNoOngoingStateTransitions

func (m *MockStore) EnsureNoOngoingStateTransitions(wallet, asset string) error

func (*MockStore) GetActiveHomeChannel

func (m *MockStore) GetActiveHomeChannel(wallet, asset string) (*core.Channel, error)

func (*MockStore) GetAppCount

func (m *MockStore) GetAppCount(ownerWallet string) (uint64, error)

func (*MockStore) GetChannelByID

func (m *MockStore) GetChannelByID(channelID string) (*core.Channel, error)

func (*MockStore) GetLastChannelSessionKeyStates

func (m *MockStore) GetLastChannelSessionKeyStates(wallet string, sessionKey *string) ([]core.ChannelSessionKeyStateV1, error)

func (*MockStore) GetLastChannelSessionKeyVersion

func (m *MockStore) GetLastChannelSessionKeyVersion(wallet, sessionKey string) (uint64, error)

func (*MockStore) GetLastUserState

func (m *MockStore) GetLastUserState(wallet, asset string, signed bool) (*core.State, error)

func (*MockStore) GetTotalUserStaked

func (m *MockStore) GetTotalUserStaked(wallet string) (decimal.Decimal, error)

func (*MockStore) GetUserActionCount

func (m *MockStore) GetUserActionCount(wallet string, gatedAction core.GatedAction, window time.Duration) (uint64, error)

func (*MockStore) GetUserActionCounts

func (m *MockStore) GetUserActionCounts(userWallet string, window time.Duration) (map[core.GatedAction]uint64, error)

func (*MockStore) GetUserChannels

func (m *MockStore) GetUserChannels(wallet string, status *core.ChannelStatus, asset *string, channelType *core.ChannelType, limit, offset uint32) ([]core.Channel, uint32, error)

func (*MockStore) LockUserState

func (m *MockStore) LockUserState(wallet, asset string) (decimal.Decimal, error)

func (*MockStore) RecordAction

func (m *MockStore) RecordAction(wallet string, gatedAction core.GatedAction) error

func (*MockStore) RecordTransaction

func (m *MockStore) RecordTransaction(tx core.Transaction) error

func (*MockStore) ScheduleInitiateEscrowWithdrawal

func (m *MockStore) ScheduleInitiateEscrowWithdrawal(stateID string, chainID uint64) error

func (*MockStore) StoreChannelSessionKeyState

func (m *MockStore) StoreChannelSessionKeyState(state core.ChannelSessionKeyStateV1) error

func (*MockStore) StoreUserState

func (m *MockStore) StoreUserState(state core.State) error

func (*MockStore) ValidateChannelSessionKeyForAsset

func (m *MockStore) ValidateChannelSessionKeyForAsset(wallet, sessionKey, asset, metadataHash string) (bool, error)

type SigValidator

type SigValidator interface {
	// Verify checks that the signature is valid for the given data and wallet address.
	// Returns an error if the signature is invalid or cannot be verified.
	Verify(wallet string, data, sig []byte) error
}

SigValidator validates cryptographic signatures on state transitions.

type SigValidatorType

type SigValidatorType string

SigValidatorType identifies the signature validation algorithm to use.

const EcdsaSigValidatorType SigValidatorType = "ecdsa"

EcdsaSigValidatorType represents the ECDSA (Elliptic Curve Digital Signature Algorithm) validator, used for Ethereum-style signature verification.

type Store

type Store interface {
	// LockUserState locks a user's balance for update, must be used within a transaction.
	// Returns the current balance.
	LockUserState(wallet, asset string) (decimal.Decimal, error)

	// GetLastUserState retrieves the most recent state for a user's asset.
	// If signed is true, only returns states with both user and node signatures.
	// Returns nil state if no matching state exists.
	GetLastUserState(wallet, asset string, signed bool) (*core.State, error)

	// CheckOpenChannel verifies if a user has an active channel for the given asset
	// and returns the approved signature validators if such a channel exists.
	CheckOpenChannel(wallet, asset string) (string, bool, error)

	// StoreUserState persists a new user state to the database.
	StoreUserState(state core.State) error

	// EnsureNoOngoingStateTransitions validates that no blockchain operations are pending
	// that would conflict with submitting a new state transition.
	EnsureNoOngoingStateTransitions(wallet, asset string) error

	// ScheduleInitiateEscrowWithdrawal queues a blockchain action to initiate
	// withdrawal from an escrow channel (triggered by escrow_lock transition).
	ScheduleInitiateEscrowWithdrawal(stateID string, chainID uint64) error

	// RecordTransaction creates a transaction record linking state transitions
	// to track the history of operations (deposits, withdrawals, transfers, etc.).
	RecordTransaction(tx core.Transaction) error

	// CreateChannel creates a new channel entity in the database.
	// This is called during channel creation before the channel exists on-chain.
	// The channel starts with OnChainStateVersion=0 to indicate it's pending blockchain confirmation.
	CreateChannel(channel core.Channel) error

	// GetChannelByID retrieves a channel by its unique identifier.
	// Returns nil if the channel doesn't exist.
	GetChannelByID(channelID string) (*core.Channel, error)

	// GetActiveHomeChannel retrieves the active home channel for a user's wallet and asset.
	// Returns nil if no home channel exists for the given wallet and asset.
	GetActiveHomeChannel(wallet, asset string) (*core.Channel, error)

	// GetUserChannels retrieves all channels for a user with optional status, asset, and type filters.
	GetUserChannels(wallet string, status *core.ChannelStatus, asset *string, channelType *core.ChannelType, limit, offset uint32) ([]core.Channel, uint32, error)

	// StoreChannelSessionKeyState persists a channel session key state.
	StoreChannelSessionKeyState(state core.ChannelSessionKeyStateV1) error

	// GetLastChannelSessionKeyVersion returns the latest version for a (wallet, sessionKey) pair.
	// Returns 0 if no state exists.
	GetLastChannelSessionKeyVersion(wallet, sessionKey string) (uint64, error)

	// GetLastChannelSessionKeyStates retrieves the latest channel session key states for a user,
	// optionally filtered by session key.
	GetLastChannelSessionKeyStates(wallet string, sessionKey *string) ([]core.ChannelSessionKeyStateV1, error)

	// ValidateChannelSessionKeyForAsset checks that a valid, non-expired session key state
	// exists at its latest version for the (wallet, sessionKey) pair, includes the given asset,
	// and matches the metadata hash.
	ValidateChannelSessionKeyForAsset(wallet, sessionKey, asset, metadataHash string) (bool, error)

	action_gateway.Store
}

Store defines the persistence layer interface for channel state management. All methods should be implemented to work within database transactions.

type StoreTxHandler

type StoreTxHandler func(Store) error

StoreTxHandler is a function that executes Store operations within a transaction. If the handler returns an error, the transaction is rolled back; otherwise it's committed.

type StoreTxProvider

type StoreTxProvider func(StoreTxHandler) error

StoreTxProvider wraps Store operations in a database transaction. It accepts a StoreTxHandler and manages transaction lifecycle (begin, commit, rollback). Returns an error if the handler fails or the transaction cannot be committed.

Jump to

Keyboard shortcuts

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