1pub struct TestKeyPair {
9 pub priv_key: &'static str,
11 pub pub_key: &'static [u8],
13 pub pub_key_hash: &'static [u8],
15}
16
17impl TestKeyPair {
18 pub fn sign_test_message(&self) -> Vec<u8> {
20 crate::ecdsa::sign_test_message(self.priv_key)
21 }
22}
23
24pub const ALICE: TestKeyPair = TestKeyPair {
29 priv_key: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
30 pub_key: &[
31 0x03, 0xa3, 0x4b, 0x99, 0xf2, 0x2c, 0x79, 0x0c, 0x4e, 0x36, 0xb2, 0xb3,
32 0xc2, 0xc3, 0x5a, 0x36, 0xdb, 0x06, 0x22, 0x6e, 0x41, 0xc6, 0x92, 0xfc,
33 0x82, 0xb8, 0xb5, 0x6a, 0xc1, 0xc5, 0x40, 0xc5, 0xbd,
34 ],
35 pub_key_hash: &[
36 0x9a, 0x1c, 0x78, 0xa5, 0x07, 0x68, 0x9f, 0x6f, 0x54, 0xb8,
37 0x47, 0xad, 0x1c, 0xef, 0x1e, 0x61, 0x4e, 0xe2, 0x3f, 0x1e,
38 ],
39};
40
41pub const BOB: TestKeyPair = TestKeyPair {
46 priv_key: "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
47 pub_key: &[
48 0x03, 0xd6, 0xbf, 0xe1, 0x00, 0xd1, 0x60, 0x0c, 0x0d, 0x8f, 0x76, 0x95,
49 0x01, 0x67, 0x6f, 0xc7, 0x4c, 0x38, 0x09, 0x50, 0x0b, 0xd1, 0x31, 0xc8,
50 0xa5, 0x49, 0xf8, 0x8c, 0xf6, 0x16, 0xc2, 0x1f, 0x35,
51 ],
52 pub_key_hash: &[
53 0x89, 0xb4, 0x60, 0xe4, 0xe9, 0x84, 0xef, 0x49, 0x6f, 0xf0,
54 0xb1, 0x35, 0x71, 0x2f, 0x3d, 0x9b, 0x9f, 0xc8, 0x04, 0x82,
55 ],
56};
57
58pub const CHARLIE: TestKeyPair = TestKeyPair {
63 priv_key: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
64 pub_key: &[
65 0x02, 0xc6, 0xb7, 0x54, 0xb2, 0x08, 0x26, 0xeb, 0x92, 0x5e, 0x05, 0x2e,
66 0xe2, 0xc2, 0x52, 0x85, 0xb1, 0x62, 0xb5, 0x1f, 0xdc, 0xa7, 0x32, 0xbc,
67 0xf6, 0x7e, 0x39, 0xd6, 0x47, 0xfb, 0x68, 0x30, 0xae,
68 ],
69 pub_key_hash: &[
70 0x66, 0xc1, 0xd8, 0x57, 0x7d, 0x77, 0xbe, 0x82, 0xe3, 0xe0,
71 0xe6, 0xac, 0x0e, 0x14, 0x40, 0x2e, 0x3f, 0xc6, 0x7f, 0xf3,
72 ],
73};
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78 use crate::prelude::hash160;
79
80 #[test]
81 fn test_alice_pub_key_matches_priv_key() {
82 let derived = crate::ecdsa::pub_key_from_priv_key(ALICE.priv_key);
83 assert_eq!(derived, ALICE.pub_key);
84 }
85
86 #[test]
87 fn test_bob_pub_key_matches_priv_key() {
88 let derived = crate::ecdsa::pub_key_from_priv_key(BOB.priv_key);
89 assert_eq!(derived, BOB.pub_key);
90 }
91
92 #[test]
93 fn test_charlie_pub_key_matches_priv_key() {
94 let derived = crate::ecdsa::pub_key_from_priv_key(CHARLIE.priv_key);
95 assert_eq!(derived, CHARLIE.pub_key);
96 }
97
98 #[test]
99 fn test_alice_pub_key_hash() {
100 let h = hash160(ALICE.pub_key);
101 assert_eq!(h.as_slice(), ALICE.pub_key_hash);
102 }
103
104 #[test]
105 fn test_bob_pub_key_hash() {
106 let h = hash160(BOB.pub_key);
107 assert_eq!(h.as_slice(), BOB.pub_key_hash);
108 }
109
110 #[test]
111 fn test_charlie_pub_key_hash() {
112 let h = hash160(CHARLIE.pub_key);
113 assert_eq!(h.as_slice(), CHARLIE.pub_key_hash);
114 }
115
116 #[test]
117 fn test_alice_sign_and_verify() {
118 let sig = ALICE.sign_test_message();
119 assert!(crate::ecdsa::ecdsa_verify(&sig, ALICE.pub_key));
120 }
121
122 #[test]
123 fn test_bob_sign_and_verify() {
124 let sig = BOB.sign_test_message();
125 assert!(crate::ecdsa::ecdsa_verify(&sig, BOB.pub_key));
126 }
127
128 #[test]
129 fn test_cross_key_reject() {
130 let alice_sig = ALICE.sign_test_message();
131 assert!(!crate::ecdsa::ecdsa_verify(&alice_sig, BOB.pub_key));
132 }
133}