Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RPC API for voucher validation #18

Merged
merged 12 commits into from
Oct 18, 2023
2 changes: 1 addition & 1 deletion cmd/create-channels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func createChannels() error {
logging.SetupDefaultFileLogger(LOG_FILE, slog.LevelDebug)

url := fmt.Sprintf(":%d/api/v1", participantOpts.RpcPort)
clientConnection, err := http.NewHttpTransportAsClient(url, 500*time.Millisecond)
clientConnection, err := http.NewHttpTransportAsClient(url, true, 500*time.Millisecond)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/create-ledger-channel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
Usage: "Creates a ledger channel with the specified counterparty and amount",
Flags: flags,
Action: func(cCtx *cli.Context) error {
clientConnection, err := http.NewHttpTransportAsClient(cCtx.String(NITRO_ENDPOINT), 10*time.Millisecond)
clientConnection, err := http.NewHttpTransportAsClient(cCtx.String(NITRO_ENDPOINT), true, 10*time.Millisecond)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/libp2p/go-libp2p-kad-dht v0.24.2
github.com/tidwall/buntdb v1.2.10
github.com/urfave/cli/v2 v2.25.3
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63

)

Expand Down Expand Up @@ -145,6 +144,7 @@ require (
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
Expand Down
5 changes: 3 additions & 2 deletions internal/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"log/slog"

"github.com/statechannels/go-nitro/node"
"github.com/statechannels/go-nitro/paymentsmanager"
"github.com/statechannels/go-nitro/rpc"
"github.com/statechannels/go-nitro/rpc/transport"
httpTransport "github.com/statechannels/go-nitro/rpc/transport/http"
"github.com/statechannels/go-nitro/rpc/transport/nats"
)

func InitializeRpcServer(node *node.Node, rpcPort int, useNats bool, cert *tls.Certificate) (*rpc.RpcServer, error) {
func InitializeRpcServer(node *node.Node, paymentManager paymentsmanager.PaymentsManager, rpcPort int, useNats bool, cert *tls.Certificate) (*rpc.RpcServer, error) {
var transport transport.Responder
var err error

Expand All @@ -27,7 +28,7 @@ func InitializeRpcServer(node *node.Node, rpcPort int, useNats bool, cert *tls.C
return nil, err
}

rpcServer, err := rpc.NewRpcServer(node, transport)
rpcServer, err := rpc.NewRpcServer(node, paymentManager, transport)
if err != nil {
return nil, err
}
Expand Down
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"strings"
"sync"
"syscall"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/statechannels/go-nitro/node/engine/chainservice"
p2pms "github.com/statechannels/go-nitro/node/engine/messageservice/p2p-message-service"
"github.com/statechannels/go-nitro/node/engine/store"
"github.com/statechannels/go-nitro/paymentsmanager"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
Expand Down Expand Up @@ -198,14 +200,12 @@ func main() {
altsrc.NewStringFlag(&cli.StringFlag{
Name: TLS_CERT_FILEPATH,
Usage: "Filepath to the TLS certificate. If not specified, TLS will not be used with the RPC transport.",
Value: "./tls/statechannels.org.pem",
Category: TLS_CATEGORY,
Destination: &tlsCertFilepath,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: TLS_KEY_FILEPATH,
Usage: "Filepath to the TLS private key. If not specified, TLS will not be used with the RPC transport.",
Value: "./tls/statechannels.org_key.pem",
Category: TLS_CATEGORY,
Destination: &tlsKeyFilepath,
}),
Expand Down Expand Up @@ -251,16 +251,33 @@ func main() {
if err != nil {
return err
}
var cert tls.Certificate

paymentsManager, err := paymentsmanager.NewPaymentsManager(node)
if err != nil {
return err
}

wg := new(sync.WaitGroup)
defer wg.Wait()

paymentsManager.Start(wg)
defer func() {
err := paymentsManager.Stop()
if err != nil {
panic(err)
}
}()

var cert *tls.Certificate
if tlsCertFilepath != "" && tlsKeyFilepath != "" {
cert, err = tls.LoadX509KeyPair(tlsCertFilepath, tlsKeyFilepath)
loadedCert, err := tls.LoadX509KeyPair(tlsCertFilepath, tlsKeyFilepath)
if err != nil {
panic(err)
}
cert = &loadedCert
}

rpcServer, err := rpc.InitializeRpcServer(node, rpcPort, useNats, &cert)
rpcServer, err := rpc.InitializeRpcServer(node, paymentsManager, rpcPort, useNats, cert)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions node_test/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
p2pms "github.com/statechannels/go-nitro/node/engine/messageservice/p2p-message-service"
"github.com/statechannels/go-nitro/node/engine/store"
"github.com/statechannels/go-nitro/node/query"
"github.com/statechannels/go-nitro/paymentsmanager"
"github.com/statechannels/go-nitro/protocols/directfund"
"github.com/statechannels/go-nitro/protocols/virtualfund"
"github.com/statechannels/go-nitro/rpc"
Expand Down Expand Up @@ -440,7 +441,8 @@ func setupNitroNodeWithRPCClient(
panic(err)
}

rpcServer, err := interRpc.InitializeRpcServer(&node, rpcPort, useNats, &cert)
paymentsManager := paymentsmanager.PaymentsManager{}
rpcServer, err := interRpc.InitializeRpcServer(&node, paymentsManager, rpcPort, useNats, &cert)
if err != nil {
t.Fatal(err)
}
Expand All @@ -455,7 +457,7 @@ func setupNitroNodeWithRPCClient(
}
case transport.Http:

clientConnection, err = http.NewHttpTransportAsClient(rpcServer.Url(), 10*time.Millisecond)
clientConnection, err = http.NewHttpTransportAsClient(rpcServer.Url(), true, 10*time.Millisecond)
if err != nil {
panic(err)
}
Expand Down
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
25 changes: 20 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,19 @@ 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 +39,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 +66,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
Loading
Loading