11import 'dart:convert' ;
2- import 'dart:developer' ;
32
4- import 'package:web3dart/crypto.dart' ;
53import 'package:web3dart/web3dart.dart' ;
64import 'package:web_socket_channel/io.dart' ;
75import 'package:http/http.dart' as http;
86
97import '../config.dart' ;
108import '../secrets.dart' ;
119
12- class UserContractService {
13-
14- Future <Map <String , String >> deploy ({
15- required String name,
16- required String email,
17- required String dob,
18- required String country,
19- required String mobile,
20- required String gender
21- }) async {
10+ class UserContractService {
11+ Future <Map <String , String >> deploy (
12+ {required String name,
13+ required String email,
14+ required String dob,
15+ required String country,
16+ required String mobile,
17+ required String gender}) async {
2218 // return the address of deployed contract
23- final response = await http.post (Uri .parse ("${Config .backendUrl }/contract" ),
24- headers: {
25- "apiKey" : APIKEY ,
26- "Content-Type" : "application/json"
27- },
28- body: {
29- "name" : name,
30- "email" : email,
31- "dob" : dob,
32- "country" : country,
33- "mobile" : mobile,
34- "gender" : gender
35- });
36-
37- if (response.statusCode == 200 ){
19+ final response =
20+ await http.post (Uri .parse ("${Config .backendUrl }/contract" ), headers: {
21+ "apiKey" : APIKEY ,
22+ }, body: {
23+ "name" : name,
24+ "email" : email,
25+ "dob" : dob,
26+ "country" : country,
27+ "mobile" : mobile,
28+ "gender" : gender
29+ });
30+
31+ if (response.statusCode == 200 ) {
3832 var responseJson = jsonDecode (response.body);
39- return {
40- 'private-key' : responseJson['private-key' ]
41- };
42- }
43- else {
33+ return {'private-key' : responseJson['private-key' ]};
34+ } else {
4435 throw Exception ("Unable to connect to the backend" );
4536 }
4637 }
4738
48- Future <Object > getAbi () async {
39+ Future <Object > getAbi () async {
4940 // get the abi from calling the application backend
50- final response = await http.get (
51- Uri .parse ("${Config .backendUrl }/contract" ),
52- headers: {
53- "apiKey" : APIKEY
54- }
55- );
56- if (response.statusCode == 200 ){
41+ final response = await http.get (Uri .parse ("${Config .backendUrl }/contract" ),
42+ headers: {"apiKey" : APIKEY });
43+ if (response.statusCode == 200 ) {
5744 var responseJson = jsonDecode (response.body);
5845 return responseJson['data' ];
59- }
60- else {
46+ } else {
6147 throw Exception ("Unable to connect to the backend" );
6248 }
6349 }
6450
65- Future <Map <String , String >> getAll (String contractAddress, String privateKey) async {
51+ Future <Map <String , String >> getAll (
52+ String contractAddress, String privateKey) async {
6653 EthereumAddress contract = EthereumAddress .fromHex (contractAddress);
6754 EthPrivateKey credentials = EthPrivateKey .fromHex (privateKey);
6855 EthereumAddress sender = await credentials.extractAddress ();
6956
7057 Object abi = await getAbi ();
7158
72- DeployedContract smartContract = DeployedContract (ContractAbi .fromJson (jsonEncode (abi), "User" ), contract);
59+ DeployedContract smartContract = DeployedContract (
60+ ContractAbi .fromJson (jsonEncode (abi), "User" ), contract);
7361
7462 ContractFunction getName = smartContract.function ("getName" );
7563 ContractFunction getEmail = smartContract.function ("getEmail" );
@@ -78,32 +66,39 @@ class UserContractService{
7866 ContractFunction getCountry = smartContract.function ("getCountry" );
7967 ContractFunction getGender = smartContract.function ("getGender" );
8068
81- Web3Client web3client = Web3Client (Config .rpcUrl, http.Client (), socketConnector: (){
69+ Web3Client web3client =
70+ Web3Client (Config .rpcUrl, http.Client (), socketConnector: () {
8271 return IOWebSocketChannel .connect (Config .wsUrl).cast <String >();
8372 });
8473
8574 try {
86- var name = await web3client.call (contract: smartContract,
75+ var name = await web3client.call (
76+ contract: smartContract,
8777 function: getName,
8878 params: [],
8979 sender: sender);
90- var email = await web3client.call (contract: smartContract,
80+ var email = await web3client.call (
81+ contract: smartContract,
9182 function: getEmail,
9283 params: [],
9384 sender: sender);
94- var dob = await web3client.call (contract: smartContract,
85+ var dob = await web3client.call (
86+ contract: smartContract,
9587 function: getDOB,
9688 params: [],
9789 sender: sender);
98- var mobile = await web3client.call (contract: smartContract,
90+ var mobile = await web3client.call (
91+ contract: smartContract,
9992 function: getMobile,
10093 params: [],
10194 sender: sender);
102- var country = await web3client.call (contract: smartContract,
95+ var country = await web3client.call (
96+ contract: smartContract,
10397 function: getCountry,
10498 params: [],
10599 sender: sender);
106- var gender = await web3client.call (contract: smartContract,
100+ var gender = await web3client.call (
101+ contract: smartContract,
107102 function: getGender,
108103 params: [],
109104 sender: sender);
@@ -116,93 +111,105 @@ class UserContractService{
116111 'Phone' : mobile[0 ] as String ,
117112 'Gender' : gender[0 ] as String
118113 };
119- }
120- catch (e){
114+ } catch (e) {
121115 throw Exception ("Non authorized access" );
122- }
123- finally {
116+ } finally {
124117 web3client.dispose ();
125118 }
126119 }
127120
128- Future <String > _setUserProperty (String setFunctionName, String newValue, {required String contractAddress, required String privateKey}) async {
121+ Future <String > _setUserProperty (String setFunctionName, String newValue,
122+ {required String contractAddress, required String privateKey}) async {
129123 EthereumAddress contract = EthereumAddress .fromHex (contractAddress);
130124 EthPrivateKey credentials = EthPrivateKey .fromHex (privateKey);
131125
132126 Object abi = await getAbi ();
133127
134- DeployedContract smartContract = DeployedContract (ContractAbi .fromJson (jsonEncode (abi), "User" ), contract);
128+ DeployedContract smartContract = DeployedContract (
129+ ContractAbi .fromJson (jsonEncode (abi), "User" ), contract);
135130 ContractFunction setFunction = smartContract.function (setFunctionName);
136131
137- Web3Client web3client = Web3Client (Config .rpcUrl, http.Client (), socketConnector: (){
132+ Web3Client web3client =
133+ Web3Client (Config .rpcUrl, http.Client (), socketConnector: () {
138134 return IOWebSocketChannel .connect (Config .wsUrl).cast <String >();
139135 });
140136
141- try {
142- var transactionId = await web3client.sendTransaction (credentials, Transaction .callContract (contract: smartContract, function: setFunction, parameters: [newValue]));
137+ try {
138+ var transactionId = await web3client.sendTransaction (
139+ credentials,
140+ Transaction .callContract (
141+ contract: smartContract,
142+ function: setFunction,
143+ parameters: [newValue]));
143144 return transactionId;
144- }
145- catch (e){
145+ } catch (e) {
146146 throw Exception ("Failed due to $e " );
147- }
148- finally {
147+ } finally {
149148 web3client.dispose ();
150149 }
151150 }
152151
153- Future <String > setName (String name, {required String contractAddress, required String privateKey}) async {
154- return _setUserProperty ("setName" , name, contractAddress: contractAddress, privateKey: privateKey);
152+ Future <String > setName (String name,
153+ {required String contractAddress, required String privateKey}) async {
154+ return _setUserProperty ("setName" , name,
155+ contractAddress: contractAddress, privateKey: privateKey);
155156 }
156157
157- Future <String > setEmail (String email, {required String contractAddress, required String privateKey}) async {
158- return _setUserProperty ("setEmail" , email, contractAddress: contractAddress, privateKey: privateKey);
158+ Future <String > setEmail (String email,
159+ {required String contractAddress, required String privateKey}) async {
160+ return _setUserProperty ("setEmail" , email,
161+ contractAddress: contractAddress, privateKey: privateKey);
159162 }
160163
161- Future <String > setCountry (String country, {required String contractAddress, required String privateKey}) async {
162- return _setUserProperty ("setCountry" , country, contractAddress: contractAddress, privateKey: privateKey);
164+ Future <String > setCountry (String country,
165+ {required String contractAddress, required String privateKey}) async {
166+ return _setUserProperty ("setCountry" , country,
167+ contractAddress: contractAddress, privateKey: privateKey);
163168 }
164169
165- Future <String > setMobile (String mobile, {required String contractAddress, required String privateKey}) async {
166- return _setUserProperty ("setMobile" , mobile, contractAddress: contractAddress, privateKey: privateKey);
170+ Future <String > setMobile (String mobile,
171+ {required String contractAddress, required String privateKey}) async {
172+ return _setUserProperty ("setMobile" , mobile,
173+ contractAddress: contractAddress, privateKey: privateKey);
167174 }
168175
169- Future <String > setGender (String gender, {required String contractAddress, required String privateKey}) async {
170- return _setUserProperty ("setGender" , gender, contractAddress: contractAddress, privateKey: privateKey);
176+ Future <String > setGender (String gender,
177+ {required String contractAddress, required String privateKey}) async {
178+ return _setUserProperty ("setGender" , gender,
179+ contractAddress: contractAddress, privateKey: privateKey);
171180 }
172181
173- Future <String > verify (String verifierContractAddress, String token, {required String contractAddress, required String privateKey}) async {
182+ Future <String > verify (String verifierContractAddress, String token,
183+ {required String contractAddress, required String privateKey}) async {
174184 EthereumAddress contract = EthereumAddress .fromHex (contractAddress);
175- EthereumAddress verifierContract = EthereumAddress .fromHex (verifierContractAddress);
185+ EthereumAddress verifierContract =
186+ EthereumAddress .fromHex (verifierContractAddress);
176187 EthPrivateKey credentials = EthPrivateKey .fromHex (privateKey);
177188 Object abi = await getAbi ();
178189
179- DeployedContract smartContract = DeployedContract (ContractAbi .fromJson (jsonEncode (abi), "User" ), contract);
190+ DeployedContract smartContract = DeployedContract (
191+ ContractAbi .fromJson (jsonEncode (abi), "User" ), contract);
180192 ContractFunction verifyFunction = smartContract.function ("verify" );
181193
182- Web3Client web3client = Web3Client (Config .rpcUrl, http.Client (), socketConnector: (){
194+ Web3Client web3client =
195+ Web3Client (Config .rpcUrl, http.Client (), socketConnector: () {
183196 return IOWebSocketChannel .connect (Config .wsUrl).cast <String >();
184197 });
185198
186199 var transaction = Transaction .callContract (
187- contract: smartContract,
188- function: verifyFunction,
189- parameters: [verifierContract, contract, token],
200+ contract: smartContract,
201+ function: verifyFunction,
202+ parameters: [verifierContract, contract, token],
190203 );
191204
192- try {
193- String tx = await web3client.sendTransaction (
194- credentials,
195- transaction,
196- chainId: Config .chainId
197- );
205+ try {
206+ String tx = await web3client.sendTransaction (credentials, transaction,
207+ chainId: Config .chainId);
198208 return tx;
199- }
200- catch (e){
209+ } catch (e) {
201210 throw Exception ("Failed due to $e " );
202- }
203- finally {
211+ } finally {
204212 web3client.dispose ();
205213 }
206214 }
207-
208- }
215+ }
0 commit comments