Extract calculate functions from scanning function

This commit is contained in:
cygnet 2023-07-23 14:15:45 +02:00
parent 8715e492a2
commit 3194cb2d56
2 changed files with 38 additions and 22 deletions

View File

@ -89,7 +89,7 @@ fn main() {
// todo labels // todo labels
let outputs_to_check: Vec<XOnlyPublicKey> = given let mut outputs_to_check: Vec<XOnlyPublicKey> = given
.outputs .outputs
.iter() .iter()
.map(|x| XOnlyPublicKey::from_str(x).unwrap()) .map(|x| XOnlyPublicKey::from_str(x).unwrap())
@ -104,7 +104,7 @@ fn main() {
B_spend, B_spend,
A_sum, A_sum,
outpoints_hash, outpoints_hash,
outputs_to_check, &mut outputs_to_check,
labels, labels,
); );

View File

@ -66,6 +66,38 @@ pub fn encode_silent_payment_address(
bech32::encode(hrp, data, bech32::Variant::Bech32m).unwrap() bech32::encode(hrp, data, bech32::Variant::Bech32m).unwrap()
} }
fn calculate_P_n(B_spend: &PublicKey, t_n: [u8; 32] ) -> XOnlyPublicKey {
let secp = Secp256k1::new();
let G: PublicKey = SecretKey::from_slice(&Scalar::ONE.to_be_bytes())
.unwrap()
.public_key(&secp);
let intermediate = G
.mul_tweak(&secp, &Scalar::from_be_bytes(t_n).unwrap())
.unwrap();
let P_n = intermediate.combine(&B_spend).unwrap();
let (P_n_xonly, _) = P_n.x_only_public_key();
P_n_xonly
}
fn calculate_t_n(ecdh_shared_secret: &[u8; 33], n: u32) -> [u8; 32] {
let mut bytes: Vec<u8> = Vec::new();
bytes.extend_from_slice(ecdh_shared_secret);
bytes.extend_from_slice(&ser_uint32(n));
sha256(&bytes)
}
fn calculate_ecdh_secret(A_sum: &PublicKey, b_scan: SecretKey, outpoints_hash: [u8; 32]) -> [u8; 33] {
let secp = Secp256k1::new();
let intermediate = A_sum.mul_tweak(&secp, &b_scan.into()).unwrap();
let scalar = Scalar::from_be_bytes(outpoints_hash).unwrap();
let ecdh_shared_secret = intermediate.mul_tweak(&secp, &scalar).unwrap().serialize();
ecdh_shared_secret
}
#[derive(Debug)] #[derive(Debug)]
pub struct WalletItem { pub struct WalletItem {
pub pub_key: String, pub pub_key: String,
@ -77,29 +109,13 @@ pub fn scanning(
B_spend: PublicKey, B_spend: PublicKey,
A_sum: PublicKey, A_sum: PublicKey,
outpoints_hash: [u8; 32], outpoints_hash: [u8; 32],
outputs_to_check: Vec<XOnlyPublicKey>, outputs_to_check: &mut Vec<XOnlyPublicKey>,
_labels: &HashMap<String, u32>, _labels: &HashMap<String, u32>,
) -> Vec<WalletItem> { ) -> Vec<WalletItem> {
let secp = Secp256k1::new(); let ecdh_shared_secret = calculate_ecdh_secret(&A_sum, b_scan, outpoints_hash);
let intermediate = A_sum.mul_tweak(&secp, &b_scan.into()).unwrap();
let scalar = Scalar::from_be_bytes(outpoints_hash).unwrap();
let ecdh_shared_secret = intermediate.mul_tweak(&secp, &scalar).unwrap().serialize();
let n = 0; let n = 0;
let mut bytes: Vec<u8> = Vec::new(); let t_n = calculate_t_n(&ecdh_shared_secret, n);
bytes.extend_from_slice(&ecdh_shared_secret); let P_n_xonly = calculate_P_n(&B_spend, t_n);
bytes.extend_from_slice(&ser_uint32(n));
let t_n = sha256(&bytes);
let G: PublicKey = SecretKey::from_slice(&Scalar::ONE.to_be_bytes())
.unwrap()
.public_key(&secp);
let intermediate = G
.mul_tweak(&secp, &Scalar::from_be_bytes(t_n).unwrap())
.unwrap();
let P_n = intermediate.combine(&B_spend).unwrap();
let (P_n_xonly, _) = P_n.x_only_public_key();
let mut wallet: Vec<WalletItem> = vec![]; let mut wallet: Vec<WalletItem> = vec![];
for output in outputs_to_check { for output in outputs_to_check {