use bitcoin::blockdata::script::{Builder, Script};
use bitcoin::hashes::Hash;
use bitcoin::hash_types::{WPubkeyHash, WScriptHash};
-use bitcoin::secp256k1::key::PublicKey;
+use bitcoin::secp256k1::PublicKey;
+use bitcoin::util::address::WitnessVersion;
use ln::features::InitFeatures;
use ln::msgs::DecodeError;
use util::ser::{Readable, Writeable, Writer};
use core::convert::TryFrom;
-use core::num::NonZeroU8;
use io;
/// A script pubkey for shutting down a channel as defined by [BOLT #2].
/// Generates a P2WPKH script pubkey from the given [`WPubkeyHash`].
pub fn new_p2wpkh(pubkey_hash: &WPubkeyHash) -> Self {
- Self(ShutdownScriptImpl::Bolt2(Script::new_v0_wpkh(pubkey_hash)))
+ Self(ShutdownScriptImpl::Bolt2(Script::new_v0_p2wpkh(pubkey_hash)))
}
/// Generates a P2WSH script pubkey from the given [`WScriptHash`].
pub fn new_p2wsh(script_hash: &WScriptHash) -> Self {
- Self(ShutdownScriptImpl::Bolt2(Script::new_v0_wsh(script_hash)))
+ Self(ShutdownScriptImpl::Bolt2(Script::new_v0_p2wsh(script_hash)))
}
/// Generates a witness script pubkey from the given segwit version and program.
/// # Errors
///
/// This function may return an error if `program` is invalid for the segwit `version`.
- pub fn new_witness_program(version: NonZeroU8, program: &[u8]) -> Result<Self, InvalidShutdownScript> {
+ pub fn new_witness_program(version: WitnessVersion, program: &[u8]) -> Result<Self, InvalidShutdownScript> {
let script = Builder::new()
- .push_int(version.get().into())
+ .push_int(version as i64)
.push_slice(&program)
.into_script();
Self::try_from(script)
fn into(self) -> Script {
match self.0 {
ShutdownScriptImpl::Legacy(pubkey) =>
- Script::new_v0_wpkh(&WPubkeyHash::hash(&pubkey.serialize())),
+ Script::new_v0_p2wpkh(&WPubkeyHash::hash(&pubkey.serialize())),
ShutdownScriptImpl::Bolt2(script_pubkey) => script_pubkey,
}
}
#[cfg(test)]
mod shutdown_script_tests {
use super::ShutdownScript;
- use bitcoin::bech32::u5;
use bitcoin::blockdata::opcodes;
use bitcoin::blockdata::script::{Builder, Script};
use bitcoin::secp256k1::Secp256k1;
- use bitcoin::secp256k1::key::{PublicKey, SecretKey};
+ use bitcoin::secp256k1::{PublicKey, SecretKey};
use ln::features::InitFeatures;
use core::convert::TryFrom;
- use core::num::NonZeroU8;
+ use bitcoin::util::address::WitnessVersion;
- fn pubkey() -> bitcoin::util::ecdsa::PublicKey {
+ fn pubkey() -> bitcoin::util::key::PublicKey {
let secp_ctx = Secp256k1::signing_only();
let secret_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]).unwrap();
- bitcoin::util::ecdsa::PublicKey::new(PublicKey::from_secret_key(&secp_ctx, &secret_key))
+ bitcoin::util::key::PublicKey::new(PublicKey::from_secret_key(&secp_ctx, &secret_key))
}
fn redeem_script() -> Script {
fn generates_p2wpkh_from_pubkey() {
let pubkey = pubkey();
let pubkey_hash = pubkey.wpubkey_hash().unwrap();
- let p2wpkh_script = Script::new_v0_wpkh(&pubkey_hash);
+ let p2wpkh_script = Script::new_v0_p2wpkh(&pubkey_hash);
- let shutdown_script = ShutdownScript::new_p2wpkh_from_pubkey(pubkey.key);
+ let shutdown_script = ShutdownScript::new_p2wpkh_from_pubkey(pubkey.inner);
assert!(shutdown_script.is_compatible(&InitFeatures::known()));
assert!(shutdown_script.is_compatible(&InitFeatures::known().clear_shutdown_anysegwit()));
assert_eq!(shutdown_script.into_inner(), p2wpkh_script);
#[test]
fn generates_p2wpkh_from_pubkey_hash() {
let pubkey_hash = pubkey().wpubkey_hash().unwrap();
- let p2wpkh_script = Script::new_v0_wpkh(&pubkey_hash);
+ let p2wpkh_script = Script::new_v0_p2wpkh(&pubkey_hash);
let shutdown_script = ShutdownScript::new_p2wpkh(&pubkey_hash);
assert!(shutdown_script.is_compatible(&InitFeatures::known()));
#[test]
fn generates_p2wsh_from_script_hash() {
let script_hash = redeem_script().wscript_hash();
- let p2wsh_script = Script::new_v0_wsh(&script_hash);
+ let p2wsh_script = Script::new_v0_p2wsh(&script_hash);
let shutdown_script = ShutdownScript::new_p2wsh(&script_hash);
assert!(shutdown_script.is_compatible(&InitFeatures::known()));
#[test]
fn generates_segwit_from_non_v0_witness_program() {
- let version = u5::try_from_u8(16).unwrap();
- let witness_program = Script::new_witness_program(version, &[0; 40]);
-
- let version = NonZeroU8::new(version.to_u8()).unwrap();
- let shutdown_script = ShutdownScript::new_witness_program(version, &[0; 40]).unwrap();
+ let witness_program = Script::new_witness_program(WitnessVersion::V16, &[0; 40]);
+ let shutdown_script = ShutdownScript::new_witness_program(WitnessVersion::V16, &[0; 40]).unwrap();
assert!(shutdown_script.is_compatible(&InitFeatures::known()));
assert!(!shutdown_script.is_compatible(&InitFeatures::known().clear_shutdown_anysegwit()));
assert_eq!(shutdown_script.into_inner(), witness_program);
assert!(ShutdownScript::try_from(op_return).is_err());
}
- #[test]
- fn fails_from_invalid_segwit_version() {
- let version = NonZeroU8::new(17).unwrap();
- assert!(ShutdownScript::new_witness_program(version, &[0; 40]).is_err());
- }
-
#[test]
fn fails_from_invalid_segwit_v0_witness_program() {
- let witness_program = Script::new_witness_program(u5::try_from_u8(0).unwrap(), &[0; 2]);
+ let witness_program = Script::new_witness_program(WitnessVersion::V0, &[0; 2]);
assert!(ShutdownScript::try_from(witness_program).is_err());
}
#[test]
fn fails_from_invalid_segwit_non_v0_witness_program() {
- let version = u5::try_from_u8(16).unwrap();
- let witness_program = Script::new_witness_program(version, &[0; 42]);
+ let witness_program = Script::new_witness_program(WitnessVersion::V16, &[0; 42]);
assert!(ShutdownScript::try_from(witness_program).is_err());
- let version = NonZeroU8::new(version.to_u8()).unwrap();
- assert!(ShutdownScript::new_witness_program(version, &[0; 42]).is_err());
+ assert!(ShutdownScript::new_witness_program(WitnessVersion::V16, &[0; 42]).is_err());
}
}