Skip to content

Commit

Permalink
refactor: Optimised the keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Oct 31, 2023
1 parent 2a9ee7a commit acb7a0a
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lib/app/shared/enum/type/did_key_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ extension DidKeyTypeX on DidKeyType {
String getTitle(AppLocalizations l10n) {
switch (this) {
case DidKeyType.secp256k1:
return l10n.manageKeyDecentralizedIDSecp256k1;
return l10n.keyDecentralizedIDSecp256k1;
case DidKeyType.p256:
return l10n.keyDecentralizedIDP256;
case DidKeyType.ebsiv3:
return l10n.manageEbsiV3DecentralizedId;
return l10n.ebsiV3DecentralizedId;
case DidKeyType.jwkP256:
return l10n.jwkDecentralizedIDP256;
}
Expand Down
43 changes: 22 additions & 21 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,22 +252,16 @@ Future<String> getPrivateKey({
}) async {
late String storageKey;

if (didKeyType == DidKeyType.secp256k1) {
if (oidc4vc == null) throw Exception();
final mnemonic = await secureStorage.get(SecureStorageKeys.ssiMnemonic);
final index = getIndexValue(isEBSIV3: true);
final key = await oidc4vc.privateKeyFromMnemonic(
mnemonic: mnemonic!,
indexValue: index,
);
return key;
}

/// for other than DidKeyType.secp256k1
switch (didKeyType) {
case DidKeyType.secp256k1:
throw Exception();
if (oidc4vc == null) throw Exception();
final mnemonic = await secureStorage.get(SecureStorageKeys.ssiMnemonic);
final index = getIndexValue(isEBSIV3: true);
final key = await oidc4vc.privateKeyFromMnemonic(
mnemonic: mnemonic!,
indexValue: index,
);
return key;

case DidKeyType.p256:
storageKey = SecureStorageKeys.p256PrivateKey;
Expand All @@ -277,10 +271,11 @@ Future<String> getPrivateKey({
storageKey = SecureStorageKeys.p256PrivateKey3;
}

/// return key if it is already created
final String? p256PrivateKey = await secureStorage.get(storageKey);
if (p256PrivateKey != null) return p256PrivateKey.replaceAll('=', '');

if (p256PrivateKey != null) return p256PrivateKey;

/// create key if it is not created
final jwk = JsonWebKey.generate('ES256');

final json = jwk.toJson();
Expand All @@ -290,9 +285,11 @@ Future<String> getPrivateKey({
json.entries.toList()..sort((e1, e2) => e1.key.compareTo(e2.key)),
)..remove('keyOperations');

await secureStorage.set(storageKey, jsonEncode(sortedJwk));
final newKey = jsonEncode(sortedJwk).replaceAll('=', '');

await secureStorage.set(storageKey, newKey);

return jsonEncode(sortedJwk);
return newKey;
}

DidKeyType? getDidKeyFromString(String? didKeyTypeString) {
Expand Down Expand Up @@ -444,21 +441,25 @@ Future<(String, String)> getDidAndKid({
final List<int> prefixByteList = [0xd1, 0xd6, 0x03];
final List<int> prefix = prefixByteList.map((byte) => byte).toList();

final encodedData = sortedPublcJwk(private);
final encodedData = sortedPublcJwkBytes(private);
final encodedAddress = Base58Encode([...prefix, ...encodedData]);

did = 'did:key:z$encodedAddress';
final String lastPart = did.split(':')[2];
kid = '$did#$lastPart';
case DidKeyType.jwkP256:
final private = jsonDecode(privateKey) as Map<String, dynamic>;
final base64EncodedJWK = base64UrlEncode(utf8.encode(private.toString()));

final encodedData = sortedPublcJwkBytes(private);

final base64EncodedJWK = base64UrlEncode(encodedData);
did = 'did:jwk:$base64EncodedJWK';

kid = '$did#0';
case DidKeyType.p256:
case DidKeyType.secp256k1:
if (didKitProvider == null) throw Exception();

const didMethod = AltMeStrings.defaultDIDMethod;
did = didKitProvider.keyToDID(didMethod, privateKey);
kid = await didKitProvider.keyToVerificationMethod(didMethod, privateKey);
Expand Down Expand Up @@ -502,7 +503,7 @@ Future<(String, String)> fetchDidAndKid({
return (did, kid);
}

List<int> sortedPublcJwk(Map<String, dynamic> privateKey) {
List<int> sortedPublcJwkBytes(Map<String, dynamic> privateKey) {
final publicJWK = Map.of(privateKey)..removeWhere((key, value) => key == 'd');

/// we use crv P-256K in the rest of the package to ensure compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class DIDPrivateKeyCubit extends Cubit<String> {
Future<void> initialize() async {
final key =
await secureStorageProvider.get(SecureStorageKeys.ssiKey) ?? '...';
emit(key);
emit(key.replaceAll('=', ''));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ManageDIDEdDSAPage extends StatelessWidget {
final l10n = context.l10n;
final did = context.read<DIDCubit>().state.did ?? '...';
return BasePage(
title: l10n.manageKeyDecentralizedIdEdSA,
title: l10n.keyDecentralizedIdEdSA,
titleAlignment: Alignment.topCenter,
scrollView: false,
titleLeading: const BackLeadingButton(),
Expand Down
2 changes: 1 addition & 1 deletion lib/dashboard/drawer/ssi/manage_did/view/did_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DidView extends StatelessWidget {
height: Sizes.spaceSmall,
),
DrawerItem(
title: l10n.manageKeyDecentralizedIdEdSA,
title: l10n.keyDecentralizedIdEdSA,
onTap: () {
Navigator.of(context)
.push<void>(ManageDIDEdDSAPage.route());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ManageDidPolygonIdPage extends StatelessWidget {
Widget build(BuildContext context) {
final l10n = context.l10n;
return BasePage(
title: l10n.managePolygonIdDecentralizedId,
title: l10n.polygonIdDecentralizedId,
titleAlignment: Alignment.topCenter,
scrollView: false,
titleLeading: const BackLeadingButton(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _ManageDidEbsiPageState extends State<ManageOtherDidPage> {
Widget build(BuildContext context) {
final l10n = context.l10n;
return BasePage(
title: l10n.manageEbsiV3DecentralizedId,
title: l10n.decentralizedIDKey,
titleAlignment: Alignment.topCenter,
scrollView: false,
titleLeading: const BackLeadingButton(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class _OtherDidPrivateKeyPageState extends State<OtherDidPrivateKeyPage>
final l10n = context.l10n;
return BasePage(
scrollView: false,
title: l10n.decentralizedIDKey,
title: widget.didKeyType.getTitle(l10n),
titleAlignment: Alignment.topCenter,
titleLeading: const BackLeadingButton(),
secureScreen: true,
Expand Down
9 changes: 5 additions & 4 deletions lib/l10n/arb/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,10 @@
"transactionDoneDialogDescription": "It can take a few minutes for the transfer to complete",
"withdrawalFailedMessage": "The withdrawal from the account was unsuccessful",
"credentialRequiredMessage": "You need to get those credentials in your wallet to acquire this card:",
"manageKeyDecentralizedIdEdSA": "Key Decentralized ID EdDSA",
"manageKeyDecentralizedIDSecp256k1": "Key Decentralized ID Secp256k1",
"managePolygonIdDecentralizedId": "Polygon Decentralized ID",
"manageEbsiV3DecentralizedId": "EBSI V3 Decentralized ID",
"keyDecentralizedIdEdSA": "Key Decentralized ID EdDSA",
"keyDecentralizedIDSecp256k1": "Key Decentralized ID Secp256k1",
"polygonIdDecentralizedId": "Polygon Decentralized ID",
"ebsiV3DecentralizedId": "EBSI V3 Decentralized ID",
"requiredCredentialNotFoundTitle": "We are unable to find the credential\nyou need in Altme.",
"requiredCredentialNotFoundSubTitle": "The required credential is not in your wallet",
"requiredCredentialNotFoundDescription": "Please contact us on :",
Expand Down Expand Up @@ -979,6 +979,7 @@
"dateOfRequest": "Date of Request",
"keyDecentralizedIDP256": "Key Decentralized ID P-256",
"jwkDecentralizedIDP256": "JWK Decentralized ID P-256",
"jwkDecentralizedIDP256": "JWK Decentralized ID P-256",
"defaultDid": "Default DID",
"selectOneOfTheDid": "Select one of the DIDs",
"subjectSyntaxType": "Subject syntax type",
Expand Down
32 changes: 16 additions & 16 deletions lib/l10n/untranslated.json
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,10 @@
"transactionDoneDialogDescription",
"withdrawalFailedMessage",
"credentialRequiredMessage",
"manageKeyDecentralizedIdEdSA",
"manageKeyDecentralizedIDSecp256k1",
"managePolygonIdDecentralizedId",
"manageEbsiV3DecentralizedId",
"keyDecentralizedIdEdSA",
"keyDecentralizedIDSecp256k1",
"polygonIdDecentralizedId",
"ebsiV3DecentralizedId",
"requiredCredentialNotFoundTitle",
"requiredCredentialNotFoundSubTitle",
"requiredCredentialNotFoundDescription",
Expand Down Expand Up @@ -1648,10 +1648,10 @@
"transactionDoneDialogDescription",
"withdrawalFailedMessage",
"credentialRequiredMessage",
"manageKeyDecentralizedIdEdSA",
"manageKeyDecentralizedIDSecp256k1",
"managePolygonIdDecentralizedId",
"manageEbsiV3DecentralizedId",
"keyDecentralizedIdEdSA",
"keyDecentralizedIDSecp256k1",
"polygonIdDecentralizedId",
"ebsiV3DecentralizedId",
"requiredCredentialNotFoundTitle",
"requiredCredentialNotFoundSubTitle",
"requiredCredentialNotFoundDescription",
Expand Down Expand Up @@ -1859,10 +1859,10 @@
"saveBackupPolygonCredentialSubtitle",
"creationDate",
"altmeSupport",
"manageKeyDecentralizedIdEdSA",
"manageKeyDecentralizedIDSecp256k1",
"managePolygonIdDecentralizedId",
"manageEbsiV3DecentralizedId",
"keyDecentralizedIdEdSA",
"keyDecentralizedIDSecp256k1",
"polygonIdDecentralizedId",
"ebsiV3DecentralizedId",
"requiredCredentialNotFoundTitle",
"requiredCredentialNotFoundSubTitle",
"requiredCredentialNotFoundDescription",
Expand Down Expand Up @@ -2767,10 +2767,10 @@
"transactionDoneDialogDescription",
"withdrawalFailedMessage",
"credentialRequiredMessage",
"manageKeyDecentralizedIdEdSA",
"manageKeyDecentralizedIDSecp256k1",
"managePolygonIdDecentralizedId",
"manageEbsiV3DecentralizedId",
"keyDecentralizedIdEdSA",
"keyDecentralizedIDSecp256k1",
"polygonIdDecentralizedId",
"ebsiV3DecentralizedId",
"requiredCredentialNotFoundTitle",
"requiredCredentialNotFoundSubTitle",
"requiredCredentialNotFoundDescription",
Expand Down
6 changes: 3 additions & 3 deletions packages/oidc4vc/lib/src/oidc4vc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ class OIDC4VC {
/// the same as secp256k1, but we are using secp256k1 now
final jwk = {
'crv': 'secp256k1',
'd': d,
'd': d.replaceAll('=', ''),
'kty': 'EC',
'x': x,
'y': y,
'x': x.replaceAll('=', ''),
'y': y.replaceAll('=', ''),
};
return jwk;
}
Expand Down

0 comments on commit acb7a0a

Please sign in to comment.