Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

janky did:web resolution #42

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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);
}
}
20 changes: 20 additions & 0 deletions packages/web5/lib/src/dids/structures/did_document.dart
Original file line number Diff line number Diff line change
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>(),
);
}
}
8 changes: 8 additions & 0 deletions packages/web5/lib/src/dids/structures/service.dart
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(),
);
}
mistermoe marked this conversation as resolved.
Show resolved Hide resolved
}
13 changes: 13 additions & 0 deletions packages/web5/lib/src/dids/structures/verification_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ class DidVerificationMethod implements DidResource {

return json;
}

// Assuming Jwk has a fromJson constructor
mistermoe marked this conversation as resolved.
Show resolved Hide resolved
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 work', () async {
mistermoe marked this conversation as resolved.
Show resolved Hide resolved
final result = await DidWeb.resolve('did:web:www.linkedin.com');
expect(result.didDocument, isNotNull);

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