Skip to content

Commit ce1b060

Browse files
authored
refactor: use descriptive variable names across crates (#14)
Rename cryptic, comment-dependent, and misleading local bindings, parameters, and one struct to self-documenting names so the identifier carries the meaning a comment otherwise would. Behavior-preserving. - pop: fk->funder_key (signer), c->reconstructed (recovery), v->output (chain), d->deposit (db), mult->secs_per_unit and the verify_quote_address mint_/our_ hex-comparison locals (mint), Outcome->ExactPaymentTokens (pay), misleading ct->change_suffix in two gateway-error messages (error) - pops-core-funder: eng->hash_engine (script) - pops-core-verify: premint->pre_mint_secret and bm->blinded_message (swap_ceremony); presented/req_json/req->presented_token/requirement_json/ requirement (wasm) - pops-gateway: creq_a->encoded_challenge, charge_req->charge_requirement
1 parent 73406a6 commit ce1b060

11 files changed

Lines changed: 111 additions & 93 deletions

File tree

‎crates/pop/src/chain.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ impl Esplora {
144144
) -> Result<(u64, ScriptBuf), Box<dyn std::error::Error>> {
145145
let vouts = self.tx_vouts(txid).await?;
146146
let idx = usize::try_from(vout).map_err(|_| "vout does not fit usize")?;
147-
let v = vouts
147+
let output = vouts
148148
.get(idx)
149149
.ok_or_else(|| format!("funding tx has no vout[{vout}]"))?;
150-
let bytes = hex::decode(&v.scriptpubkey)
150+
let bytes = hex::decode(&output.scriptpubkey)
151151
.map_err(|e| format!("esplora scriptpubkey hex decode failed: {e}"))?;
152-
Ok((v.value, ScriptBuf::from_bytes(bytes)))
152+
Ok((output.value, ScriptBuf::from_bytes(bytes)))
153153
}
154154

155155
/// Lists the UTXOs currently at an address.

‎crates/pop/src/commands/mint.rs‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ fn parse_duration_secs(s: &str) -> Result<u64, Box<dyn std::error::Error>> {
629629
let n: u64 = num.parse().map_err(|_| {
630630
PopError::invalid_input(format!("invalid duration `{s}` (expected e.g. 30d, 12h, 45m)"))
631631
})?;
632-
let mult = match unit {
632+
let secs_per_unit = match unit {
633633
"d" | "" => SECS_PER_DAY,
634634
"h" => 3_600,
635635
"m" => 60,
@@ -641,7 +641,7 @@ fn parse_duration_secs(s: &str) -> Result<u64, Box<dyn std::error::Error>> {
641641
.into())
642642
}
643643
};
644-
n.checked_mul(mult)
644+
n.checked_mul(secs_per_unit)
645645
.ok_or_else(|| PopError::invalid_input(format!("duration `{s}` is too large")).into())
646646
}
647647

@@ -725,34 +725,34 @@ fn verify_quote_address(
725725
.into());
726726
}
727727

728-
if let Some(ik) = &quote.internal_key {
729-
let ours = hex::encode(c.internal_key.serialize());
730-
if !ik.eq_ignore_ascii_case(&ours) {
728+
if let Some(mint_internal_key) = &quote.internal_key {
729+
let our_internal_key = hex::encode(c.internal_key.serialize());
730+
if !mint_internal_key.eq_ignore_ascii_case(&our_internal_key) {
731731
return Err(PopError::AddressMismatch {
732-
expected: ours,
733-
got: ik.clone(),
732+
expected: our_internal_key,
733+
got: mint_internal_key.clone(),
734734
}
735735
.into());
736736
}
737737
}
738738

739-
if let Some(ls) = &quote.leaf_script {
740-
let ours = hex::encode(c.leaf_script.as_bytes());
741-
if !ls.eq_ignore_ascii_case(&ours) {
739+
if let Some(mint_leaf_script) = &quote.leaf_script {
740+
let our_leaf_script = hex::encode(c.leaf_script.as_bytes());
741+
if !mint_leaf_script.eq_ignore_ascii_case(&our_leaf_script) {
742742
return Err(PopError::AddressMismatch {
743-
expected: ours,
744-
got: ls.clone(),
743+
expected: our_leaf_script,
744+
got: mint_leaf_script.clone(),
745745
}
746746
.into());
747747
}
748748
}
749749

750-
if let Some(fp) = &quote.funder_pubkey {
751-
let ours = hex::encode(funder_xonly.serialize());
752-
if !fp.eq_ignore_ascii_case(&ours) {
750+
if let Some(mint_funder_pubkey) = &quote.funder_pubkey {
751+
let our_funder_pubkey = hex::encode(funder_xonly.serialize());
752+
if !mint_funder_pubkey.eq_ignore_ascii_case(&our_funder_pubkey) {
753753
return Err(PopError::AddressMismatch {
754-
expected: ours,
755-
got: fp.clone(),
754+
expected: our_funder_pubkey,
755+
got: mint_funder_pubkey.clone(),
756756
}
757757
.into());
758758
}

‎crates/pop/src/commands/pay.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub async fn run(
206206
.proofs(&keyset_infos)
207207
.map_err(|e| PopError::invalid_input(format!("--token proofs are unreadable: {e}")))?;
208208

209-
let Outcome {
209+
let ExactPaymentTokens {
210210
send_token,
211211
change_token,
212212
} = build_exact_payment(
@@ -337,7 +337,7 @@ fn ensure_post_swap_token_bearing(
337337
}
338338

339339
/// The two tokens produced by the exact-amount construction.
340-
struct Outcome {
340+
struct ExactPaymentTokens {
341341
/// The token worth EXACTLY the charge — this is what is presented.
342342
send_token: String,
343343
/// The leftover change token (`None` when the held token equalled the charge).
@@ -358,7 +358,7 @@ async fn build_exact_payment(
358358
token_total: u64,
359359
unit: &CurrencyUnit,
360360
keyset_infos: &[KeySetInfo],
361-
) -> Result<Outcome, Box<dyn std::error::Error>> {
361+
) -> Result<ExactPaymentTokens, Box<dyn std::error::Error>> {
362362
let mint_url_typed = MintUrl::from_str(base)
363363
.map_err(|e| PopError::invalid_input(format!("mint url `{base}` is invalid: {e}")))?;
364364

@@ -381,7 +381,7 @@ async fn build_exact_payment(
381381
send_proofs_json: Some(send_proofs_json),
382382
change_proofs_json: None,
383383
})?;
384-
return Ok(Outcome {
384+
return Ok(ExactPaymentTokens {
385385
send_token,
386386
change_token: None,
387387
});
@@ -459,7 +459,7 @@ async fn build_exact_payment(
459459
None => None,
460460
};
461461

462-
Ok(Outcome {
462+
Ok(ExactPaymentTokens {
463463
send_token,
464464
change_token,
465465
})

‎crates/pop/src/db.rs‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl Db {
231231
/// # Errors
232232
///
233233
/// Propagates SQLite errors (including a duplicate id).
234-
pub fn insert_deposit(&self, d: &Deposit) -> Result<(), Box<dyn std::error::Error>> {
234+
pub fn insert_deposit(&self, deposit: &Deposit) -> Result<(), Box<dyn std::error::Error>> {
235235
self.conn
236236
.execute(
237237
r#"INSERT INTO deposits (
@@ -244,26 +244,26 @@ impl Db {
244244
?15, ?16, ?17, ?18, ?19, ?20
245245
)"#,
246246
params![
247-
d.id,
248-
d.label,
249-
d.mint_url,
250-
d.unit,
251-
d.ts_expiry,
252-
d.amount,
253-
d.funder_index,
254-
d.funder_pubkey,
255-
d.quote_lock_pubkey,
256-
d.p_internal,
257-
d.leaf_script,
258-
d.nonce,
259-
d.mint_pubkey,
260-
d.funding_address,
261-
d.quote_id,
262-
d.state.as_str(),
263-
d.funding_txid,
264-
d.funding_vout,
265-
d.recovery_txid,
266-
d.created_at,
247+
deposit.id,
248+
deposit.label,
249+
deposit.mint_url,
250+
deposit.unit,
251+
deposit.ts_expiry,
252+
deposit.amount,
253+
deposit.funder_index,
254+
deposit.funder_pubkey,
255+
deposit.quote_lock_pubkey,
256+
deposit.p_internal,
257+
deposit.leaf_script,
258+
deposit.nonce,
259+
deposit.mint_pubkey,
260+
deposit.funding_address,
261+
deposit.quote_id,
262+
deposit.state.as_str(),
263+
deposit.funding_txid,
264+
deposit.funding_vout,
265+
deposit.recovery_txid,
266+
deposit.created_at,
267267
],
268268
)
269269
.map_err(|e| format!("failed to insert deposit: {e}"))?;

‎crates/pop/src/error.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,13 @@ impl PopError {
736736
change_token,
737737
..
738738
} => {
739-
let ct = match change_token {
739+
let change_suffix = match change_token {
740740
Some(_) => " plus a change token",
741741
None => "",
742742
};
743743
format!(
744744
"the gateway rejected the payment (HTTP {status}): {body}. The gateway did \
745-
NOT redeem, so the send token{ct} are unspent ecash — RECOVER them \
745+
NOT redeem, so the send token{change_suffix} are unspent ecash — RECOVER them \
746746
(json: `details.send_token`/`details.change_token`; human mode prints them below)"
747747
)
748748
}
@@ -751,14 +751,14 @@ impl PopError {
751751
change_token,
752752
..
753753
} => {
754-
let ct = match change_token {
754+
let change_suffix = match change_token {
755755
Some(_) => " plus a change token",
756756
None => "",
757757
};
758758
format!(
759759
"the payment retry to the gateway failed after the swap already spent the \
760760
held proofs ({reason}). The retry never reached the gateway, so the send \
761-
token{ct} are unspent ecash — RECOVER them and present the send token to \
761+
token{change_suffix} are unspent ecash — RECOVER them and present the send token to \
762762
the gateway directly; do NOT retry with the original --token (it is spent) \
763763
(json: `details.send_token`/`details.change_token`; human mode prints them below)"
764764
)

‎crates/pop/src/recovery.rs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ impl RecoveryFile {
9090
funder_derivation_path: &str,
9191
funding_outpoint: Option<&str>,
9292
) -> Self {
93-
let c = reconstruct(params);
94-
let descriptor = descriptor(&c.internal_key, params.ts_expiry, &params.funder_pubkey);
93+
let reconstructed = reconstruct(params);
94+
let descriptor = descriptor(
95+
&reconstructed.internal_key,
96+
params.ts_expiry,
97+
&params.funder_pubkey,
98+
);
9599
RecoveryFile {
96100
version: RECOVERY_VERSION.to_string(),
97101
deposit_id: deposit_id.to_string(),
@@ -105,10 +109,10 @@ impl RecoveryFile {
105109
funder_pubkey: hex::encode(params.funder_pubkey.serialize()),
106110
funder_derivation_path: funder_derivation_path.to_string(),
107111
network: params.network.to_string(),
108-
p_internal: hex::encode(c.internal_key.serialize()),
109-
leaf_script: hex::encode(c.leaf_script.as_bytes()),
112+
p_internal: hex::encode(reconstructed.internal_key.serialize()),
113+
leaf_script: hex::encode(reconstructed.leaf_script.as_bytes()),
110114
descriptor,
111-
funding_address: c.address,
115+
funding_address: reconstructed.address,
112116
funding_outpoint: funding_outpoint.map(str::to_string),
113117
recover_after_utc: utc_iso8601(params.ts_expiry),
114118
how_to_recover: format!(

‎crates/pop/src/signer.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,20 @@ impl HotKeySigner {
110110
impl Signer for HotKeySigner {
111111
fn funder_pubkey(&self, index: u32) -> Result<FunderPubkey, SignerError> {
112112
let secp = Secp256k1::new();
113-
let fk = derive_funder_key(&self.seed, self.network, index)
113+
let funder_key = derive_funder_key(&self.seed, self.network, index)
114114
.map_err(|e| SignerError::Derivation(e.to_string()))?;
115-
let compressed = fk.secret_key.public_key(&secp);
115+
let compressed = funder_key.secret_key.public_key(&secp);
116116
Ok(FunderPubkey {
117-
xonly: fk.xonly,
117+
xonly: funder_key.xonly,
118118
compressed,
119119
})
120120
}
121121

122122
fn sign(&self, sighash: TapSighash) -> Result<schnorr::Signature, SignerError> {
123123
let secp = Secp256k1::new();
124-
let fk = derive_funder_key(&self.seed, self.network, self.index)
124+
let funder_key = derive_funder_key(&self.seed, self.network, self.index)
125125
.map_err(|e| SignerError::Derivation(e.to_string()))?;
126-
let keypair = Keypair::from_secret_key(&secp, &fk.secret_key);
126+
let keypair = Keypair::from_secret_key(&secp, &funder_key.secret_key);
127127
let msg = Message::from_digest(sighash.to_byte_array());
128128
Ok(secp.sign_schnorr_no_aux_rand(&msg, &keypair))
129129
}
@@ -135,9 +135,9 @@ impl Signer for HotKeySigner {
135135
) -> Result<(), SignerError> {
136136
// Bridge the funder secret into cdk-common's key type (hex round-trip),
137137
// NUT-20-sign + self-verify in place.
138-
let fk = derive_funder_key(&self.seed, self.network, index)
138+
let funder_key = derive_funder_key(&self.seed, self.network, index)
139139
.map_err(|e| SignerError::Derivation(e.to_string()))?;
140-
let funder_secret_hex = Zeroizing::new(hex::encode(fk.secret_key.secret_bytes()));
140+
let funder_secret_hex = Zeroizing::new(hex::encode(funder_key.secret_key.secret_bytes()));
141141
let cdk_secret = cdk_common::SecretKey::from_hex(funder_secret_hex.as_str())
142142
.map_err(|e| SignerError::Signing(format!("funder secret -> cdk secret: {e}")))?;
143143
req.sign(cdk_secret.clone())

‎crates/pops-core-funder/src/script.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ pub fn compute_cm(
5757
) -> [u8; 32] {
5858
// Tagged hash: SHA256(SHA256(tag) || SHA256(tag) || msg).
5959
let tag_hash = sha256::Hash::hash(POP_COMMIT_TAG);
60-
let mut eng = sha256::Hash::engine();
61-
eng.input(tag_hash.as_ref());
62-
eng.input(tag_hash.as_ref());
63-
eng.input(mint_pubkey);
64-
eng.input(&ts_expiry.to_be_bytes());
65-
eng.input(nonce);
66-
eng.input(&funder_pubkey.serialize());
67-
sha256::Hash::from_engine(eng).to_byte_array()
60+
let mut hash_engine = sha256::Hash::engine();
61+
hash_engine.input(tag_hash.as_ref());
62+
hash_engine.input(tag_hash.as_ref());
63+
hash_engine.input(mint_pubkey);
64+
hash_engine.input(&ts_expiry.to_be_bytes());
65+
hash_engine.input(nonce);
66+
hash_engine.input(&funder_pubkey.serialize());
67+
sha256::Hash::from_engine(hash_engine).to_byte_array()
6868
}
6969

7070
/// Computes `P_internal = NUMS_H + cm·G` (`cm` as a scalar mod the curve order,

‎crates/pops-core-verify/src/swap_ceremony.rs‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn verify_swap_output_dleq(
5353
)));
5454
}
5555

56-
for (sig, premint) in signatures.iter().zip(pre_mint.secrets.iter()) {
56+
for (sig, pre_mint_secret) in signatures.iter().zip(pre_mint.secrets.iter()) {
5757
// No advertised key for this amount ⇒ the mint signed an amount it never
5858
// published a key for — reject.
5959
let key = keys.amount_key(sig.amount).ok_or_else(|| {
@@ -64,7 +64,7 @@ fn verify_swap_output_dleq(
6464
})?;
6565

6666
// STRICT: both present-but-invalid AND missing reject here.
67-
sig.verify_dleq(key, premint.blinded_message.blinded_secret)
67+
sig.verify_dleq(key, pre_mint_secret.blinded_message.blinded_secret)
6868
.map_err(|e| match e {
6969
nut12::Error::MissingDleqProof => MintClientError::SwapOutputDleqInvalid(
7070
"mint omitted the DLEQ proof on a swap-output blind signature \
@@ -405,36 +405,50 @@ mod tests {
405405
let last = outputs.len().saturating_sub(1);
406406

407407
let mut signatures = Vec::with_capacity(outputs.len());
408-
for (i, bm) in outputs.iter().enumerate() {
409-
let k = self.secret_key(bm.amount);
408+
for (i, blinded_message) in outputs.iter().enumerate() {
409+
let k = self.secret_key(blinded_message.amount);
410410
// Correct blind signature C_ = k * B_ in every mode, so the
411411
// ONLY thing under test is the DLEQ (not the unblinding).
412-
let c = sign_message(&k, &bm.blinded_secret).expect("mock sign_message");
412+
let c =
413+
sign_message(&k, &blinded_message.blinded_secret).expect("mock sign_message");
413414

414415
let attach_valid_dleq = |c: PublicKey, k: &SecretKey| -> BlindSignature {
415-
BlindSignature::new(bm.amount, c, id, &bm.blinded_secret, k.clone())
416-
.expect("mock DLEQ generation")
416+
BlindSignature::new(
417+
blinded_message.amount,
418+
c,
419+
id,
420+
&blinded_message.blinded_secret,
421+
k.clone(),
422+
)
423+
.expect("mock DLEQ generation")
417424
};
418425

419426
let sig = match self.mode {
420427
DleqMode::Valid => attach_valid_dleq(c, &k),
421428
DleqMode::Missing => BlindSignature {
422-
amount: bm.amount,
429+
amount: blinded_message.amount,
423430
keyset_id: id,
424431
c,
425432
dleq: None,
426433
},
427434
DleqMode::InvalidWrongKey => {
428435
// Correct C_, but the DLEQ is proved against a DIFFERENT
429436
// key, so it fails verification against the real key.
430-
let wrong = mint_secret_for_amount(u64::from(bm.amount) + 1000);
431-
BlindSignature::new(bm.amount, c, id, &bm.blinded_secret, wrong)
432-
.expect("mock (wrong-key) DLEQ generation")
437+
let wrong =
438+
mint_secret_for_amount(u64::from(blinded_message.amount) + 1000);
439+
BlindSignature::new(
440+
blinded_message.amount,
441+
c,
442+
id,
443+
&blinded_message.blinded_secret,
444+
wrong,
445+
)
446+
.expect("mock (wrong-key) DLEQ generation")
433447
}
434448
DleqMode::ValidButOneMissing => {
435449
if i == last {
436450
BlindSignature {
437-
amount: bm.amount,
451+
amount: blinded_message.amount,
438452
keyset_id: id,
439453
c,
440454
dleq: None,

0 commit comments

Comments
 (0)