Skip to content

Commit 9c5f22c

Browse files
committed
Wrap KeyPair by DerivedSigningPubkey
InvoiceBuilder is parameterized by a SigningPubkeyStrategy, either ExplicitSigningPubkey and DerivedSigningPubkey. It also holds an Option<KeyPair>, which may be None and Some for those strategies, respectively. This leads to methods for InvoiceBuilder parameterized by DerivedSigningPubkey needing to blindly unwrap the Option<KeyPair>. Instead, have DerivedSigningPubkey wrap KeyPair.
1 parent d287dcc commit 9c5f22c

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

lightning/src/offers/invoice.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ pub const SIGNATURE_TAG: &'static str = concat!("lightning", "invoice", "signatu
147147
pub struct InvoiceBuilder<'a, S: SigningPubkeyStrategy> {
148148
invreq_bytes: &'a Vec<u8>,
149149
invoice: InvoiceContents,
150-
keys: Option<KeyPair>,
151-
signing_pubkey_strategy: core::marker::PhantomData<S>,
150+
signing_pubkey_strategy: S,
152151
}
153152

154153
/// Indicates how [`Bolt12Invoice::signing_pubkey`] was set.
@@ -164,7 +163,7 @@ pub struct ExplicitSigningPubkey {}
164163
/// [`Bolt12Invoice::signing_pubkey`] was derived.
165164
///
166165
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
167-
pub struct DerivedSigningPubkey {}
166+
pub struct DerivedSigningPubkey(KeyPair);
168167

169168
impl SigningPubkeyStrategy for ExplicitSigningPubkey {}
170169
impl SigningPubkeyStrategy for DerivedSigningPubkey {}
@@ -183,7 +182,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
183182
),
184183
};
185184

186-
Self::new(&invoice_request.bytes, contents, None)
185+
Self::new(&invoice_request.bytes, contents, ExplicitSigningPubkey {})
187186
}
188187

189188
pub(super) fn for_refund(
@@ -198,7 +197,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
198197
),
199198
};
200199

201-
Self::new(&refund.bytes, contents, None)
200+
Self::new(&refund.bytes, contents, ExplicitSigningPubkey {})
202201
}
203202
}
204203

@@ -216,7 +215,7 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
216215
),
217216
};
218217

219-
Self::new(&invoice_request.bytes, contents, Some(keys))
218+
Self::new(&invoice_request.bytes, contents, DerivedSigningPubkey(keys))
220219
}
221220

222221
pub(super) fn for_refund_using_keys(
@@ -232,7 +231,7 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
232231
),
233232
};
234233

235-
Self::new(&refund.bytes, contents, Some(keys))
234+
Self::new(&refund.bytes, contents, DerivedSigningPubkey(keys))
236235
}
237236
}
238237

@@ -262,18 +261,13 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
262261
}
263262

264263
fn new(
265-
invreq_bytes: &'a Vec<u8>, contents: InvoiceContents, keys: Option<KeyPair>
264+
invreq_bytes: &'a Vec<u8>, contents: InvoiceContents, signing_pubkey_strategy: S
266265
) -> Result<Self, Bolt12SemanticError> {
267266
if contents.fields().payment_paths.is_empty() {
268267
return Err(Bolt12SemanticError::MissingPaths);
269268
}
270269

271-
Ok(Self {
272-
invreq_bytes,
273-
invoice: contents,
274-
keys,
275-
signing_pubkey_strategy: core::marker::PhantomData,
276-
})
270+
Ok(Self { invreq_bytes, invoice: contents, signing_pubkey_strategy })
277271
}
278272

279273
/// Sets the [`Bolt12Invoice::relative_expiry`] as seconds since [`Bolt12Invoice::created_at`].
@@ -359,10 +353,11 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
359353
}
360354
}
361355

362-
let InvoiceBuilder { invreq_bytes, invoice, keys, .. } = self;
356+
let InvoiceBuilder {
357+
invreq_bytes, invoice, signing_pubkey_strategy: DerivedSigningPubkey(keys)
358+
} = self;
363359
let unsigned_invoice = UnsignedBolt12Invoice::new(invreq_bytes, invoice);
364360

365-
let keys = keys.unwrap();
366361
let invoice = unsigned_invoice
367362
.sign::<_, Infallible>(
368363
|message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))

0 commit comments

Comments
 (0)