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

42 lines
797 B
TypeScript
Raw Normal View History

2018-12-17 15:54:58 +01:00
import IdGenerator from "./IdGenerator"
const prefix = "did:op:"
export default class DID {
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
}
public static generate(): DID {
return new DID(IdGenerator.generateId())
}
private id: string
private constructor(id: string) {
this.id = id
}
public getDid(): string {
return `${prefix}${this.id}`
}
public getId(): string {
return this.id
}
}