Skip to content

Commit

Permalink
write test for dns packet -> diddoc conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
mistermoe committed Apr 24, 2024
1 parent bc8653a commit 1e877eb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
12 changes: 10 additions & 2 deletions packages/web5/lib/src/crypto/secp256k1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,15 @@ class Secp256k1 {
}

static Jwk bytesToPublicKey(Uint8List input) {
// TODO: implement bytesToPublicKey
throw UnimplementedError();
final xBytes = input.sublist(1, 33);
final yBytes = input.sublist(33, 65);

return Jwk(
kty: kty,
alg: alg,
crv: crv,
x: Base64Url.encode(xBytes),
y: Base64Url.encode(yBytes),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ class DidDocumentConverter {
return DnsPacket.create(answers);
}

static DidDocument convertDnsPacket(String did, Uint8List input) {
final dnsPacket = DnsPacket.decode(input);
static DidDocument convertDnsPacket(String did, DnsPacket dnsPacket) {
final didDocument = DidDocument(id: did);

final purposesMap = {};
Expand Down
4 changes: 3 additions & 1 deletion packages/web5/lib/src/dids/did_dht/did_dht.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:typed_data';
import 'package:web5/src/crypto.dart';
import 'package:web5/src/dids.dart';
import 'package:web5/src/dids/did_dht/bep44.dart';
import 'package:web5/src/dids/did_dht/dns_packet.dart';
import 'package:web5/src/dids/did_dht/registered_types.dart';
import 'package:web5/src/dids/did_dht/converters/did_document_converter.dart';
import 'package:web5/src/encoders/zbase.dart';
Expand Down Expand Up @@ -144,8 +145,9 @@ class DidDht {
);

try {
final dnsPacket = DnsPacket.decode(bep44Message.v);
final document =
DidDocumentConverter.convertDnsPacket(did.uri, bep44Message.v);
DidDocumentConverter.convertDnsPacket(did.uri, dnsPacket);
return DidResolutionResult(didDocument: document);
} catch (e) {
return DidResolutionResult.withError(DidResolutionError.invalidDid);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:test/test.dart';
import 'package:web5/src/dids/did_dht/dns_packet.dart';
import 'package:web5/src/dids/did_dht/converters/did_document_converter.dart';
Expand Down Expand Up @@ -77,5 +79,72 @@ void main() {
}
}
});

test('convertDnsPacket', () {
final answers = [
Answer<TxtData>(
name: RecordName(
'_k0._did.hpmp9uur565nkimpwdzom7ehbuabnsba658xwwynyk7awcd15bko',
),
type: RecordType.TXT,
klass: RecordClass.IN,
data: TxtData([
'id=0;t=0;k=41bfzmTftiVVbaDvBfUcDPARWDj2zvpQAgK7ijBy2FU',
]),
ttl: 7200,
),
Answer<TxtData>(
name: RecordName(
'_k1._did.hpmp9uur565nkimpwdzom7ehbuabnsba658xwwynyk7awcd15bko',
),
type: RecordType.TXT,
klass: RecordClass.IN,
data: TxtData([
'id=sig;t=0;k=Ix9rT44QKnIjNeB51-ORlwoCbLKr-hsOYgl4gN9TzIU',
]),
ttl: 7200,
),
Answer<TxtData>(
name: RecordName(
'_k2._did.hpmp9uur565nkimpwdzom7ehbuabnsba658xwwynyk7awcd15bko',
),
type: RecordType.TXT,
klass: RecordClass.IN,
data: TxtData([
'id=enc;t=1;k=BGAiiS0vNnoe9L9lcget6zalDDj8ZxBLwZVIa8HwzjupkA76lNJ4i190uJVelQjZ9txYbUU8pyk3axgHxyDRVH8',
]),
ttl: 7200,
),
Answer<TxtData>(
name: RecordName(
'_s0._did.hpmp9uur565nkimpwdzom7ehbuabnsba658xwwynyk7awcd15bko',
),
type: RecordType.TXT,
klass: RecordClass.IN,
data: TxtData([
'id=dwn;t=DecentralizedWebNode;se=https://example.com/dwn2;enc=#enc;sig=#sig',
]),
ttl: 7200,
),
Answer<TxtData>(
name: RecordName(
'_did.hpmp9uur565nkimpwdzom7ehbuabnsba658xwwynyk7awcd15bko',
),
type: RecordType.TXT,
klass: RecordClass.IN,
data: TxtData([
'v=0;vm=k0,k1,k2;auth=k0,k1;asm=k0,k1;agm=k2;del=k0;inv=k0;svc=s0',
]),
ttl: 7200,
),
];

final dnsPacket = DnsPacket.create(answers);
final did =
'did:dht:hpmp9uur565nkimpwdzom7ehbuabnsba658xwwynyk7awcd15bko';
final didDocument = DidDocumentConverter.convertDnsPacket(did, dnsPacket);

print(didDocument.toJson());
});
});
}

0 comments on commit 1e877eb

Please sign in to comment.