Skip to content

Commit

Permalink
Merge pull request #1588 from blockchain-certificates/perf/async-load…
Browse files Browse the repository at this point in the history
…-did-key-resolver

perf(Bundle): dynamically serve did key code as needed
  • Loading branch information
lemoustachiste authored Oct 30, 2023
2 parents fe2022c + 42a021f commit c230492
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bundle-esm-stats.html

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 33 additions & 7 deletions src/domain/did/useCases/resolveDidKeyDocument.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
import type { IDidDocument } from '../../../models/DidDocument';
import * as ed25519 from '@transmute/did-key-ed25519';
import * as secp256k1 from '@transmute/did-key-secp256k1';
import type { ResolutionOptions } from '@transmute/did-key-common/dist/types/ResolutionOptions';
import type { ResolutionResponse } from '@transmute/did-key-common/src/types/ResolutionResponse';

const supportedSuiteMap = {
'did:key:z6Mk': ed25519,
'did:key:zQ3s': secp256k1
interface TransmuteDidKeyResolver {
generate: (keyGenOptions: any, resolutionOptions: ResolutionOptions) => any;
resolve: (didKeyUri: string, options?: ResolutionOptions) => Promise<ResolutionResponse>;
}

enum SupportedSuite {
ED25519 = 'ed25519',
SECP256K1 = 'secp256k1'
}

const supportedSuiteMap: { [key: string]: SupportedSuite } = {
'did:key:z6Mk': SupportedSuite.ED25519,
'did:key:zQ3s': SupportedSuite.SECP256K1
};

async function getResolver (suite: SupportedSuite): Promise<TransmuteDidKeyResolver> {
if (suite === SupportedSuite.ED25519) {
const didKeyResolver = await import('@transmute/did-key-ed25519');
return didKeyResolver;
}

if (suite === SupportedSuite.SECP256K1) {
const didKeyResolver = await import('@transmute/did-key-secp256k1');
return didKeyResolver;
}

throw new Error('Error loading did key resolver');
}

export default async function resolveDidKeyDocument (didKeyUri: string): Promise<IDidDocument> {
const keyPrefix = didKeyUri.substring(0, 12);
if (supportedSuiteMap[keyPrefix]) {
const { didDocument } = await supportedSuiteMap[keyPrefix].resolve(didKeyUri, { accept: 'application/did+json' });
return didDocument;
const resolver = await getResolver(supportedSuiteMap[keyPrefix]);

const { didDocument } = await resolver.resolve(didKeyUri, { accept: 'application/did+json' });
return didDocument as unknown as IDidDocument; // transmute libs has a lighter definition of DidDocument
}

throw new Error('Unsupported did:key suite');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe('Verifier domain getIssuerProfile use case test suite', function () {
toISOString (): string {
return '2022-06-10T18:29:41.468Z';
}

static now (): number {
return 1654885781468;
}
}
global.Date = MockDate as any;

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"noImplicitAny": false,
"target": "ESNext",
"lib": ["ESNext", "dom"],
"importsNotUsedAsValues": "error",
"importsNotUsedAsValues": "remove",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node",
Expand Down

0 comments on commit c230492

Please sign in to comment.