diff --git a/src/secretstore/DocumentKeys.ts b/src/secretstore/DocumentKeys.ts new file mode 100644 index 0000000..1adb0ea --- /dev/null +++ b/src/secretstore/DocumentKeys.ts @@ -0,0 +1,5 @@ +export default class DocumentKeys { + commonPoint: string + encryptedKey: string + encryptedPoint: string +} diff --git a/src/secretstore/ParityClient.ts b/src/secretstore/ParityClient.ts index c040e0a..cdaf8e7 100644 --- a/src/secretstore/ParityClient.ts +++ b/src/secretstore/ParityClient.ts @@ -2,6 +2,7 @@ import * as jayson from "jayson" import {Client} from "jayson" import {URL} from "url" import Logger from "../utils/Logger" +import DocumentKeys from "./DocumentKeys" function add0xPrefix(key) { return key.startsWith("0x") ? key : "0x" + key @@ -9,10 +10,14 @@ function add0xPrefix(key) { export default class ParityClient { + private address: string + private password: string private rpcClient: Client - constructor(private url: string, private address: string, private password: string) { - this.rpcClient = jayson.Client.http(new URL(this.url)) + constructor(config: { url: string, address: string, password: string }) { + this.password = config.password + this.address = config.address + this.rpcClient = jayson.Client.http(new URL(config.url)) } public async signKeyId(keyId): Promise { @@ -20,33 +25,35 @@ export default class ParityClient { "secretstore_signRawHash", [this.address, this.password, add0xPrefix(keyId)]) .then((result: string) => { - Logger.log("fu", result) return result }) - } - public generateDocumentKeyFromKey(serverKey) { + public async generateDocumentKeyFromKey(serverKey: string): Promise { return this.sendJsonRpcRequest(this.rpcClient, "secretstore_generateDocumentKey", [this.address, this.password, serverKey]) - .then((result: string) => { - return result + .then((result: any) => { + return { + commonPoint: result.common_point, + encryptedKey: result.encrypted_key, + encryptedPoint: result.encrypted_point, + } as DocumentKeys }) - } - public encryptDocument(encryptedKey, document: string) { + public encryptDocument(encryptedKey, document: any): Promise { // `document` must be encoded in hex when sent to encryption return this.sendJsonRpcRequest(this.rpcClient, "secretstore_encrypt", [this.address, this.password, encryptedKey, - add0xPrefix(new Buffer(document).toString("hex"))]) + add0xPrefix(new Buffer(JSON.stringify(document)).toString("hex"))]) .then((result: string) => { return result }) } - public decryptDocument(decryptedSecret, commonPoint, decryptShadows, encryptedDocument) { + public decryptDocument(decryptedSecret: string, commonPoint: string, + decryptShadows: string, encryptedDocument: string): Promise { return this.sendJsonRpcRequest(this.rpcClient, "secretstore_shadowDecrypt", [this.address, this.password, decryptedSecret, @@ -58,9 +65,7 @@ export default class ParityClient { private sendJsonRpcRequest(rpcClient: Client, methodName: string, paramsList: any[]) { return new Promise((resolve, reject) => { - rpcClient.request( - methodName, - paramsList, + rpcClient.request(methodName, paramsList, (err, response) => { const error = response.error || err if (error) { @@ -68,7 +73,7 @@ export default class ParityClient { Logger.error(`Method ${methodName}`) return reject(error) } - return resolve(response.result.toString()) + return resolve(response.result) }) }) } diff --git a/src/secretstore/SecretStore.ts b/src/secretstore/SecretStore.ts index 0dd5e1f..59b6150 100644 --- a/src/secretstore/SecretStore.ts +++ b/src/secretstore/SecretStore.ts @@ -9,7 +9,10 @@ export default class SecretStore { constructor(config: { secretStoreUrl: string, parityUrl: string, address: string, password: string }) { - this.partiyClient = new ParityClient(config.parityUrl, config.address, config.password) + this.partiyClient = new ParityClient({ + url: config.parityUrl, address: config.address, + password: config.password, + }) this.secretStoreClient = new SecretStoreClient(config.secretStoreUrl) } @@ -29,18 +32,14 @@ export default class SecretStore { public async storeDocumentKey(serverKeyId: string, documentKeyId): Promise { const serverKeyIdSig = await this.partiyClient.signKeyId(serverKeyId) - const documentKeyIdSig = await this.partiyClient.signKeyId(documentKeyId) - Logger.log("serverKeyId:", serverKeyId, "serverKeyIdSig:", serverKeyIdSig) + const serverKey = await this.secretStoreClient.generateServerKey( + serverKeyId, serverKeyIdSig) + Logger.log("key:", serverKey) - const key = await this.secretStoreClient.storeDocumentKey( - serverKeyId, serverKeyIdSig, - documentKeyId, documentKeyIdSig, - ) + const documentKey = this.partiyClient.generateDocumentKeyFromKey(serverKey) - Logger.log("key:", key) - - return key + return documentKey } public async retrieveDocumentKey(serverKeyId: string): Promise { @@ -56,4 +55,11 @@ export default class SecretStore { return key } + public encryptDocument(document: string) { + } + + public decryptDocument(encryptedDocument: string) { + + } + } diff --git a/test/secretstore/ParityClient.test_.ts b/test/secretstore/ParityClient.test_.ts new file mode 100644 index 0000000..1f97125 --- /dev/null +++ b/test/secretstore/ParityClient.test_.ts @@ -0,0 +1,72 @@ +import BigNumber from "bignumber.js" +import {assert} from "chai" +import ConfigProvider from "../../src/ConfigProvider" +import Config from "../../src/models/Config" +import DocumentKeys from "../../src/secretstore/DocumentKeys" +import ParityClient from "../../src/secretstore/ParityClient" + +const parityUrl = "http://localhost:8545" + +ConfigProvider.configure({ + nodeUri: parityUrl, +} as Config) + +const address = "0xa50f397644973dba99624404b2894825840aa03b" +const password = "unittest" +const serverKey = + "0x36131d552e561d8231cd91c8020d869e14c11b16e79fb80ecf8302ea0a0539c969dbc0b547398daf293c259431d7c483ee5974b0ef179297edbe6d39af4374d5" + +const testDocument = { + so: "secure", + soWow: true, +} +const parityClient: ParityClient = new ParityClient({ + url: parityUrl, + address, password, +}) + +function generateRandomId(): string { + const id: string = BigNumber.random(64).toString().replace("0.", "") + + // sometimes it only generates 63 digits + return id.length === 63 ? id + "0" : id +} + +describe("ParityClient", () => { + + describe("#signKeyId()", () => { + it("should generate sig from given key", async () => { + + const keyId = generateRandomId() + const keyIdSig = await parityClient.signKeyId(keyId) + + assert(keyIdSig) + }) + }) + + describe("#generateDocumentKeyFromKey()", () => { + it("should generate a document key from a server key", async () => { + + const documentKey = await parityClient.generateDocumentKeyFromKey(serverKey) + assert(documentKey) + }) + }) + + describe("#encryptDocument()", () => { + it("should encrypt an document", async () => { + + const documentKey: DocumentKeys = await parityClient.generateDocumentKeyFromKey(serverKey) + const encryptedDocument = await parityClient.encryptDocument(documentKey.encryptedKey, testDocument) + assert(encryptedDocument) + }) + }) + + describe("#decryptDocument()", () => { + it("should decrypt an document", async () => { + + const documentKey: DocumentKeys = await parityClient.generateDocumentKeyFromKey(serverKey) + const encryptedDocument = await parityClient.encryptDocument(documentKey.encryptedKey, testDocument) + assert(encryptedDocument) + }) + }) +}) diff --git a/test/secretstore/SecretStore.test_.ts b/test/secretstore/SecretStore.test_.ts index e107b55..7ca8c87 100644 --- a/test/secretstore/SecretStore.test_.ts +++ b/test/secretstore/SecretStore.test_.ts @@ -8,7 +8,7 @@ const parityUrl = "http://localhost:8545" const ssUrl = "https://secret-store.dev-ocean.com" ConfigProvider.configure({ - nodeUri: ssUrl, + nodeUri: parityUrl, } as Config) const address = "0xa50f397644973dba99624404b2894825840aa03b"