mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
added holder for parity document key storage, wip, added unit tests
This commit is contained in:
parent
2662a7795b
commit
da5386f490
5
src/secretstore/DocumentKeys.ts
Normal file
5
src/secretstore/DocumentKeys.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export default class DocumentKeys {
|
||||
commonPoint: string
|
||||
encryptedKey: string
|
||||
encryptedPoint: string
|
||||
}
|
@ -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<string> {
|
||||
@ -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<any> {
|
||||
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<string> {
|
||||
// `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<any> {
|
||||
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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -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<string> {
|
||||
|
||||
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<string> {
|
||||
@ -56,4 +55,11 @@ export default class SecretStore {
|
||||
return key
|
||||
}
|
||||
|
||||
public encryptDocument(document: string) {
|
||||
}
|
||||
|
||||
public decryptDocument(encryptedDocument: string) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
72
test/secretstore/ParityClient.test_.ts
Normal file
72
test/secretstore/ParityClient.test_.ts
Normal file
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user