Skip to content

Commit

Permalink
updated create function with algorithm handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fingersonfire committed Apr 1, 2024
1 parent 3225579 commit da62ad5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/web5/lib/src/crypto/in_memory_key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class InMemoryKeyManager implements KeyManager, KeyImporter, KeyExporter {
final Map<String, Jwk> _keyStore = {};

@override
Future<String> generatePrivateKey(AlgorithmId algId) async {
Future<Jwk> generatePrivateKey(AlgorithmId algId) async {
final privateKeyJwk = await Crypto.generatePrivateKey(algId);
final alias = privateKeyJwk.computeThumbprint();

_keyStore[alias] = privateKeyJwk;

return alias;
return privateKeyJwk;
}

@override
Expand Down
2 changes: 1 addition & 1 deletion packages/web5/lib/src/crypto/key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract interface class KeyManager {
/// Generates and securely stores a private key based on the provided
/// algorithm. Returns a unique alias that can be utilized to reference the
/// generated key for future operations.
Future<String> generatePrivateKey(AlgorithmId algId);
Future<Jwk> generatePrivateKey(AlgorithmId algId);

/// Retrieves the public key associated with a previously stored private key,
/// identified by the provided alias.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ class DidVerificationMethod implements DidResource {

class DidCreateVerificationMethod {
DidCreateVerificationMethod({
required this.algorithm,
required this.controller,
this.id,
required this.purposes,
required this.type,
});

final AlgorithmId algorithm;
final String controller;
final String? id;
final List<VerificationPurpose> purposes;
Expand Down
15 changes: 9 additions & 6 deletions packages/web5/lib/src/dids/did_dht/did_dht.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class DidDht {
static final resolver = DidMethodResolver(name: methodName, resolve: resolve);

static Future<BearerDid> create({
required AlgorithmId algorithm,
required KeyManager keyManager,
KeyManager? keyManager,
List<String>? alsoKnownAs,
List<String>? controllers,
String? gatewayUri,
Expand All @@ -29,9 +28,12 @@ class DidDht {
List<DidDhtRegisteredDidType>? types,
List<DidCreateVerificationMethod>? verificationMethods,
}) async {
final AlgorithmId idAlgorithm = AlgorithmId.ed25519;
keyManager ??= InMemoryKeyManager();

// Generate random key material for the Identity Key.
final Jwk identityKeyUri = await Crypto.generatePrivateKey(algorithm);
final Jwk identityKey = await Crypto.computePublicKey(identityKeyUri);
final Jwk idKeyUri = await keyManager.generatePrivateKey(idAlgorithm);
final Jwk identityKey = await Crypto.computePublicKey(idKeyUri);

final String didUri = identityKeyToIdentifier(identityKey: identityKey);
final DidDocument doc = DidDocument(
Expand All @@ -52,6 +54,7 @@ class DidDht {
if (identityMethods.isEmpty) {
methodsToAdd.add(
DidCreateVerificationMethod(
algorithm: AlgorithmId.ed25519,
id: '0',
type: 'JsonWebKey',
controller: didUri,
Expand All @@ -73,9 +76,9 @@ class DidDht {
late Jwk keyUri;

if (vm.id?.split('#').last == '0') {
keyUri = identityKeyUri;
keyUri = idKeyUri;
} else {
keyUri = await Crypto.generatePrivateKey(algorithm);
keyUri = await keyManager.generatePrivateKey(vm.algorithm);
}

final Jwk publicKey = await Crypto.computePublicKey(keyUri);
Expand Down

0 comments on commit da62ad5

Please sign in to comment.