Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jolocom/web-service-base",
"version": "3.0.0-rc3",
"version": "3.0.0-rc4",
"description": "Generic Jolocom Web Service for HTTP and WebSockets support for the Jolocom SDK",
"main": "dist/index.js",
"repository": "https://github.com/jolocom/web-service-base",
Expand Down
48 changes: 40 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ interface JWTDesc {

interface RPCHandlerCtx {
createChannel: ({ description: string }) => Promise<Channel>
createInteractionCallbackURL: (cb: (payload: string) => Promise<JSONWebToken<any> | void>) => string
createInteractionCallbackURL: (cb: (payload: string, websocket: WebSocket | undefined) => Promise<JSONWebToken<any> | void>) => string
wrapJWT: (jwt: string | JSONWebToken<any>) => Promise<JWTDesc>
}

interface RPCMap {
[key: string]: (request: any, ctx: RPCHandlerCtx) => Promise<any>
[key: string]: (request: any, ctx: RPCHandlerCtx, websocket: WebSocket | undefined) => Promise<any>
}

const defaultRPCMap: RPCMap = {
Expand All @@ -40,7 +40,7 @@ const defaultRPCMap: RPCMap = {
export class JolocomWebServiceBase {
agent: Agent
rpcMap: RPCMap

protected publicHostport?: string
protected publicWsUrl!: string
protected publicHttpUrl!: string
Expand All @@ -61,10 +61,13 @@ export class JolocomWebServiceBase {
}

protected _callbacks: {
[id: string]: (payload: string) => Promise<JSONWebToken<any> | void>
[id: string]: (payload: string, websocket) => Promise<JSONWebToken<any> | void>
} = {}
protected clientsWS: {
[id: string]: WebSocket
} = {}

createInteractionCallbackURL(cb: (payload: string) => Promise<JSONWebToken<any> | void>) {
createInteractionCallbackURL(cb: (payload: string, websocket: WebSocket | undefined) => Promise<JSONWebToken<any> | void>) {
const id = uuidv4()
this._callbacks[id] = cb
return `${this.publicHttpUrl}${this.paths.interxn}/${id}`
Expand All @@ -75,7 +78,31 @@ export class JolocomWebServiceBase {

const cb = this._callbacks[cbId]
if (!cb) throw new Error('no callback for ' + cbId)
const tokenResp = await cb(payload.token)
const interxn = await this.agent.processJWT(payload.token);

// Pass the websocket to the call back to enable it to send addional custom data
const tokenResp = await cb(payload.token, this.clientsWS[interxn.id])

try {
if (this.clientsWS[interxn.id]) {
try {
console.log("Client's WebSocket has been added to the list for the interxn.id", interxn.id);
const message = JSON.stringify({
id: interxn.id,
status: "success",
response: tokenResp,
});
this.clientsWS[interxn.id].send(message);
} catch (error) {
this.clientsWS[interxn.id].send(
JSON.stringify({ id: interxn.id, status: "error", error })
);
}
}
} catch (error) {
console.error("Someting when wrong while trying to send to the client over websocket", error);
}

if (tokenResp) {
return { token: tokenResp.encode() } // NOTE: legacy, smartwallet 1.9 expects this
} else {
Expand Down Expand Up @@ -113,7 +140,7 @@ export class JolocomWebServiceBase {
return this.agent.channels.create(initInterxn)
}

async processRPC(msg: { id: string, rpc: string, request: string }) {
async processRPC(msg: { id: string, rpc: string, request: string }, websocket: WebSocket | undefined) {
const handler = this.rpcMap[msg.rpc]
if (!handler) throw new Error('unknown RPC Call "' + msg.rpc + '"')

Expand All @@ -122,7 +149,12 @@ export class JolocomWebServiceBase {
createChannel: this.createChannel.bind(this),
createInteractionCallbackURL: this.createInteractionCallbackURL.bind(this)
}
const response = await handler(msg.request, ctx)
const response = await handler(msg.request, ctx, websocket)

if (websocket) {
const callbackID = response.id;
this.clientsWS[callbackID] = websocket;
}
return {
id: msg.id,
request: msg,
Expand Down