diff --git a/packages/polygonid/lib/src/polygonid.dart b/packages/polygonid/lib/src/polygonid.dart index b13f2fba4..0492af428 100644 --- a/packages/polygonid/lib/src/polygonid.dart +++ b/packages/polygonid/lib/src/polygonid.dart @@ -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'; @@ -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, @@ -91,19 +97,19 @@ class PolygonId { /// PolygonId SDK getEnv Future getEnv() async { - return PolygonIdSdk.I.getEnv(); + return polygonIdSdk.getEnv(); } /// check if curcuit is already downloaded Future isCircuitsDownloaded() async { final isDownloaded = - await PolygonIdSdk.I.proof.isAlreadyDownloadedCircuitsFromServer(); + await polygonIdSdk.proof.isAlreadyDownloadedCircuitsFromServer(); return isDownloaded; } /// init Circuits Download And Get Info Stream Stream get initCircuitsDownloadAndGetInfoStream { - return PolygonIdSdk.I.proof.initCircuitsDownloadAndGetInfoStream; + return polygonIdSdk.proof.initCircuitsDownloadAndGetInfoStream; } /// Create Identity @@ -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) { @@ -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, @@ -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, ); @@ -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, ); @@ -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> getIdentities() { - final sdk = PolygonIdSdk.I; - return sdk.identity.getIdentities(); + return polygonIdSdk.identity.getIdentities(); } /// Returns a [Iden3MessageEntity] from an iden3comm message string. @@ -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 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 @@ -241,10 +241,8 @@ 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, @@ -252,7 +250,7 @@ class PolygonId { /// 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, @@ -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, @@ -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, @@ -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, ); @@ -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, @@ -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, ); @@ -418,8 +408,7 @@ class PolygonId { Future>> 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 @@ -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, @@ -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')) { @@ -485,7 +472,8 @@ class PolygonId { network: network, ); - final List response = await sdk.iden3comm.getProofs( + final List response = + await polygonIdSdk.iden3comm.getProofs( message: contractIden3messageEntity, genesisDid: userIdentity.did, privateKey: userIdentity.privateKey, diff --git a/packages/polygonid/pubspec.yaml b/packages/polygonid/pubspec.yaml index 590605453..c680a37ce 100644 --- a/packages/polygonid/pubspec.yaml +++ b/packages/polygonid/pubspec.yaml @@ -20,4 +20,5 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter + mocktail: ^1.0.2 very_good_analysis: ^5.0.0+1 diff --git a/packages/polygonid/test/src/polygonid_test.dart b/packages/polygonid/test/src/polygonid_test.dart index 8978d590f..2c94e254b 100644 --- a/packages/polygonid/test/src/polygonid_test.dart +++ b/packages/polygonid/test/src/polygonid_test.dart @@ -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); }); }); }