Skip to content

Commit

Permalink
add some polygon id test
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed May 31, 2024
1 parent 0aa043d commit 0c7bd1f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 51 deletions.
88 changes: 38 additions & 50 deletions packages/polygonid/lib/src/polygonid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ import 'package:web3dart/web3dart.dart';
/// {@endtemplate}
class PolygonId {
/// {@macro polygonid}
factory PolygonId() {
return _instance ??= PolygonId._();
factory PolygonId({PolygonIdSdk? polygonIdSdk}) {
if (polygonIdSdk != null) {
_instance.polygonIdSdk = polygonIdSdk;
return _instance;
} else {
_instance.polygonIdSdk = PolygonIdSdk.I;
return _instance;
}
}

/// private contructor
PolygonId._();
PolygonId._internal();

late PolygonIdSdk polygonIdSdk;

/// _instance
static PolygonId? _instance;
static final PolygonId _instance = PolygonId._internal();

/// blockchain
static const blockchain = 'polygon';
Expand Down Expand Up @@ -75,7 +81,7 @@ class PolygonId {
required String network,
required String ipfsUrl,
}) {
return PolygonIdSdk.I.setEnv(
return polygonIdSdk.setEnv(
env: EnvEntity(
blockchain: blockchain,
network: network,
Expand All @@ -91,19 +97,19 @@ class PolygonId {

/// PolygonId SDK getEnv
Future<EnvEntity> getEnv() async {
return PolygonIdSdk.I.getEnv();
return polygonIdSdk.getEnv();
}

/// check if curcuit is already downloaded
Future<bool> isCircuitsDownloaded() async {
final isDownloaded =
await PolygonIdSdk.I.proof.isAlreadyDownloadedCircuitsFromServer();
await polygonIdSdk.proof.isAlreadyDownloadedCircuitsFromServer();
return isDownloaded;
}

/// init Circuits Download And Get Info Stream
Stream<DownloadInfo> get initCircuitsDownloadAndGetInfoStream {
return PolygonIdSdk.I.proof.initCircuitsDownloadAndGetInfoStream;
return polygonIdSdk.proof.initCircuitsDownloadAndGetInfoStream;
}

/// Create Identity
Expand All @@ -121,9 +127,8 @@ class PolygonId {
required String network,
}) async {
try {
final sdk = PolygonIdSdk.I;
final secret = bip393.mnemonicToEntropy(mnemonic);
final identity = await sdk.identity.addIdentity(secret: secret);
final identity = await polygonIdSdk.identity.addIdentity(secret: secret);
return identity;
} catch (e) {
if (e is IdentityAlreadyExistsException) {
Expand Down Expand Up @@ -158,9 +163,8 @@ class PolygonId {
required String mnemonic,
required String network,
}) async {
final sdk = PolygonIdSdk.I;
final privateKey = await getPrivateKey(mnemonic: mnemonic);
final did = await sdk.identity.getDidIdentifier(
final did = await polygonIdSdk.identity.getDidIdentifier(
blockchain: blockchain,
network: network,
privateKey: privateKey,
Expand All @@ -174,12 +178,11 @@ class PolygonId {
required String mnemonic,
required String network,
}) async {
final sdk = PolygonIdSdk.I;
final userIdentity = await getUserIdentity(
mnemonic: mnemonic,
network: network,
);
final identity = await sdk.identity.restoreIdentity(
final identity = await polygonIdSdk.identity.restoreIdentity(
privateKey: userIdentity.privateKey,
genesisDid: userIdentity.did,
);
Expand All @@ -192,13 +195,12 @@ class PolygonId {
required String privateKey,
required String network,
}) async {
final sdk = PolygonIdSdk.I;
final genesisDid = await sdk.identity.getDidIdentifier(
final genesisDid = await polygonIdSdk.identity.getDidIdentifier(
blockchain: blockchain,
network: network,
privateKey: privateKey,
);
return sdk.identity.removeIdentity(
return polygonIdSdk.identity.removeIdentity(
privateKey: privateKey,
genesisDid: genesisDid,
);
Expand All @@ -207,8 +209,7 @@ class PolygonId {
/// Get a list of public info of [IdentityEntity] associated
/// to the identities stored in the Polygon ID Sdk.
Future<List<IdentityEntity>> getIdentities() {
final sdk = PolygonIdSdk.I;
return sdk.identity.getIdentities();
return polygonIdSdk.identity.getIdentities();
}

/// Returns a [Iden3MessageEntity] from an iden3comm message string.
Expand All @@ -219,8 +220,7 @@ class PolygonId {
/// iden3comm message string needs to be parsed to a supported
/// [Iden3MessageEntity] by the Polygon Id Sdk using this method.
Future<Iden3MessageEntity> getIden3Message({required String message}) {
final sdk = PolygonIdSdk.I;
return sdk.iden3comm.getIden3Message(message: message);
return polygonIdSdk.iden3comm.getIden3Message(message: message);
}

/// Authenticate response from iden3Message sharing the needed
Expand All @@ -241,18 +241,16 @@ class PolygonId {
required String mnemonic,
}) async {
try {
final sdk = PolygonIdSdk.I;

final privateKey = await getPrivateKey(mnemonic: mnemonic);
final did = await sdk.identity.getDidIdentifier(
final did = await polygonIdSdk.identity.getDidIdentifier(
blockchain: blockchain,
network: network,
privateKey: privateKey,
);

/// Authenticate response from iden3Message sharing the needed
/// (if any) proofs requested by it
await sdk.iden3comm.authenticate(
await polygonIdSdk.iden3comm.authenticate(
message: iden3MessageEntity,
genesisDid: did,
privateKey: privateKey,
Expand Down Expand Up @@ -281,16 +279,14 @@ class PolygonId {
required String network,
}) async {
try {
final sdk = PolygonIdSdk.I;

final privateKey = await getPrivateKey(mnemonic: mnemonic);
final did = await sdk.identity.getDidIdentifier(
final did = await polygonIdSdk.identity.getDidIdentifier(
blockchain: blockchain,
network: network,
privateKey: privateKey,
);

final claimEntities = await sdk.iden3comm.fetchAndSaveClaims(
final claimEntities = await polygonIdSdk.iden3comm.fetchAndSaveClaims(
message: iden3MessageEntity,
genesisDid: did,
privateKey: privateKey,
Expand All @@ -311,16 +307,14 @@ class PolygonId {
required String mnemonic,
required String network,
}) async {
final sdk = PolygonIdSdk.I;

final privateKey = await getPrivateKey(mnemonic: mnemonic);
final did = await sdk.identity.getDidIdentifier(
final did = await polygonIdSdk.identity.getDidIdentifier(
blockchain: blockchain,
network: network,
privateKey: privateKey,
);

return sdk.credential.getClaimsByIds(
return polygonIdSdk.credential.getClaimsByIds(
claimIds: [claimId],
genesisDid: did,
privateKey: privateKey,
Expand All @@ -345,12 +339,11 @@ class PolygonId {
required String mnemonic,
required String network,
}) async {
final sdk = PolygonIdSdk.I;
final userIdentity = await getUserIdentity(
mnemonic: mnemonic,
network: network,
);
return sdk.identity.backupIdentity(
return polygonIdSdk.identity.backupIdentity(
privateKey: userIdentity.privateKey,
genesisDid: userIdentity.did,
);
Expand All @@ -375,12 +368,11 @@ class PolygonId {
required String encryptedDb,
required String network,
}) async {
final sdk = PolygonIdSdk.I;
final userIdentity = await getUserIdentity(
mnemonic: mnemonic,
network: network,
);
final identity = await sdk.identity.restoreIdentity(
final identity = await polygonIdSdk.identity.restoreIdentity(
privateKey: userIdentity.privateKey,
genesisDid: userIdentity.did,
encryptedDb: encryptedDb,
Expand All @@ -401,9 +393,7 @@ class PolygonId {
required PrivateIdentityEntity privateIdentityEntity,
}) async {
try {
final sdk = PolygonIdSdk.I;

final claimEntities = await sdk.credential.getClaims(
final claimEntities = await polygonIdSdk.credential.getClaims(
genesisDid: privateIdentityEntity.did,
privateKey: privateIdentityEntity.privateKey,
);
Expand All @@ -418,8 +408,7 @@ class PolygonId {
Future<List<Map<String, dynamic>>> getSchemas({
required Iden3MessageEntity message,
}) async {
final sdk = PolygonIdSdk.I;
return sdk.iden3comm.getSchemas(message: message);
return polygonIdSdk.iden3comm.getSchemas(message: message);
}

/// Get a list of [ClaimEntity] from iden3comm message
Expand All @@ -440,17 +429,16 @@ class PolygonId {
required String network,
}) async {
try {
final sdk = PolygonIdSdk.I;

final privateKey = await getPrivateKey(mnemonic: mnemonic);

final did = await sdk.identity.getDidIdentifier(
final did = await polygonIdSdk.identity.getDidIdentifier(
blockchain: blockchain,
network: network,
privateKey: privateKey,
);

final claimEntities = await sdk.iden3comm.getClaimsFromIden3Message(
final claimEntities =
await polygonIdSdk.iden3comm.getClaimsFromIden3Message(
message: iden3MessageEntity,
genesisDid: did,
privateKey: privateKey,
Expand All @@ -469,7 +457,6 @@ class PolygonId {
required String mnemonic,
required String network,
}) async {
final sdk = PolygonIdSdk.I;
var challenge = walletAddress;

if (challenge.toLowerCase().startsWith('0x')) {
Expand All @@ -485,7 +472,8 @@ class PolygonId {
network: network,
);

final List<Iden3commProofEntity> response = await sdk.iden3comm.getProofs(
final List<Iden3commProofEntity> response =
await polygonIdSdk.iden3comm.getProofs(
message: contractIden3messageEntity,
genesisDid: userIdentity.did,
privateKey: userIdentity.privateKey,
Expand Down
1 change: 1 addition & 0 deletions packages/polygonid/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
mocktail: ^1.0.2
very_good_analysis: ^5.0.0+1
47 changes: 46 additions & 1 deletion packages/polygonid/test/src/polygonid_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
// ignore_for_file: prefer_const_constructors
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:polygonid/polygonid.dart';
import 'package:polygonid_flutter_sdk/common/domain/entities/env_entity.dart';
import 'package:polygonid_flutter_sdk/sdk/polygon_id_sdk.dart';

class MockPolygonIdSdk extends Mock implements PolygonIdSdk {}

void main() {
final MockPolygonIdSdk polygonIdSdk = MockPolygonIdSdk();
final PolygonId polygonId = PolygonId(polygonIdSdk: polygonIdSdk);

final env = EnvEntity(
blockchain: 'polygon',
network: 'network',
web3Url: 'web3Url',
web3RdpUrl: 'web3RdpUrl',
web3ApiKey: 'web3ApiKey',
idStateContract: 'idStateContract',
pushUrl: 'pushUrl',
ipfsUrl: 'ipfsUrl',
);

group('PolygonId', () {
test('can be instantiated', () {
expect(PolygonId(), isNotNull);
expect(polygonId, isNotNull);
});

test('initialised default value is correct', () {
expect(PolygonId.blockchain, 'polygon');
expect(polygonId.isInitialized, false);
});

test('setEnv works correctly', () async {
when(() => polygonIdSdk.setEnv(env: env)).thenAnswer((_) async => {});
await polygonId.setEnv(
network: 'network',
web3Url: 'web3Url',
web3RdpUrl: 'web3RdpUrl',
web3ApiKey: 'web3ApiKey',
idStateContract: 'idStateContract',
pushUrl: 'pushUrl',
ipfsUrl: 'ipfsUrl',
);
verify(() => polygonIdSdk.setEnv(env: env)).called(1);
});

test('getEnv works correctly', () async {
when(polygonIdSdk.getEnv).thenAnswer((_) async => env);
final data = await polygonId.getEnv();
expect(data, env);
verify(polygonIdSdk.getEnv).called(1);
});
});
}

0 comments on commit 0c7bd1f

Please sign in to comment.