Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions packages/core/src/schema/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class DIDSchema extends Schema.API {
const schema = Schema.string().refine(new DIDSchema())

export const did = () => schema

/**
*
* @param {unknown} input
*/
export const read = input => schema.read(input)
Expand All @@ -46,7 +46,7 @@ export const from = input => match({}).from(input)

/**
* @template {string} Method
* @extends {Schema.API<API.DID<Method> & API.URI<"did:">, unknown, void|Method>}
* @extends {Schema.API<API.PrincipalView<API.DID<Method> & API.URI<"did:">>, unknown, void|Method>}
*/
class DIDBytesSchema extends Schema.API {
/**
Expand All @@ -57,26 +57,27 @@ class DIDBytesSchema extends Schema.API {
if (!(source instanceof Uint8Array)) {
return Schema.typeError({ expect: 'Uint8Array', actual: source })
}
let did
let principal
try {
did = DID.decode(source).did()
principal = DID.decode(source)
} catch (err) {
return Schema.error(`Unable to parse bytes as did: ${err}`)
return Schema.error(`Unable to decode bytes as DID: ${err}`)
}
const prefix = method ? `did:${method}:` : `did:`
if (!did.startsWith(prefix)) {
return Schema.error(`Expected a ${prefix} but got "${did}" instead`)
} else {
return { ok: /** @type {API.DID<Method>} */ (did) }
}
if (!principal.did().startsWith(prefix)) {
return Schema.error(
`Expected a ${prefix} but got "${principal.did()}" instead`
)
}
return { ok: /** @type {API.PrincipalView<API.DID<Method>>} */ (principal) }
}
}

const schemaBytes = new DIDBytesSchema()

export const didBytes = () => schemaBytes

/**
*
* @param {unknown} input
*/
export const readBytes = input => schemaBytes.read(input)
Expand All @@ -86,7 +87,7 @@ export const readBytes = input => schemaBytes.read(input)
* @param {{method?: Method}} options
*/
export const matchBytes = (options = {}) =>
/** @type {Schema.Schema<API.DID<Method> & API.URI<"did:">>} */ (
/** @type {Schema.Schema<API.PrincipalView<API.DID<Method> & API.URI<"did:">>>} */ (
new DIDBytesSchema(options.method)
)

Expand Down
31 changes: 23 additions & 8 deletions packages/core/test/extra-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,11 @@ test('URI.from', () => {
const dataset = [
[undefined, /Expected value of type Uint8Array instead got undefined/],
[null, /Expected value of type Uint8Array instead got null/],
[Uint8Array.from([1, 2, 3]), /Unable to parse bytes as did:/],
[DIDTools.parse('did:echo:1'), { ok: 'did:echo:1' }],
[Uint8Array.from([1, 2, 3]), /Unable to decode bytes as DID:/],
[
DIDTools.parse('did:echo:1'),
{ ok: new Uint8Array([157, 26, 101, 99, 104, 111, 58, 49]) },
],
]

for (const [input, out] of dataset) {
Expand All @@ -416,9 +419,17 @@ test('URI.from', () => {
[
{ method: 'echo' },
Uint8Array.from([1, 2, 3]),
/Unable to parse bytes as did:/,
/Unable to decode bytes as DID:/,
],
[
{ method: 'echo' },
DIDTools.parse('did:echo:hello'),
{
ok: new Uint8Array([
157, 26, 101, 99, 104, 111, 58, 104, 101, 108, 108, 111,
]),
},
],
[{ method: 'echo' }, DIDTools.parse('did:echo:hello'), { ok: 'did:echo:hello' }],
[
{ method: 'foo' },
DIDTools.parse('did:echo:hello'),
Expand All @@ -438,7 +449,11 @@ test('URI.from', () => {
const dataset = [
[{}, undefined, { ok: undefined }],
[{}, null, /Expected value of type Uint8Array instead got null/],
[{}, DIDTools.parse('did:echo:bar'), { ok: 'did:echo:bar' }],
[
{},
DIDTools.parse('did:echo:bar'),
{ ok: new Uint8Array([157, 26, 101, 99, 104, 111, 58, 98, 97, 114]) },
],
[{ method: 'echo' }, undefined, { ok: undefined }],
[
{ method: 'echo' },
Expand All @@ -453,7 +468,7 @@ test('URI.from', () => {
[
{ method: 'echo' },
Uint8Array.from([1, 2, 3]),
/Unable to parse bytes as did:/,
/Unable to decode bytes as DID:/,
],
]

Expand All @@ -471,7 +486,7 @@ test('URI.from', () => {
[DIDTools.parse('did:foo:bar'), null],
[DIDTools.parse('did:web:example.com'), null],
[DIDTools.parse('did:twosegments'), null],
[Uint8Array.from([1, 2, 3]), /Unable to parse bytes as did:/],
[Uint8Array.from([1, 2, 3]), /Unable to decode bytes as DID:/],
[
undefined,
/TypeError: Expected value of type Uint8Array instead got undefined/,
Expand All @@ -492,4 +507,4 @@ test('URI.from', () => {
}
})
}
}
}
8 changes: 6 additions & 2 deletions packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Signature,
SignatureView,
Principal,
PrincipalView,
MulticodecCode,
SigAlg,
ToJSON,
Expand Down Expand Up @@ -45,7 +46,7 @@ import {
Revoked,
InferCapability,
Authorization,
Reader
Reader,
} from './capability.js'
import type * as Transport from './transport.js'
import type { Tuple, Block } from './transport.js'
Expand Down Expand Up @@ -77,6 +78,7 @@ export type {
MultibaseEncoder,
MulticodecCode,
Principal,
PrincipalView,
ToJSON,
ToString,
UnknownLink,
Expand Down Expand Up @@ -977,7 +979,9 @@ export interface HTTPError {
/**
* Options for UCAN validation.
*/
export interface ValidatorOptions extends PrincipalResolver, Partial<AuthorityProver> {
export interface ValidatorOptions
extends PrincipalResolver,
Partial<AuthorityProver> {
/**
* Schema allowing invocations to be accepted for audiences other than the
* service itself.
Expand Down