-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MONDOOOO refactor
- Loading branch information
Showing
48 changed files
with
1,110 additions
and
710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum AlgorithmId { | ||
ed25519, | ||
secp256k1, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'); | ||
} | ||
} | ||
} |
Oops, something went wrong.