Skip to content

Commit 6e88033

Browse files
committed
feat: add DID document
1 parent 61330bd commit 6e88033

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import {
3030
withDelegationsStorage,
3131
withDelegationStubs,
3232
withOptionsRequest,
33-
withCarParkFetch
33+
withCarParkFetch,
34+
withDidDocumentHandler
3435
} from './middleware/index.js'
3536
import { instrument } from '@microlabs/otel-cf-workers'
3637
import { NoopSpanProcessor, TraceIdRatioBasedSampler } from '@opentelemetry/sdk-trace-base'
@@ -60,6 +61,7 @@ const middleware = composeMiddleware(
6061
withVersionHeader,
6162
withErrorHandler,
6263
withGatewayIdentity,
64+
withDidDocumentHandler,
6365
withDelegationsStorage,
6466

6567
// Handle UCAN invocations (POST requests only)

src/middleware/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { withCarParkFetch } from './withCarParkFetch.js'
99
export { withEgressTracker } from './withEgressTracker.js'
1010
export { withEgressClient } from './withEgressClient.js'
1111
export { withDelegationStubs } from './withDelegationStubs.js'
12+
export { withDidDocumentHandler } from './withDidDocumentHandler.js'
1213
export { withGatewayIdentity } from './withGatewayIdentity.js'
1314
export { withUcanInvocationHandler } from './withUcanInvocationHandler.js'
1415
export { withDelegationsStorage } from './withDelegationsStorage.js'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @import { Middleware, Environment } from '@web3-storage/gateway-lib'
3+
* @import { GatewayIdentityContext } from './withGatewayIdentity.types.js'
4+
*/
5+
6+
/**
7+
* Handles a GET request for `/.well-known/did.json` and passes on all other
8+
* requests to the next handler in the chain.
9+
*
10+
* @type {Middleware<GatewayIdentityContext, GatewayIdentityContext, Environment>}
11+
*/
12+
export function withDidDocumentHandler (handler) {
13+
return async (request, env, ctx) => {
14+
if (request.method !== 'GET' || new URL(request.url).pathname !== '/.well-known/did.json') {
15+
return handler(request, env, ctx)
16+
}
17+
18+
const webKey = ctx.gatewayIdentity.did()
19+
const publicKeyMultibase = ctx.gatewaySigner.did().replace('did:key:', '')
20+
const verificationMethods = [
21+
{
22+
id: `${webKey}#owner`,
23+
type: 'Ed25519VerificationKey2020',
24+
controller: webKey,
25+
publicKeyMultibase
26+
},
27+
]
28+
29+
const headers = { 'Content-Type': 'application/json' }
30+
const body = JSON.stringify({
31+
'@context': ['https://w3id.org/did/v1'],
32+
id: webKey,
33+
verificationMethod: verificationMethods,
34+
assertionMethod: verificationMethods.map(k => k.id),
35+
authentication: verificationMethods.map(k => k.id)
36+
}, null, 2)
37+
38+
return new Response(body, { headers })
39+
}
40+
}

test/miniflare/freeway.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ describe('freeway', () => {
6767
CONTENT_CLAIMS_SERVICE_URL: claimsService.url.toString(),
6868
CARPARK_PUBLIC_BUCKET_URL: url.toString(),
6969
GATEWAY_SERVICE_DID: 'did:example:gateway',
70+
// did:key:z6MkvB4hvuVZhkKsi67rUKtNTKN4gpWjvWCoGPpBpGUdJMpF
71+
GATEWAY_PRINCIPAL_KEY: 'MgCatBcQ1htL3Q/kXNZ9idtpoP9EeFxUSXD106hMWXc8si+0B6ZTgeQlqgoY9Z21skaKK6FpFRAAEF50T1c9WhWTa14w=',
7072
DAGPB_CONTENT_CACHE: 'DAGPB_CONTENT_CACHE',
7173
FF_DAGPB_CONTENT_CACHE_ENABLED: 'true',
7274
FF_DAGPB_CONTENT_CACHE_TTL_SECONDS: 300,
@@ -763,4 +765,29 @@ describe('freeway', () => {
763765
assertBlobEqual(input, await res.blob())
764766
assert.equal(cachedContent.keys.length, 0, 'Cache should be empty')
765767
})
768+
769+
it('should return DID document', async () => {
770+
const res = await miniflare.dispatchFetch(
771+
'http://localhost:8787/.well-known/did.json'
772+
)
773+
assert(res.ok)
774+
const didDoc = await res.json()
775+
776+
assert(didDoc)
777+
assert(typeof didDoc === 'object')
778+
779+
assert('id' in didDoc)
780+
assert.equal(didDoc.id, 'did:example:gateway')
781+
782+
assert('verificationMethod' in didDoc)
783+
assert(Array.isArray(didDoc.verificationMethod))
784+
assert(didDoc.verificationMethod.length === 1)
785+
786+
const method = didDoc.verificationMethod[0]
787+
assert('publicKeyMultibase' in method)
788+
assert.equal(
789+
method.publicKeyMultibase,
790+
'z6MkvB4hvuVZhkKsi67rUKtNTKN4gpWjvWCoGPpBpGUdJMpF'
791+
)
792+
})
766793
})

0 commit comments

Comments
 (0)