-
Notifications
You must be signed in to change notification settings - Fork 23
/
client.rs
2361 lines (2214 loc) · 77.8 KB
/
client.rs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use clap::AppSettings;
use client_server_helpers::*;
use concordium_base::{
common::{
types::{Amount, CredentialIndex, KeyIndex, KeyPair, TransactionTime},
*,
},
dodis_yampolskiy_prf as prf,
elgamal::{self, PublicKey, SecretKey},
id::{
self,
account_holder::*,
constants::{ArCurve, IpPairing},
curve_arithmetic::*,
identity_provider::*,
secret_sharing::*,
types::*,
},
pedersen_commitment::Value as PedersenValue,
ps_sig,
};
use dialoguer::{Input, MultiSelect, Select};
use ed25519_dalek as ed25519;
use either::Either::{Left, Right};
use key_derivation::{ConcordiumHdWallet, CredentialContext, Net};
use rand::*;
use serde_json::{json, to_value};
use std::{
cmp::max,
collections::btree_map::BTreeMap,
convert::TryFrom,
fs::File,
io::{self, Write},
path::{Path, PathBuf},
};
use structopt::StructOpt;
type Bls12 = IpPairing;
type G1 = ArCurve;
static IP_NAME_PREFIX: &str = "identity_provider-";
static AR_NAME_PREFIX: &str = "AR-";
fn mk_ip_filename(path: &Path, n: usize) -> (PathBuf, PathBuf) {
let mut public = path.to_path_buf();
public.push(format!("{}{}.pub.json", IP_NAME_PREFIX, n));
let mut private = path.to_path_buf();
private.push(format!("{}{}.json", IP_NAME_PREFIX, n));
(public, private)
}
fn mk_ip_description(n: usize) -> Description {
let mut s = IP_NAME_PREFIX.to_string();
s.push_str(&n.to_string());
mk_dummy_description(s)
}
// Generate name for the n-th anonymity revoker.
// Returns the pair for public and public + private data.
fn mk_ar_filename(path: &Path, n: u32) -> (PathBuf, PathBuf) {
let mut public = path.to_path_buf();
public.push(format!("{}{}.pub.json", AR_NAME_PREFIX, n));
let mut private = path.to_path_buf();
private.push(format!("{}{}.json", AR_NAME_PREFIX, n));
(public, private)
}
fn mk_ar_description(n: u32) -> Description {
let mut s = AR_NAME_PREFIX.to_string();
s.push_str(&n.to_string());
mk_dummy_description(s)
}
/// Read validTo from stdin in format YYYYMM and return YearMonth
fn read_validto() -> io::Result<YearMonth> {
let input: String = Input::new()
.with_prompt("Enter valid to (YYYYMM)")
.interact()?;
match parse_yearmonth(&input) {
Some(ym) => Ok(ym),
None => panic!("Unable to parse YYYYMM"),
}
}
#[derive(StructOpt)]
struct CreateHdWallet {
#[structopt(
long = "out",
help = "Optional file to write the hd wallet to. If not provided, the hd wallet JSON will \
be written to standard output."
)]
out: Option<PathBuf>,
#[structopt(long = "testnet")]
testnet: bool,
}
#[derive(StructOpt)]
struct GenerateIdRecoveryRequest {
#[structopt(
long = "ip-info",
help = "File with information about the identity provider."
)]
ip_info: PathBuf,
#[structopt(
long = "request-out",
help = "File to write the request to that is to be sent to the identity provider."
)]
request_file: PathBuf,
#[structopt(
long = "cryptographic-parameters",
help = "File with cryptographic parameters."
)]
global: PathBuf,
#[structopt(long = "chi", help = "File with input credential holder information.")]
chi: PathBuf,
}
#[derive(StructOpt)]
struct ValidateIdRecoveryRequest {
#[structopt(long = "request", help = "File with id recovery request.")]
request: PathBuf,
#[structopt(
long = "ip-info",
help = "File with information about the identity provider."
)]
ip_info: PathBuf,
#[structopt(
long = "cryptographic-parameters",
help = "File with cryptographic parameters."
)]
global: PathBuf,
}
#[derive(StructOpt)]
struct CreateChi {
#[structopt(long = "out")]
out: Option<PathBuf>,
#[structopt(
long = "hd-wallet",
help = "File with hd wallet.",
requires = "identity-index"
)]
hd_wallet: Option<PathBuf>,
#[structopt(
long = "identity-provider-index",
help = "Identity provider index.",
requires = "hd-wallet"
)]
identity_provider_index: Option<u32>,
#[structopt(
long = "identity-index",
help = "Identity index.",
requires = "hd-wallet"
)]
identity_index: Option<u32>,
}
#[derive(StructOpt)]
struct CreateIdUseData {
#[structopt(long = "out")]
out: Option<PathBuf>,
#[structopt(
long = "hd-wallet",
help = "File with hd wallet.",
requires = "identity-index"
)]
hd_wallet: Option<PathBuf>,
#[structopt(
long = "identity-provider-index",
help = "Identity provider index.",
requires = "hd-wallet"
)]
identity_provider_index: Option<u32>,
#[structopt(
long = "identity-index",
help = "Identity index.",
requires = "hd-wallet"
)]
identity_index: Option<u32>,
}
#[derive(StructOpt)]
struct StartIp {
#[structopt(long = "chi", help = "File with input credential holder information.")]
chi: PathBuf,
#[structopt(long = "ips", help = "File with a list of identity providers.", default_value = IDENTITY_PROVIDERS)]
identity_providers: PathBuf,
#[structopt(
long = "ip",
help = "Which identity provider to choose. If not given an interactive choice will be \
provided."
)]
identity_provider: Option<u32>,
#[structopt(
long = "ars",
help = "File with a list of anonymity revokers..",
default_value = "database/anonymity_revokers.json"
)]
anonymity_revokers: PathBuf,
#[structopt(long = "private", help = "File to write the private ACI data to.")]
private: Option<PathBuf>,
#[structopt(
long = "public",
help = "File to write the public data to be sent to the identity provider."
)]
public: Option<PathBuf>,
#[structopt(
long = "global",
help = "File with global parameters.",
default_value = "database/global.json"
)]
global: PathBuf,
#[structopt(
name = "ar-threshold",
long = "ar-threshold",
help = "Anonymity revocation threshold.",
requires = "selected-ars"
)]
threshold: Option<u8>,
#[structopt(
long = "selected-ars",
help = "Indices of selected ars. If none are provided an interactive choice will be given.",
requires = "ar-threshold"
)]
selected_ars: Vec<u32>,
}
#[derive(StructOpt)]
struct StartIpV1 {
#[structopt(
long = "id-use-data",
help = "File with input credential holder information and blinding randomness."
)]
id_use_data: PathBuf,
#[structopt(long = "ips", help = "File with a list of identity providers.", default_value = IDENTITY_PROVIDERS)]
identity_providers: PathBuf,
#[structopt(
long = "ip",
help = "Which identity provider to choose. If not given an interactive choice will be \
provided."
)]
identity_provider: Option<u32>,
#[structopt(
long = "ars",
help = "File with a list of anonymity revokers..",
default_value = "database/anonymity_revokers.json"
)]
anonymity_revokers: PathBuf,
#[structopt(
long = "public",
help = "File to write the public data to be sent to the identity provider."
)]
public: Option<PathBuf>,
#[structopt(
long = "global",
help = "File with global parameters.",
default_value = "database/global.json"
)]
global: PathBuf,
#[structopt(
name = "ar-threshold",
long = "ar-threshold",
help = "Anonymity revocation threshold.",
requires = "selected-ars"
)]
threshold: Option<u8>,
#[structopt(
long = "selected-ars",
help = "Indices of selected ars. If none are provided an interactive choice will be given.",
requires = "ar-threshold"
)]
selected_ars: Vec<u32>,
}
#[derive(StructOpt)]
struct GenerateIps {
#[structopt(
long = "num",
help = "Number of identity providers to generate.",
default_value = "5",
env = "NUM_IPS"
)]
num: usize,
#[structopt(
long = "num-ars",
help = "Number of anonymity revokers to generate.",
default_value = "5",
env = "NUM_ARS"
)]
num_ars: u32,
#[structopt(
long = "global",
help = "File with global parameters.",
default_value = "database/global.json",
env = "GLOBAL_FILE"
)]
global: PathBuf,
#[structopt(
long = "key-capacity",
help = "Size of the identity provider key. The length of this key limits the number of \
attributes the identity provider can sign.",
default_value = "30",
env = "KEY_CAPACITY"
)]
key_capacity: usize,
#[structopt(
long = "out-dir",
help = "Directory to write the generate identity providers to.",
default_value = "database",
env = "OUT_DIR"
)]
output_dir: PathBuf,
}
#[derive(StructOpt)]
struct GenerateGlobal {
#[structopt(
long = "out-file",
help = "File to write the generated global parameters to.",
default_value = "database/global.json",
env = "OUT_FILE"
)]
output_file: PathBuf,
#[structopt(
long = "string",
help = "Genesis string to add to the global context.",
default_value = "genesis_string",
env = "GENESIS_STRING"
)]
genesis_string: String,
#[structopt(
long = "seed",
help = "Seed file to use when generating group generators.",
env = "SEED_FILE"
)]
seed_file: Option<PathBuf>,
}
#[derive(StructOpt)]
struct IpSignPio {
#[structopt(
long = "pio",
help = "File with input pre-identity object information."
)]
pio: PathBuf,
#[structopt(
long = "ip-data",
help = "File with all information about the identity provider (public and private)."
)]
ip_data: PathBuf,
#[structopt(long = "out", help = "File to write the signed identity object to.")]
out_file: Option<PathBuf>,
#[structopt(
long = "bin-out",
help = "File to output the binary transaction payload to (regarding the initial account)."
)]
bin_out: Option<PathBuf>,
#[structopt(
long = "initial-cdi-out",
help = "File to output the JSON transaction payload to (regarding the initial account)."
)]
out_icdi: Option<PathBuf>,
#[structopt(
long = "global",
help = "File with global parameters.",
default_value = "database/global.json"
)]
global: PathBuf,
#[structopt(
long = "ars",
help = "File with a list of anonymity revokers..",
default_value = "database/anonymity_revokers.json"
)]
anonymity_revokers: PathBuf,
#[structopt(
long = "expiry",
help = "Expiry time of the initial credential message. In seconds from __now__.",
required = true
)]
expiry: u64,
#[structopt(
long = "id-object-expiry",
help = "Expiry time of the identity object message. As YYYYMM."
)]
id_expiry: Option<YearMonth>,
#[structopt(
long = "no-attributes",
help = "Do not select any attributes to reveal."
)]
no_attributes: bool,
}
#[derive(StructOpt)]
struct IpSignPioV1 {
#[structopt(
long = "pio",
help = "File with input pre-identity object information."
)]
pio: PathBuf,
#[structopt(
long = "ip-data",
help = "File with all information about the identity provider (public and private)."
)]
ip_data: PathBuf,
#[structopt(long = "out", help = "File to write the signed identity object to.")]
out_file: Option<PathBuf>,
#[structopt(
long = "global",
help = "File with global parameters.",
default_value = "database/global.json"
)]
global: PathBuf,
#[structopt(
long = "ars",
help = "File with a list of anonymity revokers..",
default_value = "database/anonymity_revokers.json"
)]
anonymity_revokers: PathBuf,
#[structopt(
long = "id-object-expiry",
help = "Expiry time of the identity object message. As YYYYMM."
)]
id_expiry: Option<YearMonth>,
#[structopt(
long = "no-attributes",
help = "Do not select any attributes to reveal."
)]
no_attributes: bool,
}
#[derive(StructOpt)]
struct CreateCredential {
#[structopt(long = "hd-wallet", help = "File with hd wallet.")]
hd_wallet: Option<PathBuf>,
#[structopt(
long = "identity-index",
help = "The index of the identity to create the credential from.",
requires = "hd-wallet"
)]
identity_index: Option<u32>,
#[structopt(
long = "id-object",
help = "File with the JSON encoded identity object."
)]
id_object: PathBuf,
#[structopt(
long = "global",
help = "File with global parameters.",
default_value = "database/global.json"
)]
global: PathBuf,
#[structopt(
long = "ip-info",
help = "File with the JSON encoded information about the identity provider."
)]
ip_info: PathBuf,
#[structopt(
long = "private",
help = "File with private credential holder information used to generate the identity \
object.",
required_unless = "hd-wallet",
conflicts_with = "hd-wallet"
)]
private: Option<PathBuf>,
#[structopt(
long = "account",
help = "Account address onto which the credential should be deployed.",
requires = "key-index"
)]
account: Option<AccountAddress>,
#[structopt(
long = "expiry",
help = "Expiry time of the credential message. In seconds from __now__.",
required_unless = "account",
conflicts_with = "account"
)]
expiry: Option<u64>,
#[structopt(
name = "key-index",
long = "key-index",
help = "Credential index of the new credential.",
requires = "account",
conflicts_with = "expiry"
)]
key_index: Option<u8>,
#[structopt(long = "out", help = "File to output the JSON transaction payload to.")]
out: Option<PathBuf>,
#[structopt(
long = "keys-out",
help = "File to output account keys.",
default_value = "account_keys.json"
)]
keys_out: PathBuf,
#[structopt(
long = "ars",
help = "File with a list of anonymity revokers.",
default_value = "database/anonymity_revokers.json"
)]
anonymity_revokers: PathBuf,
#[structopt(
long = "index",
help = "Index of the account/credential to be created."
)]
index: Option<u8>,
}
#[derive(StructOpt)]
struct VerifyCredential {
#[structopt(
long = "credential",
help = "File with the JSON encoded credential object."
)]
credential: PathBuf,
#[structopt(
long = "global",
help = "File with global parameters.",
default_value = "database/global.json"
)]
global: PathBuf,
#[structopt(
long = "ip-info",
help = "File with the JSON encoded information about the identity provider."
)]
ip_info: PathBuf,
#[structopt(
long = "ars",
help = "File with a list of anonymity revokers.",
default_value = "database/anonymity_revokers.json"
)]
anonymity_revokers: PathBuf,
#[structopt(
long = "expiry",
help = "Expiry time of the credential message. NB: This is seconds since the unix epoch.",
required_unless = "account",
conflicts_with = "account"
)]
expiry: Option<TransactionTime>,
#[structopt(
long = "account",
help = "Address of the account onto which the credential will be deployed."
)]
account: Option<AccountAddress>,
}
#[derive(StructOpt)]
struct ExtendIpList {
#[structopt(
long = "ips-meta-file",
help = "File with identity providers with metadata.",
default_value = "identity-providers-with-metadata.json"
)]
ips_with_metadata: PathBuf,
#[structopt(
long = "ip",
help = "File with public information about the new identity provider"
)]
ip: PathBuf,
#[structopt(
long = "metadata",
help = "File with metadata that should be included with the identity provider."
)]
metadata: PathBuf,
#[structopt(
long = "ars",
help = "File with a list of all known anonymity revokers.",
default_value = "database/anonymity_revokers.json"
)]
anonymity_revokers: PathBuf,
#[structopt(
long = "selected-ars",
help = "List of identifiers for anonymity revokers that should be included with the \
identity provider."
)]
selected_ars: Vec<u32>,
}
#[derive(StructOpt)]
/// Construct a genesis account from a multitude of files. In the main genesis
/// process the credentials will be created by the desktop wallet, and baker
/// keys by another tool. These all need to be combined into a single account.
struct MakeAccount {
#[structopt(
long = "credential",
help = "List of credentials that make the account. The order is significant. Indices will \
be assigned according to it."
)]
credentials: Vec<PathBuf>,
#[structopt(long = "amount", help = "Balance of the account. Specified in GTU.")]
balance: Amount,
#[structopt(name = "threshold", long = "threshold", help = "Account threshold.")]
threshold: u8,
#[structopt(
name = "baker-keys",
long = "baker-keys",
help = "If the account is a baker, its baker keys and baker id.",
requires_all = &["stake"]
)]
baker_keys: Option<PathBuf>,
#[structopt(
name = "stake",
long = "stake",
help = "If a baker, its initial stake in GTU.",
requires_all = &["baker-keys"]
)]
stake: Option<Amount>,
#[structopt(
name = "no-restake",
long = "no-restake",
help = "If a baker, do not restake earnings automatically.",
requires_all = &["baker-keys", "stake"]
)]
no_restake: bool,
#[structopt(long = "out", help = "The file to output the account data into.")]
out: PathBuf,
}
// This is the type of credentials that is output by the desktop wallet for
// genesis creation.
#[derive(SerdeDeserialize)]
#[serde(rename_all = "camelCase")]
struct GenesisCredentialInput {
credential: AccountCredentialWithoutProofs<ArCurve, ExampleAttribute>,
generated_address: AccountAddress,
}
#[derive(StructOpt)]
#[structopt(
about = "Prototype client showcasing ID layer interactions.",
author = "Concordium",
version = "2.1.0"
)]
enum IdClient {
#[structopt(
name = "create-hd-wallet",
about = "Create hd-wallet from a list of 24 BIP39 words."
)]
CreateHdWallet(CreateHdWallet),
#[structopt(
name = "create-chi",
about = "Create new credential holder information."
)]
CreateChi(CreateChi),
#[structopt(
name = "create-id-use-data",
about = "Create new id use data, either deterministically from a hd wallet, or using \
system randomness."
)]
CreateIdUseData(CreateIdUseData),
#[structopt(
name = "start-ip",
about = "Generate data to send to the identity provider to sign and verify."
)]
StartIp(StartIp),
#[structopt(
name = "start-ip-v1",
about = "Generate data to send to the identity provider to sign and verify."
)]
StartIpV1(StartIpV1),
#[structopt(
name = "generate-ips",
about = "Generate given number of identity providers and anonymity revokers. With public \
and private keys."
)]
GenerateIps(GenerateIps),
#[structopt(name = "generate-global")]
GenerateGlobal(GenerateGlobal),
#[structopt(
name = "ip-sign-pio",
about = "Act as the identity provider, checking and signing a version 0 pre-identity \
object."
)]
IpSignPio(IpSignPio),
#[structopt(
name = "ip-sign-pio-v1",
about = "Act as the identity provider, checking and signing a version 1 pre-identity \
object."
)]
IpSignPioV1(IpSignPioV1),
#[structopt(
name = "create-credential",
about = "Take the identity object, select attributes to reveal and create a credential \
object to deploy on chain."
)]
CreateCredential(CreateCredential),
#[structopt(name = "verify-credential", about = "Verify the given credential.")]
VerifyCredential(VerifyCredential),
#[structopt(
name = "extend-ip-list",
about = "Extend the list of identity providers as served by the wallet-proxy."
)]
ExtendIpList(ExtendIpList),
#[structopt(
name = "create-genesis-account",
about = "Create a genesis account from credentials and possibly baker information."
)]
MakeAccount(MakeAccount),
#[structopt(name = "recover-identity", about = "Generate id recovery request.")]
GenerateIdRecoveryRequest(GenerateIdRecoveryRequest),
#[structopt(
name = "validate-recovery-request",
about = "Validate id recovery request."
)]
ValidateIdRecoveryRequest(ValidateIdRecoveryRequest),
}
fn main() {
let app = IdClient::clap()
.setting(AppSettings::ArgRequiredElseHelp)
.global_setting(AppSettings::ColoredHelp);
let matches = app.get_matches();
let client = IdClient::from_clap(&matches);
use IdClient::*;
match client {
CreateChi(chi) => handle_create_chi(chi),
CreateHdWallet(chw) => handle_create_hd_wallet(chw),
CreateIdUseData(iud) => handle_create_id_use_data(iud),
StartIp(ip) => handle_start_ip(ip),
StartIpV1(ip) => handle_start_ip_v1(ip),
GenerateIps(ips) => handle_generate_ips(ips),
GenerateGlobal(gl) => handle_generate_global(gl),
IpSignPio(isp) => handle_act_as_ip(isp),
IpSignPioV1(isp) => handle_act_as_ip_v1(isp),
CreateCredential(cc) => handle_create_credential(cc),
ExtendIpList(eil) => handle_extend_ip_list(eil),
VerifyCredential(vcred) => handle_verify_credential(vcred),
MakeAccount(macc) => handle_make_account(macc),
GenerateIdRecoveryRequest(girr) => handle_recovery(girr),
ValidateIdRecoveryRequest(vir) => handle_validate_recovery(vir),
}
}
/// Construct an account out of multiple credentials and possibly baker keys.
/// This is used to construct the accounts that must go into the genesis block
/// from individual credentials.
fn handle_make_account(macc: MakeAccount) {
if macc.credentials.is_empty() {
eprintln!("No credentials specified. Terminating.");
return;
}
let mut credentials: Vec<GenesisCredentialInput> = Vec::with_capacity(macc.credentials.len());
for cred_file in macc.credentials.iter() {
match read_json_from_file(cred_file) {
Ok(c) => credentials.push(c),
Err(e) => eprintln!("Could not parse credential: {}. Terminating.", e),
}
}
let addr = credentials[0].generated_address;
// the credentials will be given indices 0..
let versioned_credentials = Versioned::new(
VERSION_0,
(0..)
.zip(credentials.into_iter().map(|x| x.credential))
.collect::<BTreeMap<u8, _>>(),
);
// if the baker keys are specified the account will be a baker, so we construct
// the baker structure suitable for inclusion in genesis.
let out = match macc.baker_keys {
Some(keys_file) => {
let mut keys = match read_json_from_file(keys_file) {
Ok(serde_json::Value::Object(mp)) => mp,
Ok(_) => {
eprintln!(
"The baker key file does not have the correct format. Expected an object."
);
return;
}
Err(e) => {
eprintln!("Could not read baker keys: {}", e);
return;
}
};
keys.insert("stake".to_string(), json!(macc.stake.unwrap())); // unwrap is safe because of `required_all` directive
keys.insert("restakeEarnings".to_string(), json!(!macc.no_restake));
json!({
"address": addr,
"balance": macc.balance,
"accountThreshold": macc.threshold,
"credentials": versioned_credentials,
"baker": keys,
})
} // and if no baker keys are given we simply combine the credentials.
None => json!({
"address": addr,
"balance": macc.balance,
"accountThreshold": macc.threshold,
"credentials": versioned_credentials,
}),
};
if let Err(e) = write_json_to_file(&macc.out, &out) {
eprintln!("Could not output credentials: {}", e);
}
}
fn handle_verify_credential(vcred: VerifyCredential) {
let ip_info = match read_ip_info(vcred.ip_info) {
Ok(v) => v,
Err(err) => {
eprintln!("Could not read identity provider info because {}", err);
return;
}
};
// we also read the global context from another json file (called
// global.context). We need commitment keys and other data in there.
let global_ctx = {
if let Some(gc) = read_global_context(vcred.global) {
gc
} else {
eprintln!("Cannot read global context information database. Terminating.");
return;
}
};
let all_ars_infos = match read_anonymity_revokers(vcred.anonymity_revokers) {
Ok(v) => v,
Err(x) => {
eprintln!("Could not decode anonymity revokers file due to {}", x);
return;
}
};
let credential = match read_credential(vcred.credential) {
Ok(v) => v,
Err(e) => {
eprintln!("Error reading credential: {}", e);
return;
}
};
let new_or_existing = match (vcred.expiry, vcred.account) {
(None, None) => panic!("One of (expiry, address) is required."),
(None, Some(addr)) => Right(addr),
(Some(tt), None) => Left(tt),
(Some(_), Some(_)) => panic!("Exactly one of (expiry, address) is required."),
};
if let Err(e) = id::chain::verify_cdi(
&global_ctx,
&ip_info,
&all_ars_infos.anonymity_revokers,
&credential,
&new_or_existing,
) {
eprintln!("Credential verification failed due to {}", e)
} else {
eprintln!("Credential verifies.")
}
}
#[derive(SerdeSerialize, SerdeDeserialize)]
struct IpsWithMetadata {
#[serde(rename = "metadata")]
metadata: IpMetadata,
#[serde(rename = "ipInfo")]
ip_info: IpInfo<Bls12>,
#[serde(rename = "arsInfos")]
ars_infos: BTreeMap<ArIdentity, ArInfo<G1>>,
}
fn handle_extend_ip_list(eil: ExtendIpList) {
let mut existing_db = {
if eil.ips_with_metadata.exists() {
match read_json_from_file::<_, Vec<IpsWithMetadata>>(eil.ips_with_metadata.clone()) {
Ok(v) => v,
Err(x) => {
eprintln!("Could not decode file because {}", x);
return;
}
}
} else {
Vec::new()
}
};
let metadata = match read_json_from_file(eil.metadata) {
Ok(v) => v,
Err(x) => {
eprintln!("Could not decode metadata file because {}", x);
return;
}
};
let ip_info = match read_identity_provider(eil.ip) {
Ok(v) => v,
Err(x) => {
eprintln!("Could not decode identity provider because {}", x);
return;
}
};
let all_ars_infos = match read_anonymity_revokers(eil.anonymity_revokers) {
Ok(v) => v,
Err(x) => {
eprintln!("Could not decode anonymity revokers file because {}", x);
return;
}
};
let mut selected_ars = BTreeMap::new();
for ar_id in eil.selected_ars {
match ArIdentity::try_from(ar_id) {
Err(err) => {
eprintln!("{} is not a valid ArIdentity: {}", ar_id, err);
return;
}
Ok(ar_id) => {
if let Some(ar) = all_ars_infos.anonymity_revokers.get(&ar_id) {
let _ = selected_ars.insert(ar_id, ar.clone());
} else {
eprintln!("Selected AR {} not found.", ar_id);
return;
}
}
}
}
existing_db.push(IpsWithMetadata {
ip_info,
metadata,
ars_infos: selected_ars,
});
if let Err(err) = write_json_to_file(eil.ips_with_metadata, &existing_db) {
eprintln!("Could not write output due to {}", err);
} else {
println!("Done.")
}
}
enum SomeIdentityObject<
P: Pairing,
C: Curve<Scalar = P::ScalarField>,
AttributeType: Attribute<C::Scalar>,
> {
IdoV0(IdentityObject<P, C, AttributeType>),
IdoV1(IdentityObjectV1<P, C, AttributeType>),
}
impl<P: Pairing, C: Curve<Scalar = P::ScalarField>, AttributeType: Attribute<C::Scalar>>
HasIdentityObjectFields<P, C, AttributeType> for SomeIdentityObject<P, C, AttributeType>
{
fn get_common_pio_fields(&self) -> CommonPioFields<P, C> {
match self {
SomeIdentityObject::IdoV0(ido) => ido.get_common_pio_fields(),
SomeIdentityObject::IdoV1(ido) => ido.get_common_pio_fields(),
}
}
fn get_attribute_list(&self) -> &AttributeList<C::Scalar, AttributeType> {
match self {
SomeIdentityObject::IdoV0(ido) => ido.get_attribute_list(),
SomeIdentityObject::IdoV1(ido) => ido.get_attribute_list(),
}
}
fn get_signature(&self) -> &ps_sig::Signature<P> {
match self {
SomeIdentityObject::IdoV0(ido) => ido.get_signature(),
SomeIdentityObject::IdoV1(ido) => ido.get_signature(),
}
}
}
/// Read the identity object, select attributes to reveal and create a
/// transaction.
fn handle_create_credential(cc: CreateCredential) {
let id_object = {
match read_id_object(cc.id_object.clone()) {
Ok(v) => SomeIdentityObject::IdoV0(v),
Err(_) => match read_id_object_v1(cc.id_object) {
Ok(v) => SomeIdentityObject::IdoV1(v),
Err(x) => {
eprintln!("Could not read identity object because {}", x);
return;
}
},
}
};
let ip_info = match read_ip_info(cc.ip_info) {
Ok(v) => v,
Err(err) => {
eprintln!("Could not read identity provider info because {}", err);
return;
}
};
// we also read the global context from another json file (called
// global.context). We need commitment keys and other data in there.
let global_ctx = {
if let Some(gc) = read_global_context(cc.global) {
gc
} else {
eprintln!("Cannot read global context information database. Terminating.");
return;
}
};
// now we have all the data ready.
// we first ask the user to select which attributes they wish to reveal
let alist = &id_object.get_attribute_list().alist;