Taproot P2TR address

This commit is contained in:
Dr Maxim Orlovsky 2021-01-30 21:39:10 +01:00
parent 4bd3e1ddef
commit 5573a546ca
No known key found for this signature in database
GPG Key ID: FFC0250947E5C6F7
2 changed files with 18 additions and 0 deletions

View File

@ -40,6 +40,7 @@ use core::num::ParseIntError;
use core::str::FromStr; use core::str::FromStr;
#[cfg(feature = "std")] use std::error; #[cfg(feature = "std")] use std::error;
use secp256k1::schnorrsig;
use bech32; use bech32;
use hashes::Hash; use hashes::Hash;
use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash}; use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash};
@ -137,6 +138,8 @@ pub enum AddressType {
P2wpkh, P2wpkh,
/// pay-to-witness-script-hash /// pay-to-witness-script-hash
P2wsh, P2wsh,
/// pay-to-taproot
P2tr,
} }
impl fmt::Display for AddressType { impl fmt::Display for AddressType {
@ -146,6 +149,7 @@ impl fmt::Display for AddressType {
AddressType::P2sh => "p2sh", AddressType::P2sh => "p2sh",
AddressType::P2wpkh => "p2wpkh", AddressType::P2wpkh => "p2wpkh",
AddressType::P2wsh => "p2wsh", AddressType::P2wsh => "p2wsh",
AddressType::P2tr => "p2tr",
}) })
} }
} }
@ -158,6 +162,7 @@ impl FromStr for AddressType {
"p2sh" => Ok(AddressType::P2sh), "p2sh" => Ok(AddressType::P2sh),
"p2wpkh" => Ok(AddressType::P2wpkh), "p2wpkh" => Ok(AddressType::P2wpkh),
"p2wsh" => Ok(AddressType::P2wsh), "p2wsh" => Ok(AddressType::P2wsh),
"p2tr" => Ok(AddressType::P2tr),
_ => Err(()), _ => Err(()),
} }
} }
@ -490,6 +495,17 @@ impl Address {
} }
} }
/// Create a pay to taproot address
pub fn p2tr(taptweaked_key: schnorrsig::PublicKey, network: Network) -> Address {
Address {
network: network,
payload: Payload::WitnessProgram {
version: WitnessVersion::V1,
program: taptweaked_key.serialize().to_vec()
}
}
}
/// Get the address type of the address. /// Get the address type of the address.
/// None if unknown, non-standard or related to the future witness version. /// None if unknown, non-standard or related to the future witness version.
pub fn address_type(&self) -> Option<AddressType> { pub fn address_type(&self) -> Option<AddressType> {
@ -507,6 +523,7 @@ impl Address {
32 => Some(AddressType::P2wsh), 32 => Some(AddressType::P2wsh),
_ => None, _ => None,
}, },
WitnessVersion::V1 if prog.len() == 32 => Some(AddressType::P2tr),
_ => None, _ => None,
} }
} }

View File

@ -168,6 +168,7 @@ mod message_signing {
Some(AddressType::P2sh) => false, Some(AddressType::P2sh) => false,
Some(AddressType::P2wpkh) => false, Some(AddressType::P2wpkh) => false,
Some(AddressType::P2wsh) => false, Some(AddressType::P2wsh) => false,
Some(AddressType::P2tr) => false,
None => false, None => false,
}) })
} }