-
Notifications
You must be signed in to change notification settings - Fork 6
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
Feature/remote key manager #202
base: feature/p2pmessaging
Are you sure you want to change the base?
Changes from 1 commit
e53d002
6ad4672
736638c
2d8519c
706be90
5275f85
7b829d3
6ac6520
f1d0e66
4444029
0fbe28b
7a8a5d0
838d4bb
8155a26
65fea20
0616570
f85b7e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import {makeUUID4} from "blockstack/lib"; | ||
import { createNanoEvents, DefaultEvents, Emitter } from "nanoevents"; | ||
import { CruxId } from "src/packages"; | ||
import { CruxId } from "../../packages"; | ||
import { IKeyManager } from "../interfaces"; | ||
import { CruxProtocolMessenger, SecureCruxNetwork } from "./crux-messenger"; | ||
|
||
|
@@ -71,7 +71,7 @@ export class RemoteKeyHost { | |
console.log("Inside RemoteKeyHost::in::Msg, senderId: ", msg, senderId); | ||
const data = await this.handleMessage(msg); | ||
console.log("Inside RemoteKeyHost::initialize::Data(handleMessage): ", data); | ||
this.sendInvocationResult(data, senderId!); | ||
await this.sendInvocationResult(data, CruxId.fromString(senderId!)); | ||
}); | ||
} | ||
private async sendInvocationResult(result: any, receiverId: CruxId) { | ||
|
@@ -80,40 +80,19 @@ export class RemoteKeyHost { | |
} | ||
const resultData = this.generateInvocationResponse(result); | ||
console.log("RemoteKeyHost::Inside sendInvocationResult::resultData: ", resultData); | ||
// @ts-ignore | ||
await this.cruxProtocolMessenger.send({ | ||
content: resultData, | ||
type: "KEY_MANAGER_RESPONSE", | ||
}, receiverId); | ||
} | ||
|
||
private async handleMessage(message: any) { | ||
if (!VALID_METHODS.includes(message.method)) { | ||
throw new Error("Invalid key manager method"); | ||
} | ||
if (!this.keyManager) { | ||
throw new Error("Key Manager not available"); | ||
} | ||
let data; | ||
if (message.method === "signWebToken") { | ||
console.log("HandleMessage entrance:signWebToken"); | ||
data = await this.keyManager.signWebToken(message.args[0]); | ||
console.log("HandleMessage exit:signWebToken", data); | ||
} else if (message.method === "getPubKey") { | ||
console.log("HandleMessage entrance:getPubKey"); | ||
data = await this.keyManager.getPubKey(); | ||
console.log("HandleMessage exit:getPubKey", data); | ||
} else if (message.method === "deriveSharedSecret") { | ||
console.log("HandleMessage entrance:deriveSharedSecret"); | ||
// @ts-ignore | ||
data = await this.keyManager.deriveSharedSecret(message.args[0]); | ||
console.log("HandleMessage exit:deriveSharedSecret", data); | ||
} else if (message.method === "decryptMessage") { | ||
console.log("HandleMessage entrance:decryptMessage"); | ||
// @ts-ignore | ||
data = await this.keyManager.decryptMessage(message.args[0]); | ||
console.log("HandleMessage exit:decryptMessage", data); | ||
} | ||
// @ts-ignore | ||
data = await this.keyManager[message.method](message.args[0]); | ||
return { | ||
data, | ||
invocationId: message.invocationId, | ||
|
@@ -141,52 +120,29 @@ export class RemoteKeyManager implements IKeyManager { | |
await this.remoteKeyClient.initialize(); | ||
} | ||
// @ts-ignore | ||
public signWebToken = async (token: any) => { | ||
return new Promise(async (resolve, reject) => { | ||
const invocationId = await this.remoteKeyClient.invoke("signWebToken", [token]); | ||
console.log("RemoteKeyManager::signWebToken::invokationId: ", invocationId); | ||
this.remoteKeyClient.listenToInvocation(invocationId, (msg, senderId) => { | ||
console.log("RemoteKeyManager::signWebToken::msg: ", msg); | ||
resolve(msg.result.data); | ||
}, (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
public signWebToken = async (args: any) => { | ||
return this.makeRemoteMessageCall("signWebToken", [args]); | ||
} | ||
// @ts-ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove ts ignore and fix errors. Let me know if you're getting confused with 'args' and how to make it generic for arguments. |
||
public getPubKey = async () => { | ||
return new Promise(async (resolve, reject) => { | ||
const invocationId = await this.remoteKeyClient.invoke("getPubKey", []); | ||
console.log("RemoteKeyManager::getPubKey::invokationId: ", invocationId); | ||
this.remoteKeyClient.listenToInvocation(invocationId, (msg, senderId) => { | ||
console.log("RemoteKeyManager::getPubKey::msg: ", msg); | ||
resolve(msg.result.data); | ||
}, (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
return this.makeRemoteMessageCall("getPubKey"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can do an optimization here - |
||
} | ||
// @ts-ignore | ||
public deriveSharedSecret = async (publicKey: string) => { | ||
const invocationId = await this.remoteKeyClient.invoke("deriveSharedSecret", [publicKey]); | ||
console.log("RemoteKeyManager::deriveSharedSecret::invokationId: ", invocationId); | ||
return new Promise(async (resolve, reject) => { | ||
this.remoteKeyClient.listenToInvocation(invocationId, (msg, senderId) => { | ||
console.log("RemoteKeyManager::deriveSharedSecret::msg: ", msg); | ||
resolve(msg.result.data); | ||
}, (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
public deriveSharedSecret = async (args: string) => { | ||
return this.makeRemoteMessageCall("deriveSharedSecret", [args]); | ||
} | ||
// @ts-ignore | ||
public decryptMessage = async (encryptedMessage: string) => { | ||
console.log("RemoteKeyManager::decryptMessage::entry: encryptedMessage: ", encryptedMessage); | ||
const invocationId = await this.remoteKeyClient.invoke("decryptMessage", [encryptedMessage]); | ||
console.log("RemoteKeyManager::decryptMessage::invokationId: ", invocationId); | ||
public decryptMessage = async (args: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong naming of parameter in all methods - look at other KeyManager implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry will take care of it |
||
return this.makeRemoteMessageCall("decryptMessage", [args]); | ||
} | ||
|
||
private makeRemoteMessageCall = async (method: string, args: any = []) => { | ||
console.log("makeRemoteMessageCall::", method, args); | ||
const invocationId = await this.remoteKeyClient.invoke(method, args); | ||
console.log("RemoteKeyManager::makeRemoteMessageCall::invokationId: ", invocationId); | ||
return new Promise(async (resolve, reject) => { | ||
this.remoteKeyClient.listenToInvocation(invocationId, (msg, senderId) => { | ||
console.log("RemoteKeyManager::decryptMessage::msg: ", msg); | ||
console.log("RemoteKeyManager::deriveSharedSecret::msg: ", msg); | ||
resolve(msg.result.data); | ||
}, (err) => { | ||
reject(err); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this? Remove if it was for your personal debugging only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually it was for catching errors from setupCruxMessenger but now we can remove it(options are being used till here only)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'catching error' is not a valid purpose. 'catching error to _________' is a valid purpose
Here you've suppressed it and logged it. Any particular cases/errors you're trying to catch here? If this function is too failure prone then it may block other functionalities of SDK. So then it may be a good idea to suppress error here like youve done
But in that case we should figure out why its failing and fix that.