2019-06-20 00:20:09 +02:00
|
|
|
import Web3Provider from '../keeper/Web3Provider'
|
|
|
|
import { Ocean } from '../ocean/Ocean'
|
|
|
|
import { Authentication } from './Authentication'
|
|
|
|
import { Proof } from './Proof'
|
|
|
|
import { PublicKey } from './PublicKey'
|
|
|
|
import { Service, ServiceType } from './Service'
|
2018-11-01 11:04:59 +01:00
|
|
|
|
2019-01-09 16:08:08 +01:00
|
|
|
/**
|
|
|
|
* DID Descriptor Object.
|
|
|
|
* Contains all the data related to an asset.
|
|
|
|
*/
|
2019-02-04 11:46:24 +01:00
|
|
|
export class DDO {
|
2019-01-09 16:08:08 +01:00
|
|
|
/**
|
|
|
|
* Serializes the DDO object.
|
|
|
|
* @param {DDO} DDO to be serialized.
|
|
|
|
* @return {string} DDO serialized.
|
|
|
|
*/
|
2018-11-01 11:04:59 +01:00
|
|
|
public static serialize(ddo: DDO): string {
|
|
|
|
return JSON.stringify(ddo, null, 2)
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:08:08 +01:00
|
|
|
/**
|
|
|
|
* Deserializes the DDO object.
|
|
|
|
* @param {DDO} DDO to be deserialized.
|
|
|
|
* @return {string} DDO deserialized.
|
|
|
|
*/
|
2018-11-01 11:04:59 +01:00
|
|
|
public static deserialize(ddoString: string): DDO {
|
|
|
|
const ddo = JSON.parse(ddoString)
|
|
|
|
|
2019-02-04 16:26:56 +01:00
|
|
|
return new DDO(ddo)
|
2018-11-01 11:04:59 +01:00
|
|
|
}
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
public '@context': string = 'https://w3id.org/did/v1'
|
2019-02-04 11:46:24 +01:00
|
|
|
|
2019-01-09 16:08:08 +01:00
|
|
|
/**
|
|
|
|
* DID, descentralized ID.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2019-03-11 22:52:53 +01:00
|
|
|
public id: string = null
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-02-14 11:30:03 +01:00
|
|
|
public created: string
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2020-02-11 21:11:37 +01:00
|
|
|
public updated: string
|
|
|
|
|
2019-03-11 22:52:53 +01:00
|
|
|
public publicKey: PublicKey[] = []
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-03-11 22:52:53 +01:00
|
|
|
public authentication: Authentication[] = []
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-03-11 22:52:53 +01:00
|
|
|
public service: Service[] = []
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-02-14 11:30:03 +01:00
|
|
|
public proof: Proof
|
2018-11-01 11:04:59 +01:00
|
|
|
|
2019-03-14 18:44:26 +01:00
|
|
|
public constructor(ddo: Partial<DDO> = {}) {
|
2019-03-11 22:52:53 +01:00
|
|
|
Object.assign(this, ddo, {
|
2019-11-15 00:00:10 +01:00
|
|
|
created:
|
|
|
|
(ddo && ddo.created) || new Date().toISOString().replace(/\.[0-9]{3}/, '')
|
2019-03-11 22:52:53 +01:00
|
|
|
})
|
2018-11-01 11:04:59 +01:00
|
|
|
}
|
2018-11-14 15:44:25 +01:00
|
|
|
|
2019-02-21 17:58:54 +01:00
|
|
|
public shortId(): string {
|
2019-06-20 00:20:09 +02:00
|
|
|
return this.id.replace('did:op:', '')
|
2019-02-21 17:58:54 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 16:08:08 +01:00
|
|
|
/**
|
2019-08-16 16:12:42 +02:00
|
|
|
* Finds a service of a DDO by index.
|
|
|
|
* @param {number} Service index.
|
2019-01-09 16:08:08 +01:00
|
|
|
* @return {Service} Service.
|
|
|
|
*/
|
2019-08-16 17:00:14 +02:00
|
|
|
public findServiceById<T extends ServiceType>(index: number): Service<T> {
|
2019-09-13 13:38:23 +02:00
|
|
|
if (isNaN(index)) {
|
2019-08-16 16:12:42 +02:00
|
|
|
throw new Error('index is not set')
|
2018-11-14 15:44:25 +01:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:15:56 +02:00
|
|
|
const service = this.service.find((s) => s.index === index)
|
2018-11-14 15:44:25 +01:00
|
|
|
|
2019-02-15 19:36:30 +01:00
|
|
|
return service as Service<T>
|
2018-11-14 15:44:25 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 16:08:08 +01:00
|
|
|
/**
|
|
|
|
* Finds a service of a DDO by type.
|
|
|
|
* @param {string} serviceType Service type.
|
|
|
|
* @return {Service} Service.
|
|
|
|
*/
|
2019-09-09 12:18:54 +02:00
|
|
|
public findServiceByType<T extends ServiceType>(serviceType: T): Service<T> {
|
2018-11-14 15:44:25 +01:00
|
|
|
if (!serviceType) {
|
2019-06-20 00:20:09 +02:00
|
|
|
throw new Error('serviceType not set')
|
2018-11-14 15:44:25 +01:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:15:56 +02:00
|
|
|
return this.service.find((s) => s.type === serviceType) as Service<T>
|
2018-11-14 15:44:25 +01:00
|
|
|
}
|
2019-02-04 16:26:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the checksum using the current content.
|
|
|
|
* @return {string[]} DDO checksum.
|
|
|
|
*/
|
|
|
|
public getChecksum(): string {
|
2019-08-16 16:12:42 +02:00
|
|
|
const { attributes } = this.findServiceByType('metadata')
|
|
|
|
const { files, name, author, license } = attributes.main
|
2019-02-04 16:26:56 +01:00
|
|
|
|
|
|
|
const values = [
|
2020-05-19 16:15:56 +02:00
|
|
|
...(files || []).map(({ checksum }) => checksum).filter((_) => !!_),
|
2019-02-04 16:26:56 +01:00
|
|
|
name,
|
|
|
|
author,
|
|
|
|
license,
|
2019-06-20 00:20:09 +02:00
|
|
|
this.id
|
2019-02-04 16:26:56 +01:00
|
|
|
]
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
return Web3Provider.getWeb3()
|
|
|
|
.utils.sha3(values.join(''))
|
|
|
|
.replace(/^0x([a-f0-9]{64})(:!.+)?$/i, '0x$1')
|
2019-02-04 16:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates proof using personal sing.
|
2019-11-11 12:27:18 +01:00
|
|
|
* @param {Ocean} ocean Ocean instance.
|
2019-02-04 16:26:56 +01:00
|
|
|
* @param {string} publicKey Public key to be used on personal sign.
|
2019-08-15 13:23:56 +02:00
|
|
|
* @param {string} password Password if it's required.
|
2019-02-04 16:26:56 +01:00
|
|
|
* @return {Promise<Proof>} Proof object.
|
|
|
|
*/
|
2019-11-15 00:00:10 +01:00
|
|
|
public async generateProof(
|
|
|
|
ocean: Ocean,
|
|
|
|
publicKey: string,
|
|
|
|
password?: string
|
|
|
|
): Promise<Proof> {
|
2019-02-04 18:13:54 +01:00
|
|
|
const checksum = this.getChecksum()
|
2019-02-04 16:26:56 +01:00
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
const signature = await ocean.utils.signature.signText(
|
|
|
|
checksum,
|
|
|
|
publicKey,
|
|
|
|
password
|
|
|
|
)
|
2019-02-04 16:26:56 +01:00
|
|
|
|
|
|
|
return {
|
2019-06-20 00:20:09 +02:00
|
|
|
created: new Date().toISOString().replace(/\.[0-9]{3}/, ''),
|
2019-02-04 16:26:56 +01:00
|
|
|
creator: publicKey,
|
2019-06-20 00:20:09 +02:00
|
|
|
type: 'DDOIntegritySignature',
|
|
|
|
signatureValue: signature
|
2019-02-04 16:26:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates and adds a proof using personal sing on the DDO.
|
2019-11-11 12:27:18 +01:00
|
|
|
* @param {Ocean} ocean Ocean instance.
|
2019-02-04 16:26:56 +01:00
|
|
|
* @param {string} publicKey Public key to be used on personal sign.
|
2019-11-12 14:21:29 +01:00
|
|
|
* @param {string} password Password if it's required.
|
2019-02-04 16:26:56 +01:00
|
|
|
* @return {Promise<Proof>} Proof object.
|
|
|
|
*/
|
2019-11-15 00:00:10 +01:00
|
|
|
public async addProof(
|
|
|
|
ocean: Ocean,
|
|
|
|
publicKey: string,
|
|
|
|
password?: string
|
|
|
|
): Promise<void> {
|
2019-02-04 16:26:56 +01:00
|
|
|
if (this.proof) {
|
2019-06-20 00:20:09 +02:00
|
|
|
throw new Error('Proof already exists')
|
2019-02-04 16:26:56 +01:00
|
|
|
}
|
2019-11-11 12:27:18 +01:00
|
|
|
this.proof = await this.generateProof(ocean, publicKey, password)
|
2019-02-04 16:26:56 +01:00
|
|
|
}
|
2018-11-01 11:04:59 +01:00
|
|
|
}
|