forked from cygnet3/rust-silentpayments
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
460990f
commit b7b30b2
Showing
8 changed files
with
122 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::io::Write; | ||
|
||
use crate::Result; | ||
use secp256k1::{ | ||
hashes::{sha256, Hash}, | ||
Scalar, | ||
}; | ||
|
||
pub mod receiving; | ||
pub mod sending; | ||
|
||
pub fn hash_outpoints(sending_data: &Vec<(String, u32)>) -> Result<Scalar> { | ||
let mut outpoints: Vec<Vec<u8>> = vec![]; | ||
|
||
for outpoint in sending_data { | ||
let mut bytes: Vec<u8> = hex::decode(outpoint.0.as_str())?; | ||
|
||
// txid in string format is big endian and we need little endian | ||
bytes.reverse(); | ||
|
||
bytes.extend_from_slice(&outpoint.1.to_le_bytes()); | ||
outpoints.push(bytes); | ||
} | ||
|
||
// sort outpoints | ||
outpoints.sort(); | ||
|
||
let mut engine = sha256::HashEngine::default(); | ||
|
||
for v in outpoints { | ||
engine.write_all(&v)?; | ||
} | ||
|
||
Ok(Scalar::from_be_bytes( | ||
sha256::Hash::from_engine(engine).into_inner(), | ||
)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::{utils::hash_outpoints, Result}; | ||
use secp256k1::{PublicKey, SecretKey}; | ||
|
||
pub fn recipient_calculate_tweak_data( | ||
input_pub_keys: &Vec<PublicKey>, | ||
outpoints: &Vec<(String, u32)>, | ||
) -> Result<PublicKey> { | ||
let secp = secp256k1::Secp256k1::new(); | ||
let A_sum = recipient_get_A_sum_public_keys(input_pub_keys); | ||
let outpoints_hash = hash_outpoints(outpoints)?; | ||
|
||
Ok(A_sum.mul_tweak(&secp, &outpoints_hash)?) | ||
} | ||
|
||
pub fn recipient_calculate_shared_secret( | ||
tweak_data: PublicKey, | ||
b_scan: SecretKey, | ||
) -> Result<PublicKey> { | ||
let secp = secp256k1::Secp256k1::new(); | ||
|
||
Ok(tweak_data.mul_tweak(&secp, &b_scan.into())?) | ||
} | ||
|
||
fn recipient_get_A_sum_public_keys(input: &Vec<PublicKey>) -> PublicKey { | ||
let keys_refs: &Vec<&PublicKey> = &input.iter().collect(); | ||
|
||
PublicKey::combine_keys(keys_refs).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::Result; | ||
use secp256k1::{Scalar, SecretKey}; | ||
|
||
pub fn sender_calculate_partial_secret( | ||
a_sum: SecretKey, | ||
outpoints_hash: Scalar, | ||
) -> Result<SecretKey> { | ||
Ok(a_sum.mul_tweak(&outpoints_hash)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters