siwe 0.6.1

Rust implementation of EIP-4361: Sign In With Ethereum
Documentation
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
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg), feature(doc_cfg))]
#![doc = include_str!("../README.md")]

mod nonce;
mod rfc3339;

#[cfg(feature = "ethers")]
mod eip1271;

use ::core::{
    convert::Infallible,
    fmt::{self, Display, Formatter},
    str::FromStr,
};
use hex::FromHex;
use http::uri::{Authority, InvalidUri};
use iri_string::types::UriString;
use k256::ecdsa::{RecoveryId, Signature, VerifyingKey};
use sha3::{Digest, Keccak256};
use std::convert::{TryFrom, TryInto};
use thiserror::Error;
use time::OffsetDateTime;

#[cfg(feature = "ethers")]
use ethers::prelude::*;

#[cfg(feature = "serde")]
use serde::{
    de::{self, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};

pub use nonce::generate_nonce;
pub use rfc3339::TimeStamp;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// EIP-4361 version.
pub enum Version {
    /// V1
    V1 = 1,
}

impl FromStr for Version {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "1" {
            Ok(Self::V1)
        } else {
            Err(ParseError::Format("Bad Version"))
        }
    }
}

/// EIP-4361 message.
///
/// # Example
/// ```
/// # use siwe::Message;
/// #
/// let msg = r#"localhost:4361 wants you to sign in with your Ethereum account:
/// 0x6Da01670d8fc844e736095918bbE11fE8D564163
///
/// SIWE Notepad Example
///
/// URI: http://localhost:4361
/// Version: 1
/// Chain ID: 1
/// Nonce: kEWepMt9knR6lWJ6A
/// Issued At: 2021-12-07T18:28:18.807Z"#;
/// let message: Message = msg.parse().unwrap();
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Message {
    /// The RFC 3986 authority that is requesting the signing.
    pub domain: Authority,
    /// The Ethereum address performing the signing conformant to capitalization encoded checksum specified in EIP-55 where applicable.
    pub address: [u8; 20],
    /// A human-readable ASCII assertion that the user will sign, and it must not contain '\n' (the byte 0x0a).
    pub statement: Option<String>,
    /// An RFC 3986 URI referring to the resource that is the subject of the signing (as in the subject of a claim).
    pub uri: UriString,
    /// The current version of the message, which MUST be 1 for this specification.
    pub version: Version,
    /// The EIP-155 Chain ID to which the session is bound, and the network where Contract Accounts MUST be resolved.
    pub chain_id: u64,
    /// A randomized token typically chosen by the relying party and used to prevent replay attacks, at least 8 alphanumeric characters.
    pub nonce: String,
    /// The ISO 8601 datetime string of the current time.
    pub issued_at: TimeStamp,
    /// The ISO 8601 datetime string that, if present, indicates when the signed authentication message is no longer valid.
    pub expiration_time: Option<TimeStamp>,
    /// The ISO 8601 datetime string that, if present, indicates when the signed authentication message will become valid.
    pub not_before: Option<TimeStamp>,
    /// An system-specific identifier that may be used to uniquely refer to the sign-in request.
    pub request_id: Option<String>,
    /// A list of information or references to information the user wishes to have resolved as part of authentication by the relying party. They are expressed as RFC 3986 URIs separated by "\n- " where \n is the byte 0x0a.
    pub resources: Vec<UriString>,
}

impl Display for Message {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        writeln!(f, "{}{}", &self.domain, PREAMBLE)?;
        writeln!(f, "{}", eip55(&self.address))?;
        writeln!(f)?;
        if let Some(statement) = &self.statement {
            writeln!(f, "{}", statement)?;
        }
        writeln!(f)?;
        writeln!(f, "{}{}", URI_TAG, &self.uri)?;
        writeln!(f, "{}{}", VERSION_TAG, self.version as u64)?;
        writeln!(f, "{}{}", CHAIN_TAG, &self.chain_id)?;
        writeln!(f, "{}{}", NONCE_TAG, &self.nonce)?;
        write!(f, "{}{}", IAT_TAG, &self.issued_at)?;
        if let Some(exp) = &self.expiration_time {
            write!(f, "\n{}{}", EXP_TAG, &exp)?
        };
        if let Some(nbf) = &self.not_before {
            write!(f, "\n{}{}", NBF_TAG, &nbf)?
        };
        if let Some(rid) = &self.request_id {
            write!(f, "\n{}{}", RID_TAG, rid)?
        };
        if !self.resources.is_empty() {
            write!(f, "\n{}", RES_TAG)?;
            for res in &self.resources {
                write!(f, "\n- {}", res)?;
            }
        };
        Ok(())
    }
}

#[derive(Error, Debug)]
/// Errors raised during parsing/deserialization.
pub enum ParseError {
    #[error("Invalid Domain: {0}")]
    /// Domain field is non-conformant.
    Domain(#[from] InvalidUri),
    #[error("Formatting Error: {0}")]
    /// Catch-all for all other parsing errors.
    Format(&'static str),
    #[error("Invalid Address: {0}")]
    /// Address field is non-conformant.
    Address(#[from] hex::FromHexError),
    #[error("Invalid URI: {0}")]
    /// URI field is non-conformant.
    Uri(#[from] iri_string::validate::Error),
    #[error("Invalid Timestamp: {0}")]
    /// Timestamp is non-conformant.
    TimeStamp(#[from] time::Error),
    #[error(transparent)]
    /// Chain ID is non-conformant.
    ParseIntError(#[from] std::num::ParseIntError),
    #[error(transparent)]
    /// Infallible variant.
    Never(#[from] Infallible),
}

fn tagged<'a>(tag: &'static str, line: Option<&'a str>) -> Result<&'a str, ParseError> {
    line.and_then(|l| l.strip_prefix(tag))
        .ok_or(ParseError::Format(tag))
}

fn parse_line<S: FromStr<Err = E>, E: Into<ParseError>>(
    tag: &'static str,
    line: Option<&str>,
) -> Result<S, ParseError> {
    tagged(tag, line).and_then(|s| S::from_str(s).map_err(|e| e.into()))
}

fn tag_optional<'a>(
    tag: &'static str,
    line: Option<&'a str>,
) -> Result<Option<&'a str>, ParseError> {
    match tagged(tag, line).map(Some) {
        Err(ParseError::Format(t)) if t == tag => Ok(None),
        r => r,
    }
}

impl FromStr for Message {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut lines = s.split('\n');
        let domain = lines
            .next()
            .and_then(|preamble| preamble.strip_suffix(PREAMBLE))
            .map(Authority::from_str)
            .ok_or(ParseError::Format("Missing Preamble Line"))??;
        let address = tagged(ADDR_TAG, lines.next())
            .and_then(|a| {
                if is_checksum(a) {
                    Ok(a)
                } else {
                    Err(ParseError::Format("Address is not in EIP-55 format"))
                }
            })
            .and_then(|a| <[u8; 20]>::from_hex(a).map_err(|e| e.into()))?;

        // Skip the new line:
        lines.next();
        let statement = match lines.next() {
            None => return Err(ParseError::Format("No lines found after address")),
            Some("") => None,
            Some(s) => {
                lines.next();
                Some(s.to_string())
            }
        };

        let uri = parse_line(URI_TAG, lines.next())?;
        let version = parse_line(VERSION_TAG, lines.next())?;
        let chain_id = parse_line(CHAIN_TAG, lines.next())?;
        let nonce = parse_line(NONCE_TAG, lines.next()).and_then(|nonce: String| {
            if nonce.len() < 8 {
                Err(ParseError::Format("Nonce must be longer than 8 characters"))
            } else {
                Ok(nonce)
            }
        })?;
        let issued_at = tagged(IAT_TAG, lines.next())?.parse()?;

        let mut line = lines.next();
        let expiration_time = match tag_optional(EXP_TAG, line)? {
            Some(exp) => {
                line = lines.next();
                Some(exp.parse()?)
            }
            None => None,
        };
        let not_before = match tag_optional(NBF_TAG, line)? {
            Some(nbf) => {
                line = lines.next();
                Some(nbf.parse()?)
            }
            None => None,
        };

        let request_id = match tag_optional(RID_TAG, line)? {
            Some(rid) => {
                line = lines.next();
                Some(rid.into())
            }
            None => None,
        };

        let resources = match line {
            Some(RES_TAG) => lines.map(|s| parse_line("- ", Some(s))).collect(),
            Some(_) => Err(ParseError::Format("Unexpected Content")),
            None => Ok(vec![]),
        }?;

        Ok(Message {
            domain,
            address,
            statement,
            uri,
            version,
            chain_id,
            nonce,
            issued_at,
            expiration_time,
            not_before,
            request_id,
            resources,
        })
    }
}

#[cfg(feature = "serde")]
impl Serialize for Message {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.to_string().as_str())
    }
}

#[cfg(feature = "serde")]
struct MessageVisitor;

#[cfg(feature = "serde")]
impl<'de> Visitor<'de> for MessageVisitor {
    type Value = Message;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("an EIP-4361 formatted message")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        match Message::from_str(value) {
            Ok(message) => Ok(message),
            Err(error) => Err(E::custom(format!("error parsing message: {}", error))),
        }
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Message {
    fn deserialize<D>(deserializer: D) -> Result<Message, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(MessageVisitor)
    }
}

// Fixes the documentation to show the typed builder impl as behind a feature flag.
macro_rules! typed_builder_doc {
    ($struct:item) => {
        #[cfg(feature = "typed-builder")]
        mod tb {
            use super::*;
            #[derive(typed_builder::TypedBuilder)]
            #[builder(doc)]
            #[cfg_attr(docsrs, doc(cfg(all())))]
            $struct
        }

        #[cfg(not(feature = "typed-builder"))]
        mod tb {
            use super::*;
            #[cfg_attr(docsrs, doc(cfg(all())))]
            $struct
        }

        pub use tb::*;
    }
}

typed_builder_doc! {
    /// Verification options and configuration
    pub struct VerificationOpts {
        /// Expected domain field.
        pub domain: Option<Authority>,
        /// Expected nonce field.
        pub nonce: Option<String>,
        /// Datetime for which the message should be valid at.
        pub timestamp: Option<OffsetDateTime>,
        #[cfg(feature = "ethers")]
        /// RPC Provider used for on-chain checks. Necessary for contract wallets signatures.
        pub rpc_provider: Option<Provider<Http>>,
    }
}

// Non-derived implementation needed, otherwise the implementation is marked as being behind the
// typed-builder feature flag.
#[allow(clippy::derivable_impls)]
impl Default for VerificationOpts {
    fn default() -> Self {
        Self {
            domain: None,
            nonce: None,
            timestamp: None,
            #[cfg(feature = "ethers")]
            rpc_provider: None,
        }
    }
}

#[derive(Error, Debug)]
/// Reasons for the verification of a signed message to fail.
pub enum VerificationError {
    #[error(transparent)]
    /// Signature is not a valid k256 signature (it can be returned if the contract wallet verification failed or is not enabled).
    Crypto(#[from] k256::ecdsa::Error),
    #[error(transparent)]
    /// Message failed to be serialized.
    Serialization(#[from] fmt::Error),
    #[error("Recovered key does not match address or contract wallet support is not enabled.")]
    /// Catch-all for invalid signature (it can be returned if contract wallet support is not enabled).
    Signer,
    #[error("Message is not currently valid")]
    /// Message is not currently valid.
    Time,
    #[error("Message domain does not match")]
    /// Expected message domain does not match.
    DomainMismatch,
    #[error("Message nonce does not match")]
    /// Expected message nonce does not match.
    NonceMismatch,
    #[cfg(feature = "ethers")]
    // Using a String because the original type requires a lifetime.
    #[error("Contract wallet query failed: {0}")]
    /// Contract wallet verification failed unexpectedly.
    ContractCall(String),
    #[error("The signature is not 65 bytes long. It might mean that it is a EIP1271 signature and you have the `ethers` feature disabled or configured a provider.")]
    /// The signature is not 65 bytes long. It might mean that it is a EIP1271 signature and you have the `ethers` feature disabled or configured a provider.
    SignatureLength,
}

/// Takes an UNPREFIXED eth address and returns whether it is in checksum format or not.
pub fn is_checksum(address: &str) -> bool {
    match <[u8; 20]>::from_hex(address) {
        Ok(s) => {
            let sum = eip55(&s);
            let sum = sum.trim_start_matches("0x");
            sum == address
        }
        Err(_) => false,
    }
}

impl Message {
    /// Verify the integrity of the message by matching its signature.
    ///
    /// # Arguments
    /// - `sig` - Signature of the message signed by the wallet
    ///
    /// # Example
    /// ```
    /// # use siwe::Message;
    /// # use hex::FromHex;
    /// #
    /// # let msg = r#"localhost:4361 wants you to sign in with your Ethereum account:
    /// # 0x6Da01670d8fc844e736095918bbE11fE8D564163
    /// #
    /// # SIWE Notepad Example
    /// #
    /// # URI: http://localhost:4361
    /// # Version: 1
    /// # Chain ID: 1
    /// # Nonce: kEWepMt9knR6lWJ6A
    /// # Issued At: 2021-12-07T18:28:18.807Z"#;
    /// # let message: Message = msg.parse().unwrap();
    /// let signature = <[u8; 65]>::from_hex(r#"6228b3ecd7bf2df018183aeab6b6f1db1e9f4e3cbe24560404112e25363540eb679934908143224d746bbb5e1aa65ab435684081f4dbb74a0fec57f98f40f5051c"#).unwrap();
    /// let signer: Vec<u8> = message.verify_eip191(&signature).unwrap();
    /// ```
    pub fn verify_eip191(&self, sig: &[u8; 65]) -> Result<Vec<u8>, VerificationError> {
        let prehash = self.eip191_hash()?;
        let signature: Signature = Signature::from_slice(&sig[..64])?;
        let recovery_id = RecoveryId::try_from(&sig[64] % 27)?;

        let pk: VerifyingKey =
            VerifyingKey::recover_from_prehash(&prehash, &signature, recovery_id)?;

        let recovered_address = Keccak256::default()
            .chain_update(&pk.to_encoded_point(false).as_bytes()[1..])
            .finalize();

        let recovered_address: &[u8] = &recovered_address[12..];

        if recovered_address != self.address {
            Err(VerificationError::Signer)
        } else {
            Ok(pk.to_sec1_bytes().to_vec())
        }
    }

    #[cfg(feature = "ethers")]
    /// Verify the integrity of a, potentially, EIP-1271 signed message.
    ///
    /// # Arguments
    /// - `sig` - Signature of the message signed by the wallet.
    /// - `provider` - Provider used to query the chain.
    ///
    /// # Example (find a provider at https://ethereumnodes.com/)
    /// ```ignore
    /// let is_valid: bool = message.verify_eip1271(&signature, "https://provider.example.com/".try_into().unwrap())?;
    /// ```
    pub async fn verify_eip1271(
        &self,
        sig: &[u8],
        provider: &Provider<Http>,
    ) -> Result<bool, VerificationError> {
        let hash = Keccak256::new_with_prefix(self.eip191_bytes().unwrap()).finalize();
        eip1271::verify_eip1271(self.address, hash.as_ref(), sig, provider).await
    }

    /// Validates time constraints and integrity of the object by matching it's signature.
    ///
    /// # Arguments
    /// - `sig` - Signature of the message signed by the wallet
    /// - `opts` - Verification options and configuration
    ///
    /// # Example
    /// ```
    /// # use hex::FromHex;
    /// # use siwe::{Message, TimeStamp, VerificationOpts};
    /// # use std::str::FromStr;
    /// # use time::{format_description::well_known::Rfc3339, OffsetDateTime};
    /// #
    /// # #[tokio::main]
    /// # async fn main() {
    /// # let msg = r#"localhost:4361 wants you to sign in with your Ethereum account:
    /// # 0x6Da01670d8fc844e736095918bbE11fE8D564163
    /// #
    /// # SIWE Notepad Example
    /// #
    /// # URI: http://localhost:4361
    /// # Version: 1
    /// # Chain ID: 1
    /// # Nonce: kEWepMt9knR6lWJ6A
    /// # Issued At: 2021-12-07T18:28:18.807Z"#;
    /// # let message: Message = msg.parse().unwrap();
    /// let signature = <[u8; 65]>::from_hex(r#"6228b3ecd7bf2df018183aeab6b6f1db1e9f4e3cbe24560404112e25363540eb679934908143224d746bbb5e1aa65ab435684081f4dbb74a0fec57f98f40f5051c"#).unwrap();
    ///
    /// let verification_opts = VerificationOpts {
    ///     domain: Some("localhost:4361".parse().unwrap()),
    ///     nonce: Some("kEWepMt9knR6lWJ6A".into()),
    ///     timestamp: Some(OffsetDateTime::parse("2021-12-08T00:00:00Z", &Rfc3339).unwrap()),
    ///     ..Default::default()
    /// };
    ///
    /// message.verify(&signature, &verification_opts).await.unwrap();
    /// # }
    /// ```
    pub async fn verify(
        &self,
        sig: &[u8],
        opts: &VerificationOpts,
    ) -> Result<(), VerificationError> {
        match (
            opts.timestamp
                .as_ref()
                .map(|t| self.valid_at(t))
                .unwrap_or_else(|| self.valid_now()),
            opts.domain.as_ref(),
            opts.nonce.as_ref(),
        ) {
            (false, _, _) => return Err(VerificationError::Time),
            (_, Some(d), _) if *d != self.domain => return Err(VerificationError::DomainMismatch),
            (_, _, Some(n)) if *n != self.nonce => return Err(VerificationError::NonceMismatch),
            _ => (),
        };

        let res = if sig.len() == 65 {
            self.verify_eip191(sig.try_into().unwrap())
        } else {
            Err(VerificationError::SignatureLength)
        };

        #[cfg(feature = "ethers")]
        if let Err(e) = res {
            if let Some(provider) = &opts.rpc_provider {
                if self.verify_eip1271(sig, provider).await? {
                    return Ok(());
                }
            }
            return Err(e);
        }
        res.map(|_| ())
    }

    /// Validates the time constraints of the message at current time.
    ///
    /// # Example
    /// ```
    /// # use siwe::Message;
    /// # use time::OffsetDateTime;
    /// #
    /// # let msg = r#"localhost:4361 wants you to sign in with your Ethereum account:
    /// # 0x6Da01670d8fc844e736095918bbE11fE8D564163
    /// #
    /// # SIWE Notepad Example
    /// #
    /// # URI: http://localhost:4361
    /// # Version: 1
    /// # Chain ID: 1
    /// # Nonce: kEWepMt9knR6lWJ6A
    /// # Issued At: 2021-12-07T18:28:18.807Z"#;
    /// # let message: Message = msg.parse().unwrap();
    /// assert!(message.valid_now());
    ///
    /// // equivalent to
    /// assert!(message.valid_at(&OffsetDateTime::now_utc()));
    /// ```
    pub fn valid_now(&self) -> bool {
        self.valid_at(&OffsetDateTime::now_utc())
    }

    /// Validates the time constraints of the message at a specific point in time.
    ///
    /// # Arguments
    /// - `t` - timestamp to use when validating time constraints
    ///
    /// # Example
    /// ```
    /// # use siwe::Message;
    /// # use time::OffsetDateTime;
    /// #
    /// # let msg = r#"localhost:4361 wants you to sign in with your Ethereum account:
    /// # 0x6Da01670d8fc844e736095918bbE11fE8D564163
    /// #
    /// # SIWE Notepad Example
    /// #
    /// # URI: http://localhost:4361
    /// # Version: 1
    /// # Chain ID: 1
    /// # Nonce: kEWepMt9knR6lWJ6A
    /// # Issued At: 2021-12-07T18:28:18.807Z"#;
    /// # let message: Message = msg.parse().unwrap();
    /// assert!(message.valid_at(&OffsetDateTime::now_utc()));
    /// ```
    pub fn valid_at(&self, t: &OffsetDateTime) -> bool {
        self.not_before.as_ref().map(|nbf| nbf < t).unwrap_or(true)
            && self
                .expiration_time
                .as_ref()
                .map(|exp| exp >= t)
                .unwrap_or(true)
    }

    /// Produces EIP-191 Personal-Signature pre-hash signing input
    ///
    /// # Example
    /// ```
    /// # use siwe::Message;
    /// #
    /// # let msg = r#"localhost:4361 wants you to sign in with your Ethereum account:
    /// # 0x6Da01670d8fc844e736095918bbE11fE8D564163
    /// #
    /// # SIWE Notepad Example
    /// #
    /// # URI: http://localhost:4361
    /// # Version: 1
    /// # Chain ID: 1
    /// # Nonce: kEWepMt9knR6lWJ6A
    /// # Issued At: 2021-12-07T18:28:18.807Z"#;
    /// # let message: Message = msg.parse().unwrap();
    /// let eip191_bytes: Vec<u8> = message.eip191_bytes().unwrap();
    /// ```
    pub fn eip191_bytes(&self) -> Result<Vec<u8>, fmt::Error> {
        let s = self.to_string();
        Ok(format!("\x19Ethereum Signed Message:\n{}{}", s.as_bytes().len(), s).into())
    }

    /// Produces EIP-191 Personal-Signature Hashed signing-input
    ///
    /// # Example
    /// ```
    /// # use siwe::Message;
    /// #
    /// # let msg = r#"localhost:4361 wants you to sign in with your Ethereum account:
    /// # 0x6Da01670d8fc844e736095918bbE11fE8D564163
    /// #
    /// # SIWE Notepad Example
    /// #
    /// # URI: http://localhost:4361
    /// # Version: 1
    /// # Chain ID: 1
    /// # Nonce: kEWepMt9knR6lWJ6A
    /// # Issued At: 2021-12-07T18:28:18.807Z"#;
    /// # let message: Message = msg.parse().unwrap();
    /// let eip191_hash: [u8; 32] = message.eip191_hash().unwrap();
    /// ```
    pub fn eip191_hash(&self) -> Result<[u8; 32], fmt::Error> {
        Ok(Keccak256::default()
            .chain_update(self.eip191_bytes()?)
            .finalize()
            .into())
    }
}

/// Takes an eth address and returns it as a checksum formatted string.
pub fn eip55(addr: &[u8; 20]) -> String {
    let addr_str = hex::encode(addr);
    let hash = Keccak256::digest(addr_str.as_bytes());
    "0x".chars()
        .chain(addr_str.chars().enumerate().map(|(i, c)| {
            match (c, hash[i >> 1] & if i % 2 == 0 { 128 } else { 8 } != 0) {
                ('a'..='f' | 'A'..='F', true) => c.to_ascii_uppercase(),
                _ => c.to_ascii_lowercase(),
            }
        }))
        .collect()
}

const PREAMBLE: &str = " wants you to sign in with your Ethereum account:";
const ADDR_TAG: &str = "0x";
const URI_TAG: &str = "URI: ";
const VERSION_TAG: &str = "Version: ";
const CHAIN_TAG: &str = "Chain ID: ";
const NONCE_TAG: &str = "Nonce: ";
const IAT_TAG: &str = "Issued At: ";
const EXP_TAG: &str = "Expiration Time: ";
const NBF_TAG: &str = "Not Before: ";
const RID_TAG: &str = "Request ID: ";
const RES_TAG: &str = "Resources:";

#[cfg(test)]
mod tests {
    use time::format_description::well_known::Rfc3339;

    use super::*;
    use std::convert::TryInto;

    #[test]
    fn parsing() {
        // correct order
        let message = r#"service.org wants you to sign in with your Ethereum account:
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2

I accept the ServiceOrg Terms of Service: https://service.org/tos

URI: https://service.org/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json"#;

        assert!(Message::from_str(message).is_ok());

        assert_eq!(message, &Message::from_str(message).unwrap().to_string());

        // incorrect order
        assert!(Message::from_str(
            r#"service.org wants you to sign in with your Ethereum account:
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2

I accept the ServiceOrg Terms of Service: https://service.org/tos

URI: https://service.org/login
Version: 1
Nonce: 32891756
Chain ID: 1
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json"#,
        )
        .is_err());

        //  no statement
        let message = r#"service.org wants you to sign in with your Ethereum account:
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2


URI: https://service.org/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json"#;

        assert!(Message::from_str(message).is_ok());

        assert_eq!(message, &Message::from_str(message).unwrap().to_string());
    }

    #[tokio::test]
    async fn verification() {
        let message = Message::from_str(
            r#"localhost:4361 wants you to sign in with your Ethereum account:
0x6Da01670d8fc844e736095918bbE11fE8D564163

SIWE Notepad Example

URI: http://localhost:4361
Version: 1
Chain ID: 1
Nonce: kEWepMt9knR6lWJ6A
Issued At: 2021-12-07T18:28:18.807Z"#,
        )
        .unwrap();
        let correct = <[u8; 65]>::from_hex(r#"6228b3ecd7bf2df018183aeab6b6f1db1e9f4e3cbe24560404112e25363540eb679934908143224d746bbb5e1aa65ab435684081f4dbb74a0fec57f98f40f5051c"#).unwrap();

        let verify_result = message.verify_eip191(&correct);
        dbg!(&verify_result);
        assert!(verify_result.is_ok());

        let incorrect = <[u8; 65]>::from_hex(r#"7228b3ecd7bf2df018183aeab6b6f1db1e9f4e3cbe24560404112e25363540eb679934908143224d746bbb5e1aa65ab435684081f4dbb74a0fec57f98f40f5051c"#).unwrap();
        assert!(message.verify_eip191(&incorrect).is_err());
    }

    #[tokio::test]
    async fn verification1() {
        let message = Message::from_str(r#"localhost wants you to sign in with your Ethereum account:
0x4b60ffAf6fD681AbcC270Faf4472011A4A14724C

Allow localhost to access your orbit using their temporary session key: did:key:z6Mktud6LcDFb3heS7FFWoJhiCafmUPkCAgpvJLv5E6fgBJg#z6Mktud6LcDFb3heS7FFWoJhiCafmUPkCAgpvJLv5E6fgBJg

URI: did:key:z6Mktud6LcDFb3heS7FFWoJhiCafmUPkCAgpvJLv5E6fgBJg#z6Mktud6LcDFb3heS7FFWoJhiCafmUPkCAgpvJLv5E6fgBJg
Version: 1
Chain ID: 1
Nonce: PPrtjztx2lYqWbqNs
Issued At: 2021-12-20T12:29:25.907Z
Expiration Time: 2021-12-20T12:44:25.906Z
Resources:
- kepler://bafk2bzacecn2cdbtzho72x4c62fcxvcqj23padh47s5jyyrv42mtca3yrhlpa#put
- kepler://bafk2bzacecn2cdbtzho72x4c62fcxvcqj23padh47s5jyyrv42mtca3yrhlpa#del
- kepler://bafk2bzacecn2cdbtzho72x4c62fcxvcqj23padh47s5jyyrv42mtca3yrhlpa#get
- kepler://bafk2bzacecn2cdbtzho72x4c62fcxvcqj23padh47s5jyyrv42mtca3yrhlpa#list"#).unwrap();
        let correct = <[u8; 65]>::from_hex(r#"20c0da863b3dbfbb2acc0fb3b9ec6daefa38f3f20c997c283c4818ebeca96878787f84fccc25c4087ccb31ebd782ae1d2f74be076a49c0a8604419e41507e9381c"#).unwrap();
        assert!(message.verify_eip191(&correct).is_ok());
        let incorrect = <[u8; 65]>::from_hex(r#"30c0da863b3dbfbb2acc0fb3b9ec6daefa38f3f20c997c283c4818ebeca96878787f84fccc25c4087ccb31ebd782ae1d2f74be076a49c0a8604419e41507e9381c"#).unwrap();
        assert!(message.verify_eip191(&incorrect).is_err());
    }

    const PARSING_POSITIVE: &str = include_str!("../tests/siwe/test/parsing_positive.json");
    const PARSING_NEGATIVE: &str = include_str!("../tests/siwe/test/parsing_negative.json");
    const VERIFICATION_POSITIVE: &str =
        include_str!("../tests/siwe/test/verification_positive.json");
    const VERIFICATION_NEGATIVE: &str =
        include_str!("../tests/siwe/test/verification_negative.json");
    #[cfg(feature = "ethers")]
    const VERIFICATION_EIP1271: &str = include_str!("../tests/siwe/test/eip1271.json");

    fn fields_to_message(fields: &serde_json::Value) -> anyhow::Result<Message> {
        let fields = fields.as_object().unwrap();
        Ok(Message {
            domain: fields["domain"].as_str().unwrap().try_into().unwrap(),
            address: <[u8; 20]>::from_hex(
                fields["address"]
                    .as_str()
                    .unwrap()
                    .strip_prefix("0x")
                    .unwrap(),
            )
            .unwrap(),
            statement: fields
                .get("statement")
                .map(|s| s.as_str().unwrap().try_into().unwrap()),
            uri: fields["uri"].as_str().unwrap().try_into().unwrap(),
            version: <Version as std::str::FromStr>::from_str(fields["version"].as_str().unwrap())
                .unwrap(),
            chain_id: fields["chainId"].as_u64().unwrap(),
            nonce: fields["nonce"].as_str().unwrap().try_into().unwrap(),
            issued_at: <TimeStamp as std::str::FromStr>::from_str(
                fields["issuedAt"].as_str().unwrap(),
            )?,
            expiration_time: match fields.get("expirationTime") {
                Some(e) => Some(<TimeStamp as std::str::FromStr>::from_str(
                    e.as_str().unwrap(),
                )?),
                None => None,
            },
            not_before: if let Some(not_before) = fields.get("notBefore") {
                Some(<TimeStamp as std::str::FromStr>::from_str(
                    not_before.as_str().unwrap(),
                )?)
            } else {
                None
            },
            request_id: fields
                .get("requestId")
                .map(|e| e.as_str().unwrap().to_string()),
            resources: fields
                .get("resources")
                .map(|e| {
                    e.as_array()
                        .unwrap()
                        .iter()
                        .map(|r| {
                            <UriString as std::str::FromStr>::from_str(r.as_str().unwrap()).unwrap()
                        })
                        .collect()
                })
                .unwrap_or_default(),
        })
    }

    #[test]
    fn parsing_positive() {
        let tests: serde_json::Value = serde_json::from_str(PARSING_POSITIVE).unwrap();
        for (test_name, test) in tests.as_object().unwrap() {
            print!("{} -> ", test_name);
            let parsed_message = Message::from_str(test["message"].as_str().unwrap()).unwrap();
            let fields = &test["fields"];
            let expected_message = fields_to_message(fields).unwrap();
            assert!(parsed_message == expected_message);
            println!("")
        }
    }

    #[test]
    fn parsing_negative() {
        let tests: serde_json::Value = serde_json::from_str(PARSING_NEGATIVE).unwrap();
        for (test_name, test) in tests.as_object().unwrap() {
            print!("{} -> ", test_name);
            assert!(Message::from_str(test.as_str().unwrap()).is_err());
            println!("")
        }
    }

    #[tokio::test]
    async fn verification_positive() {
        let tests: serde_json::Value = serde_json::from_str(VERIFICATION_POSITIVE).unwrap();
        for (test_name, test) in tests.as_object().unwrap() {
            print!("{} -> ", test_name);
            let fields = &test;
            let message = fields_to_message(fields).unwrap();
            let signature = <[u8; 65]>::from_hex(
                fields.as_object().unwrap()["signature"]
                    .as_str()
                    .unwrap()
                    .strip_prefix("0x")
                    .unwrap(),
            )
            .unwrap();
            let timestamp = fields
                .as_object()
                .unwrap()
                .get("time")
                .and_then(|timestamp| {
                    OffsetDateTime::parse(timestamp.as_str().unwrap(), &Rfc3339).ok()
                });
            let opts = VerificationOpts {
                timestamp,
                ..Default::default()
            };
            assert!(message.verify(&signature, &opts).await.is_ok());
            println!("")
        }
    }

    #[cfg(feature = "ethers")]
    #[tokio::test]
    async fn verification_eip1271() {
        let tests: serde_json::Value = serde_json::from_str(VERIFICATION_EIP1271).unwrap();
        for (test_name, test) in tests.as_object().unwrap() {
            print!("{} -> ", test_name);
            let message = Message::from_str(test["message"].as_str().unwrap()).unwrap();
            let signature = <Vec<u8>>::from_hex(
                test["signature"]
                    .as_str()
                    .unwrap()
                    .strip_prefix("0x")
                    .unwrap(),
            )
            .unwrap();
            let opts = VerificationOpts {
                rpc_provider: Some("https://eth.llamarpc.com".try_into().unwrap()),
                ..Default::default()
            };
            assert!(message.verify(&signature, &opts).await.is_ok());
            println!("")
        }
    }

    #[tokio::test]
    async fn verification_negative() {
        let tests: serde_json::Value = serde_json::from_str(VERIFICATION_NEGATIVE).unwrap();
        for (test_name, test) in tests.as_object().unwrap() {
            print!("{} -> ", test_name);
            let fields = &test;
            let message = fields_to_message(fields);
            let signature = <Vec<u8>>::from_hex(
                fields.as_object().unwrap()["signature"]
                    .as_str()
                    .unwrap()
                    .strip_prefix("0x")
                    .unwrap(),
            );
            let domain_binding =
                fields
                    .as_object()
                    .unwrap()
                    .get("domainBinding")
                    .and_then(|domain_binding| {
                        Authority::from_str(domain_binding.as_str().unwrap()).ok()
                    });
            let match_nonce = fields
                .as_object()
                .unwrap()
                .get("matchNonce")
                .and_then(|match_nonce| match_nonce.as_str())
                .map(|n| n.to_string());
            let timestamp = fields
                .as_object()
                .unwrap()
                .get("time")
                .and_then(|timestamp| {
                    OffsetDateTime::parse(timestamp.as_str().unwrap(), &Rfc3339).ok()
                });
            #[allow(clippy::needless_update)]
            let opts = VerificationOpts {
                domain: domain_binding,
                nonce: match_nonce,
                timestamp,
                ..Default::default()
            };
            assert!(
                message.is_err()
                    || signature.is_err()
                    || message
                        .unwrap()
                        .verify(&signature.unwrap(), &opts,)
                        .await
                        .is_err()
            );
            println!("")
        }
    }

    const VALID_CASES: &[&str] = &[
        // From the spec:
        // All caps
        "0x52908400098527886E0F7030069857D2E4169EE7",
        "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
        // All Lower
        "0xde709f2102306220921060314715629080e2fb77",
        "0x27b1fdb04752bbc536007a920d24acb045561c26",
        "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
        "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
        "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
        "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
    ];

    const INVALID_CASES: &[&str] = &[
        // From eip55 Crate:
        "0xD1220a0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
        "0xdbF03B407c01e7cD3CBea99509d93f8DDDC8C6FB",
        "0xfb6916095ca1df60bB79Ce92cE3Ea74c37c5D359",
        "0x5aAeb6053f3E94C9b9A09f33669435E7Ef1BeAed",
        // FROM SO QUESTION:
        "0xCF5609B003B2776699EEA1233F7C82D5695CC9AA",
        // From eip55 Crate Issue
        "0x000000000000000000000000000000000000dEAD",
    ];

    #[test]
    fn test_is_checksum() {
        for case in VALID_CASES {
            let c = case.trim_start_matches("0x");
            assert!(is_checksum(c))
        }

        for case in INVALID_CASES {
            let c = case.trim_start_matches("0x");
            assert!(!is_checksum(c))
        }
    }

    #[test]
    fn eip55_test() {
        // vectors from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md

        assert!(test_eip55(
            "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
            "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"
        ));
        assert!(test_eip55(
            "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
            "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"
        ));
        assert!(test_eip55(
            "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
            "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"
        ));
        assert!(test_eip55(
            "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
            "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"
        ));

        assert!(test_eip55(
            "0x52908400098527886E0F7030069857D2E4169EE7",
            "0x52908400098527886E0F7030069857D2E4169EE7",
        ));
        assert!(test_eip55(
            "0x8617e340b3d01fa5f11f306f4090fd50e238070d",
            "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
        ));
        assert!(test_eip55(
            "0xde709f2102306220921060314715629080e2fb77",
            "0xde709f2102306220921060314715629080e2fb77",
        ));
        assert!(test_eip55(
            "0x27b1fdb04752bbc536007a920d24acb045561c26",
            "0x27b1fdb04752bbc536007a920d24acb045561c26"
        ));
        assert!(test_eip55(
            "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
            "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
        ));
        assert!(test_eip55(
            "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
            "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"
        ));
        assert!(test_eip55(
            "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
            "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
        ));
        assert!(test_eip55(
            "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
            "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"
        ));
    }

    fn test_eip55(addr: &str, checksum: &str) -> bool {
        let unprefixed = addr.strip_prefix("0x").unwrap();
        eip55(&<[u8; 20]>::from_hex(unprefixed).unwrap()) == checksum
            && eip55(&<[u8; 20]>::from_hex(unprefixed.to_lowercase()).unwrap()) == checksum
            && eip55(&<[u8; 20]>::from_hex(unprefixed.to_uppercase()).unwrap()) == checksum
    }
}