Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lnurl auth signer #1088

Merged
merged 7 commits into from
Oct 20, 2024
Merged

Implement lnurl auth signer #1088

merged 7 commits into from
Oct 20, 2024

Conversation

roeierez
Copy link
Member

This PR changes the lnurl auth functionality to support signers that don't expose private keys.
Instead of passing the private key (linking key) to the lnurl module for signing the lnurl module expect a signer implementation with some basic building blocks for siginig and deriving public keys.
Also the caller now doesn't need to have a knowledge about the lnurl auth internals (constructing the linking key for example) as the lnurl module takes care of it.
This is needed in order also to support splitting the signer from the sdk implementation in breez-ask-liquid project.

@roeierez roeierez marked this pull request as ready for review September 17, 2024 19:28
let sig = Secp256k1::new().sign_ecdsa(&k1_to_sign, &linking_keys.secret_key());
let url = Url::from_str(&req_data.url).map_err(|e| LnUrlError::InvalidUri(e.to_string()))?;
let derivation_path = get_derivation_path(signer, url)?;
let sig = signer.sign_ecdsa(req_data.k1.as_bytes(), &derivation_path)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this k1 needs to be decoded from hex

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I wonder how it is working also with this code...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let url = Url::from_str(&req_data.url).map_err(|e| LnUrlError::InvalidUri(e.to_string()))?;
let derivation_path = get_derivation_path(signer, url)?;
let sig = signer.sign_ecdsa(req_data.k1.as_bytes(), &derivation_path)?;
let xpub = signer.derive_bip32_pub_key(&derivation_path)?;

// <LNURL_hostname_and_path>?<LNURL_existing_query_parameters>&sig=<hex(sign(utf8ToBytes(k1), linkingPrivKey))>&key=<hex(linkingKey)>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From LUD-04:

Suggested change
// <LNURL_hostname_and_path>?<LNURL_existing_query_parameters>&sig=<hex(sign(utf8ToBytes(k1), linkingPrivKey))>&key=<hex(linkingKey)>
// <LNURL_hostname_and_path>?<LNURL_existing_query_parameters>&sig=<hex(sign(hexToBytes(k1), linkingPrivKey))>&key=<hex(linkingKey)>

fn sign_ecdsa(&self, msg: &[u8], derivation_path: &[ChildNumber]) -> LnUrlResult<Vec<u8>> {
let xpriv = self.node_api.derive_bip32_key(derivation_path.to_vec())?;
let sig = Secp256k1::new().sign_ecdsa(
&Message::from_slice(msg).map_err(|_| LnUrlError::generic("failed to sign"))?,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&Message::from_slice(msg).map_err(|_| LnUrlError::generic("failed to sign"))?,
&Message::from_slice(msg).map_err(|_| LnUrlError::generic("Failed to sign"))?,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


use crate::node_api::NodeAPI;

pub(crate) struct SDKLnurlAuthSigner {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit about naming, Sdk is used throughout for structs and enums (correct or not, just for consistency)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

use reqwest::Url;

use crate::prelude::*;

pub trait LnurAuthSigner {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just spotted:

Suggested change
pub trait LnurAuthSigner {
pub trait LnurlAuthSigner {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Done.

Copy link
Contributor

@ok300 ok300 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked before and after the last 2 commits, but it's more readable now. Tested on stacker news.

@roeierez roeierez merged commit 387e37f into main Oct 20, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants