diff --git a/lib/src/crypto/secp256k1.dart b/lib/src/crypto/secp256k1.dart index 74feed3..f0e22c2 100644 --- a/lib/src/crypto/secp256k1.dart +++ b/lib/src/crypto/secp256k1.dart @@ -141,7 +141,10 @@ class Secp256k1 implements Dsa { @override Future verify( - Jwk publicKeyJwk, Uint8List payload, Uint8List signature) { + Jwk publicKeyJwk, + Uint8List payload, + Uint8List signature, + ) { final xBytes = base64UrlDecoder.convertNoPadding(publicKeyJwk.x!); final x = bytesToBigInt(xBytes); diff --git a/lib/src/jwt.dart b/lib/src/jwt.dart index 9243874..c0fc2b3 100644 --- a/lib/src/jwt.dart +++ b/lib/src/jwt.dart @@ -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) { @@ -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. /// diff --git a/test/crypto/jwt_test.dart b/test/crypto/jwt_test.dart new file mode 100644 index 0000000..703b2c0 --- /dev/null +++ b/test/crypto/jwt_test.dart @@ -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)); + }); + }); +} diff --git a/test/crypto/secp256k1_test.dart b/test/crypto/secp256k1_test.dart new file mode 100644 index 0000000..bdff893 --- /dev/null +++ b/test/crypto/secp256k1_test.dart @@ -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); + }); + }); +} diff --git a/test/did_jwk_test.dart b/test/dids/did_jwk_test.dart similarity index 79% rename from test/did_jwk_test.dart rename to test/dids/did_jwk_test.dart index 2ca8376..46ee014 100644 --- a/test/did_jwk_test.dart +++ b/test/dids/did_jwk_test.dart @@ -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); @@ -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', ); @@ -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); @@ -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', ); diff --git a/test/jwt_test.dart b/test/jwt_test.dart deleted file mode 100644 index ad0847c..0000000 --- a/test/jwt_test.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:tbdex/src/jwt.dart'; -import 'package:tbdex/tbdex.dart'; -import 'package:test/test.dart'; - -void main() { - test('should work', () 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)); - }); -} diff --git a/test/secp256k1_test.dart b/test/secp256k1_test.dart deleted file mode 100644 index ff720d7..0000000 --- a/test/secp256k1_test.dart +++ /dev/null @@ -1,18 +0,0 @@ -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:tbdex/src/crypto/secp256k1.dart'; -import 'package:test/test.dart'; - -void main() { - test('should work', () 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); - }); -}