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

added better DID verification

This commit is contained in:
Pedro Gutiérrez 2019-01-22 14:14:49 +01:00
parent 69ed0428b1
commit 5e4a33f4e2
2 changed files with 7 additions and 8 deletions

View File

@ -14,11 +14,10 @@ export default class DID {
*/ */
public static parse(didString: string): DID { public static parse(didString: string): DID {
let did: DID let did: DID
if (didString.startsWith(prefix)) { const didMatch = didString.match(/^did:op:([a-f0-9]{64})$/i)
const id = didString.split(prefix)[1]
if (!id.startsWith("0x")) { if (didMatch) {
did = new DID(id) did = new DID(didMatch[1])
}
} }
if (!did) { if (!did) {

View File

@ -13,7 +13,7 @@ describe("DID", () => {
describe("#parse()", () => { describe("#parse()", () => {
it("should parse a valid did", () => { it("should parse a valid did", () => {
const id = "1234" const id = "a".repeat(64)
const did: DID = DID.parse(`did:op:${id}`) const did: DID = DID.parse(`did:op:${id}`)
assert(did) assert(did)
@ -33,7 +33,7 @@ describe("DID", () => {
it("should throw if id does not match", (done) => { it("should throw if id does not match", (done) => {
const id = "0x1234" const id = "xyz"
try { try {
const did: DID = DID.parse(`did:op:${id}`) const did: DID = DID.parse(`did:op:${id}`)
assert(!did) assert(!did)
@ -56,7 +56,7 @@ describe("DID", () => {
describe("#getDid()", () => { describe("#getDid()", () => {
it("should return only the id part of the did", () => { it("should return only the id part of the did", () => {
const id = "1234" const id = "a".repeat(64)
const did: DID = DID.parse(`did:op:${id}`) const did: DID = DID.parse(`did:op:${id}`)
assert(did) assert(did)