-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1088 from breez/signer
Implement lnurl auth signer
- Loading branch information
Showing
4 changed files
with
87 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::sync::Arc; | ||
|
||
use gl_client::bitcoin::{ | ||
hashes::{sha256, Hash, HashEngine, Hmac, HmacEngine}, | ||
secp256k1::{Message, Secp256k1}, | ||
util::bip32::{ChildNumber, ExtendedPubKey}, | ||
}; | ||
use sdk_common::prelude::{LnUrlError, LnUrlResult, LnurlAuthSigner}; | ||
|
||
use crate::node_api::NodeAPI; | ||
|
||
pub(crate) struct SdkLnurlAuthSigner { | ||
node_api: Arc<dyn NodeAPI>, | ||
} | ||
|
||
impl SdkLnurlAuthSigner { | ||
pub fn new(node_api: Arc<dyn NodeAPI>) -> Self { | ||
Self { node_api } | ||
} | ||
} | ||
|
||
impl LnurlAuthSigner for SdkLnurlAuthSigner { | ||
fn derive_bip32_pub_key(&self, derivation_path: &[ChildNumber]) -> LnUrlResult<Vec<u8>> { | ||
let xpriv = self.node_api.derive_bip32_key(derivation_path.to_vec())?; | ||
Ok(ExtendedPubKey::from_priv(&Secp256k1::new(), &xpriv) | ||
.encode() | ||
.to_vec()) | ||
} | ||
|
||
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"))?, | ||
&xpriv.private_key, | ||
); | ||
Ok(sig.serialize_der().to_vec()) | ||
} | ||
|
||
fn hmac_sha256( | ||
&self, | ||
key_derivation_path: &[ChildNumber], | ||
input: &[u8], | ||
) -> LnUrlResult<Vec<u8>> { | ||
let priv_key = self | ||
.node_api | ||
.derive_bip32_key(key_derivation_path.to_vec())?; | ||
let mut engine = HmacEngine::<sha256::Hash>::new(priv_key.encode().as_slice()); | ||
engine.input(input); | ||
Ok(Hmac::<sha256::Hash>::from_engine(engine) | ||
.as_inner() | ||
.to_vec()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod auth; | ||
pub mod pay; | ||
|
||
#[cfg(test)] | ||
|