From bd28601bf9dea55d9f0292b50254a15c268b549b Mon Sep 17 00:00:00 2001 From: mcclurejt Date: Wed, 8 Feb 2023 11:50:39 -0300 Subject: [PATCH 1/2] expand the DID scalar regex to accommodate did:pkh --- src/scalars/DID.ts | 3 ++- tests/DID.test.ts | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/scalars/DID.ts b/src/scalars/DID.ts index ba91fdd6b..5f7203355 100644 --- a/src/scalars/DID.ts +++ b/src/scalars/DID.ts @@ -2,7 +2,8 @@ import { GraphQLScalarType, Kind, GraphQLScalarTypeConfig, ASTNode } from 'graph import { createGraphQLError } from '../error.js'; // See: https://www.w3.org/TR/2021/PR-did-core-20210803/#did-syntax -const DID_REGEX = /^did:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+$/; +const DID_REGEX = + /^did:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*:?[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*:?[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*$/; const validate = (value: any, ast?: ASTNode) => { if (typeof value !== 'string') { diff --git a/tests/DID.test.ts b/tests/DID.test.ts index 9e4c794ac..6a2d28526 100644 --- a/tests/DID.test.ts +++ b/tests/DID.test.ts @@ -3,7 +3,7 @@ import { Kind } from 'graphql/language'; import { GraphQLDID } from '../src/scalars/DID.js'; describe('DID', () => { - describe('valid - DID', () => { + describe('valid - DID example', () => { test('serialize', () => { expect(GraphQLDID.serialize('did:example:123456789abcdefghi')).toBe('did:example:123456789abcdefghi'); }); @@ -19,6 +19,29 @@ describe('DID', () => { }); }); + describe('valid - DID pkh', () => { + test('serialize', () => { + expect(GraphQLDID.serialize('did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377')).toBe( + 'did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377' + ); + }); + + test('parseValue', () => { + expect(GraphQLDID.parseValue('did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377')).toEqual( + 'did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377' + ); + }); + + test('parseLiteral', () => { + expect( + GraphQLDID.parseLiteral( + { value: 'did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377', kind: Kind.STRING }, + {} + ) + ).toEqual('did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377'); + }); + }); + describe('invalid', () => { describe('not a DID', () => { expect(() => GraphQLDID.serialize('invaliddid')).toThrow(/Value is not a valid DID/); From 56ce99cf45653dcedab6997bb9e1acf23d7cfdf9 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Fri, 24 Mar 2023 14:47:34 +0300 Subject: [PATCH 2/2] Add changest --- .changeset/curly-pugs-shave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/curly-pugs-shave.md diff --git a/.changeset/curly-pugs-shave.md b/.changeset/curly-pugs-shave.md new file mode 100644 index 000000000..e70479269 --- /dev/null +++ b/.changeset/curly-pugs-shave.md @@ -0,0 +1,5 @@ +--- +'graphql-scalars': patch +--- + +Expand the DID scalar regex to accommodate did:pkh