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

65 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-12-17 15:54:58 +01:00
import IdGenerator from "./IdGenerator"
const prefix = "did:op:"
2019-01-09 16:17:23 +01:00
/**
* Decentralized ID.
*/
2018-12-17 15:54:58 +01:00
export default class DID {
2019-01-09 16:17:23 +01:00
/**
* Parses a DID from a string.
* @param {string} didString DID in string.
* @return {DID}
*/
2018-12-17 15:54:58 +01:00
public static parse(didString: string): DID {
let did: DID
if (didString.startsWith(prefix)) {
const id = didString.split(prefix)[1]
if (!id.startsWith("0x")) {
did = new DID(id)
}
}
if (!did) {
throw new Error(`Parsing DID failed, ${didString}`)
}
return did
}
2019-01-09 16:17:23 +01:00
/**
* Returns a new DID.
* @return {DID}
*/
2018-12-17 15:54:58 +01:00
public static generate(): DID {
return new DID(IdGenerator.generateId())
}
2019-01-09 16:17:23 +01:00
/**
* ID.
* @type {string}
*/
2018-12-17 15:54:58 +01:00
private id: string
private constructor(id: string) {
this.id = id
}
2019-01-09 16:17:23 +01:00
/**
* Returns the DID.
* @return {string}
*/
2018-12-17 15:54:58 +01:00
public getDid(): string {
return `${prefix}${this.id}`
}
2019-01-09 16:17:23 +01:00
/**
* Returns the ID.
* @return {string}
*/
2018-12-17 15:54:58 +01:00
public getId(): string {
return this.id
}
}