|
| 1 | +import log from 'loglevel'; |
| 2 | +import Multiaddr from 'multiaddr'; |
| 3 | +import PeerId, { isPeerId } from 'peer-id'; |
| 4 | + |
| 5 | +import { AquaCallHandler } from './internal/AquaHandler'; |
| 6 | +import { ClientImpl } from './internal/ClientImpl'; |
| 7 | +import { PeerIdB58 } from './internal/commonTypes'; |
| 8 | +import { generatePeerId, seedToPeerId } from './internal/peerIdUtils'; |
| 9 | +import { RequestFlow } from './internal/RequestFlow'; |
| 10 | +import { RequestFlowBuilder } from './internal/RequestFlowBuilder'; |
| 11 | + |
| 12 | +/** |
| 13 | + * The class represents interface to Fluence Platform. To create a client use @see {@link createClient} function. |
| 14 | + */ |
| 15 | +export interface FluenceClient { |
| 16 | + /** |
| 17 | + * { string } Gets the base58 representation of the current peer id. Read only |
| 18 | + */ |
| 19 | + readonly relayPeerId: PeerIdB58 | undefined; |
| 20 | + |
| 21 | + /** |
| 22 | + * { string } Gets the base58 representation of the connected relay's peer id. Read only |
| 23 | + */ |
| 24 | + readonly selfPeerId: PeerIdB58; |
| 25 | + |
| 26 | + /** |
| 27 | + * { string } True if the client is connected to network. False otherwise. Read only |
| 28 | + */ |
| 29 | + readonly isConnected: boolean; |
| 30 | + |
| 31 | + /** |
| 32 | + * The base handler which is used by every RequestFlow executed by this FluenceClient. |
| 33 | + * Please note, that the handler is combined with the handler from RequestFlow before the execution occures. |
| 34 | + * After this combination, middlewares from RequestFlow are executed before client handler's middlewares. |
| 35 | + */ |
| 36 | + readonly aquaCallHandler: AquaCallHandler; |
| 37 | + |
| 38 | + /** |
| 39 | + * Disconnects the client from the network |
| 40 | + */ |
| 41 | + disconnect(): Promise<void>; |
| 42 | + |
| 43 | + /** |
| 44 | + * Establish a connection to the node. If the connection is already established, disconnect and reregister all services in a new connection. |
| 45 | + * |
| 46 | + * @param multiaddr |
| 47 | + */ |
| 48 | + connect(multiaddr: string | Multiaddr): Promise<void>; |
| 49 | + |
| 50 | + /** |
| 51 | + * Initiates RequestFlow execution @see { @link RequestFlow } |
| 52 | + * @param { RequestFlow } [ request ] - RequestFlow to start the execution of |
| 53 | + */ |
| 54 | + initiateFlow(request: RequestFlow): Promise<void>; |
| 55 | +} |
| 56 | + |
| 57 | +type Node = { |
| 58 | + peerId: string; |
| 59 | + multiaddr: string; |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Creates a Fluence client. If the `connectTo` is specified connects the client to the network |
| 64 | + * @param { string | Multiaddr | Node } [connectTo] - Node in Fluence network to connect to. If not specified client will not be connected to the n |
| 65 | + * @param { PeerId | string } [peerIdOrSeed] - The Peer Id of the created client. Specified either as PeerId structure or as seed string. Will be generated randomly if not specified |
| 66 | + * @returns { Promise<FluenceClient> } Promise which will be resolved with the created FluenceClient |
| 67 | + */ |
| 68 | +export const createClient = async ( |
| 69 | + connectTo?: string | Multiaddr | Node, |
| 70 | + peerIdOrSeed?: PeerId | string, |
| 71 | +): Promise<FluenceClient> => { |
| 72 | + let peerId; |
| 73 | + if (!peerIdOrSeed) { |
| 74 | + peerId = await generatePeerId(); |
| 75 | + } else if (isPeerId(peerIdOrSeed)) { |
| 76 | + // keep unchanged |
| 77 | + peerId = peerIdOrSeed; |
| 78 | + } else { |
| 79 | + // peerId is string, therefore seed |
| 80 | + peerId = await seedToPeerId(peerIdOrSeed); |
| 81 | + } |
| 82 | + |
| 83 | + const client = new ClientImpl(peerId); |
| 84 | + await client.initAquamarineRuntime(); |
| 85 | + |
| 86 | + if (connectTo) { |
| 87 | + let theAddress: Multiaddr; |
| 88 | + let fromNode = (connectTo as any).multiaddr; |
| 89 | + if (fromNode) { |
| 90 | + theAddress = new Multiaddr(fromNode); |
| 91 | + } else { |
| 92 | + theAddress = new Multiaddr(connectTo as string); |
| 93 | + } |
| 94 | + |
| 95 | + await client.connect(theAddress); |
| 96 | + if (!(await checkConnection(client))) { |
| 97 | + throw new Error('Connection check failed. Check if the node is working or try to connect to another node'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return client; |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * Checks the network connection by sending a ping-like request to relat node |
| 106 | + * @param { FluenceClient } client - The Fluence Client instance. |
| 107 | + */ |
| 108 | +export const checkConnection = async (client: FluenceClient): Promise<boolean> => { |
| 109 | + if (!client.isConnected) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + const msg = Math.random().toString(36).substring(7); |
| 114 | + const callbackFn = 'checkConnection'; |
| 115 | + const callbackService = '_callback'; |
| 116 | + |
| 117 | + const [request, promise] = new RequestFlowBuilder() |
| 118 | + .withRawScript( |
| 119 | + `(seq |
| 120 | + (call init_relay ("op" "identity") [msg] result) |
| 121 | + (call %init_peer_id% ("${callbackService}" "${callbackFn}") [result]) |
| 122 | + )`, |
| 123 | + ) |
| 124 | + .withVariables({ |
| 125 | + msg, |
| 126 | + }) |
| 127 | + .buildAsFetch<[[string]]>(callbackService, callbackFn); |
| 128 | + |
| 129 | + await client.initiateFlow(request); |
| 130 | + |
| 131 | + try { |
| 132 | + const [[result]] = await promise; |
| 133 | + if (result != msg) { |
| 134 | + log.warn("unexpected behavior. 'identity' must return arguments the passed arguments."); |
| 135 | + } |
| 136 | + return true; |
| 137 | + } catch (e) { |
| 138 | + log.error('Error on establishing connection: ', e); |
| 139 | + return false; |
| 140 | + } |
| 141 | +}; |
0 commit comments