fedimint_mintv2_client/
issuance.rs1use bitcoin_hashes::{Hash, hash160, sha256};
2use fedimint_client_module::secret::DeriveableSecretClientExt;
3use fedimint_core::core::Account;
4use fedimint_core::encoding::{Decodable, Encodable};
5use fedimint_core::secp256k1::rand::Rng;
6use fedimint_core::secp256k1::{Keypair, PublicKey, SECP256K1};
7use fedimint_derive_secret::{ChildId, DerivableSecret};
8use fedimint_mintv2_common::{Denomination, MintOutput, nonce_message};
9use tbs::{BlindedMessage, BlindedSignature, BlindingKey, blind_message, unblind_signature};
10
11use crate::{SpendableNote, thread_rng};
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
14pub struct NoteIssuanceRequest {
15 pub denomination: Denomination,
16 pub tweak: [u8; 16],
17 pub keypair: Keypair,
18 pub blinding_key: BlindingKey,
19}
20
21impl NoteIssuanceRequest {
22 pub fn new(
23 account: Account,
24 denomination: Denomination,
25 tweak: [u8; 16],
26 root_secret: &DerivableSecret,
27 ) -> Self {
28 let secret = output_secret(account, denomination, tweak, root_secret);
29
30 Self {
31 denomination,
32 tweak,
33 keypair: keypair(&secret),
34 blinding_key: blinding_key(&secret),
35 }
36 }
37
38 pub fn output(&self) -> MintOutput {
39 MintOutput::new_v0(self.denomination, self.blinded_message(), self.tweak)
40 }
41
42 pub fn finalize(&self, signature: BlindedSignature) -> SpendableNote {
43 SpendableNote {
44 denomination: self.denomination,
45 keypair: self.keypair,
46 signature: unblind_signature(self.blinding_key, signature),
47 }
48 }
49
50 pub fn blinded_message(&self) -> BlindedMessage {
51 blind_message(nonce_message(self.keypair.public_key()), self.blinding_key)
52 }
53}
54
55pub fn tweak_filter(root_secret: &DerivableSecret) -> [u8; 32] {
66 root_secret.to_random_bytes()
67}
68
69pub fn grind_tweak(root_secret: &DerivableSecret) -> [u8; 16] {
70 let filter = tweak_filter(root_secret);
71
72 loop {
73 let tweak = thread_rng().r#gen();
74
75 if check_tweak(tweak, filter) {
76 return tweak;
77 }
78 }
79}
80
81pub fn check_tweak(tweak: [u8; 16], seed: [u8; 32]) -> bool {
82 (tweak, seed)
83 .consensus_hash::<sha256::Hash>()
84 .to_byte_array()
85 .iter()
86 .take(2)
87 .all(|b| *b == 0)
88}
89
90pub fn check_nonce(secret: &OutputSecret, nonce_hash: hash160::Hash) -> bool {
93 blinded_message(secret).consensus_hash::<hash160::Hash>() == nonce_hash
94}
95
96pub struct OutputSecret(DerivableSecret);
99
100pub fn output_secret(
101 account: Account,
102 denomination: Denomination,
103 tweak: [u8; 16],
104 root: &DerivableSecret,
105) -> OutputSecret {
106 OutputSecret(
107 root.derive_account_secret(account)
108 .child_key(ChildId(u64::from(denomination.0)))
109 .tweak(&tweak),
110 )
111}
112
113fn keypair(secret: &OutputSecret) -> Keypair {
114 secret.0.clone().to_secp_key(SECP256K1)
115}
116
117pub fn nonce(secret: &OutputSecret) -> PublicKey {
118 keypair(secret).public_key()
119}
120
121fn blinding_key(secret: &OutputSecret) -> BlindingKey {
122 BlindingKey(secret.0.to_bls12_381_key())
123}
124
125pub fn blinded_message(secret: &OutputSecret) -> BlindedMessage {
126 blind_message(nonce_message(nonce(secret)), blinding_key(secret))
127}