Skip to content

Commit

Permalink
Refactor (#48)
Browse files Browse the repository at this point in the history
MONDOOOO refactor
  • Loading branch information
mistermoe authored Feb 23, 2024
1 parent 422ade2 commit 01f420e
Show file tree
Hide file tree
Showing 48 changed files with 1,110 additions and 710 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "web5-spec"]
path = web5-spec
url = [email protected]:TBD54566975/web5-spec.git
11 changes: 0 additions & 11 deletions packages/web5/example/web5_example.dart

This file was deleted.

6 changes: 2 additions & 4 deletions packages/web5/lib/src/crypto.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export './crypto/jwk.dart';
export './crypto/dsa.dart';
export './crypto/crypto.dart';
export './crypto/ed25519.dart';
export './crypto/dsa_name.dart';
export './crypto/dsa_alias.dart';
export './crypto/secp256k1.dart';
export './crypto/key_manager.dart';
export './crypto/dsa_algorithms.dart';
export './crypto/algorithm_id.dart';
export './crypto/in_memory_key_manager.dart';
4 changes: 4 additions & 0 deletions packages/web5/lib/src/crypto/algorithm_id.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum AlgorithmId {
ed25519,
secp256k1,
}
84 changes: 84 additions & 0 deletions packages/web5/lib/src/crypto/crypto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'dart:typed_data';

import 'package:web5/src/crypto/algorithm_id.dart';
import 'package:web5/src/crypto/ecdsa.dart';
import 'package:web5/src/crypto/eddsa.dart';
import 'package:web5/src/crypto/jwk.dart';
import 'package:web5/src/crypto/ed25519.dart';
import 'package:web5/src/crypto/secp256k1.dart';

class Crypto {
static Future<Jwk> generatePrivateKey(AlgorithmId algId) {
switch (algId) {
case AlgorithmId.ed25519:
return Ed25519.generatePrivateKey();
case AlgorithmId.secp256k1:
return Secp256k1.generatePrivateKey();
}
}

static Future<Jwk> computePublicKey(Jwk privateKeyJwk) {
switch (privateKeyJwk.kty) {
case Ed25519.kty:
return Ed25519.computePublicKey(privateKeyJwk);
case Secp256k1.kty:
return Secp256k1.computePublicKey(privateKeyJwk);
default:
throw Exception('unsupported kty: ${privateKeyJwk.kty}');
}
}

static Jwk bytesToPublicKey(AlgorithmId algId, Uint8List bytes) {
switch (algId) {
case AlgorithmId.ed25519:
return Ed25519.bytesToPublicKey(bytes);
case AlgorithmId.secp256k1:
return Secp256k1.bytesToPublicKey(bytes);
}
}

static Future<Uint8List> sign(Jwk privateKeyJwk, Uint8List payload) {
switch (privateKeyJwk.kty) {
case Ecdsa.kty:
return Ecdsa.sign(privateKeyJwk, payload);
case Eddsa.kty:
return Eddsa.sign(privateKeyJwk, payload);
default:
throw Exception('unsupported kty: ${privateKeyJwk.kty}');
}
}

static Future<void> verify({
required Jwk publicKey,
required Uint8List payload,
required Uint8List signature,
}) {
switch (publicKey.kty) {
case Ecdsa.kty:
return Ecdsa.verify(
publicKey: publicKey,
payload: payload,
signature: signature,
);
case Eddsa.kty:
return Eddsa.verify(
publicKey: publicKey,
payload: payload,
signature: signature,
);
default:
throw Exception('unsupported kty: ${publicKey.kty}');
}
}

static String getJwa(Jwk jwk) {
switch (jwk.kty) {
case Ecdsa.kty:
return Ecdsa.getJwa(jwk);
case Eddsa.kty:
return Eddsa.getJwa(jwk);
default:
throw Exception('unsupported kty: ${jwk.kty}');
}
}
}
41 changes: 0 additions & 41 deletions packages/web5/lib/src/crypto/dsa.dart

This file was deleted.

62 changes: 0 additions & 62 deletions packages/web5/lib/src/crypto/dsa_algorithms.dart

This file was deleted.

17 changes: 0 additions & 17 deletions packages/web5/lib/src/crypto/dsa_alias.dart

This file was deleted.

27 changes: 0 additions & 27 deletions packages/web5/lib/src/crypto/dsa_name.dart

This file was deleted.

51 changes: 51 additions & 0 deletions packages/web5/lib/src/crypto/ecdsa.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:typed_data';

import 'package:web5/src/crypto/jwk.dart';
import 'package:web5/src/crypto/secp256k1.dart';

class Ecdsa {
static const kty = 'EC';

static Future<Uint8List> sign(Jwk privateKeyJwk, Uint8List payload) {
if (privateKeyJwk.crv == null) {
throw Exception('expected jwk to contain crv');
}

switch (privateKeyJwk.crv) {
case Secp256k1.crv:
return Secp256k1.sign(privateKeyJwk, payload);
default:
throw Exception('unsupported crv: ${privateKeyJwk.crv}');
}
}

static Future<void> verify({
required Jwk publicKey,
required Uint8List payload,
required Uint8List signature,
}) {
if (publicKey.crv == null) {
throw Exception('expected jwk to contain crv');
}

switch (publicKey.crv) {
case Secp256k1.crv:
return Secp256k1.verify(publicKey, payload, signature);
default:
throw Exception('unsupported crv: ${publicKey.crv}');
}
}

static String getJwa(Jwk jwk) {
if (jwk.crv == null) {
throw Exception('expected jwk to contain crv');
}

switch (jwk.crv) {
case Secp256k1.crv:
return Secp256k1.alg;
default:
throw Exception('unsupported crv: ${jwk.crv}');
}
}
}
Loading

0 comments on commit 01f420e

Please sign in to comment.