2022-10-08 00:05:45 +03:00
|
|
|
/**
|
|
|
|
* Copyright 2022 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
import { Fluence, KeyPair, setLogLevel } from "@fluencelabs/fluence";
|
2022-10-10 17:25:10 +04:00
|
|
|
import { testNet } from "@fluencelabs/fluence-network-environment";
|
|
|
|
import { registerEchoService, registerServiceRecordRecord } from "./generated/export";
|
2022-10-08 00:05:45 +03:00
|
|
|
import assert from "node:assert";
|
2022-08-31 12:33:48 +04:00
|
|
|
|
2022-10-08 00:05:45 +03:00
|
|
|
// don't store your secret key in the code. This is just for the example
|
|
|
|
const secretKey = "Iz3HUmNIB78lkNNVmMkDKrju0nCivtkJNyObrFAr774=";
|
2022-08-31 12:33:48 +04:00
|
|
|
|
|
|
|
async function main() {
|
2022-10-08 00:05:45 +03:00
|
|
|
const keypair = await KeyPair.fromEd25519SK(Buffer.from(secretKey, "base64"));
|
2022-10-10 17:25:10 +04:00
|
|
|
const connectTo = testNet[0];
|
2022-10-08 00:05:45 +03:00
|
|
|
assert(connectTo !== undefined);
|
|
|
|
|
|
|
|
// connect to the Fluence network
|
|
|
|
await Fluence.start({ connectTo, KeyPair: keypair });
|
|
|
|
setLogLevel("SILENT");
|
|
|
|
|
|
|
|
const peerId = Fluence.getStatus().peerId;
|
|
|
|
const relayId = Fluence.getStatus().relayPeerId;
|
|
|
|
assert(peerId !== null && relayId !== null);
|
|
|
|
console.log(`📗 created a fluence peer ${peerId} with relay ${relayId}`);
|
|
|
|
|
|
|
|
const serviceId = "echo";
|
|
|
|
|
|
|
|
// register local service with serviceId "echo"
|
|
|
|
await registerEchoService(serviceId, {
|
|
|
|
echo(msg) {
|
|
|
|
console.log(`Received message: ${msg}`);
|
|
|
|
return `${peerId}: ${msg}`;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const resourceId = process.argv[2];
|
|
|
|
|
|
|
|
// don't register if resource id isn't passed
|
|
|
|
if (resourceId === undefined) {
|
2022-08-31 12:33:48 +04:00
|
|
|
console.log(
|
2022-10-08 00:05:45 +03:00
|
|
|
`
|
|
|
|
Copy this code to call this service:
|
2022-10-10 17:25:10 +04:00
|
|
|
|
2022-10-08 00:05:45 +03:00
|
|
|
fluence run -f 'echoJS("${peerId}", "${relayId}", "${serviceId}", "hi")'`
|
2022-08-31 12:33:48 +04:00
|
|
|
);
|
2022-10-08 00:05:45 +03:00
|
|
|
} else {
|
2022-10-10 17:25:10 +04:00
|
|
|
const [success, error] = await registerServiceRecordRecord(
|
2022-10-08 00:05:45 +03:00
|
|
|
resourceId,
|
|
|
|
"echo",
|
|
|
|
peerId,
|
|
|
|
serviceId
|
|
|
|
);
|
|
|
|
console.log(`Registration result: ${success || error}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("\nPress any key to stop fluence js peer");
|
2022-08-31 12:33:48 +04:00
|
|
|
|
2022-10-08 00:05:45 +03:00
|
|
|
// this code keeps fluence client running till any key pressed
|
|
|
|
process.stdin.setRawMode(true);
|
|
|
|
process.stdin.resume();
|
|
|
|
process.stdin.on("data", async () => {
|
|
|
|
await Fluence.stop();
|
|
|
|
process.exit(0);
|
|
|
|
});
|
2022-08-31 12:33:48 +04:00
|
|
|
}
|
|
|
|
|
2022-10-08 00:05:45 +03:00
|
|
|
main().catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
2022-08-31 12:33:48 +04:00
|
|
|
});
|