Skip to content

Commit

Permalink
Merge pull request #8 from TBD54566975/minor-test-cleanup
Browse files Browse the repository at this point in the history
fix: lint warnings and test output
  • Loading branch information
wesbillman authored Jan 3, 2024
2 parents 5e52b57 + 281ea58 commit 29bfc19
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 48 deletions.
5 changes: 4 additions & 1 deletion lib/src/crypto/secp256k1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ class Secp256k1 implements Dsa {

@override
Future<void> verify(
Jwk publicKeyJwk, Uint8List payload, Uint8List signature) {
Jwk publicKeyJwk,
Uint8List payload,
Uint8List signature,
) {
final xBytes = base64UrlDecoder.convertNoPadding(publicKeyJwk.x!);
final x = bytesToBigInt(xBytes);

Expand Down
15 changes: 11 additions & 4 deletions lib/src/jwt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class Jwt {

final publicKeyJwk = verificationMethod.publicKeyJwk!;
final dsaName = DsaName.findByAlias(
DsaAlias(algorithm: publicKeyJwk.alg, curve: publicKeyJwk.crv));
DsaAlias(algorithm: publicKeyJwk.alg, curve: publicKeyJwk.crv),
);

final signer = signers[dsaName];
if (signer == null) {
Expand Down Expand Up @@ -188,9 +189,15 @@ class JwtPayload {
int? iat;
String? jti;

JwtPayload(
{this.iss, this.sub, dynamic aud, this.exp, this.nbf, this.iat, this.jti})
: _aud = aud;
JwtPayload({
this.iss,
this.sub,
dynamic aud,
this.exp,
this.nbf,
this.iat,
this.jti,
}) : _aud = aud;

/// Sets the audience claim.
///
Expand Down
19 changes: 19 additions & 0 deletions test/crypto/jwt_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:tbdex/src/jwt.dart';
import 'package:tbdex/tbdex.dart';
import 'package:test/test.dart';

void main() {
group('Jwt', () {
test('should parse signed JWT', () async {
final km = InMemoryKeyManager();
final did = await DidJwk.create(keyManager: km);

final signedJwt =
await Jwt.sign(did: did, jwtPayload: JwtPayload(iss: did.uri));

final parsedJwt = Jwt.parse(signedJwt);
expect(parsedJwt.decoded.header.kid, contains("${did.uri}#"));
expect(parsedJwt.decoded.payload.iss, equals(did.uri));
});
});
}
20 changes: 20 additions & 0 deletions test/crypto/secp256k1_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:tbdex/src/crypto/secp256k1.dart';
import 'package:test/test.dart';

void main() {
group('Secp256k1', () {
test('should verify public key', () async {
final secp256k1 = Secp256k1();

final privateKeyJwk = await secp256k1.generatePrivateKey();
final payload = Uint8List.fromList(utf8.encode("hello"));
final signature = await secp256k1.sign(privateKeyJwk, payload);

final publicKeyJwk = await secp256k1.computePublicKey(privateKeyJwk);
await secp256k1.verify(publicKeyJwk, payload, signature);
});
});
}
13 changes: 5 additions & 8 deletions test/did_jwk_test.dart → test/dids/did_jwk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import 'package:tbdex/tbdex.dart';
import 'package:test/test.dart';

void main() {
group('create', () {
group('DidJwk', () {
test('should create a valid DID', () async {
final keyManager = InMemoryKeyManager();
final did = await DidJwk.create(keyManager: keyManager);

expect(did.uri, startsWith('did:jwk:'));
});
});

group('resolve', () {
test('returns DidResolutionResult with error if kaka DID', () async {
test('should resolve with error if kaka DID', () async {
final resolutionResult = DidJwk.resolve('hi');

expect(resolutionResult.didDocument, isNull);
Expand All @@ -22,7 +20,7 @@ void main() {
);
});

test('returns DidResolutionResult with error if not did:jwk', () async {
test('should resolve with error if not did:jwk', () async {
final resolutionResult = DidJwk.resolve(
'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH',
);
Expand All @@ -34,8 +32,7 @@ void main() {
);
});

test('returns DidResolutionResult with error if id is not valid base64url',
() async {
test('should resolve with error if id is not valid base64url', () async {
final resolutionResult = DidJwk.resolve('did:jwk:!!!');

expect(resolutionResult.didDocument, isNull);
Expand All @@ -45,7 +42,7 @@ void main() {
);
});

test('returns DidResolutionResult with didDocument if legit', () async {
test('should resolve with didDocument if legit', () async {
final resolutionResult = DidJwk.resolve(
'did:jwk:eyJraWQiOiJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6andrLXRodW1icHJpbnQ6c2hhLTI1NjpGZk1iek9qTW1RNGVmVDZrdndUSUpqZWxUcWpsMHhqRUlXUTJxb2JzUk1NIiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsImFsZyI6IkVkRFNBIiwieCI6IkFOUmpIX3p4Y0tCeHNqUlBVdHpSYnA3RlNWTEtKWFE5QVBYOU1QMWo3azQifQ',
);
Expand Down
17 changes: 0 additions & 17 deletions test/jwt_test.dart

This file was deleted.

18 changes: 0 additions & 18 deletions test/secp256k1_test.dart

This file was deleted.

0 comments on commit 29bfc19

Please sign in to comment.