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

adding proof and checksum on asset creation flow

This commit is contained in:
Pedro Gutiérrez 2019-02-04 17:45:49 +01:00 committed by Pedro Gutiérrez
parent d365650a7a
commit c24aea4bda
7 changed files with 73 additions and 14 deletions

View File

@ -90,7 +90,7 @@ export class DDO {
const {files, name, author, license} = metadata.base
const values = [
...files
...(files || [])
.map(({checksum}) => checksum)
.filter(_ => !!_),
name,
@ -109,7 +109,6 @@ export class DDO {
* @return {Promise<Proof>} Proof object.
*/
public async generateProof(publicKey: string, password?: string): Promise<Proof> {
const checksum = this.getChecksum();
const signature = await signatureHelpers.signText(checksum, publicKey, password)
@ -122,6 +121,17 @@ export class DDO {
}
}
/**
* Generated and adds the checksum.
*/
public addChecksum(): void {
const metadataService = this.findServiceByType('Metadata')
if (metadataService.metadata.base.checksum) {
throw new Error('Checksum already exists')
}
metadataService.metadata.base.checksum = this.getChecksum()
}
/**
* Generates and adds a proof using personal sing on the DDO.
* @param {string} publicKey Public key to be used on personal sign.

View File

@ -160,6 +160,8 @@ export class MetaDataBase {
* @type {string}
*/
checksum: string
encryptedFiles?: any
}
/**
@ -212,6 +214,13 @@ export interface AdditionalInformation {
uri: string
mediaType: string
}[]
/**
* Checksum of attributes to be able to compare if there are changes in
* the asset that you are purchasing.
* @type {string}
*/
checksum: string
}
export interface MetaData {

View File

@ -1,6 +1,6 @@
{
"nodeUri": "http://localhost:8545",
"aquariusUri": "https://nginx-aquarius.dev-ocean.com",
"aquariusUri": "http://localhost:5000",
"brizoUri": "http://localhost:8030",
"parityUri": "http://localhost:9545",
"secretStoreUri": "http://localhost:12001",

View File

@ -12,6 +12,24 @@ import OceanBase from "./OceanBase"
export default class Account extends OceanBase {
private balance: Balance
private password?: string
/**
* Set account password.
* @param {string} password Password for account.
*/
setPassword(password: string): void {
this.password = password
}
/**
* Returns account password.
* @return {string} Account password.
*/
getPassword(): string {
return this.password
}
/**
* Balance of Ocean Token.
* @return {Promise<number>}

View File

@ -8,9 +8,9 @@ import AquariusProvider from "../aquarius/AquariusProvider"
import SearchQuery from "../aquarius/query/SearchQuery"
import BrizoProvider from "../brizo/BrizoProvider"
import ConfigProvider from "../ConfigProvider"
import DDO from "../ddo/DDO"
import MetaData from "../ddo/MetaData"
import Service from "../ddo/Service"
import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData"
import { Service } from "../ddo/Service"
import ContractEvent from "../keeper/Event"
import Config from "../models/Config"
import SecretStoreProvider from "../secretstore/SecretStoreProvider"
@ -110,8 +110,8 @@ export default class Ocean {
* @return {Promise<DDO>}
*/
@deprecated("OceanAssets.create")
public async registerAsset(metadata: MetaData, publisher: Account): Promise<DDO> {
return await this.assets.create(metadata, publisher)
public async registerAsset(metadata: MetaData, publisher: Account, services?: Service[]): Promise<DDO> {
return await this.assets.create(metadata, publisher, services)
}
/**

View File

@ -57,7 +57,7 @@ export default class OceanAssets {
* @param {Account} publisher Publicher account.
* @return {Promise<DDO>}
*/
public async create(metadata: MetaData, publisher: Account): Promise<DDO> {
public async create(metadata: MetaData, publisher: Account, services?: Service[]): Promise<DDO> {
const {didRegistry} = await Keeper.getInstance()
const aquarius = AquariusProvider.getAquarius()
const brizo = BrizoProvider.getBrizo()
@ -67,9 +67,10 @@ export default class OceanAssets {
const computeServiceDefintionId: string = "1"
const metadataServiceDefinitionId: string = "2"
metadata.base.contentUrls =
[await SecretStoreProvider.getSecretStore()
.encryptDocument(did.getId(), metadata.base.contentUrls)]
metadata.base.encryptedFiles = [
await SecretStoreProvider.getSecretStore()
.encryptDocument(did.getId(), metadata.base.contentUrls)
]
const template = new Access()
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
@ -129,11 +130,26 @@ export default class OceanAssets {
type: "Metadata",
serviceEndpoint,
serviceDefinitionId: metadataServiceDefinitionId,
metadata,
metadata: {
// Default values
curation: {
rating: 0,
numVotes: 0,
},
additionalInformation: {
updateFrecuency: "yearly",
structuredMarkup: [],
},
// Overwrites defaults
...metadata,
},
},
],
})
ddo.addChecksum()
await ddo.addProof(publisher.getId(), publisher.getPassword())
const storedDdo = await aquarius.storeDDO(ddo)
// Logger.log(JSON.stringify(storedDdo, null, 2))

View File

@ -1,9 +1,15 @@
import Web3Provider from '../keeper/Web3Provider'
import Logger from "./Logger"
export async function signText(text: string, publicKey: string, password?: string): Promise<string> {
const web3 = Web3Provider.getWeb3()
return await web3.eth.personal.sign(text, publicKey, password)
try {
return await web3.eth.personal.sign(text, publicKey, password)
} catch (e) {
Logger.error(e.message)
throw new Error('Error execution personal sign')
}
}
export async function verifyText(text: string, signature: string): Promise<string> {