Skip to content

Commit

Permalink
Fixes for auth.
Browse files Browse the repository at this point in the history
  • Loading branch information
5eeman committed Jul 25, 2024
1 parent 0b75d23 commit 27df086
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
3 changes: 3 additions & 0 deletions example/lib/src/presentation/ui/auth/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
import 'package:polygonid_flutter_sdk/common/domain/domain_constants.dart';
import 'package:polygonid_flutter_sdk/common/domain/entities/chain_config_entity.dart';
import 'package:polygonid_flutter_sdk/common/domain/entities/env_entity.dart';
import 'package:polygonid_flutter_sdk/common/domain/error_exception.dart';
import 'package:polygonid_flutter_sdk/iden3comm/domain/entities/common/iden3_message_entity.dart';
import 'package:polygonid_flutter_sdk/iden3comm/domain/exceptions/iden3comm_exceptions.dart';
import 'package:polygonid_flutter_sdk/identity/domain/entities/identity_entity.dart';
Expand Down Expand Up @@ -119,6 +120,8 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
emit(const AuthState.authenticated());
} on OperatorException catch (error) {
emit(AuthState.error(error.errorMessage));
} on PolygonIdSDKException catch (error) {
emit(AuthState.error(error.errorMessage));
} catch (error) {
emit(AuthState.error(error.toString()));
}
Expand Down
32 changes: 10 additions & 22 deletions lib/iden3comm/authenticate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ import 'package:polygonid_flutter_sdk/iden3comm/domain/use_cases/generate_iden3c
import 'package:polygonid_flutter_sdk/iden3comm/domain/use_cases/get_iden3message_use_case.dart';
import 'package:polygonid_flutter_sdk/identity/data/data_sources/lib_babyjubjub_data_source.dart';
import 'package:polygonid_flutter_sdk/identity/data/data_sources/lib_pidcore_identity_data_source.dart';
import 'package:polygonid_flutter_sdk/identity/data/data_sources/wallet_data_source.dart';
import 'package:polygonid_flutter_sdk/identity/data/dtos/circuit_type.dart';
import 'package:polygonid_flutter_sdk/identity/domain/entities/hash_entity.dart';
import 'package:polygonid_flutter_sdk/identity/domain/entities/node_entity.dart';
import 'package:polygonid_flutter_sdk/identity/data/mappers/hex_mapper.dart';
import 'package:polygonid_flutter_sdk/identity/domain/entities/identity_entity.dart';
import 'package:polygonid_flutter_sdk/identity/domain/entities/tree_state_entity.dart';
import 'package:polygonid_flutter_sdk/identity/domain/entities/tree_type.dart';
import 'package:polygonid_flutter_sdk/identity/domain/repositories/identity_repository.dart';
import 'package:polygonid_flutter_sdk/identity/domain/repositories/smt_repository.dart';
import 'package:polygonid_flutter_sdk/identity/domain/use_cases/get_did_identifier_use_case.dart';
import 'package:polygonid_flutter_sdk/identity/libs/bjj/bjj_wallet.dart';
Expand Down Expand Up @@ -438,7 +440,7 @@ class Authenticate {
proofRequest.scope.circuitId == CircuitType.sigonchain.name ||
proofRequest.scope.circuitId == CircuitType.circuitsV3onchain.name) {
/// SIGN MESSAGE
String signature = signMessage(
String signature = await signMessage(
privateKey: privateKeyBytes,
message: challenge!,
);
Expand Down Expand Up @@ -1016,25 +1018,10 @@ class Authenticate {
}

/// SIGN MESSAGE WITH BJJ KEY
String signMessage({required Uint8List privateKey, required String message}) {
BigInt? messHash;
if (message.toLowerCase().startsWith("0x")) {
message = strip0x(message);
messHash = BigInt.tryParse(message, radix: 16);
} else {
messHash = BigInt.tryParse(message, radix: 10);
}
final bjjKey = bjj.PrivateKey(privateKey);
if (messHash != null) {
final signature = bjjKey.sign(messHash);
return signature;
} else {
_stacktraceManager.addError(
"[Authenticate] message string couldn't be parsed as BigInt",
);
throw const FormatException(
"message string couldn't be parsed as BigInt");
}
Future<String> signMessage(
{required Uint8List privateKey, required String message}) async {
final walletDs = getItSdk<WalletDataSource>();
return walletDs.signMessage(privateKey: privateKey, message: message);
}

Future<AuthBodyDidDocResponseDTO?> _getDidDoc({
Expand Down Expand Up @@ -1167,7 +1154,7 @@ class Authenticate {

String authChallenge = await libBabyJubJub.hashPoseidon(qNormalized);

String signature = signMessage(
String signature = await signMessage(
privateKey: privateKeyBytes,
message: authChallenge,
);
Expand Down Expand Up @@ -1264,7 +1251,8 @@ class Authenticate {
var libPolygonIdCredential =
getItSdk<LibPolygonIdCoreCredentialDataSource>();

List<String> publicKey = BjjWallet(privateKeyBytes).publicKey;
final identityRepo = await getItSdk.getAsync<IdentityRepository>();
final publicKey = await identityRepo.getPublicKeys(privateKey: privateKey);

String authClaimSchema = AUTH_CLAIM_SCHEMA;
String issuedAuthClaim = libPolygonIdCredential.issueClaim(
Expand Down

0 comments on commit 27df086

Please sign in to comment.