import { UpdateChannelParams, CreateChannelParams, SyncChannelParams, JoinChannelParams, CloseChannelParams, GetStateParams, Participant as APIParticipant, ChannelId, Message, ChannelResult } from '@statechannels/client-api-schema'; import { Address as CoreAddress, PrivateKey } from '@statechannels/wallet-core'; import * as Either from 'fp-ts/lib/Either'; import Knex from 'knex'; import { Logger } from 'pino'; import { ValidationErrorItem } from 'joi'; import { Bytes32 } from '../type-aliases'; import { ChainRequest } from '../chain-service'; import { ObjectiveManager } from '../objectives'; import { LedgerManager } from '../protocols/ledger-manager'; import { WalletObjective } from '../models/objective'; import { Store } from './store'; import { SingleChannelOutput, MultipleChannelOutput, EngineInterface, EngineConfig } from './types'; export declare class ConfigValidationError extends Error { errors: ValidationErrorItem[]; constructor(errors: ValidationErrorItem[]); } /** * A single-threaded Nitro engine */ export declare class SingleThreadedEngine implements EngineInterface { private config; protected logger: Logger; knex: Knex; store: Store; chainNetworkId: string; objectiveManager: ObjectiveManager; ledgerManager: LedgerManager; static create(engineConfig: EngineConfig, logger: Logger): Promise; /** * Protected method. Initialize engine via Engine.create(..) * @readonly */ protected constructor(config: EngineConfig, logger: Logger); /** * Adds an ethereum private key to the engine's database * * @remarks * * This key will be used to sign state channel upates. * If a key is not added, a random key will be generated the first time it is required. * If a private key already exists, calling this function wil be a no-op. * * @param privateKey - An ethereum private key * @returns A promise that resolves when the key has been successfully added. */ addSigningKey(privateKey: PrivateKey): Promise; /** * Stores the supplied ForceMoveApp definition bytecode against the supplied blockchain address. * * @remarks * Storing the bytecode is necessary for the engine to verify ForceMoveApp transitions. * * @param appDefinition - An ethereum address where ForceMoveApp rules are deployed. * @param bytecode - The bytecode at that address. * @returns A promise that resolves when the bytecode has been successfully stored. */ registerAppBytecode(appDefinition: string, bytecode: string): Promise; /** * Destroy this engine instance * * @remarks * Removes listeners from the chainService and destroys the engine's database connection. * * @returns A promise that resolves when the engine has been destroyed. */ destroy(): Promise; /** * Trigger a response containing a message for counterparties, with all stored states for each a set of channels. * * @param channelIds - List of channel ids to be sync'ed * @returns A promise that resolves to an object containing the messages. */ syncChannels(channelIds: Bytes32[]): Promise; /** * Trigger a response containing a message for counterparties, with all objectives and channels for a set of objectives. * @param objectiveIds The ids of the objectives that should be sent to the counterparties * @returns A promise that resolves to an object containing the messages. */ syncObjectives(objectiveIds: string[]): Promise; /** * Trigger a response containing a message for all counterparties, with all stored states for a given channel. * * @param channelId - The channel id to be sync'ed * @returns A promise that resolves to an object containing the messages. */ syncChannel({ channelId }: SyncChannelParams): Promise; private _syncChannel; /** * Trigger an on-chain challenge. * * @param challengeState - The state to raise the challenge with. * @returns A promise that resolves to a channel output. */ challenge(channelId: string): Promise; /** * Get the signing address for this engine, or create it if it does not exist. * * @returns A promise that resolves to the address. */ getSigningAddress(): Promise; /** * Creates a ledger channel. * * @remarks * The channel will have a null app definition and null app data. * This method is otherwise identical to {@link SingleThreadedEngine.createChannel}. * * @returns A promise that resolves to the channel output. */ createLedgerChannel(args: Pick, fundingStrategy?: 'Direct' | 'Fake'): Promise; /** * Creates a channel. * * @remarks * The channel's nonce will be automatically chosen. * The channel will be registered with the engine's chain service. * The 0th state will be created and signed. * An OpenChannel objective will be created and approved. * * @param args - Parameters to create the channel with. * @returns A promise that resolves to the channel output. */ createChannel(args: CreateChannelParams): Promise; /** * Creates multiple channels with the same parameters. See {@link SingleThreadedEngine.createChannel}. * * @param args - Parameters to create the channels with. * @param numberOfChannels - The number of desired channels. * @returns A promise that resolves to the channel output. */ createChannels(args: CreateChannelParams, numberOfChannels: number): Promise; private _createChannel; /** * Joins a list of channels. * * @remarks * Approves an OpenChannel objective for each channel, if it exists, and cranks it. * Registers each channel with the engine's chain service. * * @param channelIds - The list of ids of the channels to join. * @returns A promise that resolves to the channel output. */ joinChannels(channelIds: ChannelId[]): Promise; private approveObjective; approveObjectives(objectiveIds: string[]): Promise<{ objectives: WalletObjective[]; messages: Message[]; chainRequests: ChainRequest[]; channelResults: ChannelResult[]; }>; /** * Joins a channel. * * @remarks * Approves an OpenChannel objective for this channel, if it exists, and cranks it. * Registers the channel with the engine's chain service. * Throws an error if the channel is not known to this engine. * Throws an error if no objectives are known that have this channel in scope. * * @param channelId - The id of the channel to join. * @returns A promise that resolves to the channel output. */ joinChannel({ channelId }: JoinChannelParams): Promise; /** * Attempts to make progress on any open objectives for the specified channels. * This is used to make progress on objectives after something has changed in the store. * @param channelIds The channels that progress should be made on. * @returns The channel output which contains any new messages, chain requests, or objective events. */ crank(channelIds: string[]): Promise; /** * Updates a channel with a new state. * * @remarks * Signs and stores the new state, returns the result in a message for counterparties. * Throws an error if the channel is not known to this engine. * Throws an error if no objectives are known that have this channel in scope. * * @param channelId - The id of the channel to update. * @param allocations - New allocations describing a new outcome (distribution of assets) for the channel. * @param appData - New application-specific data. * @returns A promise that resolves to the channel output. */ updateChannel({ channelId, allocations, appData, }: UpdateChannelParams): Promise; /** * Attempts to collaboratively close a list of channels. * * @remarks * Signs, stores, and sends an isFinal=true state for each channel in the list. * Creates, approves and cranks a CloseChannel objective for each channel in the list. See {@link SingleThreadedEngine.closeChannel}. * * @param channelId - The id of the channel to try and close. * @returns A promise that resolves to the channel output. */ closeChannels(channelIds: Bytes32[]): Promise; /** * Attempts to collaboratively close a channel. * * @remarks * Signs, stores, and sends an isFinal=true state. * Creates, approves and cranks a CloseChannel objective for the supplied channel. * This objective continues working after this call resolves, and will attempt to defund the channel. * * @param channelId - The id of the channel to try and close. * @returns A promise that resolves to the channel output. Will always return a new objective. */ closeChannel({ channelId, }: CloseChannelParams): Promise; private _closeChannel; /** * Gets the latest state for each ledger channel in the engine's store. * * @param participants - The list of participants in the ledger channel (filters the query). * @returns A promise that resolves to the channel output. */ getLedgerChannels(participants: APIParticipant[]): Promise; /** * Gets the latest state for each channel in the engine's store. * * @returns A promise that resolves to the channel output. */ getChannels(): Promise; /** * Gets the objective for a given id. * * @returns A promise that resolves to a WalletObjective, with a progressLastMadeAt timestamp */ getObjective(objectiveId: string): Promise; /** * Gets any objectives with an approved status * @returns A promise that resolves to a collection of WalletObjectives */ getApprovedObjectives(): Promise; /** * Gets the latest state for a channel. * * @privateRemarks TODO: Consider renaming this to getChannel() to match getChannels() * @returns A promise that resolves to the channel output. */ getState({ channelId }: GetStateParams): Promise; /** * Push a message from a counterparty into the engine. * * @remarks * Fresh states will be stored. * Requests will be handled. * Objectives with updated channels in scope will be cranked. * * @param rawPayload - The message to be pushed. Will be validated against a schema. * @returns A promise that resolves to the channel output. */ pushMessage(rawPayload: unknown): Promise; /** * Push a message containing a single update to a running application channel. * * @remarks * A single fresh state will be stored. * No fresh requests or objectives will stored. * No objectives will be cranked. * * @param rawPayload - The message to be pushed. Will be validated against a schema. * @returns A promise that resolves to the channel output. */ pushUpdate(rawPayload: unknown): Promise; private _pushMessage; /** * Active objectives for the "touched" channels are cranked. Theoretically, this may touch other * channels, resulting in a cascade of cranked objectives. * * @remarks * Emits an 'objectiveSucceded' event for objectives that succeed. * * @param channels channels touched by the caller * @param response EngineResponse that is modified in place while cranking objectives */ private takeActions; } export declare function getOrThrow(result: Either.Either): T; //# sourceMappingURL=engine.d.ts.map