mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
42 lines
797 B
TypeScript
42 lines
797 B
TypeScript
|
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
|
||
|
}
|
||
|
}
|