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
//! # DID Peer Method
//!
//! The `did-peer` method is a DID method that is designed to be used for peer-to-peer communication.
//! It is based on did:key which can be used for Verification (V) and Encryption (E) purposes.
//! It also supports services (S) which can be used to define endpoints for communication.
//!
//! Example:
//! ```ignore
//! let peer = DIDPeer;
//! match peer.resolve(DID::new::<str>("did:peer:2.Vabc...").unwrap()).await {
//!    Ok(res) => {
//!        println!("DID DOcument: {:#?}", res.document.into_document()),
//!    },
//!    Err(e) => {
//!      println!("Error: {:?}", e);
//!   }
//! }
//! ```
//!
use base64::prelude::*;
use iref::UriBuf;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use ssi::{
    dids::{
        document::{
            self,
            representation::{self, MediaType},
            service::Endpoint,
            verification_method, DIDVerificationMethod, Service, VerificationRelationships,
        },
        key::VerificationMethodType,
        resolution::{self, Error, Options, Output},
        DIDBuf, DIDKey, DIDMethod, DIDMethodResolver, DIDURLBuf, DIDURLReferenceBuf, Document,
        RelativeDIDURLBuf, DID,
    },
    jwk::Params,
    prelude::*,
    OneOrMany,
};
use std::{collections::BTreeMap, fmt};
use thiserror::Error;
use wasm_bindgen::prelude::*;

#[derive(Error, Debug)]
pub enum DIDPeerError {
    #[error("Unsupported key type")]
    UnsupportedKeyType,
    #[error("Unsupported curve: {0}")]
    UnsupportedCurve(String),
    #[error("Unsupported source")]
    UnsupportedSource,
    #[error("Syntax error on Service definition: {0}")]
    SyntaxErrorServiceDefinition(String),
    #[error("Unsupported Method. Must be method 2")]
    MethodNotSupported,
    #[error("Key Parsing error {0}")]
    KeyParsingError(String),
    #[error("DID Document doesn't contain any verificationMethod items")]
    MissingVerificationMethods,
    #[error("JSON Parsing error: {0}")]
    JsonParsingError(String),
    #[error("Internal error: {0}")]
    InternalError(String),
}

// Converts DIDPeerError to JsValue which is required for propagating errors to WASM
impl From<DIDPeerError> for JsValue {
    fn from(err: DIDPeerError) -> JsValue {
        JsValue::from(err.to_string())
    }
}

pub struct DIDPeer;

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum PeerServiceEndPoint {
    Short(PeerServiceEndPointShort),
    Long(PeerServiceEndPointLong),
}

impl PeerServiceEndPoint {
    pub fn to_short(&self) -> PeerServiceEndPointShort {
        match self {
            PeerServiceEndPoint::Short(short) => short.clone(),
            PeerServiceEndPoint::Long(long) => PeerServiceEndPointShort {
                uri: long.uri.clone(),
                a: long.accept.clone(),
                r: long.routing_keys.clone(),
            },
        }
    }

    pub fn to_long(&self) -> PeerServiceEndPointLong {
        match self {
            PeerServiceEndPoint::Short(short) => PeerServiceEndPointLong::from(short.clone()),
            PeerServiceEndPoint::Long(long) => long.clone(),
        }
    }
}

/// DID serviceEndPoint structure in short format
#[derive(Clone, Serialize, Deserialize)]
pub struct PeerServiceEndPointShort {
    pub uri: String,
    pub a: Vec<String>,
    pub r: Vec<String>,
}

/// DID serviceEndPoint structure in long format
#[derive(Clone, Serialize, Deserialize)]
pub struct PeerServiceEndPointLong {
    pub uri: String,
    pub accept: Vec<String>,
    pub routing_keys: Vec<String>,
}

impl From<PeerServiceEndPointShort> for PeerServiceEndPointLong {
    fn from(service: PeerServiceEndPointShort) -> Self {
        PeerServiceEndPointLong {
            uri: service.uri,
            accept: service.a,
            routing_keys: service.r,
        }
    }
}
/// DID Service structure in abbreviated format
#[derive(Serialize, Deserialize)]
pub struct DIDPeerService {
    #[serde(rename = "t")]
    #[serde(alias = "t")]
    pub _type: String,
    #[serde(rename = "s")]
    #[serde(alias = "s")]
    pub service_end_point: PeerServiceEndPoint, // serviceEndPoint
    pub id: Option<String>,
}

impl From<DIDPeerService> for Service {
    fn from(service: DIDPeerService) -> Self {
        let service_endpoint =
            match serde_json::to_value(PeerServiceEndPoint::to_long(&service.service_end_point)) {
                Ok(value) => Some(OneOrMany::One(Endpoint::Map(value))),
                Err(_) => None,
            };

        let id = if let Some(id) = service.id {
            UriBuf::new(id.into()).unwrap()
        } else {
            UriBuf::new("did:peer:#service".into()).unwrap()
        };

        Service {
            id,
            type_: OneOrMany::One("DIDCommMessaging".into()),
            service_endpoint,
            property_set: BTreeMap::new(),
        }
    }
}

#[derive(Clone)]
#[wasm_bindgen]
/// DID Peer Key Purpose (used to create a new did:peer: string)
///   Verification: Keys are referenced in the authentication and assertionMethod fields
///   Encryption: Keys are referenced in the keyAgreement field
pub enum DIDPeerKeys {
    Verification,
    Encryption,
}

impl fmt::Display for DIDPeerKeys {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DIDPeerKeys::Verification => write!(f, "verification"),
            DIDPeerKeys::Encryption => write!(f, "encryption"),
        }
    }
}

#[derive(Clone)]
#[wasm_bindgen]
/// Supported DID Peer Key Types (used to create a new did:peer: string)
pub enum DIDPeerKeyType {
    Ed25519,
    Secp256k1,
    P256,
}

impl fmt::Display for DIDPeerKeyType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DIDPeerKeyType::Ed25519 => write!(f, "ed25519"),
            DIDPeerKeyType::Secp256k1 => write!(f, "secp256k1"),
            DIDPeerKeyType::P256 => write!(f, "p256"),
        }
    }
}

#[derive(Clone)]
#[wasm_bindgen(getter_with_clone)]
/// Structure to help with creating a new did:peer: string
///    purpose: ENUM (DIDPeerKeys) - Verification or Encryption
///    public_key_multibase: String - Optional: Multibase encoded public key (did:key:(.*))
///                                   If None, then auto-create and return the private key
pub struct DIDPeerCreateKeys {
    pub purpose: DIDPeerKeys,
    pub type_: Option<DIDPeerKeyType>,
    pub public_key_multibase: Option<String>,
}

#[wasm_bindgen]
impl DIDPeerCreateKeys {
    #[wasm_bindgen(constructor)]
    pub fn new(
        purpose: DIDPeerKeys,
        type_: Option<DIDPeerKeyType>,
        public_key_multibase: Option<String>,
    ) -> Self {
        DIDPeerCreateKeys {
            purpose,
            type_,
            public_key_multibase,
        }
    }
}

/// DIDPeerCreatedKeys, contains information related to any keys that were created
///
/// key_multibase: `String`, the multibase_58 encoded key value (e.g. did:key:(.*))
/// curve: `String`, the elliptic curve method used
/// d: `String`, private key value in Base64URL_NOPAD
/// x: `String`, public key value in Base64URL_NOPAD
/// y: `Option<String>`, Optional: Y coordinate for EC keys in Base64URL_NOPAD
#[derive(Clone, Debug, Serialize, Deserialize)]
#[wasm_bindgen(getter_with_clone)]
pub struct DIDPeerCreatedKeys {
    pub key_multibase: String,
    pub curve: String,
    pub d: String,
    pub x: String,
    pub y: Option<String>,
}

/// Converts a public key into a DID VerificationMethod
fn process_key(did: &str, id: &str, public_key: &str) -> Result<DIDVerificationMethod, Error> {
    let mut properties = BTreeMap::new();

    properties.insert(
        "publicKeyMultibase".to_string(),
        Value::String(public_key.to_string()),
    );

    Ok(DIDVerificationMethod {
        id: DIDURLBuf::from_string(["did:peer:", did, id].concat()).unwrap(),
        type_: "Multikey".to_string(),
        controller: DIDBuf::from_string(["did:peer:", did].concat()).unwrap(),
        properties,
    })
}

impl DIDMethodResolver for DIDPeer {
    async fn resolve_method_representation<'a>(
        &'a self,
        method_specific_id: &'a str,
        options: Options,
    ) -> Result<Output<Vec<u8>>, Error> {
        // If did:peer is type 0, then treat it as a did:key
        if let Some(id) = method_specific_id.strip_prefix('0') {
            return DIDKey.resolve_method_representation(id, options).await;
        }

        // Only supports method 2 for did:peer
        if !method_specific_id.starts_with('2') {
            return Err(Error::MethodNotSupported(
                "did:peer version 2 supported only".to_string(),
            ));
        }

        let mut context = BTreeMap::new();
        context.insert("@base".to_string(), serde_json::json!(method_specific_id));

        let mut verification_methods: Vec<DIDVerificationMethod> = Vec::new();
        let mut verification_relationships: VerificationRelationships =
            VerificationRelationships::default();

        //let mut key_agreements: Vec<DIDVerificationMethod> = Vec::new();
        //let mut key_authentications: Vec<DIDVerificationMethod> = Vec::new();
        //let mut key_assertion_methods: Vec<DIDVerificationMethod> = Vec::new();
        let mut services: Vec<Service> = Vec::new();

        // Split the DID for peer on '.'s, we skip the first one
        // did:peer:2.(process from here)
        let parts: Vec<&str> = method_specific_id[2..].split('.').collect();
        let mut key_count: u32 = 1;
        let mut service_count: u32 = 0;

        for part in parts {
            let ch = part.chars().next();
            match ch {
                Some(e) => {
                    match e {
                        'A' => {
                            // Assertion Method
                            verification_methods.push(process_key(
                                method_specific_id,
                                &["#key-", &key_count.to_string()].concat(),
                                &part[1..],
                            )?);

                            verification_relationships.assertion_method.push(
                                verification_method::ValueOrReference::Reference(
                                    DIDURLReferenceBuf::Relative(
                                        RelativeDIDURLBuf::new(
                                            ["#key-", &key_count.to_string()]
                                                .concat()
                                                .as_bytes()
                                                .to_vec(),
                                        )
                                        .unwrap(),
                                    ),
                                ),
                            );
                            key_count += 1;
                        }
                        'D' => {
                            // Capability Delegation
                            verification_methods.push(process_key(
                                method_specific_id,
                                &["#key-", &key_count.to_string()].concat(),
                                &part[1..],
                            )?);

                            verification_relationships.capability_delegation.push(
                                verification_method::ValueOrReference::Reference(
                                    DIDURLReferenceBuf::Relative(
                                        RelativeDIDURLBuf::new(
                                            ["#key-", &key_count.to_string()]
                                                .concat()
                                                .as_bytes()
                                                .to_vec(),
                                        )
                                        .unwrap(),
                                    ),
                                ),
                            );
                            key_count += 1;
                        }
                        'E' => {
                            // Key Agreement (Encryption)
                            verification_methods.push(process_key(
                                method_specific_id,
                                &["#key-", &key_count.to_string()].concat(),
                                &part[1..],
                            )?);
                            verification_relationships.key_agreement.push(
                                verification_method::ValueOrReference::Reference(
                                    DIDURLReferenceBuf::Relative(
                                        RelativeDIDURLBuf::new(
                                            ["#key-", &key_count.to_string()]
                                                .concat()
                                                .as_bytes()
                                                .to_vec(),
                                        )
                                        .unwrap(),
                                    ),
                                ),
                            );
                            key_count += 1;
                        }
                        'I' => {
                            // Capability Invocation
                            verification_methods.push(process_key(
                                method_specific_id,
                                &["#key-", &key_count.to_string()].concat(),
                                &part[1..],
                            )?);

                            verification_relationships.capability_invocation.push(
                                verification_method::ValueOrReference::Reference(
                                    DIDURLReferenceBuf::Relative(
                                        RelativeDIDURLBuf::new(
                                            ["#key-", &key_count.to_string()]
                                                .concat()
                                                .as_bytes()
                                                .to_vec(),
                                        )
                                        .unwrap(),
                                    ),
                                ),
                            );
                            key_count += 1;
                        }
                        'V' => {
                            // Authentication (Verification)
                            verification_methods.push(process_key(
                                method_specific_id,
                                &["#key-", &key_count.to_string()].concat(),
                                &part[1..],
                            )?);

                            verification_relationships.authentication.push(
                                verification_method::ValueOrReference::Reference(
                                    DIDURLReferenceBuf::Relative(
                                        RelativeDIDURLBuf::new(
                                            ["#key-", &key_count.to_string()]
                                                .concat()
                                                .as_bytes()
                                                .to_vec(),
                                        )
                                        .unwrap(),
                                    ),
                                ),
                            );
                            verification_relationships.assertion_method.push(
                                verification_method::ValueOrReference::Reference(
                                    DIDURLReferenceBuf::Relative(
                                        RelativeDIDURLBuf::new(
                                            ["#key-", &key_count.to_string()]
                                                .concat()
                                                .as_bytes()
                                                .to_vec(),
                                        )
                                        .unwrap(),
                                    ),
                                ),
                            );

                            key_count += 1;
                        }
                        'S' => {
                            // Service
                            let raw = match BASE64_URL_SAFE_NO_PAD.decode(part[1..].as_bytes()) {
                                Ok(raw) => raw,
                                Err(e) => {
                                    return Err(Error::Internal(format!(
                                        "Failed to decode base64 string: ({}) Reason: {}",
                                        &part[1..],
                                        e
                                    )));
                                }
                            };
                            let service = if let Ok(service) =
                                serde_json::from_slice::<DIDPeerService>(raw.as_slice())
                            {
                                service
                            } else {
                                return Err(Error::Internal(format!(
                                    "JSON parsing error on service. raw string ({})",
                                    String::from_utf8(raw).unwrap_or("".to_string())
                                )));
                            };

                            let mut service: Service = service.into();
                            if service_count > 0 {
                                service.id = UriBuf::new(
                                    ["#service-", &service_count.to_string()].concat().into(),
                                )
                                .unwrap();
                            }
                            services.push(service);
                            service_count += 1;
                        }
                        other => {
                            return Err(Error::RepresentationNotSupported(format!(
                                "An invalid Purpose Code ({}) was found in the DID",
                                other
                            )));
                        }
                    }
                }
                None => {
                    // We shouldn't really get here
                    // But it is ok if we do, we just skip it
                }
            }
        }

        let vm_type = match options.parameters.public_key_format {
            Some(name) => VerificationMethodType::from_name(&name).ok_or_else(|| {
                Error::Internal(format!(
                    "verification method type `{name}` unsupported by did:peer"
                ))
            })?,
            None => VerificationMethodType::Multikey,
        };

        let mut doc =
            Document::new(DIDBuf::from_string(["did:peer:", method_specific_id].concat()).unwrap());
        doc.verification_method = verification_methods;
        doc.verification_relationships = verification_relationships;
        doc.service = services;

        let mut json_ld_context = Vec::new();
        if let Some(context) = vm_type.context_entry() {
            json_ld_context.push(context)
        }

        let content_type = options.accept.unwrap_or(MediaType::JsonLd);

        let represented = doc.into_representation(representation::Options::from_media_type(
            content_type,
            move || representation::json_ld::Options {
                context: representation::json_ld::Context::array(
                    representation::json_ld::DIDContext::V1,
                    json_ld_context,
                ),
            },
        ));

        Ok(resolution::Output::new(
            represented.to_bytes(),
            document::Metadata::default(),
            resolution::Metadata::from_content_type(Some(content_type.to_string())),
        ))
    }
}

impl DIDMethod for DIDPeer {
    const DID_METHOD_NAME: &'static str = "peer";
}

impl DIDPeer {
    /// Creates a new did:peer DID
    ///
    /// This will preserve the order of the keys and services in creating the did:peer string
    ///
    /// # Examples
    /// ```ignore
    ///
    /// // Create a did:peer with pre-existing encryption key (Multibase base58-btc e.g: z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK)
    /// let keys = vec![DIDPeerCreateKeys {
    ///     type_: Some(DIDPeerKeyType::Ed25519),
    ///     purpose: DIDPeerKeys::Encryption,
    ///     public_key_multibase: Some("z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK".into()),
    /// }];
    /// let did_peer = DIDPeer::create_peer_did(&keys, None).expect("Failed to create did:peer");
    ///
    /// // Create a random did:peer with services
    /// let keys = vec![DIDPeerCreateKeys {
    ///    type_: Some(DIDPeerKeyType::Secp256k1),
    ///    purpose: DIDPeerKeys::Encryption,
    ///    public_key_multibase: Some("z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK".into()),
    ///  }];
    /// let services: Vec<DIDPeerService> = vec![DIDPeerService {
    ///    _type: "dm".into(),
    ///    id: None,
    ///    service_end_point: PeerServiceEndPoint::Long(PeerServiceEndPointLong {
    ///        uri: "http://example.com/didcomm".into(),
    ///        accept: vec!["didcomm/v2".into()],
    ///        routing_keys: vec!["did:example:123456789abcdefghi#key-1".into()],
    ///    }),
    /// }];
    ///
    /// let did_peer =
    ///    DIDPeer::create_peer_did(&keys, Some(&services)).expect("Failed to create did:peer");
    ///
    /// // How to create a key prior to creating a did:peer
    /// let did =
    ///     DIDKey::generate(&JWK::generate_ed25519().unwrap()).expect("Failed to create did:key");
    ///
    /// let keys = vec![DIDPeerCreateKeys {
    ///     type_: Some(DIDPeerKeyType::Ed25519),
    ///     purpose: DIDPeerKeys::Verification,
    ///     public_key_multibase: Some(did[8..].to_string()),
    /// }];
    ///
    /// let did_peer = DIDPeer::create_peer_did(&keys, None).expect("Failed to create did:peer");
    /// ```
    pub fn create_peer_did(
        keys: &Vec<DIDPeerCreateKeys>,
        services: Option<&Vec<DIDPeerService>>,
    ) -> Result<(String, Vec<DIDPeerCreatedKeys>), DIDPeerError> {
        let mut result = String::from("did:peer:2");

        let mut private_keys: Vec<DIDPeerCreatedKeys> = vec![];
        for key in keys {
            // Create new keys if not provided
            let public_key = if let Some(key) = key.public_key_multibase.as_ref() {
                key.clone()
            } else {
                let jwk = match &key.type_ {
                    Some(type_) => match type_ {
                        DIDPeerKeyType::Ed25519 => match JWK::generate_ed25519() {
                            Ok(k) => k,
                            Err(e) => {
                                return Err(DIDPeerError::InternalError(format!(
                                    "Failed to generate ed25519 key. Reason: {}",
                                    e
                                )))
                            }
                        },
                        DIDPeerKeyType::Secp256k1 => JWK::generate_secp256k1(),
                        DIDPeerKeyType::P256 => JWK::generate_p256(),
                    },
                    None => return Err(DIDPeerError::UnsupportedKeyType),
                };
                let did = if let Ok(output) = ssi::dids::DIDKey::generate(&jwk) {
                    output.to_string()
                } else {
                    return Err(DIDPeerError::InternalError(
                        "Couldn't create did:key".to_string(),
                    ));
                };

                match jwk.params {
                    Params::OKP(map) => {
                        let d = if let Some(d) = &map.private_key {
                            d
                        } else {
                            return Err(DIDPeerError::KeyParsingError(
                                "Missing private key".to_string(),
                            ));
                        };
                        private_keys.push(DIDPeerCreatedKeys {
                            key_multibase: did[8..].to_string(),
                            curve: map.curve.clone(),
                            d: String::from(d),
                            x: String::from(map.public_key.clone()),
                            y: None,
                        })
                    }
                    Params::EC(map) => {
                        let curve = if let Some(curve) = &map.curve {
                            curve
                        } else {
                            return Err(DIDPeerError::KeyParsingError("Missing curve".to_string()));
                        };
                        let d = if let Some(d) = &map.ecc_private_key {
                            d
                        } else {
                            return Err(DIDPeerError::KeyParsingError(
                                "Missing private key".to_string(),
                            ));
                        };

                        let x = if let Some(x) = &map.x_coordinate {
                            x
                        } else {
                            return Err(DIDPeerError::KeyParsingError(
                                "Missing public key (x)".to_string(),
                            ));
                        };
                        let y = if let Some(y) = &map.y_coordinate {
                            y
                        } else {
                            return Err(DIDPeerError::KeyParsingError(
                                "Missing public key (y)".to_string(),
                            ));
                        };

                        private_keys.push(DIDPeerCreatedKeys {
                            key_multibase: did[8..].to_string(),
                            curve: curve.into(),
                            d: String::from(d),
                            x: String::from(x),
                            y: Some(String::from(y)),
                        })
                    }
                    _ => return Err(DIDPeerError::UnsupportedKeyType),
                }

                did[8..].to_string()
            };

            // Place based on key types
            match key.purpose {
                DIDPeerKeys::Verification => {
                    result.push_str(&format!(".V{}", public_key));
                }
                DIDPeerKeys::Encryption => {
                    result.push_str(&format!(".E{}", public_key));
                }
            }
        }

        if let Some(services) = services {
            for service in services {
                let service = serde_json::to_string(&service).map_err(|e| {
                    DIDPeerError::SyntaxErrorServiceDefinition(format!(
                        "Error parsing service: {}",
                        e
                    ))
                })?;
                result.push_str(&format!(".S{}", BASE64_URL_SAFE_NO_PAD.encode(service)));
            }
        }

        Ok((result, private_keys))
    }

    /// Expands an existing DID Document from the did:key Multikeys to full JWT keys
    /// This is useful for when you want to resolve a did:peer DID Document to a full JWT included DID Document
    /// Converts base58 multi-keys to full JWTs in verificationMethod
    pub async fn expand_keys(doc: &Document) -> Result<Document, DIDPeerError> {
        let mut new_doc = doc.clone();

        let mut new_vms: Vec<DIDVerificationMethod> = vec![];
        for v_method in &doc.verification_method {
            new_vms.push(Self::_convert_vm(v_method).await?);
        }

        new_doc.verification_method = new_vms;
        Ok(new_doc)
    }

    // Converts
    async fn _convert_vm(
        method: &DIDVerificationMethod,
    ) -> Result<DIDVerificationMethod, DIDPeerError> {
        let did_key = if let Some(key) = method.properties.get("publicKeyBase58") {
            ["did:key:", key.as_str().unwrap()].concat()
        } else if let Some(key) = method.properties.get("publicKeyMultibase") {
            ["did:key:", key.as_str().unwrap()].concat()
        } else {
            return Err(DIDPeerError::KeyParsingError(
                "Failed to convert verification_method. Reason: Missing publicKeyBase58"
                    .to_string(),
            ));
        };

        let key_method = DIDKey;

        let output = match key_method
            .resolve(DID::new::<String>(&did_key.clone()).unwrap())
            .await
        {
            Ok(output) => output,
            Err(e) => {
                return Err(DIDPeerError::KeyParsingError(format!(
                    "Failed to resolve key ({}). Reason: {}",
                    did_key, e
                )));
            }
        };

        if let Some(vm) = output.document.verification_method.first() {
            Ok(vm.clone())
        } else {
            Err(DIDPeerError::KeyParsingError(
                "Failed to convert verification_method. Reason: Missing verification_method"
                    .to_string(),
            ))
        }
    }
}

// **********************************************************************************************************************************
// WASM Specific structs and code
// **********************************************************************************************************************************

/// DIDService structure, input into the DidPeerCreate structure
///
/// DIDService {
///         _type: `Option<String>` (Optional: If not specified, defaults to 'DIDCommMessaging')
///           uri: `String`         (Required: Service endpoint URI. E.g. https://localhost:7130/)
///        accept: `Vec<String>`    (Array of possible message types this service accepts)
///  routing_keys: `Vec<String>`    (Array of possible keys this Service endpoint can use)
///            id: `Option<String>` (Optional: ID of the service. If not specified, defaults to #service)
/// }
#[wasm_bindgen(getter_with_clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct DIDService {
    pub _type: Option<String>,
    pub uri: String,
    pub accept: Vec<String>,
    pub routing_keys: Vec<String>,
    pub id: Option<String>,
}

#[wasm_bindgen]
impl DIDService {
    #[wasm_bindgen(constructor)]
    pub fn new(
        uri: String,
        accept: Vec<String>,
        routing_keys: Vec<String>,
        id: Option<String>,
    ) -> Self {
        DIDService {
            _type: None,
            uri,
            accept,
            routing_keys,
            id,
        }
    }
}

impl From<DIDService> for DIDPeerService {
    fn from(service: DIDService) -> Self {
        DIDPeerService {
            _type: service._type.unwrap_or("DIDCommMessaging".into()),
            service_end_point: PeerServiceEndPoint::Short(PeerServiceEndPointShort {
                uri: service.uri,
                a: service.accept,
                r: service.routing_keys,
            }),
            id: service.id,
        }
    }
}

impl From<&DIDService> for DIDPeerService {
    fn from(service: &DIDService) -> Self {
        service.clone().into()
    }
}

/// DidPeerCreate structure,  input from JS into [create_did_peer] call
/// Contains the required keys and optional services to create a new did:peer DID
///
/// DIDPeerCreate {
///       keys: Vec<[DIDPeerCreateKeys]> (Required: Must contain at least one key for Encryption and another key for Verification)
///   services: Option<Vec<[DIDService]> (Optional: Array of DIDService structs to add to the DID Document)
/// }
#[derive(Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct DidPeerCreate {
    pub keys: Vec<DIDPeerCreateKeys>,
    pub services: Option<Vec<DIDService>>,
}

#[wasm_bindgen]
impl DidPeerCreate {
    #[wasm_bindgen(constructor)]
    pub fn new(keys: Vec<DIDPeerCreateKeys>, services: Option<Vec<DIDService>>) -> Self {
        DidPeerCreate { keys, services }
    }
}

#[derive(Serialize, Deserialize)]
#[wasm_bindgen(getter_with_clone)]
pub struct DIDPeerResult {
    pub did: String,
    pub keys: Vec<DIDPeerCreatedKeys>,
}

#[wasm_bindgen]
/// create_did_peer() wasm wrapper for [DIDPeer::create_peer_did]
/// Input: reference to [DidPeerCreate] struct
/// Returns: Error or String of the newly created did:peer DID
///
/// Notes:
///   [DidPeerCreate] contains an array of keys and an optional array of Services
///   These arrays are processed in order (as in element 0 is processed first, then element 1, etc)
///   This means the key and service identifiers are auto-generated in the order they are provided
///   i.e. #service, #service-1, #service-2 and #key-1, #key-2, #key-3 ...
pub fn create_did_peer(input: &DidPeerCreate) -> Result<DIDPeerResult, DIDPeerError> {
    // Convert DIDService to DIDPeerService
    let mut new_services: Vec<DIDPeerService> = vec![];
    if let Some(services) = input.services.as_ref() {
        for service in services {
            new_services.push(service.into());
        }
    }

    // Create the did:peer DID
    let response = DIDPeer::create_peer_did(&input.keys, Some(&new_services));

    if let Ok((did, keys)) = response {
        Ok(DIDPeerResult { did, keys })
    } else {
        Err(response.unwrap_err())
    }
}

#[wasm_bindgen]
/// resolve_did_peer() resolves a DID Peer method DID to a full DID Document represented by a JS object
/// Input: String of the DID Peer method DID (did:peer:2...)
/// Returns: Error or JSON String of the resolved DID Document
///
/// NOTE: This is an async call, so you must await the result
pub async fn resolve_did_peer(did: &str) -> Result<String, DIDPeerError> {
    let peer = DIDPeer;

    match peer
        .resolve(DID::new::<String>(&did.to_string()).unwrap())
        .await
    {
        Ok(output) => match serde_json::to_string_pretty(&output.document) {
            Ok(json) => Ok(json),
            Err(e) => Err(DIDPeerError::JsonParsingError(format!(
                "Couldn't convert DID Document to JSON. Reason: {}",
                e
            ))),
        },
        Err(e) => Err(DIDPeerError::KeyParsingError(format!(
            "Failed to resolve key ({}). Reason: {}",
            did, e
        ))),
    }
}