1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Upgrade to Keeper 0.9.0 and Secret Store access way.

This commit is contained in:
Pedro Gutiérrez 2019-04-01 12:40:45 +02:00 committed by Pedro Gutiérrez
parent c5e78899f0
commit c632307646
18 changed files with 119 additions and 213 deletions

View File

@ -1,6 +1,14 @@
import * as HDWalletProvider from "truffle-hdwallet-provider"
import { Config } from "../src"
import * as configJson from "./config/config.json"
const configJson: Config = {
"nodeUri": "http://localhost:8545",
"aquariusUri": "http://172.15.0.15:5000",
"brizoUri": "http://localhost:8030", //"https://brizo-ha.dev-ocean.com",
"brizoAddress": "0x00bd138abd70e2f00903268f3db08f2d25677c9e", // "0x413c9ba0a05b8a600899b41b0c62dd661e689354",
"secretStoreUri": "https://secret-store.dev-ocean.com/",
"verbose": false
}
if (process.env.SEED_WORDS) {
const seedphrase = process.env.SEED_WORDS

View File

@ -1,12 +0,0 @@
{
"nodeUri": "http://localhost:8545",
"aquariusUri": "http://172.15.0.15:5000",
"brizoUri": "http://localhost:8030",
"brizoAddress": "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e",
"parityUri": "http://localhost:9545",
"secretStoreUri": "http://localhost:12001",
"threshold": 0,
"password": "secret",
"address": "0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0",
"verbose": false
}

View File

@ -19,18 +19,11 @@ describe("Consume Asset", () => {
let serviceAgreementSignatureResult: {agreementId: string, signature: string}
before(async () => {
ocean = await Ocean.getInstance({
...config,
web3Provider: new Web3.providers
.HttpProvider("http://localhost:8545", 0, "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", "node0"),
})
ocean = await Ocean.getInstance(config)
// Accounts
const instanceConfig = (ocean as any).instanceConfig
publisher = new Account("0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", instanceConfig)
publisher.setPassword("node0")
consumer = new Account("0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0", instanceConfig)
consumer.setPassword("secret")
publisher = (await ocean.accounts.list())[0]
consumer = (await ocean.accounts.list())[1]
})
it("should regiester a asset", async () => {
@ -90,6 +83,12 @@ describe("Consume Asset", () => {
.grantAccess(serviceAgreementSignatureResult.agreementId, ddo.id, consumer.getId(), publisher)
assert.isTrue(granted, "The asset has not been granted correctly")
const accessGranted = await ocean.keeper.conditions
.accessSecretStoreCondition
.checkPermissions(consumer.getId(), ddo.id)
assert.isTrue(accessGranted, "Consumer has been granted.")
})
it("should consume and store the assets", async () => {
@ -112,6 +111,7 @@ describe("Consume Asset", () => {
})
})
assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.")
assert.deepEqual(files, ["file-0", "file-1"], "Stored files are not correct.")
// assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.")
})
})

View File

@ -19,18 +19,11 @@ describe("Consume Asset (Brizo)", () => {
const metadata = getMetadata()
before(async () => {
ocean = await Ocean.getInstance({
...config,
web3Provider: new Web3.providers
.HttpProvider("http://localhost:8545", 0, "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", "node0"),
})
ocean = await Ocean.getInstance(config)
// Accounts
const instanceConfig = (ocean as any).instanceConfig
publisher = new Account("0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", instanceConfig)
publisher.setPassword("node0")
consumer = new Account("0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0", instanceConfig)
consumer.setPassword("secret")
publisher = (await ocean.accounts.list())[0]
consumer = (await ocean.accounts.list())[1]
})
it("should regiester an asset", async () => {
@ -63,6 +56,7 @@ describe("Consume Asset (Brizo)", () => {
})
})
assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.")
assert.deepEqual(files, ["file-0", "file-1"], "Stored files are not correct.")
// assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.")
})
})

View File

@ -17,21 +17,13 @@ describe("Secret Store", () => {
ocean = await Ocean.getInstance(config)
// Accounts
account = new Account("0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0")
account.setPassword("secret")
account = (await ocean.accounts.list())[0]
})
it("should encrypt a text", async () => {
encryptedContent = await ocean.secretStore.encrypt(did.getId(), content, account)
assert.isDefined(encryptedContent)
assert.match(encryptedContent, /^0x[a-f0-9]{86}$/i)
})
// Only works running Barge with `--no-acl-contract`
xit("should decrypt a text", async () => {
const decryptedContent = await ocean.secretStore.decrypt(did.getId(), encryptedContent, account)
assert.deepEqual(decryptedContent, content)
assert.match(encryptedContent, /^0x[a-f0-9]{76}$/i)
})
})

View File

@ -1,5 +1,4 @@
import { assert } from "chai"
import * as Web3 from "web3"
import { config } from "../config"
@ -13,15 +12,10 @@ describe("Signature", () => {
let consumer: Account
before(async () => {
ocean = await Ocean.getInstance({
...config,
web3Provider: new (Web3 as any).providers
.HttpProvider("http://localhost:8545", 0, "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", "node0"),
})
ocean = await Ocean.getInstance(config)
// Accounts
consumer = new Account("0x00bd138abd70e2f00903268f3db08f2d25677c9e")
consumer.setPassword("node0")
consumer = (await ocean.accounts.list())[0]
})
it("should generate the correct signature", async () => {
@ -75,9 +69,6 @@ describe("Signature", () => {
],
})
const agreementConditionIds = await templates.escrowAccessSecretStoreTemplate
.getAgreementIdsFromDDO(agreementId, ddo, consumer.getId(), consumer.getId())
const signature = await ocean.utils.agreements.signServiceAgreement(
ddo,
serviceDefinitionId,
@ -89,7 +80,7 @@ describe("Signature", () => {
assert.equal(
signature,
// tslint:disable-next-line
"0xc12b8773a330fd01c7fc057e31475e5fc849eba1896cffb102881a6a45aac5fd7342069e578bbe0e1c8c95aa33a53451ac03ae1433f96928cd614c986742578e1b",
"0x3aa8a1c48b8e582d694bbd4ba3a29fde573b78da9720dc48baeb831b2163e1fa6e10e983882ebf8a00f4124de2505136354fd146934053f0d58bba4eced5f8d000",
"The signatuere is not correct.",
)
})

22
package-lock.json generated
View File

@ -129,17 +129,9 @@
}
},
"@oceanprotocol/keeper-contracts": {
"version": "0.8.7",
"resolved": "https://registry.npmjs.org/@oceanprotocol/keeper-contracts/-/keeper-contracts-0.8.7.tgz",
"integrity": "sha512-lrX7isgUYHspQ0/xUtQzugZyZYRncT/TWLom30RGBoHNgy9ZWXYupXULHQdCXq19Hjy7EMYV2sm4HVddB5hz3w=="
},
"@oceanprotocol/secret-store-client": {
"version": "0.0.14",
"resolved": "https://registry.npmjs.org/@oceanprotocol/secret-store-client/-/secret-store-client-0.0.14.tgz",
"integrity": "sha512-Yo2/9MJC1vlWXGIaU+35naYHWWYGbtzTu2t5dW4Ro1JuIRkzxfM3TSNzPUAgsCrUBUxsCsu9pd/RjvPuE45SGw==",
"requires": {
"node-fetch": "^2.3.0"
}
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/@oceanprotocol/keeper-contracts/-/keeper-contracts-0.9.0.tgz",
"integrity": "sha512-QrTCQNiQa9KszH6/dTAS0a8AoW/SIEkZazXTwA2aoePBS0X8fNpsKvT3N2OuR1YPAjCU3rGWzYdV4TNnMNbsSw=="
},
"@types/chai": {
"version": "4.1.7",
@ -6415,6 +6407,14 @@
"safe-buffer": "^5.1.0"
}
},
"secretstore": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/secretstore/-/secretstore-0.1.0.tgz",
"integrity": "sha512-Ue4Pg5cfQBJEUu8XEMfQ2NAFyz8w0/Dqb7MiwDSN0+4JhTRy1iO48nDLb9XvETPCdbq92gWhEK0RbXfop6VoPw==",
"requires": {
"web3": "^1.0.0-beta.37"
}
},
"seek-bzip": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/seek-bzip/-/seek-bzip-1.0.5.tgz",

View File

@ -59,13 +59,13 @@
},
"homepage": "https://github.com/oceanprotocol/squid-js#readme",
"dependencies": {
"@oceanprotocol/keeper-contracts": "^0.8.7",
"@oceanprotocol/secret-store-client": "~0.0.14",
"@oceanprotocol/keeper-contracts": "^0.9.0",
"bignumber.js": "^8.0.1",
"deprecated-decorator": "^0.1.6",
"ethereumjs-util": "^6.0.0",
"node-fetch": "^2.3.0",
"save-file": "^2.3.1",
"secretstore": "^0.1.0",
"uuid": "^3.3.2",
"web3": "1.0.0-beta.37",
"whatwg-url": "^7.0.0"

View File

@ -2,11 +2,18 @@ import { URL } from "whatwg-url"
import { DDO } from "../ddo/DDO"
import DID from "../ocean/DID"
import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
import { SearchQuery } from "./query/SearchQuery"
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
const apiPath = "/api/v1/aquarius/assets/ddo"
export interface SearchQuery {
text?: string
offset: number
page: number
query: {[property: string]: string | number | string[] | number[]}
sort: {[jsonPath: string]: number}
}
/**
* Provides a interface with Aquarius.
* Aquarius provides an off-chain database store for metadata about data assets.

View File

@ -1,7 +0,0 @@
export interface SearchQuery {
text?: string
offset: number
page: number
query: {[property: string]: string | number | string[] | number[]}
sort: {[jsonPath: string]: number}
}

View File

@ -28,6 +28,10 @@ export class Brizo extends Instantiable {
return `${this.url}${apiPath}/consume`
}
public getEncryptEndpoint() {
return `${this.url}${apiPath}/publish`
}
public getComputeEndpoint(pubKey: string, serviceId: string, algo: string, container: string) {
// tslint:disable-next-line
return `${this.url}${apiPath}/compute`
@ -69,17 +73,19 @@ export class Brizo extends Instantiable {
files: File[],
destination: string,
): Promise<string> {
const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId())
const filesPromises = files
.map(async ({url}, i) => {
.map(async ({}, i) => {
let consumeUrl = serviceEndpoint
consumeUrl += `?url=${url}`
consumeUrl += `?index=${i}`
consumeUrl += `&serviceAgreementId=${agreementId}`
consumeUrl += `&consumerAddress=${account.getId()}`
consumeUrl += `&signature=${agreementIdSignature}`
try {
await this.downloadFile(
consumeUrl,
url.split("/").pop() || `file-${i}`,
`file-${i}`,
destination,
)
} catch (e) {
@ -100,4 +106,35 @@ export class Brizo extends Instantiable {
await save(await response.arrayBuffer(), path)
return path
}
public async encrypt(
did: string,
signedDid: string,
document: any,
publisher: string,
): Promise<string> {
const args = {
documentId: did,
signedDocumentId: signedDid,
document: JSON.stringify(document),
publisherAddress: publisher,
}
try {
const response = await WebServiceConnectorProvider
.getConnector()
.post(
this.getEncryptEndpoint(),
decodeURI(JSON.stringify(args)),
)
if (!response.ok) {
throw new Error("HTTP request failed")
}
return await response.text()
} catch (e) {
this.logger.error(e)
throw new Error("HTTP request failed")
}
}
}

View File

@ -18,17 +18,8 @@ export class Config {
// from outside eg. metamask
public web3Provider?: any
/* Secret Store Config */
// the uri of the secret store to connect to
public secretStoreUri: string
// the uri of the parity node to connect to
public parityUri: string
// the password of the account in the local parity node to sign the serverKeyId
public password: string
// the address of the account in the local parity node to sign the serverKeyId
public address: string
// the number of nodes in the secret store that have to agree on changes
public threshold: number
/* Squid config */
public verbose: boolean | LogLevel

View File

@ -1,7 +1,7 @@
import { SearchQuery } from "../aquarius/query/SearchQuery"
import { SearchQuery } from "../aquarius/Aquarius"
import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData"
import { Service, ServiceAuthorization } from "../ddo/Service"
import { Service } from "../ddo/Service"
import Account from "./Account"
import DID from "./DID"
import { fillConditionsWithDDO } from "../utils"
@ -45,10 +45,8 @@ export class OceanAssets extends Instantiable {
const did: DID = DID.generate()
const authorizationService = (services.find(({type}) => type === "Authorization") || {}) as ServiceAuthorization
const secretStoreUrl = authorizationService.service === "SecretStore" && authorizationService.serviceEndpoint
const encryptedFiles = await this.ocean.secretStore.encrypt(did.getId(), metadata.base.files, null, secretStoreUrl)
const encryptedFiles = await this.ocean.secretStore.encrypt(did.getId(), metadata.base.files, publisher)
const serviceAgreementTemplate = await templates.escrowAccessSecretStoreTemplate.getServiceAgreementTemplate()
@ -163,10 +161,9 @@ export class OceanAssets extends Instantiable {
const ddo = await this.resolve(did)
const {metadata} = ddo.findServiceByType("Metadata")
const authorizationService = ddo.findServiceByType("Authorization")
const accessService = ddo.findServiceById(serviceDefinitionId)
const files = metadata.base.encryptedFiles
const files = metadata.base.files
const {serviceEndpoint} = accessService
@ -174,13 +171,6 @@ export class OceanAssets extends Instantiable {
throw new Error("Consume asset failed, service definition is missing the `serviceEndpoint`.")
}
const secretStoreUrl = authorizationService.service === "SecretStore" && authorizationService.serviceEndpoint
this.logger.log("Decrypting files")
const decryptedFiles = await this.ocean.secretStore
.decrypt(did, files, consumerAccount, secretStoreUrl)
this.logger.log("Files decrypted")
this.logger.log("Consuming files")
resultPath = resultPath ? `${resultPath}/datafile.${ddo.shortId()}.${agreementId}/` : undefined
@ -188,7 +178,7 @@ export class OceanAssets extends Instantiable {
agreementId,
serviceEndpoint,
consumerAccount,
decryptedFiles,
files,
resultPath,
)
this.logger.log("Files consumed")

View File

@ -1,6 +1,6 @@
import SecretStoreProvider from "../secretstore/SecretStoreProvider"
import Account from "./Account"
import { noDidPrefixed } from "../utils"
import { File } from "../ddo/MetaData"
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
/**
@ -27,35 +27,9 @@ export class OceanSecretStore extends Instantiable {
* @param {string} publisher Publisher account.
* @return {Promise<string>} Encrypted text.
*/
public async encrypt(did: string, content: any, publisher?: Account, secretStoreUrl?: string): Promise<string> {
return await this.getSecretStoreByAccount(publisher, secretStoreUrl)
.encryptDocument(noDidPrefixed(did), content)
}
public async encrypt(did: string, document: any, publisher: Account): Promise<string> {
const signedDid = await this.ocean.utils.signature.signText(noDidPrefixed(did), publisher.getId(), publisher.getPassword())
/**
* Decrypt an encrypted text using the stored encryption keys associated with the `did`.
* Decryption requires that the account owner has access permissions for this `did`
* @param {string} did Decentralized ID.
* @param {string} content Content to be encrypted.
* @param {string} consumer cONSUMER account.
* @return {Promise<string>} Encrypted text.
*/
public async decrypt(did: string, content: string, consumer?: Account, secretStoreUrl?: string): Promise<any> {
return await this.getSecretStoreByAccount(consumer, secretStoreUrl)
.decryptDocument(noDidPrefixed(did), content)
}
private getSecretStoreByAccount(account: Account, secretStoreUrl?: string) {
const config: any = {...this.config}
if (account) {
config.address = account.getId()
}
if (account && account.getPassword()) {
config.password = account.getPassword()
}
if (secretStoreUrl) {
config.secretStoreUri = secretStoreUrl
}
return SecretStoreProvider.getSecretStore(config)
return await this.ocean.brizo.encrypt(noDidPrefixed(did), signedDid, document, publisher.getId())
}
}

View File

@ -1,22 +0,0 @@
import SecretStore from "@oceanprotocol/secret-store-client"
import SecretStoreConfig from "@oceanprotocol/secret-store-client/dist/models/SecretStoreConfig"
export default class SecretStoreProvider {
public static getSecretStore(config: SecretStoreConfig): SecretStore {
const {secretStoreUri, parityUri, password, address, threshold} = config
config = {secretStoreUri, parityUri, password, address, threshold}
// Cleaning undefined parameters
Object.keys(config)
.forEach((key) => config[key] || config[key] === 0 || delete config[key])
const configRef = JSON.stringify(config)
if (!SecretStoreProvider.secretStoreWithConfig.get(configRef)) {
SecretStoreProvider.secretStoreWithConfig.set(configRef, new SecretStore({...config}))
}
return SecretStoreProvider.secretStoreWithConfig.get(configRef)
}
private static secretStoreWithConfig = new Map<string, SecretStore>()
}

View File

@ -40,47 +40,28 @@ describe("DIDRegistry", () => {
// register the second attribute with the same did
const data = "asdsad"
const receipt = await didRegistry.registerAttribute(did, "0123456789abcdef", [], data, ownerAccount.getId())
assert(receipt.status)
assert(receipt.events.DIDAttributeRegistered)
assert.isTrue(receipt.status)
assert.isDefined(receipt.events.DIDAttributeRegistered)
}
})
})
// describe("#getOwner()", () => {
describe("#getDIDOwner()", () => {
it("should get the owner of a did properly", async () => {
const ownerAccount: Account = (await ocean.accounts.list())[0]
const did = generateId()
const data = "my nice provider, is nice"
await didRegistry.registerAttribute(did, "0123456789abcdef", [], data, ownerAccount.getId())
// it("should get the owner of a did properly", async () => {
// const ownerAccount: Account = (await ocean.accounts.list())[0]
// const did = generateId()
// const data = "my nice provider, is nice"
// await didRegistry.registerAttribute(did, "0123456789abcdef", data, ownerAccount.getId())
const owner = await didRegistry.getDIDOwner(did)
// const owner = await didRegistry.getOwner(did)
// assert(owner === ownerAccount.getId(), `Got ${owner} but expected ${ownerAccount.getId()}`)
// })
// it("should get 0x00.. for a not registered did", async () => {
// const owner = await didRegistry.getOwner("1234")
// assert(owner === "0x0000000000000000000000000000000000000000")
// })
// })
// describe("#getUpdateAt()", () => {
// it("should the block number of the last update of the did attribute", async () => {
// const ownerAccount: Account = (await ocean.accounts.list())[0]
// const did = generateId()
// const data = "my nice provider, is nice"
// await didRegistry.registerAttribute(did, "0123456789abcdef", data, ownerAccount.getId())
// const updatedAt: number = await didRegistry.getUpdateAt(did)
// assert(updatedAt > 0)
// Logger.log(typeof updatedAt)
// })
// })
assert.equal(owner, ownerAccount.getId(), `Got ${owner} but expected ${ownerAccount.getId()}`)
})
it("should get 0x0 for a not registered did", async () => {
const owner = await didRegistry.getDIDOwner("1234")
assert.equal(owner, `0x${"0".repeat(40)}`)
})
})
})

View File

@ -4,7 +4,6 @@ import * as spies from "chai-spies"
import Account from "../../src/ocean/Account"
import { Ocean } from "../../src/ocean/Ocean"
import { OceanSecretStore } from "../../src/ocean/OceanSecretStore"
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
import config from "../config"
use(spies)
@ -14,10 +13,11 @@ describe("OceanSecretStore", () => {
let oceanSecretStore: OceanSecretStore
let accounts: Account[]
let ocean: Ocean
const did = "a".repeat(64)
before(async () => {
const ocean = await Ocean.getInstance(config)
ocean = await Ocean.getInstance(config)
oceanSecretStore = ocean.secretStore
accounts = await ocean.accounts.list()
})
@ -28,31 +28,13 @@ describe("OceanSecretStore", () => {
describe("#encrypt()", () => {
it("should encrypt a content", async () => {
const secretStoreToSpy = SecretStoreProvider.getSecretStore({...config, address: accounts[0].getId()})
const secretStoreEncryptSpy = spy.on(secretStoreToSpy, "encryptDocument", () => "encryptedResult")
const secretStoreProviderGetInstanceSpy = spy.on(SecretStoreProvider, "getSecretStore", () => secretStoreToSpy)
const secretStoreEncryptSpy = spy.on(ocean.brizo, "encrypt", () => "encryptedResult")
const result = await oceanSecretStore.encrypt(did, "test", accounts[0])
expect(secretStoreProviderGetInstanceSpy).to.have.been.called.with({...config, address: accounts[0].getId()})
expect(secretStoreEncryptSpy).to.have.been.called.with(did, "test")
assert.equal(result, "encryptedResult", "Result doesn't match")
})
})
describe("#decrypt()", () => {
it("should decrypt a content", async () => {
const secretStoreToSpy = SecretStoreProvider.getSecretStore({...config, address: accounts[0].getId()})
const secretStoreEncryptSpy = spy.on(secretStoreToSpy, "decryptDocument", () => "decryptedResult")
const secretStoreProviderGetInstanceSpy = spy.on(SecretStoreProvider, "getSecretStore", () => secretStoreToSpy)
const result = await oceanSecretStore.decrypt(did, "encryptedContent", accounts[0])
expect(secretStoreProviderGetInstanceSpy).to.have.been.called.with({...config, address: accounts[0].getId()})
expect(secretStoreEncryptSpy).to.have.been.called.with(did, "encryptedContent")
assert.equal(result, "decryptedResult", "Result doesn't match")
})
})
})