Skip to content

Commit

Permalink
janky did:web resolution (#42)
Browse files Browse the repository at this point in the history
* janky `did:web` resolution

* address PR comments

Co-Authored-By: Wes <[email protected]>

* address PR comment

Co-Authored-By: Wes <[email protected]>

* address PR comment

Co-Authored-By: Wes <[email protected]>

---------

Co-authored-by: Wes <[email protected]>
  • Loading branch information
mistermoe and wesbillman authored Feb 6, 2024
1 parent f9cef3b commit 95eb862
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 13 deletions.
16 changes: 8 additions & 8 deletions packages/web5/lib/src/dids/data_models.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export 'structures/service.dart';
export 'structures/did_service.dart';
export 'structures/did_resource.dart';
export 'structures/did_document.dart';
export 'structures/resolution_result.dart';
export 'structures/dereference_result.dart';
export 'structures/verification_method.dart';
export 'structures/resolution_metadata.dart';
export 'structures/dereference_options.dart';
export 'structures/dereference_metadata.dart';
export 'structures/did_resolution_result.dart';
export 'structures/did_dereference_result.dart';
export 'structures/did_verification_method.dart';
export 'structures/did_resolution_metadata.dart';
export 'structures/did_dereference_options.dart';
export 'structures/did_dereference_metadata.dart';
export 'structures/did_document_metadata.dart';
export 'structures/verification_relationship.dart';
export 'structures/did_verification_relationship.dart';
70 changes: 70 additions & 0 deletions packages/web5/lib/src/dids/did_web/did_web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'dart:convert';
import 'dart:io';

import 'package:web5/src/crypto/key_manager.dart';
import 'package:web5/src/dids/data_models.dart';
import 'package:web5/src/dids/did.dart';
import 'package:web5/src/dids/did_method_resolver.dart';
import 'package:web5/src/dids/did_uri.dart';

class DidWeb implements Did {
@override
// TODO: implement keyManager
KeyManager get keyManager => throw UnimplementedError();

@override
// TODO: implement uri
String get uri => throw UnimplementedError();

static const String methodName = 'web';

static final resolver = DidMethodResolver(name: methodName, resolve: resolve);

static Future<DidResolutionResult> resolve(String didUri) async {
final DidUri parsedDidUri;

try {
parsedDidUri = DidUri.parse(didUri);
} on Exception {
return DidResolutionResult.invalidDid();
}

if (parsedDidUri.method != methodName) {
return DidResolutionResult.invalidDid();
}

// TODO: http technically not supported. remove after temp use
var resolutionUrl = parsedDidUri.id.replaceAll(':', '/');
if (resolutionUrl.contains('localhost')) {
resolutionUrl = 'http://$resolutionUrl';
} else {
resolutionUrl = 'https://$resolutionUrl';
}

if (parsedDidUri.path != null) {
resolutionUrl = '$resolutionUrl/${parsedDidUri.path}';
} else {
resolutionUrl = '$resolutionUrl/.well-known';
}

resolutionUrl += '/did.json';
resolutionUrl = Uri.decodeFull(resolutionUrl);

final parsedUrl = Uri.parse(resolutionUrl);

final httpClient = HttpClient();
final request = await httpClient.getUrl(parsedUrl);
final response = await request.close();

if (response.statusCode != 200) {
// TODO: change this to something more appropriate
return DidResolutionResult.invalidDid();
}

final str = await response.transform(utf8.decoder).join();
final jsonParsed = json.decode(str);
final doc = DidDocument.fromJson(jsonParsed);

return DidResolutionResult(didDocument: doc);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:convert';

import 'package:web5/src/dids/structures/dereference_metadata.dart';
import 'package:web5/src/dids/structures/did_dereference_metadata.dart';
import 'package:web5/src/dids/structures/did_document_metadata.dart';
import 'package:web5/src/dids/structures/did_resource.dart';

Expand Down
26 changes: 23 additions & 3 deletions packages/web5/lib/src/dids/structures/did_document.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:collection/collection.dart';
import 'package:web5/src/dids/structures/service.dart';
import 'package:web5/src/dids/structures/did_service.dart';
import 'package:web5/src/dids/structures/did_resource.dart';
import 'package:web5/src/dids/structures/verification_method.dart';
import 'package:web5/src/dids/structures/verification_relationship.dart';
import 'package:web5/src/dids/structures/did_verification_method.dart';
import 'package:web5/src/dids/structures/did_verification_relationship.dart';

/// A set of data describing the DID subject including mechanisms such as:
/// * cryptographic public keys - used to authenticate itself and prove
Expand Down Expand Up @@ -176,4 +176,24 @@ class DidDocument implements DidResource {

return json;
}

factory DidDocument.fromJson(Map<String, dynamic> json) {
return DidDocument(
context: json['context'],
id: json['id'],
alsoKnownAs: json['alsoKnownAs']?.cast<String>(),
controller: json['controller'],
verificationMethod: (json['verificationMethod'] as List<dynamic>?)
?.map((item) => DidVerificationMethod.fromJson(item))
.toList(),
service: (json['service'] as List<dynamic>?)
?.map((item) => DidService.fromJson(item))
.toList(),
assertionMethod: json['assertionMethod']?.cast<String>(),
authentication: json['authentication']?.cast<String>(),
keyAgreement: json['keyAgreement']?.cast<String>(),
capabilityDelegation: json['capabilityDelegation']?.cast<String>(),
capabilityInvocation: json['capabilityInvocation']?.cast<String>(),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:web5/src/dids/structures/did_document.dart';
import 'package:web5/src/dids/structures/did_document_metadata.dart';
import 'package:web5/src/dids/structures/resolution_metadata.dart';
import 'package:web5/src/dids/structures/did_resolution_metadata.dart';

/// A class representing the result of a DID (Decentralized Identifier)
/// resolution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ class DidService implements DidResource {
'serviceEndpoint': serviceEndpoint,
};
}

factory DidService.fromJson(Map<String, dynamic> json) {
return DidService(
id: json['id'],
type: json['type'],
serviceEndpoint: json['serviceEndpoint'].toString(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ class DidVerificationMethod implements DidResource {

return json;
}

factory DidVerificationMethod.fromJson(Map<String, dynamic> json) {
return DidVerificationMethod(
id: json['id'],
type: json['type'],
controller: json['controller'],
publicKeyJwk: json['publicKeyJwk'] != null
? Jwk.fromJson(json['publicKeyJwk'])
: null,
publicKeyMultibase: json['publicKeyMultibase'],
);
}
}
13 changes: 13 additions & 0 deletions packages/web5/test/dids/did_web_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:test/test.dart';
import 'package:web5/src/dids/did_web/did_web.dart';

void main() {
group('DidWeb', () {
test('should resolve successfully', () async {
final result = await DidWeb.resolve('did:web:www.linkedin.com');
expect(result.didDocument, isNotNull);

expect('did:web:www.linkedin.com', result.didDocument!.id);
});
});
}

0 comments on commit 95eb862

Please sign in to comment.