Skip to content

Commit

Permalink
Upgrade uniffi version and bitcoin version.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlu committed Mar 4, 2024
1 parent d9e17c7 commit 2c58bc9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ edition = "2021"
hex = "0.4.3"
rand_core = { "version" = "0.6.4", features = ["getrandom"] }
getrandom = { version = "0.2", features = ["js"] }
uniffi = "0.23.0"
uniffi = "0.26.0"
wasm-bindgen = "0.2.87"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }
bitcoin = "0.30.1"
bitcoin = "0.31.1"
bip39 = { "version" = "2.0.0", features = ["rand"]}
ecies = { "version" = "0.2.6", default-features = false, features = ["pure"]}
lightspark-remote-signing = "=0.3.0"
Expand All @@ -29,7 +29,7 @@ bitcoin-bech32 = "0.13.0"
default = ["uniffi/cli"]

[build-dependencies]
uniffi = { version = "0.23.0", features = [ "build" ] }
uniffi = { version = "0.26.0", features = [ "build" ] }

[lib]
crate-type = ["cdylib", "staticlib"]
Expand Down
4 changes: 3 additions & 1 deletion src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl fmt::Display for CryptoError {
}
}

impl std::error::Error for CryptoError {}

pub fn sign_ecdsa(msg: Vec<u8>, private_key_bytes: Vec<u8>) -> Result<Vec<u8>, CryptoError> {
let secp = Secp256k1::new();
let sk = SecretKey::from_slice(&private_key_bytes).map_err(CryptoError::Secp256k1Error)?;
Expand Down Expand Up @@ -124,7 +126,7 @@ fn _generate_multisig_address(
builder = builder.push_opcode(all::OP_PUSHNUM_2);
builder = builder.push_opcode(all::OP_CHECKMULTISIG);

let script = builder.into_script().to_v0_p2wsh();
let script = builder.into_script().to_p2wsh();

Ok(
bitcoin_bech32::WitnessProgram::from_scriptpubkey(script.as_bytes(), network.into())
Expand Down
39 changes: 17 additions & 22 deletions src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::str::FromStr;
use std::sync::Arc;

use bitcoin::bip32::{DerivationPath, ExtendedPrivKey, ExtendedPubKey};
use bitcoin::bip32::{DerivationPath, Xpriv, Xpub};
use bitcoin::hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine};
use bitcoin::secp256k1::ecdh::SharedSecret;
use bitcoin::secp256k1::ecdsa::Signature;
Expand Down Expand Up @@ -127,8 +127,8 @@ impl InvoiceSignature {

#[wasm_bindgen]
pub struct LightsparkSigner {
master_private_key: ExtendedPrivKey,
node_private_key: ExtendedPrivKey,
master_private_key: Xpriv,
node_private_key: Xpriv,
}

#[wasm_bindgen]
Expand All @@ -139,7 +139,7 @@ impl LightsparkSigner {
Network::Testnet => bitcoin::Network::Testnet,
Network::Regtest => bitcoin::Network::Regtest,
};
let master_private_key = ExtendedPrivKey::new_master(network, seed.as_bytes().as_slice())
let master_private_key = Xpriv::new_master(network, seed.as_bytes().as_slice())
.map_err(|_| LightsparkSignerError::KeyDerivationError)?;
let secp = Secp256k1::new();
let node_key_path = DerivationPath::from_str(NODE_KEY_PATH)
Expand All @@ -163,7 +163,7 @@ impl LightsparkSigner {

pub fn get_master_public_key(&self) -> Result<String, LightsparkSignerError> {
let secp = Secp256k1::new();
let pubkey = ExtendedPubKey::from_priv(&secp, &self.master_private_key);
let pubkey = Xpub::from_priv(&secp, &self.master_private_key);
Ok(pubkey.to_string())
}

Expand All @@ -178,7 +178,7 @@ impl LightsparkSigner {
.master_private_key
.derive_priv(&secp, &path)
.map_err(|_| LightsparkSignerError::KeyDerivationError)?;
let pubkey = ExtendedPubKey::from_priv(&secp, &private_key);
let pubkey = Xpub::from_priv(&secp, &private_key);
Ok(pubkey.to_string())
}

Expand All @@ -194,7 +194,7 @@ impl LightsparkSigner {
let signing_key = self.derive_and_tweak_key(derivation_path, add_tweak, mul_tweak)?;
let signature: Signature = match is_raw {
true => {
let msg = Message::from_slice(message.as_slice())
let msg = Message::from_digest_slice(message.as_slice())
.map_err(LightsparkSignerError::Secp256k1Error)?;
secp.sign_ecdsa(&msg, &signing_key)
}
Expand Down Expand Up @@ -295,10 +295,7 @@ impl LightsparkSigner {
self.tweak_key(derived_key.private_key, add_tweak, mul_tweak)
}

fn derive_key(
&self,
derivation_path: String,
) -> Result<ExtendedPrivKey, LightsparkSignerError> {
fn derive_key(&self, derivation_path: String) -> Result<Xpriv, LightsparkSignerError> {
let secp = Secp256k1::new();
let path = DerivationPath::from_str(&derivation_path)
.map_err(|_| LightsparkSignerError::KeyDerivationError)?;
Expand All @@ -321,8 +318,8 @@ impl LightsparkSigner {
&self,
derivation_path: String,
) -> Result<String, LightsparkSignerError> {
let extended_key_string = self.derive_public_key(derivation_path)?;
let key = ExtendedPubKey::from_str(extended_key_string.as_str()).unwrap();
let extendend_key_string = self.derive_public_key(derivation_path)?;
let key = Xpub::from_str(extendend_key_string.as_str()).unwrap();
Ok(hex::encode(key.to_pub().to_bytes()))
}

Expand Down Expand Up @@ -395,7 +392,7 @@ impl LightsparkSigner {
invoice_hash: Vec<u8>,
) -> Result<Arc<InvoiceSignature>, LightsparkSignerError> {
let signing_key = self.node_private_key.private_key;
let msg = Message::from_slice(invoice_hash.as_slice())
let msg = Message::from_digest_slice(invoice_hash.as_slice())
.map_err(LightsparkSignerError::Secp256k1Error)?;
let secp = Secp256k1::new();
let sig = secp
Expand Down Expand Up @@ -433,7 +430,7 @@ impl LightsparkSigner {
invoice_hash: Vec<u8>,
) -> Result<InvoiceSignature, LightsparkSignerError> {
let signing_key = self.node_private_key.private_key;
let msg = Message::from_slice(invoice_hash.as_slice())
let msg = Message::from_digest_slice(invoice_hash.as_slice())
.map_err(LightsparkSignerError::Secp256k1Error)?;
let secp = Secp256k1::new();
let sig = secp
Expand Down Expand Up @@ -500,9 +497,7 @@ mod tests {
let xpub = signer.derive_public_key("m".to_owned()).unwrap();
assert_eq!(xpub, public_key_string);

let verification_key = ExtendedPubKey::from_str(public_key_string)
.unwrap()
.public_key;
let verification_key = Xpub::from_str(public_key_string).unwrap().public_key;

let message = b"Hello, world!";
let signature_bytes = signer
Expand All @@ -528,12 +523,12 @@ mod tests {

let signer1 = LightsparkSigner::new(&seed1, Network::Bitcoin).unwrap();
let pub1 = signer1.derive_public_key("m/0".to_owned()).unwrap();
let xpub1 = ExtendedPubKey::from_str(&pub1).unwrap();
let xpub1 = Xpub::from_str(&pub1).unwrap();
let pub1_bytes = xpub1.public_key.serialize();

let signer2 = LightsparkSigner::new(&seed2, Network::Bitcoin).unwrap();
let pub2 = signer2.derive_public_key("m/0".to_owned()).unwrap();
let xpub2 = ExtendedPubKey::from_str(&pub2).unwrap();
let xpub2 = Xpub::from_str(&pub2).unwrap();
let pub2_bytes = xpub2.public_key.serialize();

let secret_1 = signer1.ecdh(pub2_bytes.to_vec()).unwrap();
Expand Down Expand Up @@ -638,9 +633,9 @@ mod tests {
let pubkey = signer
.derive_public_key("m/3/2106220917/0".to_owned())
.unwrap();
let verification_key = ExtendedPubKey::from_str(&pubkey).unwrap().public_key;
let verification_key = Xpub::from_str(&pubkey).unwrap().public_key;

let msg = Message::from_slice(&msg).unwrap();
let msg = Message::from_digest_slice(&msg).unwrap();
let signature = Signature::from_compact(signature_bytes.as_slice()).unwrap();
let secp = Secp256k1::new();
assert!(secp
Expand Down

0 comments on commit 2c58bc9

Please sign in to comment.