mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
init DID tools
This commit is contained in:
parent
484ec08d61
commit
5fcc183a09
6275
package-lock.json
generated
6275
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
64
src/DID.ts
Normal file
64
src/DID.ts
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
|
||||
DID Tools to parse/create and convert DID and ID
|
||||
|
||||
*/
|
||||
|
||||
import * as Web3 from "web3"
|
||||
import {URL} from "whatwg-url"
|
||||
|
||||
const OCEAN_DID_METHOD = "op"
|
||||
|
||||
export function didGenerate(didId: string, path?: string, fragment?: string, method?: string) {
|
||||
method = method === undefined ? OCEAN_DID_METHOD : method
|
||||
method = method.toLowerCase().replace(/[^a-z0-9]/g, "")
|
||||
didId = didId.replace(/[^a-zA-Z0-9-.]/g, "")
|
||||
const did = ["did:", method, ":", didId]
|
||||
if (path) {
|
||||
did.push("/")
|
||||
did.push(path)
|
||||
}
|
||||
if ( fragment ) {
|
||||
did.push("#")
|
||||
did.push(fragment)
|
||||
}
|
||||
return did.join("")
|
||||
}
|
||||
|
||||
interface IDIDParse {
|
||||
method: string,
|
||||
id: string,
|
||||
path?: string,
|
||||
fragment?: string,
|
||||
idHex?: string,
|
||||
}
|
||||
|
||||
export function didParse(did: string): IDIDParse {
|
||||
|
||||
let result: IDIDParse = null
|
||||
if ( typeof did !== "string" ) {
|
||||
throw TypeError("DID must be a string")
|
||||
}
|
||||
const match = did.match(/^did:([a-z0-9]+):([a-zA-Z0-9-.]+)(.*)/)
|
||||
if ( match ) {
|
||||
|
||||
const url = new URL(match[3], "http://localhost")
|
||||
|
||||
result = {
|
||||
method: match[1],
|
||||
id: match[2],
|
||||
path: url.pathname,
|
||||
fragment: url.hash,
|
||||
idHex: null,
|
||||
}
|
||||
if ( result.method === OCEAN_DID_METHOD && result.id.match(/^[0-9A-Fa-f]{1,64}$/) ) {
|
||||
result.idHex = Web3.utils.toHex("0x" + result.id)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export function isDIDValid(did: string): boolean {
|
||||
const result = didParse(did)
|
||||
return result && result.idHex != null
|
||||
}
|
58
test/DID.test.ts
Normal file
58
test/DID.test.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import * as assert from "assert"
|
||||
import * as didTools from "../src/DID"
|
||||
|
||||
import * as Web3 from "web3"
|
||||
|
||||
describe("DID Tests", () => {
|
||||
|
||||
describe("did generate", () => {
|
||||
|
||||
it("should generate a valid DID", async () => {
|
||||
|
||||
const testId = Web3.utils.randomHex(32) + "abcdefghijklmnopqrstuvwxyz"
|
||||
const testMethod = "op"
|
||||
const validDID = "did:" + testMethod + ":" + testId
|
||||
|
||||
const did = didTools.didGenerate(testId)
|
||||
assert(did)
|
||||
assert(did.match(/did:op:[a-h0-9]+/))
|
||||
assert(did === validDID)
|
||||
|
||||
})
|
||||
it("should parse a valid DID", async () => {
|
||||
const testId = Web3.utils.randomHex(32) + "abcdefghijklmnopqrstuvwxyz"
|
||||
const testMethod = "op"
|
||||
const validDID = "did:" + testMethod + ":" + testId
|
||||
const result = didTools.didParse(validDID + "/testpath#fragment")
|
||||
assert(result)
|
||||
assert(result.method === testMethod)
|
||||
assert(result.id === testId)
|
||||
assert(result.path === "/testpath")
|
||||
assert(result.fragment === "#fragment")
|
||||
})
|
||||
|
||||
it("should parse a valid Ocean DID", async () => {
|
||||
const testId = Web3.utils.randomHex(32).substring(2)
|
||||
const testMethod = "op"
|
||||
const validDID = "did:" + testMethod + ":" + testId
|
||||
const result = didTools.didParse(validDID + "/testpath#fragment")
|
||||
assert(result)
|
||||
assert(result.method === testMethod)
|
||||
assert(result.id === testId)
|
||||
assert(result.path === "/testpath")
|
||||
assert(result.fragment === "#fragment")
|
||||
assert(result.idHex === "0x" + testId)
|
||||
|
||||
})
|
||||
|
||||
it("should validate an Ocean DID", async () => {
|
||||
const testId = Web3.utils.randomHex(32).substring(2)
|
||||
const testMethod = "op"
|
||||
const validDID = "did:" + testMethod + ":" + testId
|
||||
assert(didTools.isDIDValid(validDID))
|
||||
assert(!didTools.isDIDValid(validDID + "abcdef"))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user