-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
70 changed files
with
482 additions
and
485 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ members = [ | |
"dlc-sled-storage-provider", | ||
"electrs-blockchain-provider", | ||
] | ||
|
||
resolver = "2" |
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
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
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
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 |
---|---|---|
@@ -1,37 +1,84 @@ | ||
//! # | ||
use bitcoin::PublicKey as BitcoinPublicKey; | ||
use lightning::ln::chan_utils::{derive_public_key, derive_public_revocation_key}; | ||
use secp256k1_zkp::{PublicKey, Secp256k1, Signing, Verification}; | ||
use bitcoin::hashes::HashEngine; | ||
use bitcoin::hashes::{sha256::Hash as Sha256, Hash}; | ||
use secp256k1_zkp::{PublicKey, Scalar, Secp256k1, SecretKey}; | ||
|
||
use crate::error::Error; | ||
|
||
pub(crate) fn derive_bitcoin_public_key<C: Signing>( | ||
secp: &Secp256k1<C>, | ||
/// Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key) | ||
/// from the base point and the per_commitment_key. This is the public equivalent of | ||
/// derive_private_key - using only public keys to derive a public key instead of private keys. | ||
/// | ||
/// Taken from a previous version of ldk as it was refactored into something less practical to use | ||
/// externally. | ||
pub(crate) fn derive_public_key<T: secp256k1_zkp::Signing>( | ||
secp_ctx: &Secp256k1<T>, | ||
per_commitment_point: &PublicKey, | ||
base_point: &PublicKey, | ||
) -> Result<BitcoinPublicKey, Error> { | ||
let key = derive_public_key(secp, per_commitment_point, base_point) | ||
.map_err(|e| Error::InvalidParameters(format!("Invalid point was given {}", e)))?; | ||
Ok(BitcoinPublicKey { | ||
compressed: true, | ||
key, | ||
}) | ||
) -> PublicKey { | ||
let mut sha = Sha256::engine(); | ||
sha.input(&per_commitment_point.serialize()); | ||
sha.input(&base_point.serialize()); | ||
let res = Sha256::from_engine(sha).to_byte_array(); | ||
|
||
let hashkey = PublicKey::from_secret_key( | ||
secp_ctx, | ||
&SecretKey::from_slice(&res) | ||
.expect("Hashes should always be valid keys unless SHA-256 is broken"), | ||
); | ||
base_point.combine(&hashkey) | ||
.expect("Addition only fails if the tweak is the inverse of the key. This is not possible when the tweak contains the hash of the key.") | ||
} | ||
|
||
pub(crate) fn derive_bitcoin_public_revocation_key<C: Verification>( | ||
secp: &Secp256k1<C>, | ||
/// Derives a per-commitment-transaction revocation public key from its constituent parts. This is | ||
/// the public equivalend of derive_private_revocation_key - using only public keys to derive a | ||
/// public key instead of private keys. | ||
/// | ||
/// Only the cheating participant owns a valid witness to propagate a revoked | ||
/// commitment transaction, thus per_commitment_point always come from cheater | ||
/// and revocation_base_point always come from punisher, which is the broadcaster | ||
/// of the transaction spending with this key knowledge. | ||
/// | ||
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly | ||
/// generated (ie our own). | ||
/// | ||
/// Taken from a previous version of ldk as it was refactored into something less practical to use | ||
/// externally. | ||
pub fn derive_public_revocation_key<T: secp256k1_zkp::Verification>( | ||
secp_ctx: &Secp256k1<T>, | ||
per_commitment_point: &PublicKey, | ||
countersignatory_revocation_base_point: &PublicKey, | ||
) -> Result<BitcoinPublicKey, Error> { | ||
let key = derive_public_revocation_key( | ||
secp, | ||
per_commitment_point, | ||
countersignatory_revocation_base_point, | ||
) | ||
.map_err(|e| Error::InvalidParameters(format!("Could not derive revocation secret: {}", e)))?; | ||
Ok(BitcoinPublicKey { | ||
compressed: true, | ||
key, | ||
}) | ||
) -> PublicKey { | ||
let rev_append_commit_hash_key = { | ||
let mut sha = Sha256::engine(); | ||
sha.input(&countersignatory_revocation_base_point.serialize()); | ||
sha.input(&per_commitment_point.serialize()); | ||
|
||
Sha256::from_engine(sha).to_byte_array() | ||
}; | ||
let commit_append_rev_hash_key = { | ||
let mut sha = Sha256::engine(); | ||
sha.input(&per_commitment_point.serialize()); | ||
sha.input(&countersignatory_revocation_base_point.serialize()); | ||
|
||
Sha256::from_engine(sha).to_byte_array() | ||
}; | ||
|
||
let countersignatory_contrib = countersignatory_revocation_base_point | ||
.mul_tweak( | ||
secp_ctx, | ||
&Scalar::from_be_bytes(rev_append_commit_hash_key).unwrap(), | ||
) | ||
.expect( | ||
"Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs", | ||
); | ||
let broadcaster_contrib = per_commitment_point | ||
.mul_tweak( | ||
secp_ctx, | ||
&Scalar::from_be_bytes(commit_append_rev_hash_key).unwrap(), | ||
) | ||
.expect( | ||
"Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs", | ||
); | ||
countersignatory_contrib.combine(&broadcaster_contrib) | ||
.expect("Addition only fails if the tweak is the inverse of the key. This is not possible when the tweak commits to the key.") | ||
} |
Oops, something went wrong.