Skip to content

Commit

Permalink
fix: update regex to match DID specification (#105)
Browse files Browse the repository at this point in the history
fixes #104
  • Loading branch information
stephhuynh18 committed Oct 26, 2021
1 parent 94bb13f commit 4f71bc5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/__tests__/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,18 @@ describe('resolver', () => {
'foo:bar': 'high',
},
})

expect(parse('did:web:example.com%3A8443')).toEqual({
method: 'web',
id: 'example.com%3A8443',
didUrl: 'did:web:example.com%3A8443',
did: 'did:web:example.com%3A8443',
})

expect(parse('did:web:example.com:path:some%2Bsubpath')).toEqual({
method: 'web',
id: 'example.com:path:some%2Bsubpath',
didUrl: 'did:web:example.com:path:some%2Bsubpath',
did: 'did:web:example.com:path:some%2Bsubpath',
})

expect(
parse('did:example:test:21tDAKCERh95uGgKbJNHYp;service=agent;foo:bar=high/some/path?foo=bar#key1')
).toEqual({
Expand All @@ -115,6 +112,18 @@ describe('resolver', () => {
'foo:bar': 'high',
},
})
expect(parse('did:123:test::test2')).toEqual({
method: '123',
id: 'test::test2',
didUrl: 'did:123:test::test2',
did: 'did:123:test::test2',
})
expect(parse('did:method:%12%AF')).toEqual({
method: 'method',
id: '%12%AF',
didUrl: 'did:method:%12%AF',
did: 'did:method:%12%AF',
})
})

it('returns null if non compliant', () => {
Expand All @@ -124,6 +133,10 @@ describe('resolver', () => {
expect(parse('did:uport:')).toEqual(null)
expect(parse('did:uport:1234_12313***')).toEqual(null)
expect(parse('2nQtiQG6Cgm1GYTBaaKAgr76uY7iSexUkqX')).toEqual(null)
expect(parse('did:method:%12%1')).toEqual(null)
expect(parse('did:method:%1233%Ay')).toEqual(null)
expect(parse('did:CAP:id')).toEqual(null)
expect(parse('did:method:id::anotherid%r9')).toEqual(null)
})
})

Expand Down
7 changes: 4 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ export function noCache(parsed: ParsedDID, resolve: WrappedResolver): Promise<DI
return resolve()
}

const ID_CHAR = '[a-zA-Z0-9_.%-]'
const METHOD = '([a-zA-Z0-9_]+)'
const METHOD_ID = `(${ID_CHAR}+(:${ID_CHAR}+)*)`
const PCT_ENCODED = '(?:%[0-9a-fA-F]{2})'
const ID_CHAR = `(?:[a-zA-Z0-9._-]|${PCT_ENCODED})`
const METHOD = '([a-z0-9]+)'
const METHOD_ID = `((?:${ID_CHAR}*:)*(${ID_CHAR}+))`
const PARAM_CHAR = '[a-zA-Z0-9_.:%-]'
const PARAM = `;${PARAM_CHAR}+=${PARAM_CHAR}*`
const PARAMS = `((${PARAM})*)`
Expand Down

0 comments on commit 4f71bc5

Please sign in to comment.