Skip to content

Commit

Permalink
Add support for non-secure connections in js client
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Oct 17, 2023
1 parent 4e04a4b commit 7190cfc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
54 changes: 42 additions & 12 deletions packages/nitro-rpc-client/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ yargs(hideBin(process.argv))
type: "string",
description: "Custom hostname",
},
s: {
alias: "isSecure",
default: true,
type: "boolean",
description: "Is it a secured connection",
},
})
.command(
"version",
Expand All @@ -32,9 +38,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
const version = await rpcClient.GetVersion();
console.log(version);
Expand All @@ -49,9 +57,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
const address = await rpcClient.GetAddress();
console.log(address);
Expand All @@ -66,9 +76,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
const ledgers = await rpcClient.GetAllLedgerChannels();
for (const ledger of ledgers) {
Expand All @@ -92,9 +104,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
const paymentChans = await rpcClient.GetPaymentChannelsByLedger(
yargs.ledgerId
Expand Down Expand Up @@ -126,9 +140,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
if (yargs.n) logOutChannelUpdates(rpcClient);

Expand Down Expand Up @@ -158,9 +174,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
if (yargs.n) logOutChannelUpdates(rpcClient);

Expand Down Expand Up @@ -192,9 +210,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
if (yargs.n) logOutChannelUpdates(rpcClient);

Expand Down Expand Up @@ -234,9 +254,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);

if (yargs.n) logOutChannelUpdates(rpcClient);
Expand All @@ -263,9 +285,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);

const ledgerInfo = await rpcClient.GetLedgerChannel(yargs.channelId);
Expand All @@ -287,9 +311,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
const paymentChannelInfo = await rpcClient.GetPaymentChannel(
yargs.channelId
Expand Down Expand Up @@ -318,9 +344,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
if (yargs.n) logOutChannelUpdates(rpcClient);

Expand Down Expand Up @@ -352,9 +380,11 @@ yargs(hideBin(process.argv))
async (yargs) => {
const rpcPort = yargs.p;
const rpcHost = yargs.h;
const isSecure = yargs.s;

const rpcClient = await NitroRpcClient.CreateHttpNitroClient(
getCustomRPCUrl(rpcHost, rpcPort)
getCustomRPCUrl(rpcHost, rpcPort),
isSecure
);
if (yargs.n) logOutChannelUpdates(rpcClient);

Expand Down
5 changes: 3 additions & 2 deletions packages/nitro-rpc-client/src/rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ export class NitroRpcClient implements RpcClientApi {
* @returns A NitroRpcClient that uses WS as the transport
*/
public static async CreateHttpNitroClient(
url: string
url: string,
isSecure: boolean
): Promise<NitroRpcClient> {
const transport = await HttpTransport.createTransport(url);
const transport = await HttpTransport.createTransport(url, isSecure);
const rpcClient = new NitroRpcClient(transport);
rpcClient.authToken = await rpcClient.getAuthToken();
return rpcClient;
Expand Down
22 changes: 17 additions & 5 deletions packages/nitro-rpc-client/src/transport/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ import { Transport } from ".";

export class HttpTransport {
Notifications: EventEmitter<NotificationMethod, NotificationParams>;
isSecure: boolean;

public static async createTransport(server: string, isSecure: boolean): Promise<Transport> {
let wsPrefix = 'ws://'
if (isSecure) {
wsPrefix = 'wss://'
}

public static async createTransport(server: string): Promise<Transport> {
// eslint-disable-next-line new-cap
const ws = new w3cwebsocket(`wss://${server}/subscribe`);
const ws = new w3cwebsocket(`${wsPrefix}${server}/subscribe`);

// throw any websocket errors so we don't fail silently
ws.onerror = (e) => {
Expand All @@ -30,14 +36,19 @@ export class HttpTransport {
// Wait for onopen to fire so we know the connection is ready
await new Promise<void>((resolve) => (ws.onopen = () => resolve()));

const transport = new HttpTransport(ws, server);
const transport = new HttpTransport(ws, server, isSecure);
return transport;
}

public async sendRequest<K extends RequestMethod>(
req: RPCRequestAndResponses[K][0]
): Promise<unknown> {
const url = new URL(`https://${this.server}`).toString();
let httpPrefix = 'http://'
if (this.isSecure) {
httpPrefix = 'https://'
}

const url = new URL(`${httpPrefix}${this.server}`).toString();

const result = await axios.post(url.toString(), JSON.stringify(req));

Expand All @@ -52,9 +63,10 @@ export class HttpTransport {

private server: string;

private constructor(ws: w3cwebsocket, server: string) {
private constructor(ws: w3cwebsocket, server: string, isSecure: boolean) {
this.ws = ws;
this.server = server;
this.isSecure = isSecure;

this.Notifications = new EventEmitter();
this.ws.onmessage = (event) => {
Expand Down

0 comments on commit 7190cfc

Please sign in to comment.