-
Notifications
You must be signed in to change notification settings - Fork 29
/
polygon_id_sdk.dart
134 lines (112 loc) · 4.35 KB
/
polygon_id_sdk.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:polygonid_flutter_sdk/common/domain/domain_logger.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/repositories/config_repository.dart';
import 'package:polygonid_flutter_sdk/common/domain/use_cases/get_env_use_case.dart';
import 'package:polygonid_flutter_sdk/common/domain/use_cases/get_selected_chain_use_case.dart';
import 'package:polygonid_flutter_sdk/common/domain/use_cases/set_env_use_case.dart';
import 'package:polygonid_flutter_sdk/common/domain/use_cases/set_selected_chain_use_case.dart';
import 'package:polygonid_flutter_sdk/sdk/di/injector.dart';
import 'package:polygonid_flutter_sdk/sdk/error_handling.dart';
import 'package:polygonid_flutter_sdk/sdk/polygonid_flutter_channel.dart';
import 'credential.dart';
import 'iden3comm.dart';
import 'identity.dart';
import 'proof.dart';
class PolygonIsSdkNotInitializedException implements Exception {
String message;
PolygonIsSdkNotInitializedException(this.message);
}
class PolygonIdSdk {
static PolygonIdSdk? _ref;
static PolygonIdSdk get I {
if (_ref == null) {
throw PolygonIsSdkNotInitializedException(
"The PolygonID SDK has not been initialized,"
"please call and await PolygonIdSDK.init()");
}
return _ref!;
}
/// Initializes the PolygonId SDK
/// Pass [env] to set the environment that contains pushUrl, ipfsUrl, chainConfigs, didMethods
/// Set [newIdentity] param to use a new identity creation and private key handling mechanisms
static Future<void> init({
EnvEntity? env,
bool newIdentity = false,
}) async {
// As [PolygonIdSdk] uses path_provider plugin, we need to ensure the
// platform is initialized
WidgetsFlutterBinding.ensureInitialized();
String? stacktraceEncryptionKey = env?.stacktraceEncryptionKey;
if (stacktraceEncryptionKey != null &&
stacktraceEncryptionKey.isNotEmpty &&
utf8.encode(stacktraceEncryptionKey).length == 32) {
await Hive.initFlutter();
await Hive.openBox(
'stacktrace',
encryptionCipher: HiveAesCipher(utf8.encode(stacktraceEncryptionKey)),
);
}
// Init injection
await configureInjection(newIdentity);
await getItSdk.allReady();
// Set env
if (env != null) {
await getItSdk
.getAsync<SetEnvUseCase>()
.then((instance) => instance.execute(param: env));
}
if (env?.chainConfigs.entries.isNotEmpty ?? false) {
await getItSdk.getAsync<SetSelectedChainUseCase>().then(
(instance) => instance.execute(param: env!.chainConfigs.keys.first));
}
// SDK singleton
_ref = PolygonIdSdk._();
_ref!.identity = await getItSdk.getAsync<Identity>();
_ref!.credential = await getItSdk.getAsync<Credential>();
_ref!.proof = await getItSdk.getAsync<Proof>();
_ref!.iden3comm = await getItSdk.getAsync<Iden3comm>();
_ref!.errorHandling = getItSdk.get<ErrorHandling>();
// Channel
getItSdk<PolygonIdFlutterChannel>();
// Logging
Domain.logger = getItSdk<PolygonIdSdkLogger>();
}
late Identity identity;
late Credential credential;
late Proof proof;
late Iden3comm iden3comm;
late ErrorHandling errorHandling;
PolygonIdSdk._();
Future<void> setEnv({required EnvEntity env}) {
return getItSdk
.getAsync<SetEnvUseCase>()
.then((instance) => instance.execute(param: env));
}
Future<EnvEntity> getEnv() {
return getItSdk
.getAsync<GetEnvUseCase>()
.then((instance) => instance.execute());
}
Future<ChainConfigEntity> getSelectedChain() {
return getItSdk
.getAsync<GetSelectedChainUseCase>()
.then((instance) => instance.execute());
}
Future<String?> getSelectedChainId() {
return getItSdk
.getAsync<ConfigRepository>()
.then((instance) => instance.getSelectedChainId());
}
Future<void> setSelectedChain({required String chainConfigId}) {
return getItSdk
.getAsync<SetSelectedChainUseCase>()
.then((instance) => instance.execute(param: chainConfigId));
}
Future<void> switchLog({required bool enabled}) async {
Domain.logEnabled = enabled;
}
}