package octez-shell-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file client_keys.ml

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type error += Unregistered_key_scheme of string

type error += Wrong_key_scheme of (string * string)

type error += Invalid_uri of Uri.t

let () =
  register_error_kind
    `Permanent
    ~id:"cli.unregistered_key_scheme"
    ~title:"Unregistered key scheme"
    ~description:
      "A key has been provided with an unregistered scheme (no corresponding \
       plugin)"
    ~pp:(fun ppf s ->
      Format.fprintf ppf "No matching plugin for key scheme %s" s)
    Data_encoding.(obj1 (req "value" string))
    (function Unregistered_key_scheme s -> Some s | _ -> None)
    (fun s -> Unregistered_key_scheme s) ;
  register_error_kind
    `Permanent
    ~id:"cli.key.invalid_uri"
    ~title:"Invalid key uri"
    ~description:"A key has been provided with an invalid uri."
    ~pp:(fun ppf s -> Format.fprintf ppf "Cannot parse the key uri: %s" s)
    Data_encoding.(obj1 (req "value" string))
    (function Invalid_uri s -> Some (Uri.to_string s) | _ -> None)
    (fun s -> Invalid_uri (Uri.of_string s)) ;
  register_error_kind
    `Permanent
    ~id:"cli.wrong_key_scheme"
    ~title:"Wrong key scheme"
    ~description:
      "A certain scheme type has been requested but another one was found"
    ~pp:(fun ppf (expected, found) ->
      Format.fprintf ppf "Expected a %s scheme found a %s one" expected found)
    Data_encoding.(obj2 (req "expected" string) (req "found" string))
    (function
      | Wrong_key_scheme (expected, found) -> Some (expected, found) | _ -> None)
    (fun (expected, found) -> Wrong_key_scheme (expected, found))

module Logging = struct
  let tag = Tag.def ~doc:"Identity" "pk_alias" Format.pp_print_text
end

let uri_encoding =
  let to_uri s =
    let o = Uri.of_string s in
    match Uri.scheme o with
    | None -> Stdlib.failwith "Key URI needs a scheme"
    | Some _ -> o
  in
  Data_encoding.(conv Uri.to_string to_uri string)

type error += Unexisting_scheme of Uri.t

let () =
  register_error_kind
    `Permanent
    ~id:"cli.unexisting_scheme"
    ~title:"Unexisting scheme"
    ~description:"The requested scheme does not exist"
    ~pp:(fun ppf uri ->
      Format.fprintf
        ppf
        "The uri %a does specify a scheme to use"
        Uri.pp_hum
        uri)
    Data_encoding.(obj1 (req "uri" uri_encoding))
    (function Unexisting_scheme uri -> Some uri | _ -> None)
    (fun uri -> Unexisting_scheme uri)

type pk_uri = Uri.t

module Pk_uri_hashtbl = Hashtbl.Make (struct
  type t = pk_uri

  let equal = Uri.equal

  let hash = Hashtbl.hash
end)

let make_pk_uri (x : Uri.t) : pk_uri tzresult =
  let open Result_syntax in
  match Uri.scheme x with
  | None ->
      tzfail (Exn (Failure "Error while parsing URI: PK_URI needs a scheme"))
  | Some _ -> return x

type sk_uri = Uri.t

module CompareUri = Compare.Make (struct
  type t = Uri.t

  let compare = Uri.compare
end)

let make_sk_uri (x : Uri.t) : sk_uri tzresult =
  let open Result_syntax in
  match Uri.scheme x with
  | None ->
      tzfail (Exn (Failure "Error while parsing URI: SK_URI needs a scheme"))
  | Some _ -> return x

type error += Signature_mismatch of sk_uri

let () =
  register_error_kind
    `Permanent
    ~id:"cli.signature_mismatch"
    ~title:"Signature mismatch"
    ~description:"The signer produced an invalid signature"
    ~pp:(fun ppf sk ->
      Format.fprintf
        ppf
        "The signer for %a produced an invalid signature"
        Uri.pp_hum
        sk)
    Data_encoding.(obj1 (req "locator" uri_encoding))
    (function Signature_mismatch sk -> Some sk | _ -> None)
    (fun sk -> Signature_mismatch sk)

type sapling_uri = Uri.t

let make_sapling_uri (x : Uri.t) : sapling_uri tzresult =
  let open Result_syntax in
  match Uri.scheme x with
  | None -> tzfail (Exn (Failure "SAPLING_URI needs a scheme"))
  | Some _ -> return x

type aggregate_pk_uri = Uri.t

type aggregate_sk_uri = Uri.t

let make_aggregate_pk_uri (x : Uri.t) : aggregate_pk_uri tzresult =
  let open Result_syntax in
  match Uri.scheme x with
  | None ->
      tzfail
        (Exn
           (Failure "Error while parsing URI: AGGREGATE_PK_URI needs a scheme"))
  (* because it's possible to make an aggregate pk uri without having the signer
     in the client we can't check that scheme is linked to a known signer *)
  | Some _ -> return x

let make_aggregate_sk_uri (x : Uri.t) : aggregate_sk_uri tzresult =
  let open Result_syntax in
  match Uri.scheme x with
  | None ->
      tzfail
        (Exn
           (Failure "Error while parsing URI: AGGREGATE_SK_URI needs a scheme"))
  | Some _ -> return x

let pk_uri_parameter () =
  Tezos_clic.parameter (fun _ s -> Lwt.return @@ make_pk_uri (Uri.of_string s))

let pk_uri_param ?name ?desc params =
  let name = Option.value ~default:"uri" name in
  let desc =
    Option.value
      ~default:
        "public key\n\
         Varies from one scheme to the other.\n\
         Use command `list signing schemes` for more information."
      desc
  in
  Tezos_clic.param ~name ~desc (pk_uri_parameter ()) params

let sk_uri_parameter () =
  Tezos_clic.parameter (fun _ s -> Lwt.return (make_sk_uri @@ Uri.of_string s))

let sk_uri_param ?name ?desc params =
  let name = Option.value ~default:"uri" name in
  let desc =
    Option.value
      ~default:
        "secret key\n\
         Varies from one scheme to the other.\n\
         Use command `list signing schemes` for more information."
      desc
  in
  Tezos_clic.param ~name ~desc (sk_uri_parameter ()) params

let aggregate_sk_uri_parameter () =
  Tezos_clic.parameter (fun _ s ->
      make_aggregate_sk_uri @@ Uri.of_string s |> Lwt.return)

let aggregate_sk_uri_param ?name ?desc params =
  let name = Option.value ~default:"uri" name in
  let desc =
    Option.value
      ~default:
        "secret key\n\
         Varies from one scheme to the other.\n\
         Use command `list signing schemes` for more information."
      desc
  in
  Tezos_clic.param ~name ~desc (aggregate_sk_uri_parameter ()) params

type sapling_key = {
  sk : sapling_uri;
  (* zip32 derivation path *)
  path : int32 list;
  (* index of the next address to generate *)
  address_index : Tezos_sapling.Core.Client.Viewing_key.index;
}

module Sapling_key = Client_aliases.Alias (struct
  module S = Tezos_sapling.Core.Client

  let name = "sapling_key"

  type t = sapling_key

  include Compare.Make (struct
    type nonrec t = t

    let compare a b =
      Compare.or_else (CompareUri.compare a.sk b.sk) (fun () ->
          Compare.or_else (Stdlib.compare a.path b.path) (fun () ->
              Tezos_sapling.Core.Client.Viewing_key.compare_index
                a.address_index
                b.address_index))
  end)

  let encoding =
    let open Data_encoding in
    conv
      (fun k -> (k.sk, k.path, k.address_index))
      (fun (sk, path, address_index) -> {sk; path; address_index})
      (obj3
         (req "sk" uri_encoding)
         (req "path" (list int32))
         (req "address_index" S.Viewing_key.index_encoding))

  let of_source s =
    let open Lwt_result_syntax in
    let open Data_encoding in
    match Json.from_string s with
    | Error _ -> failwith "corrupted wallet"
    | Ok s -> return (Json.destruct encoding s)

  let to_source k =
    let open Data_encoding in
    Lwt.return_ok @@ Json.to_string (Json.construct encoding k)
end)

module Aggregate_alias = struct
  module Public_key_hash = struct
    include Client_aliases.Alias (struct
      (* includes t, Compare, encoding, of/to_b58check *)
      include Tezos_crypto.Aggregate_signature.Public_key_hash

      let of_source s = Lwt.return (of_b58check s)

      let to_source p = Lwt_result_syntax.return (to_b58check p)

      let name = "Aggregate_public_key_hash"
    end)
  end

  type pk_uri = Uri.t

  let make_pk_uri (x : Uri.t) : pk_uri tzresult =
    let open Result_syntax in
    match Uri.scheme x with
    | None ->
        tzfail
          (Exn
             (Failure "Error while parsing URI: AGGREGATE_PK_URI needs a scheme"))
    | Some _ -> return x

  module Public_key = Client_aliases.Alias (struct
    let name = "Aggregate_public_key"

    type t = pk_uri * Tezos_crypto.Aggregate_signature.Public_key.t option

    include Compare.Make (struct
      type nonrec t = t

      let compare (apk, aso) (bpk, bso) =
        Compare.or_else (CompareUri.compare apk bpk) (fun () ->
            Option.compare
              Tezos_crypto.Aggregate_signature.Public_key.compare
              aso
              bso)
    end)

    let of_source s =
      let open Lwt_result_syntax in
      let*? pk_uri = make_pk_uri @@ Uri.of_string s in
      return (pk_uri, None)

    let to_source (t, _) = Lwt_result_syntax.return (Uri.to_string t)

    let encoding =
      let open Data_encoding in
      union
        [
          case
            Json_only
            uri_encoding
            ~title:"Locator_only"
            (function uri, None -> Some uri | _, Some _ -> None)
            (fun uri -> (uri, None));
          case
            Json_only
            ~title:"Locator_and_full_key"
            (obj2
               (req "locator" uri_encoding)
               (req "key" Tezos_crypto.Aggregate_signature.Public_key.encoding))
            (function uri, Some key -> Some (uri, key) | _, None -> None)
            (fun (uri, key) -> (uri, Some key));
        ]
  end)

  type sk_uri = Uri.t

  let make_sk_uri (x : Uri.t) : sk_uri tzresult Lwt.t =
    let open Lwt_result_syntax in
    match Uri.scheme x with
    | None ->
        failwith "Error while parsing URI: AGGREGATE_SK_URI needs a scheme"
    | Some _ -> return x

  module Secret_key = Client_aliases.Alias (struct
    let name = "Aggregate_secret_key"

    type t = sk_uri

    include CompareUri

    let encoding = uri_encoding

    let of_source s = make_sk_uri @@ Uri.of_string s

    let to_source t = Lwt_result_syntax.return (Uri.to_string t)
  end)
end

module type COMMON_SIGNER = sig
  val scheme : string

  val title : string

  val description : string

  type pk_uri = private Uri.t

  type sk_uri = private Uri.t

  type public_key_hash

  type public_key

  type secret_key

  type signature

  val neuterize : sk_uri -> pk_uri tzresult Lwt.t

  val import_secret_key :
    io:Client_context.io_wallet ->
    pk_uri ->
    (public_key_hash * public_key option) tzresult Lwt.t

  val public_key : pk_uri -> public_key tzresult Lwt.t

  val public_key_hash :
    pk_uri -> (public_key_hash * public_key option) tzresult Lwt.t
end

module type AGGREGATE_SIGNER = sig
  include
    COMMON_SIGNER
      with type public_key_hash =
        Tezos_crypto.Aggregate_signature.Public_key_hash.t
       and type public_key = Tezos_crypto.Aggregate_signature.Public_key.t
       and type secret_key = Tezos_crypto.Aggregate_signature.Secret_key.t
       and type pk_uri = aggregate_pk_uri
       and type sk_uri = aggregate_sk_uri

  val sign :
    aggregate_sk_uri ->
    Bytes.t ->
    Tezos_crypto.Aggregate_signature.t tzresult Lwt.t
end

module Make_common_type (S : sig
  include Tezos_crypto.Intfs.COMMON_SIGNATURE

  type pk_uri

  type sk_uri
end) =
struct
  type pk_uri = S.pk_uri

  type sk_uri = S.sk_uri

  type public_key_hash = S.Public_key_hash.t

  type public_key = S.Public_key.t

  type secret_key = S.Secret_key.t

  type signature = S.t
end

module Aggregate_type = Make_common_type (struct
  include Tezos_crypto.Aggregate_signature

  type pk_uri = aggregate_pk_uri

  type sk_uri = aggregate_sk_uri
end)

module type Signature_S = sig
  include
    Tezos_crypto.Intfs.SIGNATURE
      with type watermark = Tezos_crypto.Signature.watermark

  val concat : Bytes.t -> t -> Bytes.t

  module Adapter : sig
    val public_key_hash :
      Tezos_crypto.Signature.Public_key_hash.t -> Public_key_hash.t tzresult

    val public_key :
      Tezos_crypto.Signature.Public_key.t -> Public_key.t tzresult

    val signature : Tezos_crypto.Signature.t -> t tzresult
  end
end

module type SIMPLE_SIGNER = sig
  include COMMON_SIGNER with type pk_uri = pk_uri and type sk_uri = sk_uri

  val sign :
    ?watermark:Tezos_crypto.Signature.watermark ->
    sk_uri ->
    Bytes.t ->
    signature tzresult Lwt.t

  val deterministic_nonce : sk_uri -> Bytes.t -> Bytes.t tzresult Lwt.t

  val deterministic_nonce_hash : sk_uri -> Bytes.t -> Bytes.t tzresult Lwt.t

  val supports_deterministic_nonces : sk_uri -> bool tzresult Lwt.t
end

module type S = sig
  type public_key_hash

  type public_key

  type secret_key

  type watermark

  type signature

  module Signature_type : sig
    type nonrec public_key_hash = public_key_hash

    type nonrec public_key = public_key

    type nonrec secret_key = secret_key

    type nonrec signature = signature

    type nonrec pk_uri = pk_uri

    type nonrec sk_uri = sk_uri
  end

  module Public_key_hash : Client_aliases.Alias with type t = public_key_hash

  module Public_key :
    Client_aliases.Alias with type t = pk_uri * public_key option

  module Secret_key : Client_aliases.Alias with type t = sk_uri

  val import_secret_key :
    io:Client_context.io_wallet ->
    pk_uri ->
    (public_key_hash * public_key option) tzresult Lwt.t

  val public_key : pk_uri -> public_key tzresult Lwt.t

  val public_key_hash :
    pk_uri -> (public_key_hash * public_key option) tzresult Lwt.t

  val neuterize : sk_uri -> pk_uri tzresult Lwt.t

  val sign :
    #Client_context.wallet ->
    ?watermark:watermark ->
    sk_uri ->
    Bytes.t ->
    signature tzresult Lwt.t

  val append :
    #Client_context.wallet ->
    ?watermark:watermark ->
    sk_uri ->
    Bytes.t ->
    Bytes.t tzresult Lwt.t

  val check :
    ?watermark:watermark ->
    pk_uri ->
    signature ->
    Bytes.t ->
    bool tzresult Lwt.t

  val deterministic_nonce : sk_uri -> Bytes.t -> Bytes.t tzresult Lwt.t

  val deterministic_nonce_hash : sk_uri -> Bytes.t -> Bytes.t tzresult Lwt.t

  val supports_deterministic_nonces : sk_uri -> bool tzresult Lwt.t

  val register_key :
    #Client_context.wallet ->
    ?force:bool ->
    public_key_hash * pk_uri * sk_uri ->
    ?public_key:public_key ->
    string ->
    unit tzresult Lwt.t

  val register_keys :
    #Client_context.wallet ->
    (string * public_key_hash * public_key * pk_uri * sk_uri) list ->
    unit tzresult Lwt.t

  val list_keys :
    #Client_context.wallet ->
    (string * public_key_hash * public_key option * sk_uri option) list tzresult
    Lwt.t

  val alias_keys :
    #Client_context.wallet ->
    string ->
    (public_key_hash * public_key option * sk_uri option) option tzresult Lwt.t

  val get_key :
    #Client_context.wallet ->
    public_key_hash ->
    (string * public_key * sk_uri) tzresult Lwt.t

  val get_public_key :
    #Client_context.wallet ->
    public_key_hash ->
    (string * public_key) tzresult Lwt.t

  val get_keys :
    #Client_context.wallet ->
    (string * public_key_hash * public_key * sk_uri) list tzresult Lwt.t

  val force_switch : unit -> (bool, 'ctx) Tezos_clic.arg
end

module type SIGNER =
  SIMPLE_SIGNER
    with type public_key_hash = Tezos_crypto.Signature.Public_key_hash.t
     and type public_key = Tezos_crypto.Signature.Public_key.t
     and type secret_key = Tezos_crypto.Signature.Secret_key.t
     and type signature = Tezos_crypto.Signature.t

type signer =
  | Simple of (module SIGNER)
  | Aggregate of (module AGGREGATE_SIGNER)

let signers_table : signer String.Hashtbl.t = String.Hashtbl.create 13

let register_signer signer =
  let module Signer = (val signer : SIGNER) in
  String.Hashtbl.replace signers_table Signer.scheme (Simple signer)

let register_aggregate_signer signer =
  let module Signer = (val signer : AGGREGATE_SIGNER) in
  String.Hashtbl.replace signers_table Signer.scheme (Aggregate signer)

let registered_signers () : (string * signer) list =
  String.Hashtbl.fold (fun k v acc -> (k, v) :: acc) signers_table []

let find_signer_for_key ~scheme : signer tzresult =
  let open Result_syntax in
  match String.Hashtbl.find signers_table scheme with
  | None -> tzfail (Unregistered_key_scheme scheme)
  | Some signer -> return signer

let find_aggregate_signer_for_key ~scheme =
  let open Result_syntax in
  let* signer = find_signer_for_key ~scheme in
  match signer with
  | Simple _signer -> tzfail (Wrong_key_scheme ("aggregate", "standard"))
  | Aggregate signer -> return signer

let with_scheme_aggregate_signer (uri : Uri.t)
    (f : (module AGGREGATE_SIGNER) -> 'a tzresult Lwt.t) : 'a tzresult Lwt.t =
  let open Lwt_result_syntax in
  match Uri.scheme uri with
  | None -> tzfail @@ Unexisting_scheme uri
  | Some scheme ->
      let*? signer = find_aggregate_signer_for_key ~scheme in
      f signer

let register_aggregate_key cctxt ?(force = false)
    (public_key_hash, pk_uri, sk_uri) ?public_key name =
  let open Lwt_result_syntax in
  let* () =
    Aggregate_alias.Public_key.add ~force cctxt name (pk_uri, public_key)
  in
  let* () = Aggregate_alias.Secret_key.add ~force cctxt name sk_uri in
  Aggregate_alias.Public_key_hash.add ~force cctxt name public_key_hash

let aggregate_neuterize (sk_uri : sk_uri) : pk_uri tzresult Lwt.t =
  with_scheme_aggregate_signer sk_uri (fun (module Signer : AGGREGATE_SIGNER) ->
      Signer.neuterize sk_uri)

let aggregate_public_key pk_uri =
  with_scheme_aggregate_signer pk_uri (fun (module Signer : AGGREGATE_SIGNER) ->
      Signer.public_key pk_uri)

(* This function is used to chose between two aliases associated
   to the same key hash; if we know the secret key for one of them
   we take it, otherwise if we know the public key for one of them
   we take it. *)
let join_keys keys1_opt keys2 =
  match (keys1_opt, keys2) with
  | Some (_, Some _, None), (_, None, None) -> keys1_opt
  | Some (_, _, Some _), _ -> keys1_opt
  | _ -> Some keys2

(* For efficiency, this function avoids loading the wallet, except for
   the call to [Public_key.update]. Indeed the arguments [pkhs],
   [pks], [sks] represent the already loaded list of public key
   hashes, public keys, and secret keys. *)
let raw_get_aggregate_key_aux (cctxt : #Client_context.wallet) pkhs pks sks pkh
    =
  let open Lwt_result_syntax in
  let rev_find_all list pkh =
    List.filter_map
      (fun (name, pkh') ->
        if Tezos_crypto.Aggregate_signature.Public_key_hash.equal pkh pkh' then
          Some name
        else None)
      list
  in
  let names = rev_find_all pkhs pkh in
  let* o =
    List.fold_left_es
      (fun keys_opt name ->
        let sk_uri_opt = List.assoc ~equal:String.equal name sks in
        let* pk_opt =
          match List.assoc ~equal:String.equal name pks with
          | None -> return_none
          | Some (_, Some pk) -> return_some pk
          | Some (pk_uri, None) ->
              let* pk = aggregate_public_key pk_uri in
              let* () =
                Aggregate_alias.Public_key.update cctxt name (pk_uri, Some pk)
              in
              return_some pk
        in
        return @@ join_keys keys_opt (name, pk_opt, sk_uri_opt))
      None
      names
  in
  match o with
  | None ->
      failwith
        "no keys for the source contract %a"
        Tezos_crypto.Aggregate_signature.Public_key_hash.pp
        pkh
  | Some keys -> return keys

let raw_get_aggregate_key (cctxt : #Client_context.wallet) pkh =
  let open Lwt_result_syntax in
  let* pkhs = Aggregate_alias.Public_key_hash.load cctxt in
  let* pks = Aggregate_alias.Public_key.load cctxt in
  let* sks = Aggregate_alias.Secret_key.load cctxt in
  raw_get_aggregate_key_aux cctxt pkhs pks sks pkh

let list_aggregate_keys cctxt =
  let open Lwt_result_syntax in
  let* pkhs = Aggregate_alias.Public_key_hash.load cctxt in
  let* pks = Aggregate_alias.Public_key.load cctxt in
  let* sks = Aggregate_alias.Secret_key.load cctxt in
  List.map_es
    (fun (name, pkh) ->
      let*! r = raw_get_aggregate_key_aux cctxt pkhs pks sks pkh in
      match r with
      | Ok (_name, pk, sk_uri) -> return (name, pkh, pk, sk_uri)
      | Error _ -> return (name, pkh, None, None))
    pkhs

let import_aggregate_secret_key ~io pk_uri =
  with_scheme_aggregate_signer pk_uri (fun (module Signer : AGGREGATE_SIGNER) ->
      Signer.import_secret_key ~io pk_uri)

let alias_aggregate_keys cctxt name =
  let open Lwt_result_syntax in
  let* pkh = Aggregate_alias.Public_key_hash.find cctxt name in
  let*! r = raw_get_aggregate_key cctxt pkh in
  match r with
  | Ok (_name, pk, sk_uri) -> return_some (pkh, pk, sk_uri)
  | Error _ -> return_none

let aggregate_sign cctxt sk_uri buf =
  let open Lwt_result_syntax in
  with_scheme_aggregate_signer sk_uri (fun (module Signer : AGGREGATE_SIGNER) ->
      let* signature = Signer.sign sk_uri buf in
      let* pk_uri = Signer.neuterize sk_uri in
      let* pubkey =
        let* o = Aggregate_alias.Secret_key.rev_find cctxt sk_uri in
        match o with
        | None -> aggregate_public_key pk_uri
        | Some name -> (
            let* r = Aggregate_alias.Public_key.find cctxt name in
            match r with
            | _, None ->
                let* pk = aggregate_public_key pk_uri in
                let* () =
                  Aggregate_alias.Public_key.update cctxt name (pk_uri, Some pk)
                in
                return pk
            | _, Some pubkey -> return pubkey)
      in
      let* () =
        fail_unless
          (Tezos_crypto.Aggregate_signature.check pubkey signature buf)
          (Signature_mismatch sk_uri)
      in
      return signature)

module Make (Signature : Signature_S) :
  S
    with type public_key_hash := Signature.Public_key_hash.t
     and type public_key := Signature.Public_key.t
     and type secret_key := Signature.Secret_key.t
     and type watermark := Signature.watermark
     and type signature := Signature.t = struct
  module Public_key_hash = struct
    include Client_aliases.Alias (struct
      (* includes t, Compare, encoding *)
      include Signature.Public_key_hash

      let of_source s = Lwt.return (Signature.Public_key_hash.of_b58check s)

      let to_source p = Lwt.return_ok (Signature.Public_key_hash.to_b58check p)

      let name = "public key hash"
    end)
  end

  module Secret_key = Client_aliases.Alias (struct
    let name = "secret_key"

    type t = sk_uri

    include (CompareUri : Compare.S with type t := t)

    let of_source s = Lwt.return (make_sk_uri @@ Uri.of_string s)

    let to_source t = Lwt.return_ok (Uri.to_string t)

    let encoding = uri_encoding
  end)

  module Public_key = Client_aliases.Alias (struct
    let name = "public_key"

    type t = pk_uri * Signature.Public_key.t option

    include Compare.Make (struct
      type nonrec t = t

      let compare (apk, aso) (bpk, bso) =
        Compare.or_else (CompareUri.compare apk bpk) (fun () ->
            Option.compare Signature.Public_key.compare aso bso)
    end)

    let of_source s =
      let open Lwt_result_syntax in
      let*? pk_uri = make_pk_uri @@ Uri.of_string s in
      let public_key = Signature.Public_key.of_b58check_opt (Uri.path pk_uri) in
      return (pk_uri, public_key)

    let to_source (t, _) = Lwt.return_ok (Uri.to_string t)

    let encoding =
      let open Data_encoding in
      union
        [
          case
            Json_only
            ~title:"Locator_only"
            uri_encoding
            (function uri, None -> Some uri | _, Some _ -> None)
            (fun uri -> (uri, None));
          case
            Json_only
            ~title:"Locator_and_full_key"
            (obj2
               (req "locator" uri_encoding)
               (req "key" Signature.Public_key.encoding))
            (function uri, Some key -> Some (uri, key) | _, None -> None)
            (fun (uri, key) -> (uri, Some key));
        ]
  end)

  module Signature_type = Make_common_type (struct
    include Signature

    type nonrec pk_uri = pk_uri

    type nonrec sk_uri = sk_uri
  end)

  module type V_SIGNER =
    SIMPLE_SIGNER
      with type public_key_hash = Signature.Public_key_hash.t
       and type public_key = Signature.Public_key.t
       and type secret_key = Signature.Secret_key.t
       and type signature = Signature.t

  module Adapt (S : SIGNER) : V_SIGNER = struct
    let scheme = S.scheme

    let title = S.title

    let description = S.description

    type pk_uri = Uri.t

    type sk_uri = Uri.t

    type public_key_hash = Signature.Public_key_hash.t

    type public_key = Signature.Public_key.t

    type secret_key = Signature.Secret_key.t

    type signature = Signature.t

    let neuterize = S.neuterize

    let import_secret_key ~io sk =
      let open Lwt_result_syntax in
      let* pkh, pk = S.import_secret_key ~io sk in
      let*? pkh = Signature.Adapter.public_key_hash pkh in
      let*? pk = Option.map_e Signature.Adapter.public_key pk in
      return (pkh, pk)

    let public_key pk =
      let open Lwt_result_syntax in
      let* pk = S.public_key pk in
      let*? pk = Signature.Adapter.public_key pk in
      return pk

    let public_key_hash pk =
      let open Lwt_result_syntax in
      let* pkh, pk = S.public_key_hash pk in
      let*? pkh = Signature.Adapter.public_key_hash pkh in
      let*? pk = Option.map_e Signature.Adapter.public_key pk in
      return (pkh, pk)

    let sign ?watermark sk msg =
      let open Lwt_result_syntax in
      let* signature = S.sign ?watermark sk msg in
      let*? signature = Signature.Adapter.signature signature in
      return signature

    let deterministic_nonce = S.deterministic_nonce

    let deterministic_nonce_hash = S.deterministic_nonce_hash

    let supports_deterministic_nonces = S.supports_deterministic_nonces
  end

  let adapt_signer (module Signer : SIGNER) =
    let module V_Signer = Adapt (Signer) in
    (module V_Signer : V_SIGNER)

  let with_scheme_signer (uri : Uri.t) (f : signer -> 'a tzresult Lwt.t) :
      'a tzresult Lwt.t =
    let open Lwt_result_syntax in
    match Uri.scheme uri with
    | None -> tzfail @@ Unexisting_scheme uri
    | Some scheme ->
        let*? signer = find_signer_for_key ~scheme in
        f signer

  let find_simple_signer_for_key ~scheme =
    let open Result_syntax in
    let* signer = find_signer_for_key ~scheme in
    match signer with
    | Simple signer -> return (adapt_signer signer)
    | Aggregate _signer -> tzfail (Wrong_key_scheme ("simple", "aggregate"))

  let with_scheme_simple_signer (uri : Uri.t)
      (f : (module V_SIGNER) -> 'a tzresult Lwt.t) : 'a tzresult Lwt.t =
    let open Lwt_result_syntax in
    match Uri.scheme uri with
    | None -> tzfail @@ Unexisting_scheme uri
    | Some scheme ->
        let*? signer = find_simple_signer_for_key ~scheme in
        f signer

  let neuterize (sk_uri : sk_uri) : pk_uri tzresult Lwt.t =
    with_scheme_simple_signer sk_uri (fun (module Signer) ->
        Signer.neuterize sk_uri)

  let public_key pk_uri =
    with_scheme_simple_signer pk_uri (fun (module Signer) ->
        Signer.public_key pk_uri)

  let public_key_hash pk_uri =
    with_scheme_simple_signer pk_uri (fun (module Signer) ->
        Signer.public_key_hash pk_uri)

  let import_secret_key ~io pk_uri =
    with_scheme_simple_signer pk_uri (fun (module Signer) ->
        Signer.import_secret_key ~io pk_uri)

  let sign cctxt ?watermark sk_uri buf =
    let open Lwt_result_syntax in
    with_scheme_simple_signer sk_uri (fun (module Signer) ->
        let* signature = Signer.sign ?watermark sk_uri buf in
        let* pk_uri = Signer.neuterize sk_uri in
        let* pubkey =
          let* o = Secret_key.rev_find cctxt sk_uri in
          match o with
          | None -> public_key pk_uri
          | Some name -> (
              let* r = Public_key.find cctxt name in
              match r with
              | _, None ->
                  let* pk = public_key pk_uri in
                  let* () = Public_key.update cctxt name (pk_uri, Some pk) in
                  return pk
              | _, Some pubkey -> return pubkey)
        in
        let* () =
          fail_unless
            (Signature.check ?watermark pubkey signature buf)
            (Signature_mismatch sk_uri)
        in
        return signature)

  let append cctxt ?watermark loc buf =
    let open Lwt_result_syntax in
    let+ signature = sign cctxt ?watermark loc buf in
    Signature.concat buf signature

  let check ?watermark pk_uri signature buf =
    let open Lwt_result_syntax in
    let* pk = public_key pk_uri in
    return (Signature.check ?watermark pk signature buf)

  let deterministic_nonce sk_uri data =
    with_scheme_simple_signer sk_uri (fun (module Signer) ->
        Signer.deterministic_nonce sk_uri data)

  let deterministic_nonce_hash sk_uri data =
    with_scheme_simple_signer sk_uri (fun (module Signer) ->
        Signer.deterministic_nonce_hash sk_uri data)

  let supports_deterministic_nonces sk_uri =
    let open Lwt_result_syntax in
    with_scheme_signer sk_uri (function
        | Simple (module Signer : SIGNER) ->
            Signer.supports_deterministic_nonces sk_uri
        | Aggregate _ -> return_false)

  let register_key cctxt ?(force = false) (public_key_hash, pk_uri, sk_uri)
      ?public_key name =
    let open Lwt_result_syntax in
    let* () = Public_key.add ~force cctxt name (pk_uri, public_key) in
    let* () = Secret_key.add ~force cctxt name sk_uri in
    Public_key_hash.add ~force cctxt name public_key_hash

  let register_keys cctxt xs =
    let open Lwt_result_syntax in
    let* () =
      Public_key.add_many
        cctxt
        (List.map
           (fun (name, _, pk, pk_uri, _) -> (name, (pk_uri, Some pk)))
           xs)
    in
    let* () =
      Secret_key.add_many
        cctxt
        (List.map (fun (name, _, _, _, sk_uri) -> (name, sk_uri)) xs)
    in
    let* () =
      Public_key_hash.add_many
        cctxt
        (List.map
           (fun (name, public_key_hash, _, _, _) -> (name, public_key_hash))
           xs)
    in
    return_unit

  (* For efficiency, this function avoids loading the wallet, except for
     the call to [Public_key.update]. Indeed the arguments [pkhs],
     [pks], [sks] represent the already loaded list of public key
     hashes, public keys, and secret keys. *)
  let raw_get_key_aux (cctxt : #Client_context.wallet) pkhs pks sks pkh =
    let open Lwt_result_syntax in
    let rev_find_all list pkh =
      List.filter_map
        (fun (name, pkh') ->
          if Signature.Public_key_hash.equal pkh pkh' then Some name else None)
        list
    in
    let*! r =
      let names = rev_find_all pkhs pkh in
      let* o =
        List.fold_left_es
          (fun keys_opt name ->
            let sk_uri_opt = List.assoc ~equal:String.equal name sks in
            let* pk_opt =
              match List.assoc ~equal:String.equal name pks with
              | None -> return_none
              | Some (_, Some pk) -> return_some pk
              | Some (pk_uri, None) ->
                  let* pk = public_key pk_uri in
                  let* () = Public_key.update cctxt name (pk_uri, Some pk) in
                  return_some pk
            in
            return @@ join_keys keys_opt (name, pk_opt, sk_uri_opt))
          None
          names
      in
      match o with
      | None ->
          failwith
            "no keys for the source contract %a"
            Signature.Public_key_hash.pp
            pkh
      | Some keys -> return keys
    in
    match r with
    | (Ok (_, _, None) | Error _) as initial_result -> (
        (* try to lookup for a remote key *)
        let*! r =
          let*? signer = find_simple_signer_for_key ~scheme:"remote" in
          let module Signer = (val signer) in
          let path = Signature.Public_key_hash.to_b58check pkh in
          let uri = Uri.make ~scheme:Signer.scheme ~path () in
          let* pk = Signer.public_key uri in
          return (path, Some pk, Some uri)
        in
        match r with
        | Error _ -> Lwt.return initial_result
        | Ok _ as success -> Lwt.return success)
    | Ok _ as success -> Lwt.return success

  let raw_get_key (cctxt : #Client_context.wallet) pkh =
    let open Lwt_result_syntax in
    let* pkhs = Public_key_hash.load cctxt in
    let* pks = Public_key.load cctxt in
    let* sks = Secret_key.load cctxt in
    raw_get_key_aux cctxt pkhs pks sks pkh

  let get_key cctxt pkh =
    let open Lwt_result_syntax in
    let* r = raw_get_key cctxt pkh in
    match r with
    | pkh, Some pk, Some sk -> return (pkh, pk, sk)
    | _pkh, _pk, None ->
        failwith "Unknown secret key for %a" Signature.Public_key_hash.pp pkh
    | _pkh, None, _sk ->
        failwith "Unknown public key for %a" Signature.Public_key_hash.pp pkh

  let get_public_key cctxt pkh =
    let open Lwt_result_syntax in
    let* r = raw_get_key cctxt pkh in
    match r with
    | pkh, Some pk, _sk -> return (pkh, pk)
    | _pkh, None, _sk ->
        failwith "Unknown public key for %a" Signature.Public_key_hash.pp pkh

  let get_keys (cctxt : #Client_context.wallet) =
    let open Lwt_result_syntax in
    let* sks = Secret_key.load cctxt in
    let* pkhs = Public_key_hash.load cctxt in
    let* pks = Public_key.load cctxt in
    let*! keys =
      List.filter_map_s
        (fun (name, sk_uri) ->
          let*! r =
            match List.assoc ~equal:String.equal name pkhs with
            | Some pkh ->
                let* pk =
                  match List.assoc ~equal:String.equal name pks with
                  | Some (_, Some pk) -> return pk
                  | Some (pk_uri, None) ->
                      let* pk = public_key pk_uri in
                      let* () =
                        Public_key.update cctxt name (pk_uri, Some pk)
                      in
                      return pk
                  | None -> failwith "no public key alias named %s" name
                in
                return (name, pkh, pk, sk_uri)
            | None -> failwith "no public key hash alias named %s" name
          in
          match r with Ok r -> Lwt.return_some r | Error _ -> Lwt.return_none)
        sks
    in
    return keys

  let list_keys cctxt =
    let open Lwt_result_syntax in
    let* pkhs = Public_key_hash.load cctxt in
    let* pks = Public_key.load cctxt in
    let* sks = Secret_key.load cctxt in
    List.map_es
      (fun (name, pkh) ->
        let*! r = raw_get_key_aux cctxt pkhs pks sks pkh in
        match r with
        | Ok (_name, pk, sk_uri) -> return (name, pkh, pk, sk_uri)
        | Error _ -> return (name, pkh, None, None))
      pkhs

  let alias_keys cctxt name =
    let open Lwt_result_syntax in
    let* pkh = Public_key_hash.find cctxt name in
    let*! r = raw_get_key cctxt pkh in
    match r with
    | Ok (_name, pk, sk_uri) -> return_some (pkh, pk, sk_uri)
    | Error _ -> return_none

  let force_switch () =
    Tezos_clic.switch ~long:"force" ~short:'f' ~doc:"overwrite existing keys" ()
end

module V0 = Make (struct
  include Tezos_crypto.Signature.V0

  let generate_key = generate_key ?algo:None

  module Adapter = struct
    let public_key_hash :
        Tezos_crypto.Signature.Public_key_hash.t -> Public_key_hash.t tzresult =
      let open Result_syntax in
      function
      | Bls _ ->
          tzfail (Exn (Failure "BLS public key hash not supported by V0"))
      | Ed25519 k -> return (Ed25519 k : Public_key_hash.t)
      | Secp256k1 k -> return (Secp256k1 k : Public_key_hash.t)
      | P256 k -> return (P256 k : Public_key_hash.t)

    let public_key :
        Tezos_crypto.Signature.Public_key.t -> Public_key.t tzresult =
      let open Result_syntax in
      function
      | Bls _ -> tzfail (Exn (Failure "BLS public key not supported by V0"))
      | Ed25519 k -> return (Ed25519 k : Public_key.t)
      | Secp256k1 k -> return (Secp256k1 k : Public_key.t)
      | P256 k -> return (P256 k : Public_key.t)

    let signature : Tezos_crypto.Signature.t -> t tzresult =
      let open Result_syntax in
      function
      | Bls _ -> tzfail (Exn (Failure "BLS signature not supported by V0"))
      | Ed25519 k -> return (Ed25519 k : t)
      | Secp256k1 k -> return (Secp256k1 k : t)
      | P256 k -> return (P256 k : t)
      | Unknown k -> return (Unknown k : t)
  end
end)

module V1 = Make (struct
  include Tezos_crypto.Signature.V1

  let generate_key = generate_key ?algo:None

  module Adapter = struct
    let identity x = Ok x

    let public_key_hash = identity

    let public_key = identity

    let signature = identity
  end
end)

module V_latest = Make (struct
  include Tezos_crypto.Signature.V_latest

  let generate_key = generate_key ?algo:None

  module Adapter = struct
    let identity x = Ok x

    let public_key_hash = identity

    let public_key = identity

    let signature = identity
  end
end)

include V_latest

module Mnemonic = struct
  let new_random = Bip39.of_entropy (Tezos_crypto.Hacl.Rand.gen 32)

  let to_32_bytes mnemonic =
    let seed_64_to_seed_32 (seed_64 : bytes) : bytes =
      assert (Bytes.length seed_64 = 64) ;
      let first_32 = Bytes.sub seed_64 0 32 in
      let second_32 = Bytes.sub seed_64 32 32 in
      let seed_32 = Bytes.create 32 in
      for i = 0 to 31 do
        Bytes.set
          seed_32
          i
          (Char.chr
             (Char.code (Bytes.get first_32 i)
             lxor Char.code (Bytes.get second_32 i)))
      done ;
      seed_32
    in
    seed_64_to_seed_32 (Bip39.to_seed mnemonic)

  let words_pp = Format.(pp_print_list ~pp_sep:pp_print_space pp_print_string)
end
OCaml

Innovation. Community. Security.