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
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
use anyhow::{anyhow, bail, ensure, Context, Error as AError, Result as AResult};
use async_trait::async_trait;
use core::fmt::Debug;
use json_patch::Patch;
use reqwest::Client;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use ssi_core::one_or_many::OneOrMany;
use ssi_dids::did_resolve::{
    DIDResolver, DocumentMetadata, HTTPDIDResolver, ResolutionInputMetadata, ResolutionMetadata,
    ERROR_INVALID_DID,
};
use ssi_dids::{
    DIDCreate, DIDDeactivate, DIDDocumentOperation, DIDMethod, DIDMethodError,
    DIDMethodTransaction, DIDRecover, DIDUpdate, Document, Service, ServiceEndpoint,
    VerificationRelationship,
};
use ssi_jwk::{Algorithm, Base64urlUInt, JWK};
use ssi_jws::Header;
use std::convert::TryFrom;
use std::fmt;
use std::marker::PhantomData;
use std::str::FromStr;
use thiserror::Error as ThisError;

const MULTIHASH_SHA2_256_PREFIX: &[u8] = &[0x12];
const MULTIHASH_SHA2_256_SIZE: &[u8] = &[0x20];

/// Verification method type for Create operation
///
/// This is used when converting JWK to [verification method map][vmm] for the Create operation.
///
/// Reference: [Sidetree §12.1.1 `add-public-keys`][apk] Step 3.2
///
/// [apk]: https://identity.foundation/sidetree/spec/v1.0.0/#add-public-keys
/// [vmm]: https://www.w3.org/TR/did-core/#verification-methods
pub const VERIFICATION_METHOD_TYPE: &str = "JsonWebSignature2020";

/// An error having to do with [Sidetree].
#[derive(ThisError, Debug)]
pub enum SidetreeError {
    /// Some functionality was not implemented.
    #[error("Not implemented: {0}")]
    NotImplemented(&'static str),
    /// Error from [serde_jcs::to_string]
    #[error("Unable to execute JSON Canonicalization Scheme (JCS)")]
    JCS(#[from] serde_json::Error),
    /// A create operation following another operation is not valid.
    #[error("Create operation cannot follow another operation")]
    CreateCannotFollow,
    /// Update commitment is missing
    #[error("Missing update commitment")]
    MissingUpdateCommitment,
    /// Recovery commitment is missing
    #[error("Missing recovery commitment")]
    MissingRecoveryCommitment,
    /// DID Suffix did not match expected value.
    #[error("DID Suffix mismatch. Expected: '{expected}', but found '{actual}'")]
    DIDSuffixMismatch {
        expected: DIDSuffix,
        actual: DIDSuffix,
    },
    /// Some error occurred.
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

/// Parameters for a Sidetree client implementation
///
/// This trait consistest of the subset of parameters defined in [Sidetree §5. Default Parameters][default-params] that are needed to implemented a Sidetree client, that is a client to the [Sidetree REST API][sidetree-rest].
///
/// [default-params]: https://identity.foundation/sidetree/spec/v1.0.0/#default-parameters
/// [sidetree-rest]: https://identity.foundation/sidetree/api/
pub trait Sidetree {
    /// [`HASH_PROTOCOL`](https://identity.foundation/sidetree/spec/v1.0.0/#hash-protocol)
    ///
    /// This should be implemented using [hash_algorithm].
    ///
    /// Default implementation calls [hash_protocol_algorithm] and returns the concatenation of the
    /// prefix and hash.
    ///
    /// This function must correspond with [hash_algorithm]. To ensure that correspondence,
    /// implementers may want to override [hash_protocol_algorithm] instead of this function.
    ///
    /// [hash_algorithm]: Self::hash_algorithm
    /// [hash_protocol_algorithm]: Self::hash_protocol_algorithm
    fn hash_protocol(data: &[u8]) -> Vec<u8> {
        let (prefix, hash) = Self::hash_protocol_algorithm(data);
        [prefix, hash].concat()
    }

    /// [`HASH_ALGORITHM`](https://identity.foundation/sidetree/spec/v1.0.0/#hash-algorithm)
    ///
    /// Default implementation calls [hash_protocol_algorithm] and returns the hash, discarding the
    /// prefix.
    ///
    /// This function must correspond with [hash_protocol]. To ensure that correspondence,
    /// implementers may want to override [hash_protocol_algorithm] instead of this function.
    ///
    /// [hash_protocol]: Self::hash_protocol
    /// [hash_protocol_algorithm]: Self::hash_protocol_algorithm
    fn hash_algorithm(data: &[u8]) -> Vec<u8> {
        let (_prefix, hash) = Self::hash_protocol_algorithm(data);
        hash
    }

    /// Combination of [hash_protocol] and [hash_algorithm]
    ///
    /// Returns multihash prefix and hash.
    ///
    /// Default implementation: SHA-256 (`sha2-256`)
    ///
    /// [hash_protocol] and [hash_algorithm] must correspond, and their default implementations
    /// call this function ([hash_protocol_algorithm]). Implementers are therefore encouraged to
    /// overwrite this function ([hash_protocol_algorithm]) rather than those ([hash_protocol] and
    /// [hash_algorithm]).
    ///
    /// [hash_protocol]: Self::hash_protocol
    /// [hash_algorithm]: Self::hash_algorithm
    /// [hash_protocol_algorithm]: Self::hash_protocol_algorithm
    fn hash_protocol_algorithm(data: &[u8]) -> (Vec<u8>, Vec<u8>) {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(data);
        let hash = hasher.finalize().to_vec();
        (
            [MULTIHASH_SHA2_256_PREFIX, MULTIHASH_SHA2_256_SIZE].concat(),
            hash,
        )
    }

    /// [`DATA_ENCODING_SCHEME`](https://identity.foundation/sidetree/spec/v1.0.0/#data-encoding-scheme)
    fn data_encoding_scheme(data: &[u8]) -> String {
        base64::encode_config(data, base64::URL_SAFE_NO_PAD)
    }

    /// [`JSON_CANONICALIZATION_SCHEME`](https://identity.foundation/sidetree/spec/v1.0.0/#json-canonicalization-scheme)
    fn json_canonicalization_scheme<T: Serialize + ?Sized>(
        value: &T,
    ) -> Result<String, SidetreeError> {
        serde_jcs::to_string(value).map_err(SidetreeError::JCS)
    }

    /// Generate a new keypair ([KEY_ALGORITHM][ka])
    ///
    /// [ka]: https://identity.foundation/sidetree/spec/v1.0.0/#key-algorithm
    fn generate_key() -> Result<JWK, SidetreeError>;

    /// Ensure that a keypair is valid for this Sidetree DID Method
    ///
    /// Check that the key uses this Sidetree DID method's [KEY_ALGORITHM][ka].
    ///
    /// [ka]: https://identity.foundation/sidetree/spec/v1.0.0/#key-algorithm
    fn validate_key(key: &JWK) -> Result<(), SidetreeError>;

    /// [`SIGNATURE_ALGORITHM`](https://identity.foundation/sidetree/spec/v1.0.0/#sig-algorithm) (JWS alg)
    const SIGNATURE_ALGORITHM: Algorithm;

    /// [`REVEAL_VALUE`](https://identity.foundation/sidetree/spec/v1.0.0/#reveal-value)
    fn reveal_value(commitment_value: &[u8]) -> String {
        // The spec implies that REVEAL_VALUE uses HASH_PROTOCOL, in §6.2.1:
        //   "Use the implementation’s HASH_PROTOCOL to hash the canonicalized public key to generate the REVEAL_VALUE"
        //   https://identity.foundation/sidetree/spec/v1.0.0/#public-key-commitment-scheme
        let hash = Self::hash_protocol(commitment_value);
        Self::data_encoding_scheme(&hash)
    }

    /// [`MAX_OPERATION_HASH_LENGTH`](https://identity.foundation/sidetree/spec/v1.0.0/#max-operation-hash-length)
    const MAX_OPERATION_HASH_LENGTH: usize = 100;

    /// [`NONCE_SIZE`](https://identity.foundation/sidetree/spec/v1.0.0/#nonce-size)
    const NONCE_SIZE: usize = 16;

    /// Method name for Sidetree-based DID
    ///
    /// Mentioned in [Sidetree §9. DID URI Composition](https://identity.foundation/sidetree/spec/v1.0.0/#did-uri-composition)
    const METHOD: &'static str;

    /// Network instance
    ///
    /// Additional segment after the method-id (METHOD), as a prefix for the method-specific-id
    /// (DID Suffix), identifiying a network instance. e.g. "testnet"
    ///
    /// Mentioned in [Note 1](https://identity.foundation/sidetree/spec/v1.0.0/#note-1)
    const NETWORK: Option<&'static str> = None;

    /// Maximum length of `controller` property
    ///
    /// Reference: [Sidetree §12.1.1 `add-public-keys`](https://identity.foundation/sidetree/spec/v1.0.0/#add-public-keys)
    const MAX_CONTROLLER_LENGTH: Option<usize> = None;

    /// Maximum length of `publicKeyMultibase` property
    ///
    /// Reference: [Sidetree §12.1.1 `add-public-keys`](https://identity.foundation/sidetree/spec/v1.0.0/#add-public-keys)
    const MAX_PKMB_LENGTH: Option<usize> = None;

    /// Hash and encode data
    ///
    /// [Sidetree §6.1 Hashing Process](https://identity.foundation/sidetree/spec/#hashing-process)
    fn hash(data: &[u8]) -> String {
        let hash = Self::hash_protocol(data);
        /*
        ensure!(
            hash.len() <= Self::MAX_OPERATION_HASH_LENGTH,
            "Hash is too long"
        );
        */
        Self::data_encoding_scheme(&hash)
    }

    /// [Public Key Commitment Scheme (Sidetree §6.2.1)][pkcs]
    ///
    /// [pkcs]: https://identity.foundation/sidetree/spec/v1.0.0/#public-key-commitment-scheme
    fn commitment_scheme(pkjwk: &PublicKeyJwk) -> AResult<String> {
        let canonicalized_public_key =
            Self::json_canonicalization_scheme(&pkjwk).context("Canonicalize JWK")?;
        // Note: hash_algorithm called here instead of reveal_value, since the underlying hash is
        // used, not the encoded/prefixed one.
        let reveal_value = Self::hash_algorithm(canonicalized_public_key.as_bytes());
        let commitment = Self::hash(&reveal_value);
        Ok(commitment)
    }

    /// Create a Sidetree-based DID using existing keys
    ///
    /// This function creates a Sidetree-based DID using existing public keys for
    /// the update key and recovery key and respective
    /// [commitments][].
    ///
    /// Sidetree specifies in ([§11.1 Create][create]) that creating a Sidetree DID involves
    /// generating a Update keypair and Recovery keypair. That is implemented in [Self::create].
    ///
    /// **Note**: The Sidetree specification ([§6.2.1 Public Key Commitment
    /// Scheme][pkcs]) recommends not reusing public keys across different commitment invocations, and
    /// requires not using public key JWK payloads across commitment invocations.
    ///
    /// [commitments]: https://identity.foundation/sidetree/spec/v1.0.0/#commitment
    /// [create]: https://identity.foundation/sidetree/spec/v1.0.0/#create
    /// [pkcs]: https://identity.foundation/sidetree/spec/v1.0.0/#public-key-commitment-scheme
    fn create_existing(
        update_pk: &PublicKeyJwk,
        recovery_pk: &PublicKeyJwk,
        patches: Vec<DIDStatePatch>,
    ) -> AResult<Operation> {
        ensure!(
            update_pk != recovery_pk,
            "Update and recovery public key JWK payload must be different."
        );

        let update_commitment =
            Self::commitment_scheme(update_pk).context("Generate update commitment")?;

        let create_operation_delta_object = Delta {
            patches,
            update_commitment,
        };
        let delta_string = Self::json_canonicalization_scheme(&create_operation_delta_object)
            .context("Canonicalize Create Operation Delta Object")?;
        let delta_hash = Self::hash(delta_string.as_bytes());

        let recovery_commitment =
            Self::commitment_scheme(recovery_pk).context("Generate recovery commitment")?;

        let create_operation_suffix_data_object = SuffixData {
            r#type: None,
            delta_hash,
            recovery_commitment,
            anchor_origin: None,
        };

        let create_operation = CreateOperation {
            suffix_data: create_operation_suffix_data_object,
            delta: create_operation_delta_object,
        };
        Ok(Operation::Create(create_operation))
    }

    /// Create a Sidetree-based DID
    ///
    /// Generate keypairs and construct a Create Operation according to [Sidetree §11.1
    /// Create][create]. Returns the private keys and the create operation.
    ///
    /// [create]: https://identity.foundation/sidetree/spec/v1.0.0/#create
    fn create(patches: Vec<DIDStatePatch>) -> AResult<(Operation, JWK, JWK)> {
        let update_keypair = Self::generate_key().context("generate update key pair")?;
        let recovery_keypair = Self::generate_key().context("Generate Recovery Key Pair")?;
        let update_pk =
            PublicKeyJwk::try_from(update_keypair.to_public()).context("Update public key")?;
        let recovery_pk =
            PublicKeyJwk::try_from(recovery_keypair.to_public()).context("Recovery public key")?;
        let create_op = Self::create_existing(&update_pk, &recovery_pk, patches)?;
        Ok((create_op, update_keypair, recovery_keypair))
    }

    /// Create a Sidetree-based DID
    ///
    /// Construct a DID Update Operation according to [Sidetree §11.2
    /// Update][update]. Returns the update operation.
    ///
    /// Unlike [Self::create] and [Self::recover], this does not generate keys, since the specification does not
    /// call for that here. Instead, the caller must generate a new update keypair, and pass
    /// its public key in the `new_update_pk` argument.
    ///
    /// Using a `update_key` with a [JWK Nonce][jwkn] is not yet supported.
    ///
    /// [update]: https://identity.foundation/sidetree/spec/v1.0.0/#update
    /// [jwkn]: https://identity.foundation/sidetree/spec/#jwk-nonce
    fn update(
        did_suffix: DIDSuffix,
        update_key: &JWK,
        new_update_pk: &PublicKeyJwk,
        patches: Vec<DIDStatePatch>,
    ) -> AResult<UpdateOperation> {
        let update_pk = PublicKeyJwk::try_from(update_key.to_public())
            .context("Convert update key to PublicKeyJwk for Update operation")?;
        let canonicalized_update_pk = Self::json_canonicalization_scheme(&update_pk)
            .context("Canonicalize update public key for reveal value for Deactivate operation")?;
        let update_reveal_value = Self::reveal_value(canonicalized_update_pk.as_bytes());

        ensure!(
            new_update_pk != &update_pk,
            "New update public key must be different."
        );

        let new_update_commitment =
            Self::commitment_scheme(new_update_pk).context("Generate new update commitment")?;

        let update_operation_delta_object = Delta {
            patches,
            update_commitment: new_update_commitment,
        };

        let delta_string = Self::json_canonicalization_scheme(&update_operation_delta_object)
            .context("Canonicalize Update Operation Delta Object")?;
        let delta_hash = Self::hash(delta_string.as_bytes());

        let algorithm = Self::SIGNATURE_ALGORITHM;
        let claims = UpdateClaims {
            update_key: update_pk,
            delta_hash,
        };
        let signed_data = ssi_jwt::encode_sign(algorithm, &claims, update_key)
            .context("Sign Update Operation")?;
        let update_op = UpdateOperation {
            did_suffix,
            reveal_value: update_reveal_value,
            delta: update_operation_delta_object,
            signed_data,
        };
        Ok(update_op)
    }

    /// Recover a Sidetree-based DID using existing keys
    ///
    /// Like [Self::recover] but does not generate or handle the new update key pair and recovery
    /// key pair; instead, their public keys must be provided by the caller in the `new_update_pk`
    /// and `new_recovery_pk` arguments.
    ///
    /// Returns the constructed DID Recover operation.
    fn recover_existing(
        did_suffix: DIDSuffix,
        recovery_key: &JWK,
        new_update_pk: &PublicKeyJwk,
        new_recovery_pk: &PublicKeyJwk,
        patches: Vec<DIDStatePatch>,
    ) -> AResult<Operation> {
        let recovery_pk = PublicKeyJwk::try_from(recovery_key.to_public())
            .context("Convert recovery key to PublicKeyJwk for Recover operation")?;
        ensure!(
            new_recovery_pk != &recovery_pk,
            "New recovery public key must be different."
        );
        let canonicalized_recovery_pk = Self::json_canonicalization_scheme(&recovery_pk)
            .context("Canonicalize recovery public key for reveal value for Recover operation")?;
        let recover_reveal_value = Self::reveal_value(canonicalized_recovery_pk.as_bytes());
        let new_update_commitment =
            Self::commitment_scheme(new_update_pk).context("Generate new update commitment")?;
        let new_recovery_commitment =
            Self::commitment_scheme(new_recovery_pk).context("Generate new update commitment")?;

        let recover_operation_delta_object = Delta {
            patches,
            update_commitment: new_update_commitment,
        };

        let delta_string = Self::json_canonicalization_scheme(&recover_operation_delta_object)
            .context("Canonicalize Recover Operation Delta Object")?;
        let delta_hash = Self::hash(delta_string.as_bytes());

        let algorithm = Self::SIGNATURE_ALGORITHM;
        let claims = RecoveryClaims {
            recovery_commitment: new_recovery_commitment,
            recovery_key: recovery_pk,
            delta_hash,
            anchor_origin: None,
        };
        let signed_data = ssi_jwt::encode_sign(algorithm, &claims, recovery_key)
            .context("Sign Recover Operation")?;
        let recover_op = RecoverOperation {
            did_suffix,
            reveal_value: recover_reveal_value,
            delta: recover_operation_delta_object,
            signed_data,
        };
        Ok(Operation::Recover(recover_op))
    }

    /// Recover a Sidetree-based DID
    ///
    /// Generate keypairs and construct a Recover Operation according to [Sidetree §11.3
    /// Recover][recover]. Returns the recover operation.
    ///
    /// [recover]: https://identity.foundation/sidetree/spec/v1.0.0/#recover
    fn recover(
        did_suffix: DIDSuffix,
        recovery_key: &JWK,
        patches: Vec<DIDStatePatch>,
    ) -> AResult<(Operation, JWK, JWK)> {
        let new_update_keypair = Self::generate_key().context("Generate New Update Key Pair")?;
        let new_update_pk = PublicKeyJwk::try_from(new_update_keypair.to_public())
            .context("Convert new update public key")?;

        let new_recovery_keypair =
            Self::generate_key().context("Generate New Recovery Key Pair")?;
        let new_recovery_pk = PublicKeyJwk::try_from(new_recovery_keypair.to_public())
            .context("Convert new recovery public key")?;

        let recover_op = Self::recover_existing(
            did_suffix,
            recovery_key,
            &new_update_pk,
            &new_recovery_pk,
            patches,
        )
        .context("Construct Recover Operation")?;
        Ok((recover_op, new_update_keypair, new_recovery_keypair))
    }

    /// Deactivate a Sidetree-based DID
    ///
    /// Construct a Deactivate Operation according to [Sidetree §11.4
    /// Deactivate][deactivate]. Returns the deactivate operation.
    ///
    /// [deactivate]: https://identity.foundation/sidetree/spec/v1.0.0/#deactivate
    fn deactivate(did_suffix: DIDSuffix, recovery_key: JWK) -> AResult<DeactivateOperation> {
        let recovery_pk = PublicKeyJwk::try_from(recovery_key.to_public())
            .context("Convert recovery key to PublicKeyJwk for Deactivate operation")?;
        let canonicalized_recovery_pk = Self::json_canonicalization_scheme(&recovery_pk).context(
            "Canonicalize recovery public key for reveal value for Deactivate operation",
        )?;
        let recover_reveal_value = Self::reveal_value(canonicalized_recovery_pk.as_bytes());
        let algorithm = Self::SIGNATURE_ALGORITHM;
        let claims = DeactivateClaims {
            did_suffix: did_suffix.clone(),
            recovery_key: recovery_pk,
        };
        let signed_data = ssi_jwt::encode_sign(algorithm, &claims, &recovery_key)
            .context("Sign Deactivate Operation")?;
        let recover_op = DeactivateOperation {
            did_suffix,
            reveal_value: recover_reveal_value,
            signed_data,
        };
        Ok(recover_op)
    }

    /// Serialize and hash [Suffix Data][SuffixData], to generate a [Short-Form Sidetree
    /// DID][SidetreeDID::Short] ([`DIDSuffix`]).
    ///
    /// Reference: <https://identity.foundation/sidetree/spec/v1.0.0/#did-uri-composition>
    fn serialize_suffix_data(suffix_data: &SuffixData) -> AResult<DIDSuffix> {
        let string =
            Self::json_canonicalization_scheme(suffix_data).context("Canonicalize Suffix Data")?;
        let hash = Self::hash(string.as_bytes());
        Ok(DIDSuffix(hash))
    }

    /// Check that a DID Suffix looks valid
    fn validate_did_suffix(suffix: &DIDSuffix) -> AResult<()> {
        let bytes =
            base64::decode_config(&suffix.0, base64::URL_SAFE_NO_PAD).context("Decode Base64")?;
        ensure!(
            bytes.len() == 34,
            "Unexpected length for Sidetree DID Suffix: {}",
            bytes.len()
        );
        ensure!(
            &bytes[0..1] == MULTIHASH_SHA2_256_PREFIX && &bytes[1..2] == MULTIHASH_SHA2_256_SIZE,
            "Expected SHA2-256 prefix for Sidetree DID Suffix"
        );
        Ok(())
    }
}

/// Sidetree DID operation
///
/// ### References
/// - <https://identity.foundation/sidetree/spec/v1.0.0/#did-operations>
/// - <https://identity.foundation/sidetree/spec/v1.0.0/#sidetree-operations>
/// - <https://identity.foundation/sidetree/api/#sidetree-operations>
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
pub enum Operation {
    Create(CreateOperation),
    Update(UpdateOperation),
    Recover(RecoverOperation),
    Deactivate(DeactivateOperation),
}

/// Partially verified DID Create operation
///
/// Converted from [CreateOperation].
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct PartiallyVerifiedCreateOperation {
    did_suffix: DIDSuffix,
    r#type: Option<String>,
    recovery_commitment: String,
    anchor_origin: Option<String>,
    hashed_delta: Delta,
}

/// Partially verified DID Create operation
///
/// Converted from [UpdateOperation].
#[derive(Debug, Clone)]
pub struct PartiallyVerifiedUpdateOperation {
    reveal_value: String,
    signed_delta: Delta,
    signed_update_key: PublicKeyJwk,
}

/// Partially verified DID Recovery operation
///
/// Converted from [RecoverOperation].
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct PartiallyVerifiedRecoverOperation {
    reveal_value: String,
    signed_delta: Delta,
    signed_recovery_commitment: String,
    signed_recovery_key: PublicKeyJwk,
    signed_anchor_origin: Option<String>,
}

/// Partially verified DID Deactivate operation
///
/// Converted from [DeactivateOperation].
#[derive(Debug, Clone)]
pub struct PartiallyVerifiedDeactivateOperation {
    signed_did_suffix: DIDSuffix,
    reveal_value: String,
    signed_recovery_key: PublicKeyJwk,
}

/// Partially verified Sidetree DID operation
///
/// Converted from [Operation].
///
/// Operation verification is described in [Sidetree §10.2.1 Operation Verification][ov].
///
/// [ov]: https://identity.foundation/sidetree/spec/v1.0.0/#operation-verification
#[derive(Debug, Clone)]
pub enum PartiallyVerifiedOperation {
    Create(PartiallyVerifiedCreateOperation),
    Update(PartiallyVerifiedUpdateOperation),
    Recover(PartiallyVerifiedRecoverOperation),
    Deactivate(PartiallyVerifiedDeactivateOperation),
}

/// A Sidetree operation
///
/// See also the enum [Operation] which implements this trait.
pub trait SidetreeOperation {
    /// The result of [partially verifying][Self::partial_verify] the operation.
    type PartiallyVerifiedForm;

    /// Partially verify the operation.
    ///
    /// Operation verification is described in [Sidetree §10.2.1 Operation Verification][ov].
    ///
    /// This function verifies the internal consistency (including signatures and hashes) of the operation,
    /// and returns the integrity-verified data.
    /// Public key commitment values are not checked; that is, the signature is verified, but
    /// whether the public key is the correct reveal value is not checked, since that depends on
    /// what the previous operation was. The DID suffix is also not checked, except for a Create
    /// operation, since it is otherwise in reference to an earlier (Create) opeation.
    ///
    /// [ov]: https://identity.foundation/sidetree/spec/v1.0.0/#operation-verification
    fn partial_verify<S: Sidetree>(self) -> AResult<Self::PartiallyVerifiedForm>;
}

impl SidetreeOperation for Operation {
    type PartiallyVerifiedForm = PartiallyVerifiedOperation;

    fn partial_verify<S: Sidetree>(self) -> AResult<Self::PartiallyVerifiedForm> {
        Ok(match self {
            Operation::Create(op) => PartiallyVerifiedOperation::Create(
                op.partial_verify::<S>()
                    .context("Partial verify Create operation")?,
            ),
            Operation::Update(op) => PartiallyVerifiedOperation::Update(
                op.partial_verify::<S>()
                    .context("Partial verify Update operation")?,
            ),
            Operation::Recover(op) => PartiallyVerifiedOperation::Recover(
                op.partial_verify::<S>()
                    .context("Partial verify Recover operation")?,
            ),
            Operation::Deactivate(op) => PartiallyVerifiedOperation::Deactivate(
                op.partial_verify::<S>()
                    .context("Partial verify Deactivate operation")?,
            ),
        })
    }
}

fn ensure_reveal_commitment<S: Sidetree>(
    recovery_commitment: &str,
    reveal_value: &str,
    pk: &PublicKeyJwk,
) -> AResult<()> {
    let canonicalized_public_key =
        S::json_canonicalization_scheme(&pk).context("Canonicalize JWK")?;
    let commitment_value = canonicalized_public_key.as_bytes();
    let computed_reveal_value = S::reveal_value(commitment_value);
    ensure!(&computed_reveal_value == reveal_value);
    let computed_commitment =
        S::commitment_scheme(pk).context("Unable to compute public key commitment")?;
    ensure!(&computed_commitment == recovery_commitment);
    Ok(())
}

impl PartiallyVerifiedOperation {
    pub fn update_commitment(&self) -> Option<&str> {
        match self {
            PartiallyVerifiedOperation::Create(create) => {
                Some(&create.hashed_delta.update_commitment)
            }
            PartiallyVerifiedOperation::Update(update) => {
                Some(&update.signed_delta.update_commitment)
            }
            PartiallyVerifiedOperation::Recover(recover) => {
                Some(&recover.signed_delta.update_commitment)
            }
            PartiallyVerifiedOperation::Deactivate(_) => None,
        }
    }

    pub fn recovery_commitment(&self) -> Option<&str> {
        match self {
            PartiallyVerifiedOperation::Create(create) => Some(&create.recovery_commitment),
            PartiallyVerifiedOperation::Update(_) => None,
            PartiallyVerifiedOperation::Recover(recover) => {
                Some(&recover.signed_recovery_commitment)
            }
            PartiallyVerifiedOperation::Deactivate(_) => None,
        }
    }

    pub fn follows<S: Sidetree>(
        &self,
        previous: &PartiallyVerifiedOperation,
    ) -> Result<(), SidetreeError> {
        match self {
            PartiallyVerifiedOperation::Create(_) => {
                return Err(SidetreeError::CreateCannotFollow);
            }
            PartiallyVerifiedOperation::Update(update) => {
                let update_commitment = previous
                    .update_commitment()
                    .ok_or(SidetreeError::MissingUpdateCommitment)?;
                ensure_reveal_commitment::<S>(
                    update_commitment,
                    &update.reveal_value,
                    &update.signed_update_key,
                )?;
            }
            PartiallyVerifiedOperation::Recover(recover) => {
                let recovery_commitment = previous
                    .recovery_commitment()
                    .ok_or(SidetreeError::MissingRecoveryCommitment)?;
                ensure_reveal_commitment::<S>(
                    recovery_commitment,
                    &recover.reveal_value,
                    &recover.signed_recovery_key,
                )?;
            }
            PartiallyVerifiedOperation::Deactivate(deactivate) => {
                if let PartiallyVerifiedOperation::Create(create) = previous {
                    return Err(SidetreeError::DIDSuffixMismatch {
                        expected: create.did_suffix.clone(),
                        actual: deactivate.signed_did_suffix.clone(),
                    });
                } else {
                    // Note: Recover operations do not sign over the DID suffix. If the deactivate
                    // operation follows a recover operation rather than a create operation, the
                    // DID Suffix must be verified by the caller.
                }
                let recovery_commitment = previous
                    .recovery_commitment()
                    .ok_or(SidetreeError::MissingRecoveryCommitment)?;
                ensure_reveal_commitment::<S>(
                    recovery_commitment,
                    &deactivate.reveal_value,
                    &deactivate.signed_recovery_key,
                )?;
            }
        }
        Ok(())
    }
}

impl SidetreeOperation for CreateOperation {
    type PartiallyVerifiedForm = PartiallyVerifiedCreateOperation;

    fn partial_verify<S: Sidetree>(self) -> AResult<PartiallyVerifiedCreateOperation> {
        let did = SidetreeDID::<S>::from_create_operation(&self)
            .context("Unable to derive DID from create operation")?;
        let did_suffix = DIDSuffix::from(did);
        let delta_string = S::json_canonicalization_scheme(&self.delta)
            .context("Unable to Canonicalize Update Operation Delta Object")?;
        let delta_hash = S::hash(delta_string.as_bytes());
        ensure!(
            delta_hash == self.suffix_data.delta_hash,
            "Delta hash mismatch"
        );
        Ok(PartiallyVerifiedCreateOperation {
            did_suffix,
            r#type: self.suffix_data.r#type,
            recovery_commitment: self.suffix_data.recovery_commitment,
            anchor_origin: self.suffix_data.anchor_origin,
            hashed_delta: self.delta,
        })
    }
}

impl SidetreeOperation for UpdateOperation {
    type PartiallyVerifiedForm = PartiallyVerifiedUpdateOperation;

    /// Partially verify an [UpdateOperation]
    ///
    /// Specifically, the following is done:
    /// - The operation's [signed data](UpdateOperation::signed_data) is verified against the
    ///   revealed [public key](UpdateClaims::update_key) that it must contain;
    /// - the revealed public key is verified against the operation's
    ///   [reveal value](UpdateOperation::reveal_value); and
    /// - the operation's [delta object](UpdateOperation::delta) is verified against the
    ///   [delta hash](UpdateClaims::update_key) in the signed data payload.
    ///
    /// The [DID Suffix](UpdateOperation::did_suffix) is **not** verified
    /// by this function. The correspondence of the reveal value's hash to the previous update
    /// commitment is not checked either, since that is not known from this function.

    fn partial_verify<S: Sidetree>(self) -> AResult<PartiallyVerifiedUpdateOperation> {
        // Verify JWS against public key in payload.
        // Then check public key against its hash (reveal value).
        let (header, claims) =
            jws_decode_verify_inner(&self.signed_data, |claims: &UpdateClaims| {
                &claims.update_key
            })
            .context("Verify Signed Update Data")?;
        ensure!(
            header.algorithm == S::SIGNATURE_ALGORITHM,
            "Update Operation must use Sidetree's signature algorithm"
        );
        let canonicalized_public_key = S::json_canonicalization_scheme(&claims.update_key)
            .context("Canonicalize Update Key")?;
        let computed_reveal_value = S::reveal_value(canonicalized_public_key.as_bytes());
        ensure!(
            self.reveal_value == computed_reveal_value,
            "Reveal value must match hash of update key. Computed: {}. Found: {}",
            computed_reveal_value,
            self.reveal_value,
        );
        let delta_string = S::json_canonicalization_scheme(&self.delta)
            .context("Canonicalize Update Operation Delta Object")?;
        let delta_hash = S::hash(delta_string.as_bytes());
        ensure!(claims.delta_hash == delta_hash, "Delta hash mismatch");
        // Note: did_suffix is dropped, since it's not signed over.
        Ok(PartiallyVerifiedUpdateOperation {
            reveal_value: self.reveal_value,
            signed_delta: self.delta,
            signed_update_key: claims.update_key,
        })
    }
}

impl SidetreeOperation for RecoverOperation {
    type PartiallyVerifiedForm = PartiallyVerifiedRecoverOperation;

    /// Partially verify a [RecoverOperation]
    fn partial_verify<S: Sidetree>(self) -> AResult<PartiallyVerifiedRecoverOperation> {
        // Verify JWS against public key in payload.
        // Then check public key against its hash (reveal value).
        let (header, claims) =
            jws_decode_verify_inner(&self.signed_data, |claims: &RecoveryClaims| {
                &claims.recovery_key
            })
            .context("Verify Signed Recover Data")?;
        ensure!(
            header.algorithm == S::SIGNATURE_ALGORITHM,
            "Recover Operation must use Sidetree's signature algorithm"
        );
        let canonicalized_public_key = S::json_canonicalization_scheme(&claims.recovery_key)
            .context("Canonicalize Recover Key")?;
        let computed_reveal_value = S::reveal_value(canonicalized_public_key.as_bytes());
        ensure!(
            self.reveal_value == computed_reveal_value,
            "Reveal value must match hash of recovery key. Computed: {}. Found: {}",
            computed_reveal_value,
            self.reveal_value,
        );
        let delta_string = S::json_canonicalization_scheme(&self.delta)
            .context("Canonicalize Recover Operation Delta Object")?;
        let delta_hash = S::hash(delta_string.as_bytes());
        ensure!(claims.delta_hash == delta_hash, "Delta hash mismatch");
        // Note: did_suffix is dropped, since it's not signed over.
        Ok(PartiallyVerifiedRecoverOperation {
            reveal_value: self.reveal_value,
            signed_delta: self.delta,
            signed_recovery_commitment: claims.recovery_commitment,
            signed_recovery_key: claims.recovery_key,
            signed_anchor_origin: claims.anchor_origin,
        })
    }
}

impl SidetreeOperation for DeactivateOperation {
    type PartiallyVerifiedForm = PartiallyVerifiedDeactivateOperation;

    /// Partially verify a [DeactivateOperation]
    fn partial_verify<S: Sidetree>(self) -> AResult<PartiallyVerifiedDeactivateOperation> {
        // Verify JWS against public key in payload.
        // Then check public key against its hash (reveal value).

        let (header, claims) =
            jws_decode_verify_inner(&self.signed_data, |claims: &DeactivateClaims| {
                &claims.recovery_key
            })
            .context("Verify Signed Deactivation Data")?;
        ensure!(
            header.algorithm == S::SIGNATURE_ALGORITHM,
            "Deactivate Operation must use Sidetree's signature algorithm"
        );
        let canonicalized_public_key = S::json_canonicalization_scheme(&claims.recovery_key)
            .context("Canonicalize Recovery Key")?;
        let computed_reveal_value = S::reveal_value(canonicalized_public_key.as_bytes());
        ensure!(
            self.reveal_value == computed_reveal_value,
            "Reveal value must match hash of recovery key. Computed: {}. Found: {}",
            computed_reveal_value,
            self.reveal_value,
        );
        ensure!(self.did_suffix == claims.did_suffix, "DID Suffix mismatch");
        Ok(PartiallyVerifiedDeactivateOperation {
            signed_did_suffix: claims.did_suffix,
            reveal_value: self.reveal_value,
            signed_recovery_key: claims.recovery_key,
        })
    }
}

/// [DID Suffix](https://identity.foundation/sidetree/spec/v1.0.0/#did-suffix)
///
/// Unique identifier string within a Sidetree DID (short or long-form)
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct DIDSuffix(pub String);

impl fmt::Display for DIDSuffix {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)?;
        Ok(())
    }
}

/// A Sidetree-based DID
///
/// Reference: [Sidetree §9. DID URI Composition][duc]
///
/// [duc]: https://identity.foundation/sidetree/spec/v1.0.0/#did-uri-composition
pub enum SidetreeDID<S: Sidetree> {
    /// Short-form Sidetree DID
    ///
    /// Reference: [§9. DID URI Composition](https://identity.foundation/sidetree/spec/v1.0.0/#short-form-did)
    Short { did_suffix: DIDSuffix },

    /// Long-form Sidetree DID
    ///
    /// Reference: [§9.1 Long-Form DID URIs](https://identity.foundation/sidetree/spec/v1.0.0/#long-form-did-uris)
    Long {
        did_suffix: DIDSuffix,
        create_operation_data: String,
        _marker: PhantomData<S>,
    },
}

/// [Create Operation Suffix Data Object][data]
///
/// [data]: https://identity.foundation/sidetree/spec/v1.0.0/#create-suffix-data-object
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SuffixData {
    /// Implementation-defined type property
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<String>,

    /// Delta Hash
    ///
    /// [Hash](Sidetree::hash) of canonicalized [Create Operation Delta Object](Delta).
    pub delta_hash: String,

    /// [Recovery commitment](https://identity.foundation/sidetree/spec/v1.0.0/#recovery-commitment)
    ///
    /// Generated in step 2 of the [Create](https://identity.foundation/sidetree/spec/v1.0.0/#create) process.
    pub recovery_commitment: String,

    /// Anchor Origin
    ///
    /// Implementation-defined identifier for most recent anchor for the DID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub anchor_origin: Option<String>,
    // TODO: extensible by method
}

/// Public key as JWK or Multibase
///
/// Property of a public key / verification method containing public key data,
/// as part of a [PublicKeyEntry][].
///
/// per [Sidetree §12.1.1 `add-public-keys`: Step 4][apk].
///
/// [apk]: https://identity.foundation/sidetree/spec/v1.0.0/#add-public-keys
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub enum PublicKey {
    /// [`publicKeyJwk`](https://www.w3.org/TR/did-core/#dfn-publickeyjwk) as defined in DID Core.
    ///
    /// JSON Web Key (JWK) is specified in [RFC 7517](https://datatracker.ietf.org/doc/html/rfc7517).
    PublicKeyJwk(PublicKeyJwk),

    /// [`publicKeyMultibase`](https://www.w3.org/TR/did-core/#dfn-publickeymultibase) as defined in DID Core.
    ///
    /// Maximum length may be set in [Sidetree::MAX_PKMB_LENGTH].
    PublicKeyMultibase(String),
}

/// Public Key Entry
///
/// Used by the [`add-public-keys`](DIDStatePatch::AddPublicKeys) and
/// [`replace`](DIDStatePatch::Replace) DID state patch actions.
///
/// Specified in [Sidetree §12.1.1 `add-public-keys`][apk].
///
/// [apk]: https://identity.foundation/sidetree/spec/v1.0.0/#add-public-keys
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyEntry {
    /// `id` property
    ///
    /// Maximum length: 50 in Base64url
    pub id: String,

    /// Verification method type
    pub r#type: String,

    /// Verification method controller (DID)
    ///
    /// Maximum length may be set in [Sidetree::MAX_CONTROLLER_LENGTH].
    #[serde(skip_serializing_if = "Option::is_none")]
    pub controller: Option<String>,

    /// `publicKeyJwk` or `publicKeyMultibase` property
    #[serde(flatten)]
    pub public_key: PublicKey,

    /// Verification relationships
    ///
    /// Defined in [DID Core](https://www.w3.org/TR/did-core/#verification-relationships).
    ///
    /// Corresponds to [`proofPurpose`](https://www.w3.org/TR/did-core/#verification-relationships) in VC Data Model.
    pub purposes: Vec<VerificationRelationship>,
}

impl TryFrom<JWK> for PublicKeyEntry {
    type Error = AError;
    fn try_from(jwk: JWK) -> Result<Self, Self::Error> {
        let id = jwk.thumbprint().context("Compute JWK thumbprint")?;
        let pkjwk = PublicKeyJwk::try_from(jwk.to_public()).context("Convert key")?;
        let public_key = PublicKey::PublicKeyJwk(pkjwk);
        Ok(PublicKeyEntry {
            id,
            r#type: VERIFICATION_METHOD_TYPE.to_owned(),
            controller: None,
            public_key,
            purposes: vec![
                VerificationRelationship::AssertionMethod,
                VerificationRelationship::Authentication,
                VerificationRelationship::KeyAgreement,
                VerificationRelationship::CapabilityInvocation,
                VerificationRelationship::CapabilityDelegation,
            ],
        })
    }
}

/// Service Endpoint Entry
///
/// Used by the [`add-services`](DIDStatePatch::AddServices) and
/// [`replace`](DIDStatePatch::Replace) DID state patch actions.
///
/// Specified in [Sidetree §12.1.3 `add-services`][as].
///
/// [as]: https://identity.foundation/sidetree/spec/v1.0.0/#add-services
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ServiceEndpointEntry {
    /// `id` property
    ///
    /// Maximum length: 50 in Base64Url
    pub id: String,

    /// Service type
    ///
    /// Maximum length: 30 in Base64Url
    pub r#type: String,

    /// Service endpoint URL or object
    pub service_endpoint: ServiceEndpoint,
}

/// DID PKI metadata state
///
/// Used by the [`replace`](DIDStatePatch::Replace) DID state patch.
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct DocumentState {
    /// Public key entries

    #[serde(skip_serializing_if = "Option::is_none")]
    pub public_keys: Option<Vec<PublicKeyEntry>>,

    /// Services
    #[serde(skip_serializing_if = "Option::is_none")]
    pub services: Option<Vec<ServiceEndpointEntry>>,
}

/// [DID State Patch][dsp] using a [Sidetree Standard Patch action][spa]
///
/// [dsp]: https://identity.foundation/sidetree/spec/v1.0.0/#did-state-patches
/// [spa]: https://identity.foundation/sidetree/spec/v1.0.0/#standard-patch-actions
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "action")]
#[serde(rename_all = "kebab-case")]
pub enum DIDStatePatch {
    /// [`add-public-keys`][apk] Patch Action
    ///
    /// [apk]: https://identity.foundation/sidetree/spec/v1.0.0/#add-public-keys
    AddPublicKeys {
        /// Keys to add or over overwrite
        #[serde(rename = "publicKeys")]
        public_keys: Vec<PublicKeyEntry>,
    },

    /// [`remove-public-keys`][rpk] Patch Action
    ///
    /// [rpk]: https://identity.foundation/sidetree/spec/v1.0.0/#remove-public-keys
    RemovePublicKeys {
        /// IDs of keys to remove
        ids: Vec<String>,
    },

    /// [`add-services`][as] Patch Action
    ///
    /// [as]: https://identity.foundation/sidetree/spec/v1.0.0/#add-services
    AddServices {
        /// Service entries to add
        services: Vec<ServiceEndpointEntry>,
    },

    /// [`remove-services`][rs] Patch Action
    ///
    /// [rs]: https://identity.foundation/sidetree/spec/v1.0.0/#remove-services
    RemoveServices {
        /// IDs of service endpoints to remove
        ids: Vec<String>,
    },

    /// [`replace`][r] Patch Action
    ///
    /// [r]: https://identity.foundation/sidetree/spec/v1.0.0/#replace
    Replace {
        /// Reset DID state
        document: DocumentState,
    },

    /// [`ietf-json-patch`][ijp] Patch Action
    ///
    /// [ijp]: https://identity.foundation/sidetree/spec/v1.0.0/#ietf-json-patch
    ///
    IetfJsonPatch {
        /// JSON Patches according to [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902).
        patches: Patch,
    },
}

/// Create/Update/Recover Delta Object
///
/// ### References
/// - [Sidetree §11.1 Create - Create Operation Delta Object][codo]
/// - [Sidetree §11.2 Update - Update Operation Delta Object][uodo]
/// - [Sidetree §11.3 Recover - Recover Operation Delta Object][rodo]
///
/// [codo]: https://identity.foundation/sidetree/spec/v1.0.0/#create-delta-object
/// [uodo]: https://identity.foundation/sidetree/spec/v1.0.0/#update-delta-object
/// [rodo]: https://identity.foundation/sidetree/spec/v1.0.0/#recover-delta-object
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Delta {
    /// DID state patches to apply.
    pub patches: Vec<DIDStatePatch>,

    /// Update commitment generated as part of a Sidetree Create or Update operation.
    pub update_commitment: String,
}

/// Sidetree DID Create operation
///
/// ### References
/// - [Sidetree §11.1 Create](https://identity.foundation/sidetree/spec/v1.0.0/#create)
/// - [Sidetree REST API §1.2.1 Create](https://identity.foundation/sidetree/api/#create)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct CreateOperation {
    pub suffix_data: SuffixData,
    pub delta: Delta,
}

/// Sidetree DID Update operation
///
/// ### References
/// - [Sidetree §11.2 Update](https://identity.foundation/sidetree/spec/v1.0.0/#update)
/// - [Sidetree REST API §1.2.2 Update](https://identity.foundation/sidetree/api/#update)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct UpdateOperation {
    pub did_suffix: DIDSuffix,
    /// Output of [Sidetree::reveal_value]
    pub reveal_value: String,
    pub delta: Delta,
    /// Compact JWS (RFC 7515) of [UpdateClaims]
    ///
    /// <https://identity.foundation/sidetree/spec/v1.0.0/#update-signed-data-object>
    pub signed_data: String,
}

/// Sidetree DID Recover operation
///
/// ### References
/// - [Sidetree §11.3 Recover](https://identity.foundation/sidetree/spec/v1.0.0/#recover)
/// - [Sidetree REST API §1.2.3 Recover](https://identity.foundation/sidetree/api/#recover)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct RecoverOperation {
    pub did_suffix: DIDSuffix,
    /// Output of [Sidetree::reveal_value]
    pub reveal_value: String,
    pub delta: Delta,
    /// Compact JWS (RFC 7515) of [RecoveryClaims]
    ///
    /// <https://identity.foundation/sidetree/spec/v1.0.0/#recover-signed-data-object>
    pub signed_data: String,
}

/// Sidetree DID Deactivate operation
///
/// ### References
/// - [Sidetree §11.4 Deactivate](https://identity.foundation/sidetree/spec/v1.0.0/#deactivate)
/// - [Sidetree REST API §1.2.4 Deactivate](https://identity.foundation/sidetree/api/#deactivate)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct DeactivateOperation {
    pub did_suffix: DIDSuffix,
    /// Output of [Sidetree::reveal_value]
    pub reveal_value: String,
    /// Compact JWS (RFC 7515) of [DeactivateClaims]
    ///
    /// <https://identity.foundation/sidetree/spec/v1.0.0/#deactivate-signed-data-object>
    pub signed_data: String,
}

/// Payload object for JWS in [UpdateOperation]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UpdateClaims {
    /// Key matching previous Update Commitment
    pub update_key: PublicKeyJwk,

    /// [Hash](Sidetree::hash) of canonicalized [Update Operation Delta Object](Delta).
    pub delta_hash: String,
}

/// Payload object for JWS in [RecoverOperation]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RecoveryClaims {
    /// [Recovery commitment](https://identity.foundation/sidetree/spec/v1.0.0/#recovery-commitment)
    ///
    /// Generated in step 9 of the [Recover](https://identity.foundation/sidetree/spec/v1.0.0/#recover) process.
    pub recovery_commitment: String,

    /// Key matching previous Recovery Commitment
    pub recovery_key: PublicKeyJwk,

    /// [Hash](Sidetree::hash) of canonicalized [Update Operation Delta Object](Delta).
    pub delta_hash: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub anchor_origin: Option<String>,
}

/// Payload object for JWS in [DeactivateOperation]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct DeactivateClaims {
    pub did_suffix: DIDSuffix,
    /// Key matching previous Recovery Commitment
    pub recovery_key: PublicKeyJwk,
}

/// Public Key JWK (JSON Web Key)
///
/// Wraps [ssi_jwk::JWK], while allowing a `nonce` property, and disallowing private key
/// properties ("d").
///
/// Sidetree may allow a `nonce` property in public key JWKs ([§6.2.2 JWK Nonce][jwkn]).
///
/// [jwkn]: https://identity.foundation/sidetree/spec/#jwk-nonce
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyJwk {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nonce: Option<Base64urlUInt>,
    #[serde(flatten)]
    jwk: Value,
}

/// Error resulting from [converting JWK to PublicKeyJwk][PublicKeyJwk::try_from]
#[derive(ThisError, Debug)]
pub enum PublicKeyJwkFromJWKError {
    /// Unable to convert JWK to [Value]
    #[error("Unable to convert JWK to Value")]
    ToValue(#[from] serde_json::Error),
    /// Public Key JWK must not contain private key parameters (e.g. "d")
    #[error("Public Key JWK must not contain private key parameters")]
    PrivateKeyParameters,
}

/// Error resulting from attempting to convert [PublicKeyJwk] to JWK
#[derive(ThisError, Debug)]
pub enum JWKFromPublicKeyJwkError {
    /// Unable to convert [Value] to JWK
    #[error("Unable to convert Value to JWK")]
    FromValue(#[from] serde_json::Error),
}

impl TryFrom<JWK> for PublicKeyJwk {
    type Error = PublicKeyJwkFromJWKError;
    fn try_from(jwk: JWK) -> Result<Self, Self::Error> {
        let jwk_value = serde_json::to_value(jwk).map_err(PublicKeyJwkFromJWKError::ToValue)?;
        if jwk_value.get("d").is_some() {
            return Err(PublicKeyJwkFromJWKError::PrivateKeyParameters);
        };
        Ok(Self {
            jwk: jwk_value,
            nonce: None,
        })
    }
}

/// Convert [PublicKeyJwk] to [JWK].
///
/// Note: `nonce` property is dropped.
impl TryFrom<PublicKeyJwk> for JWK {
    type Error = JWKFromPublicKeyJwkError;
    fn try_from(pkjwk: PublicKeyJwk) -> Result<Self, Self::Error> {
        let jwk = serde_json::from_value(pkjwk.jwk).map_err(JWKFromPublicKeyJwkError::FromValue)?;
        Ok(jwk)
    }
}

impl<S: Sidetree> FromStr for SidetreeDID<S> {
    type Err = AError;
    fn from_str(did: &str) -> Result<Self, Self::Err> {
        let mut parts = did.split(':');
        ensure!(parts.next() == Some("did"), "Expected DID URI scheme");
        ensure!(parts.next() == Some(S::METHOD), "DID Method mismatch");
        if let Some(network) = S::NETWORK {
            ensure!(parts.next() == Some(network), "Sidetree network mismatch");
        }
        let did_suffix_str = parts
            .next()
            .ok_or_else(|| anyhow!("Missing Sidetree DID Suffix"))?;
        let did_suffix = DIDSuffix(did_suffix_str.to_string());
        S::validate_did_suffix(&did_suffix).context("Validate Sidetree DID Suffix")?;
        let create_operation_data_opt = parts.next();
        ensure!(
            parts.next().is_none(),
            "Unexpected data after Sidetree Long-Form DID"
        );
        Ok(match create_operation_data_opt {
            None => Self::Short { did_suffix },
            Some(data) => Self::Long {
                did_suffix,
                create_operation_data: data.to_string(),
                _marker: PhantomData,
            },
        })
    }
}

impl<S: Sidetree> fmt::Display for SidetreeDID<S> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "did:{}:", S::METHOD)?;
        if let Some(network) = S::NETWORK {
            write!(f, "{}:", network)?;
        }
        match self {
            Self::Short { did_suffix } => f.write_str(&did_suffix.0),
            Self::Long {
                did_suffix,
                create_operation_data,
                _marker,
            } => write!(f, "{}:{}", did_suffix.0, create_operation_data),
        }
    }
}

impl<S: Sidetree> SidetreeDID<S> {
    /// Construct a [Long-Form Sidetree DID][lfdu] from a [Create Operation][CreateOperation]
    ///
    /// [lfdu]: https://identity.foundation/sidetree/spec/v1.0.0/#long-form-did-uris
    pub fn from_create_operation(create_operation: &CreateOperation) -> AResult<Self> {
        let op_json = S::json_canonicalization_scheme(&create_operation)
            .context("Canonicalize Create Operation")?;
        let op_string = S::data_encoding_scheme(op_json.as_bytes());

        let did_suffix = S::serialize_suffix_data(&create_operation.suffix_data)
            .context("Serialize DID Suffix Data")?;
        Ok(Self::Long {
            did_suffix,
            create_operation_data: op_string,
            _marker: PhantomData,
        })
    }
}

/// Convert a DID URL to an object id given a DID
///
/// Object id is an id of a [ServiceEndpointEntry] or [PublicKeyEntry].
fn did_url_to_id<S: Sidetree>(did_url: &str, did: &SidetreeDID<S>) -> AResult<String> {
    let did_string = did.to_string();
    let unprefixed = match did_url.strip_prefix(&did_string) {
        Some(s) => s,
        None => bail!("DID URL did not begin with expected DID"),
    };
    let fragment = match unprefixed.strip_prefix('#') {
        Some(s) => s,
        None => bail!("Expected DID URL with fragment"),
    };
    Ok(fragment.to_string())
}

impl<S: Sidetree> From<SidetreeDID<S>> for DIDSuffix {
    fn from(did: SidetreeDID<S>) -> DIDSuffix {
        match did {
            SidetreeDID::Short { did_suffix } => did_suffix,
            SidetreeDID::Long { did_suffix, .. } => did_suffix,
        }
    }
}

/// DID Resolver using ION/Sidetree REST API
#[derive(Debug, Clone, Default)]
pub struct HTTPSidetreeDIDResolver<S: Sidetree> {
    pub http_did_resolver: HTTPDIDResolver,
    pub _marker: PhantomData<S>,
}

impl<S: Sidetree> HTTPSidetreeDIDResolver<S> {
    pub fn new(sidetree_api_url: &str) -> Self {
        let identifiers_url = format!("{}identifiers/", sidetree_api_url);
        Self {
            http_did_resolver: HTTPDIDResolver::new(&identifiers_url),
            _marker: PhantomData,
        }
    }
}

/// Sidetree DID Method client implementation
#[derive(Clone)]
pub struct SidetreeClient<S: Sidetree> {
    pub resolver: Option<HTTPSidetreeDIDResolver<S>>,
    pub endpoint: Option<String>,
}

impl<S: Sidetree> SidetreeClient<S> {
    pub fn new(api_url_opt: Option<String>) -> Self {
        let resolver_opt = api_url_opt
            .as_ref()
            .map(|url| HTTPSidetreeDIDResolver::new(url));
        Self {
            endpoint: api_url_opt,
            resolver: resolver_opt,
        }
    }
}

/// Check that a JWK is Secp256k1
pub fn is_secp256k1(jwk: &JWK) -> bool {
    matches!(jwk, JWK {params: ssi_jwk::Params::EC(ssi_jwk::ECParams { curve: Some(curve), ..}), ..} if curve == "secp256k1")
}

struct NoOpResolver;

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl DIDResolver for NoOpResolver {
    async fn resolve(
        &self,
        _did: &str,
        _input_metadata: &ResolutionInputMetadata,
    ) -> (
        ResolutionMetadata,
        Option<Document>,
        Option<DocumentMetadata>,
    ) {
        (
            ResolutionMetadata::from_error("Missing Sidetree API endpoint"),
            None,
            None,
        )
    }
}

fn new_did_state<S: Sidetree>(
    update_key: Option<JWK>,
    recovery_key: Option<JWK>,
    verification_key: Option<JWK>,
) -> AResult<(PublicKeyJwk, PublicKeyJwk, Vec<DIDStatePatch>)> {
    let update_key = update_key.ok_or_else(|| anyhow!("Missing required update key"))?;
    S::validate_key(&update_key).context("Validate update key")?;
    let update_pk = PublicKeyJwk::try_from(update_key.to_public()).context("Convert update key")?;
    let recovery_key = recovery_key.ok_or_else(|| anyhow!("Missing required recovery key"))?;
    S::validate_key(&recovery_key).context("Validate recovery key")?;
    let recovery_pk =
        PublicKeyJwk::try_from(recovery_key.to_public()).context("Convert recovery key")?;
    let mut patches = vec![];
    if let Some(verification_key) = verification_key {
        let public_key_entry = PublicKeyEntry::try_from(verification_key)
            .context("Convert JWK to public key entry")?;
        let document = DocumentState {
            public_keys: Some(vec![public_key_entry]),
            services: None,
        };
        let patch = DIDStatePatch::Replace { document };
        patches.push(patch);
    };
    Ok((update_pk, recovery_pk, patches))
}

fn b64len(s: &str) -> usize {
    base64::encode_config(s, base64::URL_SAFE_NO_PAD).len()
}

impl DIDStatePatch {
    /// Convert a [DID Document Operation][ddo] and DID to a Sidetree [DID State Patch][dsp].
    ///
    /// [ddp]: https://identity.foundation/did-registration/#diddocumentoperation
    /// [dsp]: https://identity.foundation/sidetree/spec/v1.0.0/#did-state-patches
    fn try_from_with_did<S: Sidetree>(
        did_doc_op: DIDDocumentOperation,
        did: &SidetreeDID<S>,
    ) -> AResult<Self> {
        Ok(match did_doc_op {
            DIDDocumentOperation::SetDidDocument(_doc) => {
                bail!("setDidDocument not implemented")
            }
            DIDDocumentOperation::AddToDidDocument(_props) => {
                bail!("addToDidDocument not implemented")
            }
            DIDDocumentOperation::RemoveFromDidDocument(_props) => {
                bail!("removeFromDidDocument not implemented")
            }
            DIDDocumentOperation::SetVerificationMethod { vmm, purposes } => {
                let sub_id =
                    did_url_to_id(&vmm.id, did).context("Convert verification method id")?;
                let mut value =
                    serde_json::to_value(vmm).context("Convert verification method map")?;
                value["id"] = Value::String(sub_id);
                value["purposes"] = serde_json::to_value(purposes)
                    .context("Convert verification method purposes")?;
                let entry: PublicKeyEntry = serde_json::from_value(value)
                    .context("Convert verification method to Sidetree public key entry")?;
                // TODO: allow omitted controller property
                DIDStatePatch::AddPublicKeys {
                    public_keys: vec![entry],
                }
            }
            DIDDocumentOperation::SetService(service) => {
                let Service {
                    id,
                    type_,
                    service_endpoint,
                    property_set,
                } = service;
                ensure!(
                    !matches!(property_set, Some(map) if !map.is_empty()),
                    "Unexpected service properties"
                );
                let service_endpoint = match service_endpoint {
                    None => bail!("Missing endpoint for service"),
                    Some(OneOrMany::Many(_)) => bail!("Sidetree service must contain one endpoint"),
                    Some(OneOrMany::One(se)) => se,
                };
                let sub_id = did_url_to_id(&id, did).context("Convert service id")?;
                let service_type = match type_ {
                    OneOrMany::One(type_) => type_,
                    OneOrMany::Many(_) => bail!("Service must contain single type"),
                };
                ensure!(b64len(&service_type) <= 30, "Sidetree service type must contain no more than 30 Base64Url-encoded characters");
                ensure!(
                    b64len(&sub_id) <= 50,
                    "Sidetree service id must contain no more than 50 Base64Url-encoded characters"
                );
                let entry = ServiceEndpointEntry {
                    id: sub_id,
                    r#type: service_type,
                    service_endpoint,
                };
                DIDStatePatch::AddServices {
                    services: vec![entry],
                }
            }
            DIDDocumentOperation::RemoveVerificationMethod(did_url) => {
                let id = did_url.to_string();
                DIDStatePatch::RemovePublicKeys { ids: vec![id] }
            }
            DIDDocumentOperation::RemoveService(did_url) => {
                let id = did_url.to_string();
                DIDStatePatch::RemoveServices { ids: vec![id] }
            }
        })
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct SidetreeAPIError {
    // List of error codes: https://github.com/decentralized-identity/sidetree/blob/v1.0.0/lib/core/versions/1.0/ErrorCode.ts
    pub code: String,
    pub message: Option<String>,
}

impl fmt::Display for SidetreeAPIError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Sidetree error {}", self.code)?;
        if let Some(ref message) = self.message {
            write!(f, ": {}", message)?;
        }
        Ok(())
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<S: Sidetree + Send + Sync> DIDMethod for SidetreeClient<S> {
    fn name(&self) -> &'static str {
        S::METHOD
    }

    fn to_resolver(&self) -> &dyn DIDResolver {
        match self.resolver {
            Some(ref res) => res,
            None => &NoOpResolver,
        }
    }

    fn create(&self, create: DIDCreate) -> Result<DIDMethodTransaction, DIDMethodError> {
        let DIDCreate {
            recovery_key,
            update_key,
            verification_key,
            options,
        } = create;
        if let Some(opt) = options.keys().next() {
            return Err(DIDMethodError::OptionNotSupported {
                operation: "create",
                option: opt.clone(),
            });
        }
        let (update_pk, recovery_pk, patches) =
            new_did_state::<S>(update_key, recovery_key, verification_key)
                .context("Prepare keys for DID creation")?;
        let operation = S::create_existing(&update_pk, &recovery_pk, patches)
            .context("Construct Create operation")?;
        let tx = Self::op_to_transaction(operation).context("Construct create transaction")?;
        Ok(tx)
    }

    /// <https://identity.foundation/sidetree/api/#sidetree-operations>
    async fn submit_transaction(&self, tx: DIDMethodTransaction) -> Result<Value, DIDMethodError> {
        let op = Self::op_from_transaction(tx)
            .context("Convert DID method transaction to Sidetree operation")?;
        let endpoint = self
            .endpoint
            .as_ref()
            .ok_or_else(|| anyhow!("Missing Sidetree REST API endpoint"))?;
        let url = format!("{}operations/", endpoint);
        let client = Client::builder().build().context("Build HTTP client")?;
        let resp = client
            .post(url)
            .json(&op)
            .header("Accept", "application/json")
            .header("User-Agent", crate::USER_AGENT)
            .send()
            .await
            .context("Send HTTP request")?;
        if let Err(e) = resp.error_for_status_ref() {
            let err: SidetreeAPIError = resp
                .json()
                .await
                .context("Transaction submit failed. Unable to read HTTP response JSON")?;
            return Err(anyhow!("Transaction submit failed: {}: {}", e, err).into());
        }
        if resp.content_length() == Some(0) {
            // Update operation may return empty body with 200 OK.
            return Ok(Value::Null);
        }
        let bytes = resp.bytes().await.context("Unable to read HTTP response")?;
        let resp_json: Value = serde_json::from_slice(&bytes).context(format!(
            "Unable to parse result as JSON: {}",
            String::from_utf8(bytes.to_vec()).context("Unable to parse result as UTF-8")?
        ))?;
        Ok(resp_json)
    }

    fn did_from_transaction(&self, tx: DIDMethodTransaction) -> Result<String, DIDMethodError> {
        let op = Self::op_from_transaction(tx)
            .context("Convert DID method transaction to Sidetree operation")?;
        let did = match op {
            Operation::Create(create_op) => SidetreeDID::<S>::from_create_operation(&create_op)
                .context("Derive DID from Create operation")?,
            Operation::Update(update_op) => SidetreeDID::Short {
                did_suffix: update_op.did_suffix,
            },
            Operation::Recover(recover_op) => SidetreeDID::Short {
                did_suffix: recover_op.did_suffix,
            },
            Operation::Deactivate(deactivate_op) => SidetreeDID::Short {
                did_suffix: deactivate_op.did_suffix,
            },
        };
        Ok(did.to_string())
    }

    fn update(&self, update: DIDUpdate) -> Result<DIDMethodTransaction, DIDMethodError> {
        let DIDUpdate {
            did,
            update_key,
            new_update_key,
            operation,
            options,
        } = update;
        let did = SidetreeDID::<S>::from_str(&did).context("Parse Sidetree DID")?;
        if let Some(opt) = options.keys().next() {
            return Err(DIDMethodError::OptionNotSupported {
                operation: "update",
                option: opt.clone(),
            });
        }
        let update_key = update_key.ok_or_else(|| anyhow!("Missing required new update key"))?;
        let new_update_key =
            new_update_key.ok_or_else(|| anyhow!("Missing required new update key"))?;
        S::validate_key(&new_update_key).context("Validate update key")?;
        let new_update_pk =
            PublicKeyJwk::try_from(new_update_key.to_public()).context("Convert new update key")?;
        let patches = vec![DIDStatePatch::try_from_with_did(operation, &did)
            .context("Convert DID document operation to Sidetree patch actions")?];
        let did_suffix = DIDSuffix::from(did);
        let update_operation = S::update(did_suffix, &update_key, &new_update_pk, patches)
            .context("Construct Update operation")?;
        let tx = Self::op_to_transaction(Operation::Update(update_operation))
            .context("Construct update transaction")?;
        Ok(tx)
    }

    fn recover(&self, recover: DIDRecover) -> Result<DIDMethodTransaction, DIDMethodError> {
        let DIDRecover {
            did,
            recovery_key,
            new_recovery_key,
            new_update_key,
            new_verification_key,
            options,
        } = recover;
        let did = SidetreeDID::<S>::from_str(&did).context("Parse Sidetree DID")?;
        let did_suffix = DIDSuffix::from(did);
        if let Some(opt) = options.keys().next() {
            return Err(DIDMethodError::OptionNotSupported {
                operation: "recover",
                option: opt.clone(),
            });
        }
        let recovery_key = recovery_key.ok_or_else(|| anyhow!("Missing required recovery key"))?;
        let (new_update_pk, new_recovery_pk, patches) =
            new_did_state::<S>(new_update_key, new_recovery_key, new_verification_key)
                .context("Prepare keys for DID recovery")?;
        let operation = S::recover_existing(
            did_suffix,
            &recovery_key,
            &new_update_pk,
            &new_recovery_pk,
            patches,
        )
        .context("Construct Recover operation")?;
        let tx = Self::op_to_transaction(operation).context("Construct recover transaction")?;
        Ok(tx)
    }

    fn deactivate(
        &self,
        deactivate: DIDDeactivate,
    ) -> Result<DIDMethodTransaction, DIDMethodError> {
        let DIDDeactivate { did, key, options } = deactivate;
        let did = SidetreeDID::<S>::from_str(&did).context("Parse Sidetree DID")?;
        let recovery_key =
            key.ok_or_else(|| anyhow!("Missing required recovery key for DID deactivation"))?;
        if let Some(opt) = options.keys().next() {
            return Err(DIDMethodError::OptionNotSupported {
                operation: "deactivate",
                option: opt.clone(),
            });
        }
        let did_suffix = DIDSuffix::from(did);
        let deactivate_operation = <S as Sidetree>::deactivate(did_suffix, recovery_key)
            .context("Construct DID Deactivate operation")?;
        let tx = Self::op_to_transaction(Operation::Deactivate(deactivate_operation))
            .context("Construct DID deactivate transaction")?;
        Ok(tx)
    }
}

impl<S: Sidetree> SidetreeClient<S> {
    fn op_to_transaction(op: Operation) -> AResult<DIDMethodTransaction> {
        let value = serde_json::to_value(op).context("Convert operation to value")?;
        Ok(DIDMethodTransaction {
            did_method: S::METHOD.to_string(),
            value: serde_json::json!({ "sidetreeOperation": value }),
        })
    }

    fn op_from_transaction(tx: DIDMethodTransaction) -> AResult<Operation> {
        let mut value = tx.value;
        let op_value = value
            .get_mut("sidetreeOperation")
            .ok_or_else(|| anyhow!("Missing sidetreeOperation property"))?
            .take();
        let op: Operation =
            serde_json::from_value(op_value).context("Convert value to operation")?;
        Ok(op)
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<S: Sidetree + Send + Sync> DIDResolver for HTTPSidetreeDIDResolver<S> {
    async fn resolve(
        &self,
        did: &str,
        input_metadata: &ResolutionInputMetadata,
    ) -> (
        ResolutionMetadata,
        Option<Document>,
        Option<DocumentMetadata>,
    ) {
        let _sidetree_did = match SidetreeDID::<S>::from_str(did) {
            Err(_e) => {
                return (
                    ResolutionMetadata::from_error(ERROR_INVALID_DID),
                    None,
                    None,
                );
            }
            Ok(did) => did,
        };
        self.http_did_resolver.resolve(did, input_metadata).await
    }
}

/// An error resulting from [jws_decode_verify_inner]
#[derive(ThisError, Debug)]
pub enum JWSDecodeVerifyError {
    /// Unable to split JWS
    #[error("Unable to split JWS")]
    SplitJWS(#[source] ssi_jws::Error),
    /// Unable to decode JWS parts
    #[error("Unable to decode JWS parts")]
    DecodeJWSParts(#[source] ssi_jws::Error),
    /// Deserialize JWS payload
    #[error("Deserialize JWS payload")]
    DeserializeJWSPayload(#[source] serde_json::Error),
    /// Unable to convert PublicKeyJwk to JWK
    #[error("Unable to convert PublicKeyJwk to JWK")]
    JWKFromPublicKeyJwk(#[source] JWKFromPublicKeyJwkError),
    /// Unable to verify JWS
    #[error("Unable to verify JWS")]
    VerifyJWS(#[source] ssi_jws::Error),
}

/// Decode and verify JWS with public key inside payload
///
/// Similar to [ssi_jwt::decode_verify] or [ssi_jws::decode_verify], but for when the payload (claims) must be parsed to
/// determine the public key.
///
/// This function decodes and verifies a JWS/JWT, where the public key is expected to be found
/// within the payload (claims). Before verification, the deserialized claims object is passed to
/// the provided `get_key` function. The public key returned from the `get_key` function is then
/// used to verify the signature. The verified claims and header object are returned on successful
/// verification, along with the public key that they were verified against (as returned by the
/// `get_key` function).
///
/// The `get_key` function uses [PublicKeyJwk], for the convenience of this crate, but this
/// function converts it to [ssi_jwk::JWK] internally.
pub fn jws_decode_verify_inner<Claims: DeserializeOwned>(
    jwt: &str,
    get_key: impl FnOnce(&Claims) -> &PublicKeyJwk,
) -> Result<(Header, Claims), JWSDecodeVerifyError> {
    use ssi_jws::{decode_jws_parts, split_jws, verify_bytes, DecodedJWS};
    let (header_b64, payload_enc, signature_b64) =
        split_jws(jwt).map_err(JWSDecodeVerifyError::SplitJWS)?;
    let DecodedJWS {
        header,
        signing_input,
        payload,
        signature,
    } = decode_jws_parts(header_b64, payload_enc.as_bytes(), signature_b64)
        .map_err(JWSDecodeVerifyError::DecodeJWSParts)?;
    let claims: Claims =
        serde_json::from_slice(&payload).map_err(JWSDecodeVerifyError::DeserializeJWSPayload)?;
    let pk = get_key(&claims);
    let pk = JWK::try_from(pk.clone()).map_err(JWSDecodeVerifyError::JWKFromPublicKeyJwk)?;
    verify_bytes(header.algorithm, &signing_input, &pk, &signature)
        .map_err(JWSDecodeVerifyError::VerifyJWS)?;
    Ok((header, claims))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    struct Example;

    impl Sidetree for Example {
        fn generate_key() -> Result<JWK, SidetreeError> {
            let key = JWK::generate_secp256k1().context("Generate secp256k1 key")?;
            Ok(key)
        }
        fn validate_key(key: &JWK) -> Result<(), SidetreeError> {
            if !is_secp256k1(key) {
                return Err(anyhow!("Key must be Secp256k1").into());
            }
            Ok(())
        }
        const SIGNATURE_ALGORITHM: Algorithm = Algorithm::ES256K;
        const METHOD: &'static str = "sidetree";
    }

    /// <https://identity.foundation/sidetree/spec/v1.0.0/#did>
    static LONGFORM_DID: &str = "did:sidetree:EiDyOQbbZAa3aiRzeCkV7LOx3SERjjH93EXoIM3UoN4oWg:eyJkZWx0YSI6eyJwYXRjaGVzIjpbeyJhY3Rpb24iOiJyZXBsYWNlIiwiZG9jdW1lbnQiOnsicHVibGljS2V5cyI6W3siaWQiOiJwdWJsaWNLZXlNb2RlbDFJZCIsInB1YmxpY0tleUp3ayI6eyJjcnYiOiJzZWNwMjU2azEiLCJrdHkiOiJFQyIsIngiOiJ0WFNLQl9ydWJYUzdzQ2pYcXVwVkpFelRjVzNNc2ptRXZxMVlwWG45NlpnIiwieSI6ImRPaWNYcWJqRnhvR0otSzAtR0oxa0hZSnFpY19EX09NdVV3a1E3T2w2bmsifSwicHVycG9zZXMiOlsiYXV0aGVudGljYXRpb24iLCJrZXlBZ3JlZW1lbnQiXSwidHlwZSI6IkVjZHNhU2VjcDI1NmsxVmVyaWZpY2F0aW9uS2V5MjAxOSJ9XSwic2VydmljZXMiOlt7ImlkIjoic2VydmljZTFJZCIsInNlcnZpY2VFbmRwb2ludCI6Imh0dHA6Ly93d3cuc2VydmljZTEuY29tIiwidHlwZSI6InNlcnZpY2UxVHlwZSJ9XX19XSwidXBkYXRlQ29tbWl0bWVudCI6IkVpREtJa3dxTzY5SVBHM3BPbEhrZGI4Nm5ZdDBhTnhTSFp1MnItYmhFem5qZEEifSwic3VmZml4RGF0YSI6eyJkZWx0YUhhc2giOiJFaUNmRFdSbllsY0Q5RUdBM2RfNVoxQUh1LWlZcU1iSjluZmlxZHo1UzhWRGJnIiwicmVjb3ZlcnlDb21taXRtZW50IjoiRWlCZk9aZE10VTZPQnc4UGs4NzlRdFotMkotOUZiYmpTWnlvYUFfYnFENHpoQSJ9fQ";
    static SHORTFORM_DID: &str = "did:sidetree:EiDyOQbbZAa3aiRzeCkV7LOx3SERjjH93EXoIM3UoN4oWg";

    lazy_static::lazy_static! {

        /// <https://identity.foundation/sidetree/spec/v1.0.0/#create-2>
        static ref CREATE_OPERATION: Operation = serde_json::from_value(json!({
          "type": "create",
          "suffixData": {
            "deltaHash": "EiCfDWRnYlcD9EGA3d_5Z1AHu-iYqMbJ9nfiqdz5S8VDbg",
            "recoveryCommitment": "EiBfOZdMtU6OBw8Pk879QtZ-2J-9FbbjSZyoaA_bqD4zhA"
          },
          "delta": {
            "updateCommitment": "EiDKIkwqO69IPG3pOlHkdb86nYt0aNxSHZu2r-bhEznjdA",
            "patches": [
              {
                "action": "replace",
                "document": {
                  "publicKeys": [
                    {
                      "id": "publicKeyModel1Id",
                      "type": "EcdsaSecp256k1VerificationKey2019",
                      "publicKeyJwk": {
                        "kty": "EC",
                        "crv": "secp256k1",
                        "x": "tXSKB_rubXS7sCjXqupVJEzTcW3MsjmEvq1YpXn96Zg",
                        "y": "dOicXqbjFxoGJ-K0-GJ1kHYJqic_D_OMuUwkQ7Ol6nk"
                      },
                      "purposes": [
                        "authentication",
                        "keyAgreement"
                      ]
                    }
                  ],
                  "services": [
                    {
                      "id": "service1Id",
                      "type": "service1Type",
                      "serviceEndpoint": "http://www.service1.com"
                    }
                  ]
                }
              }
            ]
          }
        })).unwrap();

        /// <https://identity.foundation/sidetree/spec/v1.0.0/#update-2>
        static ref UPDATE_OPERATION: Operation = serde_json::from_value(json!({
          "type": "update",
          "didSuffix": "EiDyOQbbZAa3aiRzeCkV7LOx3SERjjH93EXoIM3UoN4oWg",
          "revealValue": "EiBkRSeixqX-PhOij6PIpuGfPld5Nif5MxcrgtGCw-t6LA",
          "delta": {
            "patches": [
              {
                "action": "add-public-keys",
                "publicKeys": [
                  {
                    "id": "additional-key",
                    "type": "EcdsaSecp256k1VerificationKey2019",
                    "publicKeyJwk": {
                      "kty": "EC",
                      "crv": "secp256k1",
                      "x": "aN75CTjy3VCgGAJDNJHbcb55hO8CobEKzgCNrUeOwAY",
                      "y": "K9FhCEpa_jG09pB6qriXrgSvKzXm6xtxBvZzIoXXWm4"
                    },
                    "purposes": [
                      "authentication",
                      "assertionMethod",
                      "capabilityInvocation",
                      "capabilityDelegation",
                      "keyAgreement"
                    ]
                  }
                ]
              }
            ],
            "updateCommitment": "EiDOrcmPtfMHuwIWN6YoihdeIPxOKDHy3D6sdMXu_7CN0w"
          },
          "signedData": "eyJhbGciOiJFUzI1NksifQ.eyJ1cGRhdGVLZXkiOnsia3R5IjoiRUMiLCJjcnYiOiJzZWNwMjU2azEiLCJ4Ijoid2Z3UUNKM09ScVZkbkhYa1Q4UC1MZ19HdHhCRWhYM3R5OU5VbnduSHJtdyIsInkiOiJ1aWU4cUxfVnVBblJEZHVwaFp1eExPNnFUOWtQcDNLUkdFSVJsVHBXcmZVIn0sImRlbHRhSGFzaCI6IkVpQ3BqTjQ3ZjBNcTZ4RE5VS240aFNlZ01FcW9EU19ycFEyOVd5MVY3M1ZEYncifQ.RwZK1DG5zcr4EsrRImzStb0VX5j2ZqApXZnuoAkA3IoRdErUscNG8RuxNZ0FjlJtjMJ0a-kn-_MdtR0wwvWVgg"
        })).unwrap();

        /// <https://identity.foundation/sidetree/spec/v1.0.0/#recover-2>
        static ref RECOVER_OPERATION: Operation = serde_json::from_value(json!({
          "type": "recover",
          "didSuffix": "EiDyOQbbZAa3aiRzeCkV7LOx3SERjjH93EXoIM3UoN4oWg",
          "revealValue": "EiAJ-97Is59is6FKAProwDo870nmwCeP8n5nRRFwPpUZVQ",
          "signedData": "eyJhbGciOiJFUzI1NksifQ.eyJkZWx0YUhhc2giOiJFaUNTem1ZSk0yWGpaWE00a1Q0bGpKcEVGTjVmVkM1QVNWZ3hSekVtMEF2OWp3IiwicmVjb3ZlcnlLZXkiOnsia3R5IjoiRUMiLCJjcnYiOiJzZWNwMjU2azEiLCJ4IjoibklxbFJDeDBleUJTWGNRbnFEcFJlU3Y0enVXaHdDUldzc29jOUxfbmo2QSIsInkiOiJpRzI5Vks2bDJVNXNLQlpVU0plUHZ5RnVzWGdTbEsyZERGbFdhQ004RjdrIn0sInJlY292ZXJ5Q29tbWl0bWVudCI6IkVpQ3NBN1NHTE5lZGE1SW5sb3Fub2tVY0pGejZ2S1Q0SFM1ZGNLcm1ubEpocEEifQ.lxWnrg5jaeCAhYuz1fPhidKw6Z2cScNlEc6SWcs15DtJbrHZFxl5IezGJ3cWdOSS2DlzDl4M1ZF8dDE9kRwFeQ",
          "delta": {
            "patches": [
              {
                "action": "replace",
                "document": {
                  "publicKeys": [
                    {
                      "id": "newKey",
                      "type": "EcdsaSecp256k1VerificationKey2019",
                      "publicKeyJwk": {
                        "kty": "EC",
                        "crv": "secp256k1",
                        "x": "JUWp0pAMGevNLhqq_Qmd48izuLYfO5XWpjSmy5btkjc",
                        "y": "QYaSu1NHYnxR4qfk-RkXb4NQnQf1X3XQCpDYuibvlNc"
                      },
                      "purposes": [
                        "authentication",
                        "assertionMethod",
                        "capabilityInvocation",
                        "capabilityDelegation",
                        "keyAgreement"
                      ]
                    }
                  ],
                  "services": [
                    {
                      "id": "serviceId123",
                      "type": "someType",
                      "serviceEndpoint": "https://www.url.com"
                    }
                  ]
                }
              }
            ],
            "updateCommitment": "EiD6_csybTfxELBoMgkE9O2BTCmhScG_RW_qaZQkIkJ_aQ"
          }
        })).unwrap();

        /// <https://identity.foundation/sidetree/spec/v1.0.0/#deactivate-2>
        static ref DEACTIVATE_OPERATION: Operation = serde_json::from_value(json!({
          "type": "deactivate",
          "didSuffix": "EiDyOQbbZAa3aiRzeCkV7LOx3SERjjH93EXoIM3UoN4oWg",
          "revealValue": "EiB-dib5oumdaDGH47TB17Qg1nHza036bTIGibQOKFUY2A",
          "signedData": "eyJhbGciOiJFUzI1NksifQ.eyJkaWRTdWZmaXgiOiJFaUR5T1FiYlpBYTNhaVJ6ZUNrVjdMT3gzU0VSampIOTNFWG9JTTNVb040b1dnIiwicmVjb3ZlcnlLZXkiOnsia3R5IjoiRUMiLCJjcnYiOiJzZWNwMjU2azEiLCJ4IjoiSk1ucF9KOW5BSGFkTGpJNmJfNVU3M1VwSEZqSEZTVHdtc1ZUUG9FTTVsMCIsInkiOiJ3c1QxLXN0UWJvSldPeEJyUnVINHQwVV9zX1lSQy14WXQyRkFEVUNHR2M4In19.ARTZrvupKdShOFNAJ4EWnsuaONKBgXUiwY5Ct10a9IXIp1uFsg0UyDnZGZtJT2v2bgtmYsQBmT6L9kKaaDcvUQ"
        })).unwrap();
    }

    #[test]
    fn test_did_parse_format() {
        let longform_did = SidetreeDID::<Example>::from_str(LONGFORM_DID).unwrap();
        let shortform_did = SidetreeDID::<Example>::from_str(SHORTFORM_DID).unwrap();
        assert_eq!(longform_did.to_string(), LONGFORM_DID);
        assert_eq!(shortform_did.to_string(), SHORTFORM_DID);
        assert!(LONGFORM_DID.starts_with(SHORTFORM_DID));
    }

    #[test]
    #[cfg(feature = "secp256k1")]
    fn test_longform_did_construction() {
        let create_operation = match &*CREATE_OPERATION {
            Operation::Create(op) => op,
            _ => panic!("Expected Create Operation"),
        };
        let did = SidetreeDID::<Example>::from_create_operation(create_operation).unwrap();
        assert_eq!(did.to_string(), LONGFORM_DID);
    }

    #[test]
    #[cfg(feature = "secp256k1")]
    fn test_update_verify_reveal() {
        let create_pvo = CREATE_OPERATION
            .clone()
            .partial_verify::<Example>()
            .unwrap();
        let update_pvo = UPDATE_OPERATION
            .clone()
            .partial_verify::<Example>()
            .unwrap();
        update_pvo.follows::<Example>(&create_pvo).unwrap();
    }

    #[test]
    #[cfg(feature = "secp256k1")]
    fn test_recover_verify_reveal() {
        let create_pvo = CREATE_OPERATION
            .clone()
            .partial_verify::<Example>()
            .unwrap();
        let recover_pvo = RECOVER_OPERATION
            .clone()
            .partial_verify::<Example>()
            .unwrap();
        recover_pvo.follows::<Example>(&create_pvo).unwrap();
    }

    #[test]
    #[cfg(feature = "secp256k1")]
    fn test_deactivate_verify_reveal() {
        let recover_pvo = RECOVER_OPERATION
            .clone()
            .partial_verify::<Example>()
            .unwrap();
        let deactivate_pvo = DEACTIVATE_OPERATION
            .clone()
            .partial_verify::<Example>()
            .unwrap();
        deactivate_pvo.follows::<Example>(&recover_pvo).unwrap();
    }
}