2023-02-15 03:00:42 +03:00
|
|
|
import type { SecurityTetraplet } from '@fluencelabs/avm';
|
2023-02-13 17:41:35 +03:00
|
|
|
import type { LogLevel } from '@fluencelabs/marine-js/dist/types';
|
2023-02-15 03:00:42 +03:00
|
|
|
// import type { MultiaddrInput } from '@multiformats/multiaddr';
|
|
|
|
import type { FnConfig, FunctionCallDef, ServiceDef } from './compilerSupport.js';
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Peer ID's id as a base58 string (multihash/CIDv0).
|
|
|
|
*/
|
|
|
|
export type PeerIdB58 = string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional information about a service call
|
|
|
|
* @typeparam ArgName
|
|
|
|
*/
|
|
|
|
export interface CallParams<ArgName extends string | null> {
|
|
|
|
/**
|
|
|
|
* The identifier of particle which triggered the call
|
|
|
|
*/
|
|
|
|
particleId: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The peer id which created the particle
|
|
|
|
*/
|
|
|
|
initPeerId: PeerIdB58;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Particle's timestamp when it was created
|
|
|
|
*/
|
|
|
|
timestamp: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time to live in milliseconds. The time after the particle should be expired
|
|
|
|
*/
|
|
|
|
ttl: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Particle's signature
|
|
|
|
*/
|
|
|
|
signature?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Security tetraplets
|
|
|
|
*/
|
|
|
|
tetraplets: ArgName extends string ? Record<ArgName, SecurityTetraplet[]> : Record<string, never>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node of the Fluence network specified as a pair of node's multiaddr and it's peer id
|
|
|
|
*/
|
|
|
|
type Node = {
|
|
|
|
peerId: PeerIdB58;
|
|
|
|
multiaddr: string;
|
|
|
|
};
|
|
|
|
|
2023-02-15 03:00:42 +03:00
|
|
|
// TODO: either drop support for this exact type or get it back
|
2023-02-16 14:38:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A node in Fluence network a client can connect to.
|
|
|
|
* Can be in the form of:
|
|
|
|
* - string: multiaddr in string format
|
|
|
|
* - Multiaddr: multiaddr object, @see https://github.com/multiformats/js-multiaddr
|
|
|
|
* - Node: node structure, @see Node
|
|
|
|
*/
|
2023-02-15 03:00:42 +03:00
|
|
|
export type RelayOptions = string | /* MultiaddrInput | */ Node;
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
export type KeyTypes = 'RSA' | 'Ed25519' | 'secp256k1';
|
|
|
|
|
|
|
|
export type KeyPairOptions = {
|
|
|
|
type: 'Ed25519';
|
|
|
|
source: 'random' | Uint8Array;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration used when initiating Fluence Client
|
|
|
|
*/
|
|
|
|
export interface ClientOptions {
|
|
|
|
/**
|
|
|
|
* Specify the KeyPair to be used to identify the Fluence Peer.
|
|
|
|
* Will be generated randomly if not specified
|
|
|
|
*/
|
|
|
|
keyPair?: KeyPairOptions;
|
|
|
|
|
|
|
|
connectionOptions?: {
|
|
|
|
/**
|
|
|
|
* When the peer established the connection to the network it sends a ping-like message to check if it works correctly.
|
|
|
|
* The options allows to specify the timeout for that message in milliseconds.
|
|
|
|
* If not specified the default timeout will be used
|
|
|
|
*/
|
|
|
|
skipCheckConnection?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dialing timeout in milliseconds
|
|
|
|
*/
|
|
|
|
dialTimeoutMs?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the default TTL for all particles originating from the peer with no TTL specified.
|
|
|
|
* If the originating particle's TTL is defined then that value will be used
|
|
|
|
* If the option is not set default TTL will be 7000
|
|
|
|
*/
|
|
|
|
defaultTtlMs?: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables\disabled various debugging features
|
|
|
|
*/
|
|
|
|
debug?: {
|
|
|
|
/**
|
|
|
|
* If set to true, newly initiated particle ids will be printed to console.
|
|
|
|
* Useful to see what particle id is responsible for aqua function
|
|
|
|
*/
|
|
|
|
printParticleId?: boolean;
|
|
|
|
/**
|
|
|
|
* Log level for marine services. By default logging is turned off.
|
|
|
|
*/
|
|
|
|
marineLogLevel?: LogLevel;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:38:48 +03:00
|
|
|
export type FluenceStartConfig = ClientOptions & { relay: RelayOptions };
|
|
|
|
|
|
|
|
export const ConnectionStates = ['disconnected', 'connecting', 'connected', 'disconnecting'] as const;
|
|
|
|
export type ConnectionState = typeof ConnectionStates[number];
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
export interface IFluenceClient {
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Connect to the Fluence network
|
|
|
|
* @param relay - relay node to connect to
|
|
|
|
* @param options - client options
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-02-16 14:38:48 +03:00
|
|
|
connect: (relay: RelayOptions, options?: ClientOptions) => Promise<void>;
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Disconnect from the Fluence network
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-02-16 14:38:48 +03:00
|
|
|
disconnect(): Promise<void>;
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Handle connection state changes. Immediately returns current connection state
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-02-16 14:38:48 +03:00
|
|
|
onConnectionStateChange(handler: (state: ConnectionState) => void): ConnectionState;
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Return peers secret key as byte array.
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-02-16 14:38:48 +03:00
|
|
|
getPeerSecretKey(): Uint8Array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return peers public key as a base58 string (multihash/CIDv0).
|
|
|
|
*/
|
|
|
|
getPeerId(): string;
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
// TODO: come up with a working interface for
|
|
|
|
// - particle creation
|
|
|
|
// - particle initialization
|
|
|
|
// - service registration
|
2023-02-16 14:38:48 +03:00
|
|
|
/**
|
|
|
|
* For internal use only. Do not call directly
|
|
|
|
*/
|
2023-02-13 17:41:35 +03:00
|
|
|
internals: any;
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:38:48 +03:00
|
|
|
export interface CallAquaFunctionArgs {
|
2023-02-13 17:41:35 +03:00
|
|
|
def: FunctionCallDef;
|
|
|
|
script: string;
|
|
|
|
config: FnConfig;
|
2023-02-16 14:38:48 +03:00
|
|
|
peer: IFluenceClient;
|
2023-02-13 17:41:35 +03:00
|
|
|
args: { [key: string]: any };
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:38:48 +03:00
|
|
|
export type CallAquaFunction = (args: CallAquaFunctionArgs) => Promise<unknown>;
|
|
|
|
|
2023-02-13 17:41:35 +03:00
|
|
|
export interface RegisterServiceArgs {
|
2023-02-16 14:38:48 +03:00
|
|
|
peer: IFluenceClient;
|
2023-02-13 17:41:35 +03:00
|
|
|
def: ServiceDef;
|
|
|
|
serviceId: string | undefined;
|
|
|
|
service: any;
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:38:48 +03:00
|
|
|
export type RegisterService = (args: RegisterServiceArgs) => void;
|
|
|
|
|
2023-02-13 17:41:35 +03:00
|
|
|
export const asFluencePeer = (fluencePeerCandidate: unknown): IFluenceClient => {
|
|
|
|
if (isFluencePeer(fluencePeerCandidate)) {
|
|
|
|
return fluencePeerCandidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Argument ${fluencePeerCandidate} is not a Fluence Peer`);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isFluencePeer = (fluencePeerCandidate: unknown): fluencePeerCandidate is IFluenceClient => {
|
|
|
|
if (fluencePeerCandidate && (fluencePeerCandidate as any).__isFluenceAwesome) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|