29bed53d83f561f030e4966373c4403020ac6ffc
[rust-lightning] / lightning / src / offers / test_utils.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Utilities for testing BOLT 12 Offers interfaces
11
12 use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
13 use bitcoin::secp256k1::schnorr::Signature;
14 use core::convert::AsRef; 
15 use core::time::Duration;
16 use crate::blinded_path::{BlindedHop, BlindedPath};
17 use crate::sign::EntropySource;
18 use crate::ln::PaymentHash;
19 use crate::ln::features::BlindedHopFeatures;
20 use crate::offers::invoice::BlindedPayInfo;
21 use crate::offers::merkle::TaggedHash;
22
23 pub(crate) fn fail_sign<T: AsRef<TaggedHash>>(_message: &T) -> Result<Signature, ()> {
24         Err(())
25 }
26
27 pub(crate) fn payer_keys() -> KeyPair {
28         let secp_ctx = Secp256k1::new();
29         KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
30 }
31
32 pub(crate) fn payer_sign<T: AsRef<TaggedHash>>(message: &T) -> Result<Signature, ()> {
33         let secp_ctx = Secp256k1::new();
34         let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
35         Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
36 }
37
38 pub(crate) fn payer_pubkey() -> PublicKey {
39         payer_keys().public_key()
40 }
41
42 pub(crate) fn recipient_keys() -> KeyPair {
43         let secp_ctx = Secp256k1::new();
44         KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap())
45 }
46
47 pub(crate) fn recipient_sign<T: AsRef<TaggedHash>>(message: &T) -> Result<Signature, ()> {
48         let secp_ctx = Secp256k1::new();
49         let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
50         Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
51 }
52
53 pub(crate) fn recipient_pubkey() -> PublicKey {
54         recipient_keys().public_key()
55 }
56
57 pub(super) fn pubkey(byte: u8) -> PublicKey {
58         let secp_ctx = Secp256k1::new();
59         PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
60 }
61
62 pub(super) fn privkey(byte: u8) -> SecretKey {
63         SecretKey::from_slice(&[byte; 32]).unwrap()
64 }
65
66 pub(crate) fn payment_paths() -> Vec<(BlindedPayInfo, BlindedPath)> {
67         let paths = vec![
68                 BlindedPath {
69                         introduction_node_id: pubkey(40),
70                         blinding_point: pubkey(41),
71                         blinded_hops: vec![
72                                 BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
73                                 BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
74                         ],
75                 },
76                 BlindedPath {
77                         introduction_node_id: pubkey(40),
78                         blinding_point: pubkey(41),
79                         blinded_hops: vec![
80                                 BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
81                                 BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
82                         ],
83                 },
84         ];
85
86         let payinfo = vec![
87                 BlindedPayInfo {
88                         fee_base_msat: 1,
89                         fee_proportional_millionths: 1_000,
90                         cltv_expiry_delta: 42,
91                         htlc_minimum_msat: 100,
92                         htlc_maximum_msat: 1_000_000_000_000,
93                         features: BlindedHopFeatures::empty(),
94                 },
95                 BlindedPayInfo {
96                         fee_base_msat: 1,
97                         fee_proportional_millionths: 1_000,
98                         cltv_expiry_delta: 42,
99                         htlc_minimum_msat: 100,
100                         htlc_maximum_msat: 1_000_000_000_000,
101                         features: BlindedHopFeatures::empty(),
102                 },
103         ];
104
105         payinfo.into_iter().zip(paths.into_iter()).collect()
106 }
107
108 pub(crate) fn payment_hash() -> PaymentHash {
109         PaymentHash([42; 32])
110 }
111
112 pub(crate) fn now() -> Duration {
113         std::time::SystemTime::now()
114                 .duration_since(std::time::SystemTime::UNIX_EPOCH)
115                 .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH")
116 }
117
118 pub(crate) struct FixedEntropy;
119
120 impl EntropySource for FixedEntropy {
121         fn get_secure_random_bytes(&self) -> [u8; 32] {
122                 [42; 32]
123         }
124 }