-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix did:web encoding/decoding (#3454)
- Loading branch information
1 parent
fb64d50
commit cc2a122
Showing
12 changed files
with
254 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@atproto-labs/did-resolver": patch | ||
--- | ||
|
||
Improve error descriptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@atproto/did": patch | ||
--- | ||
|
||
Fix encoding and decoding of did:web |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** @type {import('jest').Config} */ | ||
module.exports = { | ||
displayName: 'PDS', | ||
transform: { '^.+\\.(t|j)s$': '@swc/jest' }, | ||
// Jest requires all ESM dependencies to be transpiled (even if they are | ||
// dynamically import()ed). | ||
transformIgnorePatterns: [ | ||
`/node_modules/.pnpm/(?!(get-port|lande|toygrad)@)`, | ||
], | ||
testTimeout: 60000, | ||
setupFiles: ['<rootDir>/../../jest.setup.ts'], | ||
moduleNameMapper: { '^(\\.\\.?\\/.+)\\.js$': ['$1.ts', '$1.js'] }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { InvalidDidError } from '../../src/did-error.js' | ||
import { Did } from '../../src/did.js' | ||
import { asDidPlc, assertDidPlc, isDidPlc } from '../../src/methods/plc.js' | ||
|
||
const VALID: Did<'plc'>[] = [ | ||
'did:plc:l3rouwludahu3ui3bt66mfvj', | ||
'did:plc:aaaaaaaaaaaaaaaaaaaaaaaa', | ||
'did:plc:zzzzzzzzzzzzzzzzzzzzzzzz', | ||
] | ||
|
||
const INVALID: [value: unknown, message: string][] = [ | ||
['did:plc:l3rouwludahu3ui3bt66mfv0', 'Invalid character at position 31'], | ||
['did:plc:l3rouwludahu3ui3bt66mfv1', 'Invalid character at position 31'], | ||
['did:plc:l3rouwludahu3ui3bt66mfv9', 'Invalid character at position 31'], | ||
['did:plc:l3rouwludahu3ui3bt66mfv', 'did:plc must be 32 characters long'], | ||
['did:plc:l3rouwludahu3ui3bt66mfvja', 'did:plc must be 32 characters long'], | ||
['did:plc:example.com:', 'did:plc must be 32 characters long'], | ||
['did:plc:exam%3Aple.com%3A8080', 'did:plc must be 32 characters long'], | ||
[3, 'DID must be a string'], | ||
[{ toString: () => 'did:plc:foo.com' }, 'DID must be a string'], | ||
[[''], 'DID must be a string'], | ||
['random-string', 'Invalid did:plc prefix'], | ||
['did plc', 'Invalid did:plc prefix'], | ||
['lorem ipsum dolor sit', 'Invalid did:plc prefix'], | ||
] | ||
|
||
describe('isDidPlc', () => { | ||
it('returns true for various valid dids', () => { | ||
for (const did of VALID) { | ||
expect(isDidPlc(did)).toBe(true) | ||
} | ||
}) | ||
|
||
it('returns false for invalid dids', () => { | ||
for (const [did] of INVALID) { | ||
expect(isDidPlc(did)).toBe(false) | ||
} | ||
}) | ||
}) | ||
|
||
describe('assertDidPlc', () => { | ||
it('does not throw on valid dids', () => { | ||
for (const did of VALID) { | ||
expect(() => assertDidPlc(did)).not.toThrow() | ||
} | ||
}) | ||
|
||
it('throws if called with non string argument', () => { | ||
for (const [val, message] of INVALID) { | ||
expect(() => assertDidPlc(val)).toThrowError( | ||
new InvalidDidError( | ||
typeof val === 'string' ? val : typeof val, | ||
message, | ||
), | ||
) | ||
} | ||
}) | ||
}) | ||
|
||
describe('asDidPlc', () => { | ||
it('returns the input for valid dids', () => { | ||
for (const did of VALID) { | ||
expect(asDidPlc(did)).toBe(did) | ||
} | ||
}) | ||
|
||
it('throws if called with invalid dids', () => { | ||
for (const [val] of INVALID) { | ||
expect(() => asDidPlc(val)).toThrowError(InvalidDidError) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { InvalidDidError } from '../../src/did-error.js' | ||
import { Did } from '../../src/did.js' | ||
import { | ||
asDidWeb, | ||
assertDidWeb, | ||
didWebToUrl, | ||
isDidWeb, | ||
urlToDidWeb, | ||
} from '../../src/methods/web.js' | ||
|
||
const VALID: [Did<'web'>, string][] = [ | ||
['did:web:example.com', 'https://example.com/'], | ||
['did:web:sub.example.com', 'https://sub.example.com/'], | ||
['did:web:example.com%3A8080', 'https://example.com:8080/'], | ||
[ | ||
'did:web:example.com:path:to:resource', | ||
'https://example.com/path/to/resource', | ||
], | ||
[ | ||
'did:web:example.com%3A8080:path:to:resource', | ||
'https://example.com:8080/path/to/resource', | ||
], | ||
[ | ||
'did:web:xn--b48h.com%3A8080:path:to:resource', | ||
'https://🙃.com:8080/path/to/resource', | ||
], | ||
] | ||
|
||
const INVALID: [value: unknown, message: string][] = [ | ||
['did:web:', 'DID method-specific id must not be empty'], | ||
['did:web:[email protected]', 'Disallowed character in DID at position 11'], | ||
['did:web::example.com', 'did:web MSID must not start with a colon'], | ||
['did:web:example.com:', 'DID cannot end with ":"'], | ||
['did:web:exam%3Aple.com%3A8080', 'Invalid Web DID'], | ||
[3, 'DID must be a string'], | ||
[{ toString: () => 'did:web:foo.com' }, 'DID must be a string'], | ||
[[''], 'DID must be a string'], | ||
['random-string', 'Invalid did:web prefix'], | ||
['did web', 'Invalid did:web prefix'], | ||
['lorem ipsum dolor sit', 'Invalid did:web prefix'], | ||
] | ||
|
||
describe('isDidWeb', () => { | ||
it('returns true for various valid dids', () => { | ||
for (const [did] of VALID) { | ||
expect(isDidWeb(did)).toBe(true) | ||
} | ||
}) | ||
|
||
it('returns false for invalid dids', () => { | ||
for (const did of INVALID) { | ||
expect(isDidWeb(did)).toBe(false) | ||
} | ||
}) | ||
}) | ||
|
||
describe('assertDidWeb', () => { | ||
it('does not throw on valid dids', () => { | ||
for (const [did] of VALID) { | ||
expect(() => assertDidWeb(did)).not.toThrow() | ||
} | ||
}) | ||
|
||
it('throws if called with non string argument', () => { | ||
for (const [val, message] of INVALID) { | ||
expect(() => assertDidWeb(val)).toThrowError( | ||
new InvalidDidError( | ||
typeof val === 'string' ? val : typeof val, | ||
message, | ||
), | ||
) | ||
} | ||
}) | ||
}) | ||
|
||
describe('didWebToUrl', () => { | ||
it('converts valid did:web to URL', () => { | ||
for (const [did, url] of VALID) { | ||
expect(didWebToUrl(did)).toStrictEqual(new URL(url)) | ||
} | ||
}) | ||
}) | ||
|
||
describe('urlToDidWeb', () => { | ||
it('converts URL to valid did:web', () => { | ||
for (const [did, url] of VALID) { | ||
expect(urlToDidWeb(new URL(url))).toBe(did) | ||
} | ||
}) | ||
}) | ||
|
||
describe('asDidWeb', () => { | ||
it('returns the input for valid dids', () => { | ||
for (const [did] of VALID) { | ||
expect(asDidWeb(did)).toBe(did) | ||
} | ||
}) | ||
|
||
it('throws if called with invalid dids', () => { | ||
for (const [val, message] of INVALID) { | ||
expect(() => asDidWeb(val)).toThrowError( | ||
new InvalidDidError( | ||
typeof val === 'string' ? val : typeof val, | ||
message, | ||
), | ||
) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.