2022-06-29 04:05:31 +00:00
|
|
|
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Bitcoin addresses.
|
2018-08-08 21:38:50 +00:00
|
|
|
//!
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Support for ordinary base58 Bitcoin addresses and private keys.
|
2015-10-14 20:29:19 +00:00
|
|
|
//!
|
2018-09-29 21:00:37 +00:00
|
|
|
//! # Example: creating a new address from a randomly-generated key pair
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2022-11-14 17:41:56 +00:00
|
|
|
//! # #[cfg(feature = "rand-std")] {
|
|
|
|
//! use bitcoin::{Address, PublicKey, Network};
|
|
|
|
//! use bitcoin::secp256k1::{rand, Secp256k1};
|
2019-07-11 17:06:42 +00:00
|
|
|
//!
|
2021-11-05 21:58:18 +00:00
|
|
|
//! // Generate random key pair.
|
2019-08-05 19:41:07 +00:00
|
|
|
//! let s = Secp256k1::new();
|
2022-11-14 17:41:56 +00:00
|
|
|
//! let public_key = PublicKey::new(s.generate_keypair(&mut rand::thread_rng()).1);
|
2019-07-11 17:06:42 +00:00
|
|
|
//!
|
2021-11-05 21:58:18 +00:00
|
|
|
//! // Generate pay-to-pubkey-hash address.
|
2019-08-05 19:41:07 +00:00
|
|
|
//! let address = Address::p2pkh(&public_key, Network::Bitcoin);
|
2022-11-14 17:41:56 +00:00
|
|
|
//! # }
|
2018-09-29 21:00:37 +00:00
|
|
|
//! ```
|
2022-11-27 18:17:52 +00:00
|
|
|
//!
|
2022-11-14 17:41:56 +00:00
|
|
|
//! # Note: creating a new address requires the rand-std feature flag
|
2022-11-27 18:17:52 +00:00
|
|
|
//!
|
|
|
|
//! ```toml
|
2022-11-14 17:41:56 +00:00
|
|
|
//! bitcoin = { version = "...", features = ["rand-std"] }
|
2022-11-27 18:17:52 +00:00
|
|
|
//! ```
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2023-02-18 11:43:13 +00:00
|
|
|
use core::convert::{TryFrom, TryInto};
|
2021-06-09 10:40:41 +00:00
|
|
|
use core::fmt;
|
2022-12-19 15:15:20 +00:00
|
|
|
use core::marker::PhantomData;
|
2021-06-09 10:40:41 +00:00
|
|
|
use core::str::FromStr;
|
2018-03-09 17:53:20 +00:00
|
|
|
|
2019-08-13 07:58:38 +00:00
|
|
|
use bech32;
|
2023-03-22 03:09:58 +00:00
|
|
|
use hashes::{sha256, Hash, HashEngine};
|
2023-03-28 01:16:47 +00:00
|
|
|
use internals::write_err;
|
2022-08-05 03:40:26 +00:00
|
|
|
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};
|
|
|
|
|
2022-11-28 22:45:28 +00:00
|
|
|
use crate::base58;
|
2022-08-05 03:40:26 +00:00
|
|
|
use crate::blockdata::constants::{
|
|
|
|
MAX_SCRIPT_ELEMENT_SIZE, PUBKEY_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST,
|
|
|
|
SCRIPT_ADDRESS_PREFIX_MAIN, SCRIPT_ADDRESS_PREFIX_TEST,
|
|
|
|
};
|
2023-01-20 01:33:56 +00:00
|
|
|
use crate::blockdata::opcodes;
|
2022-11-15 23:22:16 +00:00
|
|
|
use crate::blockdata::opcodes::all::*;
|
2022-11-02 22:36:37 +00:00
|
|
|
use crate::blockdata::script::{
|
|
|
|
self, Instruction, PushBytes, PushBytesBuf, PushBytesErrorReport, Script, ScriptBuf,
|
|
|
|
};
|
2023-01-31 22:32:04 +00:00
|
|
|
use crate::crypto::key::{PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
|
2022-08-05 03:40:26 +00:00
|
|
|
use crate::error::ParseIntError;
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::hash_types::{PubkeyHash, ScriptHash};
|
|
|
|
use crate::network::constants::Network;
|
2022-08-05 03:40:26 +00:00
|
|
|
use crate::prelude::*;
|
2022-12-15 15:54:32 +00:00
|
|
|
use crate::taproot::TapNodeHash;
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2019-04-24 14:17:43 +00:00
|
|
|
/// Address error.
|
2021-01-30 19:50:04 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2022-05-31 04:29:50 +00:00
|
|
|
#[non_exhaustive]
|
2019-04-24 14:17:43 +00:00
|
|
|
pub enum Error {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Base58 encoding error.
|
2019-04-24 14:17:43 +00:00
|
|
|
Base58(base58::Error),
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Bech32 encoding error.
|
2019-04-24 14:17:43 +00:00
|
|
|
Bech32(bech32::Error),
|
2021-11-16 21:38:00 +00:00
|
|
|
/// The bech32 payload was empty.
|
2019-04-24 14:17:43 +00:00
|
|
|
EmptyBech32Payload,
|
2021-02-18 04:10:07 +00:00
|
|
|
/// The wrong checksum algorithm was used. See BIP-0350.
|
2021-09-09 08:19:17 +00:00
|
|
|
InvalidBech32Variant {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Bech32 variant that is required by the used Witness version.
|
2021-09-09 08:19:17 +00:00
|
|
|
expected: bech32::Variant,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// The actual Bech32 variant encoded in the address representation.
|
2022-08-05 03:40:26 +00:00
|
|
|
found: bech32::Variant,
|
2021-09-09 08:19:17 +00:00
|
|
|
},
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Script version must be 0 to 16 inclusive.
|
2019-04-24 14:17:43 +00:00
|
|
|
InvalidWitnessVersion(u8),
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Unable to parse witness version from string.
|
2021-01-30 19:50:04 +00:00
|
|
|
UnparsableWitnessVersion(ParseIntError),
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Bitcoin script opcode does not match any known witness version, the script is malformed.
|
2021-01-30 19:50:04 +00:00
|
|
|
MalformedWitnessVersion,
|
2019-04-24 14:17:43 +00:00
|
|
|
/// The witness program must be between 2 and 40 bytes in length.
|
|
|
|
InvalidWitnessProgramLength(usize),
|
|
|
|
/// A v0 witness program must be either of length 20 or 32.
|
|
|
|
InvalidSegwitV0ProgramLength(usize),
|
2020-05-23 22:20:46 +00:00
|
|
|
/// An uncompressed pubkey was used where it is not allowed.
|
|
|
|
UncompressedPubkey,
|
2021-09-15 10:46:20 +00:00
|
|
|
/// Address size more than 520 bytes is not allowed.
|
2022-06-05 14:49:26 +00:00
|
|
|
ExcessiveScriptSize,
|
|
|
|
/// Script is not a p2pkh, p2sh or witness program.
|
|
|
|
UnrecognizedScript,
|
2022-07-10 21:25:28 +00:00
|
|
|
/// Address type is either invalid or not supported in rust-bitcoin.
|
|
|
|
UnknownAddressType(String),
|
2022-12-19 15:15:20 +00:00
|
|
|
/// Address's network differs from required one.
|
|
|
|
NetworkValidation {
|
|
|
|
/// Network that was required.
|
|
|
|
required: Network,
|
|
|
|
/// Network on which the address was found to be valid.
|
2023-03-01 14:10:28 +00:00
|
|
|
found: Network,
|
|
|
|
/// The address itself
|
2022-11-02 22:36:37 +00:00
|
|
|
address: Address<NetworkUnchecked>,
|
|
|
|
},
|
2019-04-24 14:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2022-05-25 02:56:51 +00:00
|
|
|
Error::Base58(ref e) => write_err!(f, "base58 address encoding error"; e),
|
|
|
|
Error::Bech32(ref e) => write_err!(f, "bech32 address encoding error"; e),
|
2019-07-19 09:43:31 +00:00
|
|
|
Error::EmptyBech32Payload => write!(f, "the bech32 payload was empty"),
|
2021-09-09 08:19:17 +00:00
|
|
|
Error::InvalidBech32Variant { expected, found } => write!(f, "invalid bech32 checksum variant found {:?} when {:?} was expected", found, expected),
|
2019-07-19 09:43:31 +00:00
|
|
|
Error::InvalidWitnessVersion(v) => write!(f, "invalid witness script version: {}", v),
|
2022-05-25 02:56:51 +00:00
|
|
|
Error::UnparsableWitnessVersion(ref e) => write_err!(f, "incorrect format of a witness version byte"; e),
|
2021-01-30 19:50:04 +00:00
|
|
|
Error::MalformedWitnessVersion => f.write_str("bitcoin script opcode does not match any known witness version, the script is malformed"),
|
2022-01-24 00:31:39 +00:00
|
|
|
Error::InvalidWitnessProgramLength(l) => write!(f, "the witness program must be between 2 and 40 bytes in length: length={}", l),
|
|
|
|
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l),
|
|
|
|
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"),
|
2022-06-08 19:52:23 +00:00
|
|
|
Error::ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"),
|
2022-07-10 21:25:28 +00:00
|
|
|
Error::UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"),
|
|
|
|
Error::UnknownAddressType(ref s) => write!(f, "unknown address type: '{}' is either invalid or not supported in rust-bitcoin", s),
|
2023-03-01 14:10:28 +00:00
|
|
|
Error::NetworkValidation { required, found, ref address } => {
|
|
|
|
write!(f, "address ")?;
|
|
|
|
address.fmt_internal(f)?; // Using fmt_internal in order to remove the "Address<NetworkUnchecked>(..)" wrapper
|
|
|
|
write!(f, " belongs to network {} which is different from required {}", found, required)
|
|
|
|
}
|
2019-04-24 14:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-05-04 05:56:24 +00:00
|
|
|
impl std::error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
use self::Error::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Base58(e) => Some(e),
|
|
|
|
Bech32(e) => Some(e),
|
|
|
|
UnparsableWitnessVersion(e) => Some(e),
|
|
|
|
EmptyBech32Payload
|
|
|
|
| InvalidBech32Variant { .. }
|
|
|
|
| InvalidWitnessVersion(_)
|
|
|
|
| MalformedWitnessVersion
|
|
|
|
| InvalidWitnessProgramLength(_)
|
|
|
|
| InvalidSegwitV0ProgramLength(_)
|
|
|
|
| UncompressedPubkey
|
2022-06-05 14:49:26 +00:00
|
|
|
| ExcessiveScriptSize
|
2022-07-10 21:25:28 +00:00
|
|
|
| UnrecognizedScript
|
2022-12-19 15:15:20 +00:00
|
|
|
| UnknownAddressType(_)
|
|
|
|
| NetworkValidation { .. } => None,
|
2019-04-24 14:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl From<base58::Error> for Error {
|
2022-08-05 03:40:26 +00:00
|
|
|
fn from(e: base58::Error) -> Error { Error::Base58(e) }
|
2019-04-24 14:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl From<bech32::Error> for Error {
|
2022-08-05 03:40:26 +00:00
|
|
|
fn from(e: bech32::Error) -> Error { Error::Bech32(e) }
|
2019-04-24 14:17:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 20:31:49 +00:00
|
|
|
/// The different types of addresses.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2022-05-26 01:47:33 +00:00
|
|
|
#[non_exhaustive]
|
2019-07-16 20:31:49 +00:00
|
|
|
pub enum AddressType {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Pay to pubkey hash.
|
2019-07-16 20:31:49 +00:00
|
|
|
P2pkh,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Pay to script hash.
|
2019-07-16 20:31:49 +00:00
|
|
|
P2sh,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Pay to witness pubkey hash.
|
2019-07-16 20:31:49 +00:00
|
|
|
P2wpkh,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Pay to witness script hash.
|
2019-07-16 20:31:49 +00:00
|
|
|
P2wsh,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Pay to taproot.
|
2021-01-30 20:39:10 +00:00
|
|
|
P2tr,
|
2019-07-16 20:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for AddressType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.write_str(match *self {
|
|
|
|
AddressType::P2pkh => "p2pkh",
|
|
|
|
AddressType::P2sh => "p2sh",
|
|
|
|
AddressType::P2wpkh => "p2wpkh",
|
|
|
|
AddressType::P2wsh => "p2wsh",
|
2021-01-30 20:39:10 +00:00
|
|
|
AddressType::P2tr => "p2tr",
|
2019-07-16 20:31:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for AddressType {
|
2022-07-10 21:25:28 +00:00
|
|
|
type Err = Error;
|
2019-07-16 20:31:49 +00:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"p2pkh" => Ok(AddressType::P2pkh),
|
|
|
|
"p2sh" => Ok(AddressType::P2sh),
|
|
|
|
"p2wpkh" => Ok(AddressType::P2wpkh),
|
|
|
|
"p2wsh" => Ok(AddressType::P2wsh),
|
2021-01-30 20:39:10 +00:00
|
|
|
"p2tr" => Ok(AddressType::P2tr),
|
2022-07-10 21:25:28 +00:00
|
|
|
_ => Err(Error::UnknownAddressType(s.to_owned())),
|
2019-07-16 20:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 22:35:14 +00:00
|
|
|
/// Version of the witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
///
|
2021-11-18 22:35:14 +00:00
|
|
|
/// Helps limit possible versions of the witness according to the specification. If a plain `u8`
|
|
|
|
/// type was used instead it would mean that the version may be > 16, which would be incorrect.
|
|
|
|
///
|
|
|
|
/// First byte of `scriptPubkey` in transaction output for transactions starting with opcodes
|
|
|
|
/// ranging from 0 to 16 (inclusive).
|
2021-01-30 19:50:04 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum WitnessVersion {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Initial version of witness program. Used for P2WPKH and P2WPK outputs
|
2021-01-30 19:50:04 +00:00
|
|
|
V0 = 0,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Version of witness program used for Taproot P2TR outputs.
|
2021-01-30 19:50:04 +00:00
|
|
|
V1 = 1,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V2 = 2,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V3 = 3,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V4 = 4,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V5 = 5,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V6 = 6,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V7 = 7,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V8 = 8,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V9 = 9,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V10 = 10,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V11 = 11,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V12 = 12,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V13 = 13,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V14 = 14,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V15 = 15,
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Future (unsupported) version of witness program.
|
2021-01-30 19:50:04 +00:00
|
|
|
V16 = 16,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prints [`WitnessVersion`] number (from 0 to 16) as integer, without
|
|
|
|
/// any prefix or suffix.
|
|
|
|
impl fmt::Display for WitnessVersion {
|
2022-08-05 03:40:26 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", *self as u8) }
|
2021-01-30 19:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for WitnessVersion {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2022-07-26 12:50:28 +00:00
|
|
|
let version: u8 = crate::parse::int(s).map_err(Error::UnparsableWitnessVersion)?;
|
2022-05-25 06:35:34 +00:00
|
|
|
WitnessVersion::try_from(version)
|
2021-01-30 19:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WitnessVersion {
|
2022-01-19 02:41:45 +00:00
|
|
|
/// Returns integer version number representation for a given [`WitnessVersion`] value.
|
|
|
|
///
|
|
|
|
/// NB: this is not the same as an integer representation of the opcode signifying witness
|
|
|
|
/// version in bitcoin script. Thus, there is no function to directly convert witness version
|
|
|
|
/// into a byte since the conversion requires context (bitcoin script or just a version number).
|
2022-08-05 03:40:26 +00:00
|
|
|
pub fn to_num(self) -> u8 { self as u8 }
|
2021-02-18 04:10:07 +00:00
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Determines the checksum variant. See BIP-0350 for specification.
|
2021-02-18 04:10:07 +00:00
|
|
|
pub fn bech32_variant(&self) -> bech32::Variant {
|
|
|
|
match self {
|
|
|
|
WitnessVersion::V0 => bech32::Variant::Bech32,
|
|
|
|
_ => bech32::Variant::Bech32m,
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 19:50:04 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 06:35:34 +00:00
|
|
|
impl TryFrom<bech32::u5> for WitnessVersion {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
/// Converts 5-bit unsigned integer value matching single symbol from Bech32(m) address encoding
|
|
|
|
/// ([`bech32::u5`]) into [`WitnessVersion`] variant.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// Version of the Witness program.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// If the integer does not correspond to any witness version, errors with
|
|
|
|
/// [`Error::InvalidWitnessVersion`].
|
2022-08-05 03:40:26 +00:00
|
|
|
fn try_from(value: bech32::u5) -> Result<Self, Self::Error> { Self::try_from(value.to_u8()) }
|
2022-05-25 06:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<u8> for WitnessVersion {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
/// Converts an 8-bit unsigned integer value into [`WitnessVersion`] variant.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// Version of the Witness program.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// If the integer does not correspond to any witness version, errors with
|
|
|
|
/// [`Error::InvalidWitnessVersion`].
|
|
|
|
fn try_from(no: u8) -> Result<Self, Self::Error> {
|
|
|
|
use WitnessVersion::*;
|
|
|
|
|
|
|
|
Ok(match no {
|
|
|
|
0 => V0,
|
|
|
|
1 => V1,
|
|
|
|
2 => V2,
|
|
|
|
3 => V3,
|
|
|
|
4 => V4,
|
|
|
|
5 => V5,
|
|
|
|
6 => V6,
|
|
|
|
7 => V7,
|
|
|
|
8 => V8,
|
|
|
|
9 => V9,
|
|
|
|
10 => V10,
|
|
|
|
11 => V11,
|
|
|
|
12 => V12,
|
|
|
|
13 => V13,
|
|
|
|
14 => V14,
|
|
|
|
15 => V15,
|
|
|
|
16 => V16,
|
|
|
|
wrong => return Err(Error::InvalidWitnessVersion(wrong)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<opcodes::All> for WitnessVersion {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
/// Converts bitcoin script opcode into [`WitnessVersion`] variant.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// Version of the Witness program (for opcodes in range of `OP_0`..`OP_16`).
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// If the opcode does not correspond to any witness version, errors with
|
|
|
|
/// [`Error::MalformedWitnessVersion`].
|
|
|
|
fn try_from(opcode: opcodes::All) -> Result<Self, Self::Error> {
|
|
|
|
match opcode.to_u8() {
|
|
|
|
0 => Ok(WitnessVersion::V0),
|
2022-11-15 23:22:16 +00:00
|
|
|
version if version >= OP_PUSHNUM_1.to_u8() && version <= OP_PUSHNUM_16.to_u8() =>
|
2022-10-24 02:08:47 +00:00
|
|
|
WitnessVersion::try_from(version - OP_PUSHNUM_1.to_u8() + 1),
|
2022-08-05 03:40:26 +00:00
|
|
|
_ => Err(Error::MalformedWitnessVersion),
|
2022-05-25 06:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryFrom<Instruction<'a>> for WitnessVersion {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
/// Converts bitcoin script [`Instruction`] (parsed opcode) into [`WitnessVersion`] variant.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// Version of the Witness program for [`Instruction::Op`] and [`Instruction::PushBytes`] with
|
|
|
|
/// byte value within `1..=16` range.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// If the opcode does not correspond to any witness version, errors with
|
|
|
|
/// [`Error::MalformedWitnessVersion`] for the rest of opcodes.
|
|
|
|
fn try_from(instruction: Instruction) -> Result<Self, Self::Error> {
|
|
|
|
match instruction {
|
|
|
|
Instruction::Op(op) => WitnessVersion::try_from(op),
|
|
|
|
Instruction::PushBytes(bytes) if bytes.is_empty() => Ok(WitnessVersion::V0),
|
|
|
|
Instruction::PushBytes(_) => Err(Error::MalformedWitnessVersion),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 22:08:56 +00:00
|
|
|
impl From<WitnessVersion> for bech32::u5 {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Converts [`WitnessVersion`] instance into corresponding Bech32(m) u5-value ([`bech32::u5`]).
|
2021-01-30 19:50:04 +00:00
|
|
|
fn from(version: WitnessVersion) -> Self {
|
2022-06-01 22:08:56 +00:00
|
|
|
bech32::u5::try_from_u8(version.to_num()).expect("WitnessVersion must be 0..=16")
|
2021-01-30 19:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<WitnessVersion> for opcodes::All {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Converts [`WitnessVersion`] instance into corresponding Bitcoin scriptopcode (`OP_0`..`OP_16`).
|
2021-01-30 19:50:04 +00:00
|
|
|
fn from(version: WitnessVersion) -> opcodes::All {
|
|
|
|
match version {
|
2022-10-24 02:08:47 +00:00
|
|
|
WitnessVersion::V0 => OP_PUSHBYTES_0,
|
|
|
|
no => opcodes::All::from(OP_PUSHNUM_1.to_u8() + no.to_num() - 1),
|
2021-01-30 19:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// The method used to produce an address.
|
2018-08-20 09:10:21 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2023-01-20 15:26:36 +00:00
|
|
|
#[non_exhaustive]
|
2018-03-09 20:27:13 +00:00
|
|
|
pub enum Payload {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// P2PKH address.
|
2019-11-30 16:00:17 +00:00
|
|
|
PubkeyHash(PubkeyHash),
|
2021-11-16 21:38:00 +00:00
|
|
|
/// P2SH address.
|
2019-11-30 16:00:17 +00:00
|
|
|
ScriptHash(ScriptHash),
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Segwit address.
|
2023-01-19 23:30:17 +00:00
|
|
|
WitnessProgram(WitnessProgram),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Witness program as defined in BIP141.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct WitnessProgram {
|
|
|
|
/// The witness program version.
|
|
|
|
version: WitnessVersion,
|
|
|
|
/// The witness program. (Between 2 and 40 bytes)
|
2023-02-18 11:43:13 +00:00
|
|
|
program: PushBytesBuf,
|
2023-01-19 23:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WitnessProgram {
|
|
|
|
/// Creates a new witness program.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn new<P>(version: WitnessVersion, program: P) -> Result<Self, Error>
|
|
|
|
where
|
|
|
|
P: TryInto<PushBytesBuf>,
|
|
|
|
<P as TryInto<PushBytesBuf>>::Error: PushBytesErrorReport,
|
|
|
|
{
|
|
|
|
let program = program
|
|
|
|
.try_into()
|
2023-02-18 11:43:13 +00:00
|
|
|
.map_err(|error| Error::InvalidWitnessProgramLength(error.input_len()))?;
|
2023-01-19 23:30:17 +00:00
|
|
|
if program.len() < 2 || program.len() > 40 {
|
|
|
|
return Err(Error::InvalidWitnessProgramLength(program.len()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specific segwit v0 check. These addresses can never spend funds sent to them.
|
|
|
|
if version == WitnessVersion::V0 && (program.len() != 20 && program.len() != 32) {
|
|
|
|
return Err(Error::InvalidSegwitV0ProgramLength(program.len()));
|
|
|
|
}
|
|
|
|
Ok(WitnessProgram { version, program })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the witness program version.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn version(&self) -> WitnessVersion { self.version }
|
2023-01-19 23:30:17 +00:00
|
|
|
|
|
|
|
/// Returns the witness program.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn program(&self) -> &PushBytes { &self.program }
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 07:02:05 +00:00
|
|
|
impl Payload {
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Constructs a [Payload] from an output script (`scriptPubkey`).
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn from_script(script: &Script) -> Result<Payload, Error> {
|
2022-06-05 14:49:26 +00:00
|
|
|
Ok(if script.is_p2pkh() {
|
2023-01-28 22:47:24 +00:00
|
|
|
let bytes = script.as_bytes()[3..23].try_into().expect("statically 20B long");
|
|
|
|
Payload::PubkeyHash(PubkeyHash::from_byte_array(bytes))
|
2019-04-24 07:02:05 +00:00
|
|
|
} else if script.is_p2sh() {
|
2023-01-28 22:47:24 +00:00
|
|
|
let bytes = script.as_bytes()[2..22].try_into().expect("statically 20B long");
|
|
|
|
Payload::ScriptHash(ScriptHash::from_byte_array(bytes))
|
2019-04-24 07:02:05 +00:00
|
|
|
} else if script.is_witness_program() {
|
2022-07-30 12:22:18 +00:00
|
|
|
let opcode = script.first_opcode().expect("witness_version guarantees len() > 4");
|
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
let witness_program = script.as_bytes()[2..].to_vec();
|
2023-02-18 11:43:13 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
let witness_program =
|
|
|
|
WitnessProgram::new(WitnessVersion::try_from(opcode)?, witness_program)?;
|
2023-01-19 23:30:17 +00:00
|
|
|
Payload::WitnessProgram(witness_program)
|
2019-04-24 07:02:05 +00:00
|
|
|
} else {
|
2022-06-05 14:49:26 +00:00
|
|
|
return Err(Error::UnrecognizedScript);
|
2019-04-24 07:02:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates a script pubkey spending to this [Payload].
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn script_pubkey(&self) -> ScriptBuf {
|
2019-04-24 07:02:05 +00:00
|
|
|
match *self {
|
2023-01-20 01:33:56 +00:00
|
|
|
Payload::PubkeyHash(ref hash) => ScriptBuf::new_p2pkh(hash),
|
|
|
|
Payload::ScriptHash(ref hash) => ScriptBuf::new_p2sh(hash),
|
2022-11-02 22:36:37 +00:00
|
|
|
Payload::WitnessProgram(ref prog) => ScriptBuf::new_witness_program(prog),
|
2019-04-24 07:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 12:09:52 +00:00
|
|
|
|
2023-02-23 20:52:15 +00:00
|
|
|
/// Returns true if the address creates a particular script
|
|
|
|
/// This function doesn't make any allocations.
|
|
|
|
pub fn matches_script_pubkey(&self, script: &Script) -> bool {
|
|
|
|
match *self {
|
2022-11-02 22:36:37 +00:00
|
|
|
Payload::PubkeyHash(ref hash) if script.is_p2pkh() =>
|
|
|
|
&script.as_bytes()[3..23] == <PubkeyHash as AsRef<[u8; 20]>>::as_ref(hash),
|
|
|
|
Payload::ScriptHash(ref hash) if script.is_p2sh() =>
|
|
|
|
&script.as_bytes()[2..22] == <ScriptHash as AsRef<[u8; 20]>>::as_ref(hash),
|
|
|
|
Payload::WitnessProgram(ref prog) if script.is_witness_program() =>
|
|
|
|
&script.as_bytes()[2..] == prog.program.as_bytes(),
|
2023-02-23 20:52:15 +00:00
|
|
|
Payload::PubkeyHash(_) | Payload::ScriptHash(_) | Payload::WitnessProgram(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 12:09:52 +00:00
|
|
|
/// Creates a pay to (compressed) public key hash payload from a public key
|
|
|
|
#[inline]
|
2022-08-05 03:40:26 +00:00
|
|
|
pub fn p2pkh(pk: &PublicKey) -> Payload { Payload::PubkeyHash(pk.pubkey_hash()) }
|
2021-11-14 12:09:52 +00:00
|
|
|
|
|
|
|
/// Creates a pay to script hash P2SH payload from a script
|
|
|
|
#[inline]
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn p2sh(script: &Script) -> Result<Payload, Error> {
|
2021-11-14 12:09:52 +00:00
|
|
|
if script.len() > MAX_SCRIPT_ELEMENT_SIZE {
|
|
|
|
return Err(Error::ExcessiveScriptSize);
|
|
|
|
}
|
2021-12-10 21:48:10 +00:00
|
|
|
Ok(Payload::ScriptHash(script.script_hash()))
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a witness pay to public key payload from a public key
|
2022-01-08 12:59:43 +00:00
|
|
|
pub fn p2wpkh(pk: &PublicKey) -> Result<Payload, Error> {
|
2023-01-19 23:30:17 +00:00
|
|
|
let prog = WitnessProgram::new(
|
|
|
|
WitnessVersion::V0,
|
2022-11-02 22:36:37 +00:00
|
|
|
pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?,
|
2023-01-19 23:30:17 +00:00
|
|
|
)?;
|
|
|
|
Ok(Payload::WitnessProgram(prog))
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a pay to script payload that embeds a witness pay to public key
|
2022-01-08 12:59:43 +00:00
|
|
|
pub fn p2shwpkh(pk: &PublicKey) -> Result<Payload, Error> {
|
2021-11-14 12:09:52 +00:00
|
|
|
let builder = script::Builder::new()
|
|
|
|
.push_int(0)
|
2023-02-18 11:43:13 +00:00
|
|
|
.push_slice(pk.wpubkey_hash().ok_or(Error::UncompressedPubkey)?);
|
2021-11-14 12:09:52 +00:00
|
|
|
|
2021-12-10 21:48:10 +00:00
|
|
|
Ok(Payload::ScriptHash(builder.into_script().script_hash()))
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a witness pay to script hash payload.
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn p2wsh(script: &Script) -> Payload {
|
2022-11-02 22:36:37 +00:00
|
|
|
let prog = WitnessProgram::new(WitnessVersion::V0, script.wscript_hash())
|
|
|
|
.expect("wscript_hash has len 32 compatible with segwitv0");
|
2023-01-19 23:30:17 +00:00
|
|
|
Payload::WitnessProgram(prog)
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a pay to script payload that embeds a witness pay to script hash address
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn p2shwsh(script: &Script) -> Payload {
|
2022-11-02 22:36:37 +00:00
|
|
|
let ws = script::Builder::new().push_int(0).push_slice(script.wscript_hash()).into_script();
|
2021-11-14 12:09:52 +00:00
|
|
|
|
2021-12-10 21:48:10 +00:00
|
|
|
Payload::ScriptHash(ws.script_hash())
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a pay to taproot payload from untweaked key
|
|
|
|
pub fn p2tr<C: Verification>(
|
|
|
|
secp: &Secp256k1<C>,
|
|
|
|
internal_key: UntweakedPublicKey,
|
2022-12-15 15:54:32 +00:00
|
|
|
merkle_root: Option<TapNodeHash>,
|
2021-11-14 12:09:52 +00:00
|
|
|
) -> Payload {
|
|
|
|
let (output_key, _parity) = internal_key.tap_tweak(secp, merkle_root);
|
2022-11-02 22:36:37 +00:00
|
|
|
let prog = WitnessProgram::new(WitnessVersion::V1, output_key.to_inner().serialize())
|
|
|
|
.expect("taproot output key has len 32 <= 40");
|
2023-01-19 23:30:17 +00:00
|
|
|
Payload::WitnessProgram(prog)
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a pay to taproot payload from a pre-tweaked output key.
|
|
|
|
///
|
|
|
|
/// This method is not recommended for use and [Payload::p2tr()] should be used where possible.
|
|
|
|
pub fn p2tr_tweaked(output_key: TweakedPublicKey) -> Payload {
|
2022-11-02 22:36:37 +00:00
|
|
|
let prog = WitnessProgram::new(WitnessVersion::V1, output_key.to_inner().serialize())
|
|
|
|
.expect("taproot output key has len 32 <= 40");
|
2023-01-19 23:30:17 +00:00
|
|
|
Payload::WitnessProgram(prog)
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
2022-05-02 10:32:08 +00:00
|
|
|
|
2023-01-20 15:30:29 +00:00
|
|
|
/// Returns a byte slice of the inner program of the payload. If the payload
|
|
|
|
/// is a script hash or pubkey hash, a reference to the hash is returned.
|
|
|
|
fn inner_prog_as_bytes(&self) -> &[u8] {
|
2022-05-02 10:32:08 +00:00
|
|
|
match self {
|
2022-12-15 19:52:07 +00:00
|
|
|
Payload::ScriptHash(hash) => hash.as_ref(),
|
|
|
|
Payload::PubkeyHash(hash) => hash.as_ref(),
|
2023-02-18 11:43:13 +00:00
|
|
|
Payload::WitnessProgram(prog) => prog.program().as_bytes(),
|
2022-05-02 10:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A utility struct to encode an address payload with the given parameters.
|
|
|
|
/// This is a low-level utility struct. Consider using `Address` instead.
|
|
|
|
pub struct AddressEncoding<'a> {
|
|
|
|
/// The address payload to encode.
|
|
|
|
pub payload: &'a Payload,
|
|
|
|
/// base58 version byte for p2pkh payloads (e.g. 0x00 for "1..." addresses).
|
|
|
|
pub p2pkh_prefix: u8,
|
|
|
|
/// base58 version byte for p2sh payloads (e.g. 0x05 for "3..." addresses).
|
|
|
|
pub p2sh_prefix: u8,
|
|
|
|
/// hrp used in bech32 addresss (e.g. "bc" for "bc1..." addresses).
|
|
|
|
pub bech32_hrp: &'a str,
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:45:58 +00:00
|
|
|
/// Formats bech32 as upper case if alternate formatting is chosen (`{:#}`).
|
2021-11-14 12:09:52 +00:00
|
|
|
impl<'a> fmt::Display for AddressEncoding<'a> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self.payload {
|
|
|
|
Payload::PubkeyHash(hash) => {
|
|
|
|
let mut prefixed = [0; 21];
|
|
|
|
prefixed[0] = self.p2pkh_prefix;
|
|
|
|
prefixed[1..].copy_from_slice(&hash[..]);
|
2022-09-13 01:40:49 +00:00
|
|
|
base58::encode_check_to_fmt(fmt, &prefixed[..])
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
Payload::ScriptHash(hash) => {
|
|
|
|
let mut prefixed = [0; 21];
|
|
|
|
prefixed[0] = self.p2sh_prefix;
|
|
|
|
prefixed[1..].copy_from_slice(&hash[..]);
|
2022-09-13 01:40:49 +00:00
|
|
|
base58::encode_check_to_fmt(fmt, &prefixed[..])
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
2023-01-19 23:30:17 +00:00
|
|
|
Payload::WitnessProgram(witness_prog) => {
|
|
|
|
let (version, prog) = (witness_prog.version(), witness_prog.program());
|
2021-11-14 12:09:52 +00:00
|
|
|
let mut upper_writer;
|
|
|
|
let writer = if fmt.alternate() {
|
|
|
|
upper_writer = UpperWriter(fmt);
|
|
|
|
&mut upper_writer as &mut dyn fmt::Write
|
|
|
|
} else {
|
|
|
|
fmt as &mut dyn fmt::Write
|
|
|
|
};
|
|
|
|
let mut bech32_writer =
|
|
|
|
bech32::Bech32Writer::new(self.bech32_hrp, version.bech32_variant(), writer)?;
|
2023-01-19 23:30:17 +00:00
|
|
|
bech32::WriteBase32::write_u5(&mut bech32_writer, version.into())?;
|
2023-02-18 11:43:13 +00:00
|
|
|
bech32::ToBase32::write_base32(&prog.as_bytes(), &mut bech32_writer)
|
2021-11-14 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 07:02:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
mod sealed {
|
|
|
|
pub trait NetworkValidation {}
|
|
|
|
impl NetworkValidation for super::NetworkChecked {}
|
|
|
|
impl NetworkValidation for super::NetworkUnchecked {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marker of status of address's network validation. See section [*Parsing addresses*](Address#parsing-addresses)
|
|
|
|
/// on [`Address`] for details.
|
2023-01-22 08:23:10 +00:00
|
|
|
pub trait NetworkValidation: sealed::NetworkValidation {
|
|
|
|
/// Indicates whether this `NetworkValidation` is `NetworkChecked` or not.
|
|
|
|
const IS_CHECKED: bool;
|
|
|
|
}
|
2022-12-19 15:15:20 +00:00
|
|
|
|
|
|
|
/// Marker that address's network has been successfully validated. See section [*Parsing addresses*](Address#parsing-addresses)
|
|
|
|
/// on [`Address`] for details.
|
2023-01-22 08:23:10 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2022-12-19 15:15:20 +00:00
|
|
|
pub enum NetworkChecked {}
|
|
|
|
|
|
|
|
/// Marker that address's network has not yet been validated. See section [*Parsing addresses*](Address#parsing-addresses)
|
|
|
|
/// on [`Address`] for details.
|
2023-01-22 08:23:10 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2022-12-19 15:15:20 +00:00
|
|
|
pub enum NetworkUnchecked {}
|
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
impl NetworkValidation for NetworkChecked {
|
|
|
|
const IS_CHECKED: bool = true;
|
|
|
|
}
|
|
|
|
impl NetworkValidation for NetworkUnchecked {
|
|
|
|
const IS_CHECKED: bool = false;
|
|
|
|
}
|
2022-12-19 15:15:20 +00:00
|
|
|
|
2023-03-30 05:12:30 +00:00
|
|
|
/// The inner representation of an address, without the network validation tag.
|
|
|
|
///
|
|
|
|
/// An `Address` is composed of a payload and a network. This struct represents the inner
|
|
|
|
/// representation of an address without the network validation tag, which is used to ensure that
|
|
|
|
/// addresses are used only on the appropriate network.
|
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
struct AddressInner {
|
|
|
|
payload: Payload,
|
|
|
|
network: Network,
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// A Bitcoin address.
|
2022-05-28 02:50:24 +00:00
|
|
|
///
|
2022-12-19 15:15:20 +00:00
|
|
|
/// ### Parsing addresses
|
|
|
|
///
|
|
|
|
/// When parsing string as an address, one has to pay attention to the network, on which the parsed
|
|
|
|
/// address is supposed to be valid. For the purpose of this validation, `Address` has
|
|
|
|
/// [`is_valid_for_network`](Address<NetworkUnchecked>::is_valid_for_network) method. In order to provide more safety,
|
|
|
|
/// enforced by compiler, `Address` also contains a special marker type, which indicates whether network of the parsed
|
|
|
|
/// address has been checked. This marker type will prevent from calling certain functions unless the network
|
|
|
|
/// verification has been successfully completed.
|
|
|
|
///
|
|
|
|
/// The result of parsing an address is `Address<NetworkUnchecked>` suggesting that network of the parsed address
|
|
|
|
/// has not yet been verified. To perform this verification, method [`require_network`](Address<NetworkUnchecked>::require_network)
|
|
|
|
/// can be called, providing network on which the address is supposed to be valid. If the verification succeeds,
|
|
|
|
/// `Address<NetworkChecked>` is returned.
|
|
|
|
///
|
|
|
|
/// The types `Address` and `Address<NetworkChecked>` are synonymous, i. e. they can be used interchangeably.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::str::FromStr;
|
|
|
|
/// use bitcoin::{Address, Network};
|
|
|
|
/// use bitcoin::address::{NetworkUnchecked, NetworkChecked};
|
|
|
|
///
|
|
|
|
/// // variant 1
|
|
|
|
/// let address: Address<NetworkUnchecked> = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse().unwrap();
|
|
|
|
/// let address: Address<NetworkChecked> = address.require_network(Network::Bitcoin).unwrap();
|
|
|
|
///
|
|
|
|
/// // variant 2
|
|
|
|
/// let address: Address = Address::from_str("32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf").unwrap()
|
|
|
|
/// .require_network(Network::Bitcoin).unwrap();
|
|
|
|
///
|
|
|
|
/// // variant 3
|
|
|
|
/// let address: Address<NetworkChecked> = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse::<Address<_>>()
|
|
|
|
/// .unwrap().require_network(Network::Bitcoin).unwrap();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Formatting addresses
|
|
|
|
///
|
|
|
|
/// To format address into its textual representation, both `Debug` (for usage in programmer-facing,
|
|
|
|
/// debugging context) and `Display` (for user-facing output) can be used, with the following caveats:
|
|
|
|
///
|
|
|
|
/// 1. `Display` is implemented only for `Address<NetworkChecked>`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::str::FromStr;
|
|
|
|
/// # use bitcoin::address::{Address, NetworkChecked};
|
|
|
|
/// let address: Address<NetworkChecked> = Address::from_str("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM")
|
|
|
|
/// .unwrap().assume_checked();
|
|
|
|
/// assert_eq!(address.to_string(), "132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM");
|
|
|
|
/// ```
|
|
|
|
///
|
2023-01-23 02:26:15 +00:00
|
|
|
/// ```ignore
|
2022-12-19 15:15:20 +00:00
|
|
|
/// # use std::str::FromStr;
|
|
|
|
/// # use bitcoin::address::{Address, NetworkChecked};
|
|
|
|
/// let address: Address<NetworkUnchecked> = Address::from_str("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM")
|
|
|
|
/// .unwrap();
|
|
|
|
/// let s = address.to_string(); // does not compile
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// 2. `Debug` on `Address<NetworkUnchecked>` does not produce clean address but address wrapped by
|
|
|
|
/// an indicator that its network has not been checked. This is to encourage programmer to properly
|
|
|
|
/// check the network and use `Display` in user-facing context.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::str::FromStr;
|
|
|
|
/// # use bitcoin::address::{Address, NetworkUnchecked};
|
|
|
|
/// let address: Address<NetworkUnchecked> = Address::from_str("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM")
|
|
|
|
/// .unwrap();
|
|
|
|
/// assert_eq!(format!("{:?}", address), "Address<NetworkUnchecked>(132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM)");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::str::FromStr;
|
|
|
|
/// # use bitcoin::address::{Address, NetworkChecked};
|
|
|
|
/// let address: Address<NetworkChecked> = Address::from_str("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM")
|
|
|
|
/// .unwrap().assume_checked();
|
|
|
|
/// assert_eq!(format!("{:?}", address), "132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM");
|
|
|
|
/// ```
|
|
|
|
///
|
2022-05-28 02:50:24 +00:00
|
|
|
/// ### Relevant BIPs
|
|
|
|
///
|
|
|
|
/// * [BIP13 - Address Format for pay-to-script-hash](https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki)
|
|
|
|
/// * [BIP16 - Pay to Script Hash](https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki)
|
|
|
|
/// * [BIP141 - Segregated Witness (Consensus layer)](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki)
|
|
|
|
/// * [BIP142 - Address Format for Segregated Witness](https://github.com/bitcoin/bips/blob/master/bip-0142.mediawiki)
|
|
|
|
/// * [BIP341 - Taproot: SegWit version 1 spending rules](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki)
|
|
|
|
/// * [BIP350 - Bech32m format for v1+ witness addresses](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki)
|
2022-01-06 02:04:47 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2023-03-30 05:12:30 +00:00
|
|
|
///
|
|
|
|
/// The `#[repr(transparent)]` attribute is used to guarantee that the layout of the
|
|
|
|
/// `Address` struct is the same as the layout of the `AddressInner` struct. This attribute is
|
|
|
|
/// an implementation detail and users should not rely on it in their code.
|
|
|
|
///
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct Address<V = NetworkChecked>(AddressInner, PhantomData<V>)
|
2022-12-19 15:15:20 +00:00
|
|
|
where
|
2023-03-30 05:12:30 +00:00
|
|
|
V: NetworkValidation;
|
2022-12-19 15:15:20 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
struct DisplayUnchecked<'a>(&'a Address<NetworkUnchecked>);
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl fmt::Display for DisplayUnchecked<'_> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.0.fmt_internal(fmt) }
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
2022-12-19 15:15:20 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
crate::serde_utils::serde_string_serialize_impl!(Address, "a Bitcoin address");
|
|
|
|
|
2022-09-20 06:07:58 +00:00
|
|
|
#[cfg(feature = "serde")]
|
2022-12-19 15:15:20 +00:00
|
|
|
crate::serde_utils::serde_string_deserialize_impl!(Address<NetworkUnchecked>, "a Bitcoin address");
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl serde::Serialize for Address<NetworkUnchecked> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
serializer.collect_str(&DisplayUnchecked(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Methods on [`Address`] that can be called on both `Address<NetworkChecked>` and
|
|
|
|
/// `Address<NetworkUnchecked>`.
|
|
|
|
impl<V: NetworkValidation> Address<V> {
|
2023-03-30 05:12:30 +00:00
|
|
|
/// Returns a reference to the payload of this address.
|
|
|
|
pub fn payload(&self) -> &Payload { &self.0.payload }
|
|
|
|
|
|
|
|
/// Returns a reference to the network of this address.
|
|
|
|
pub fn network(&self) -> &Network { &self.0.network }
|
|
|
|
|
|
|
|
/// Returns a reference to the unchecked address, which is dangerous to use if the address
|
|
|
|
/// is invalid in the context of `NetworkUnchecked`.
|
|
|
|
pub fn as_unchecked(&self) -> &Address<NetworkUnchecked> {
|
|
|
|
unsafe { &*(self as *const Address<V> as *const Address<NetworkUnchecked>) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts and returns the network and payload components of the `Address`.
|
|
|
|
pub fn into_parts(self) -> (Network, Payload) {
|
|
|
|
let AddressInner { payload, network } = self.0;
|
|
|
|
(network, payload)
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
/// Gets the address type of the address.
|
|
|
|
///
|
|
|
|
/// This method is publicly available as [`address_type`](Address<NetworkChecked>::address_type)
|
|
|
|
/// on `Address<NetworkChecked>` but internally can be called on `Address<NetworkUnchecked>` as
|
|
|
|
/// `address_type_internal`.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// None if unknown, non-standard or related to the future witness version.
|
|
|
|
fn address_type_internal(&self) -> Option<AddressType> {
|
2023-03-30 05:12:30 +00:00
|
|
|
match self.payload() {
|
2022-12-19 15:15:20 +00:00
|
|
|
Payload::PubkeyHash(_) => Some(AddressType::P2pkh),
|
|
|
|
Payload::ScriptHash(_) => Some(AddressType::P2sh),
|
2023-01-19 23:30:17 +00:00
|
|
|
Payload::WitnessProgram(ref prog) => {
|
2022-12-19 15:15:20 +00:00
|
|
|
// BIP-141 p2wpkh or p2wsh addresses.
|
2023-01-19 23:30:17 +00:00
|
|
|
match prog.version() {
|
|
|
|
WitnessVersion::V0 => match prog.program().len() {
|
2022-12-19 15:15:20 +00:00
|
|
|
20 => Some(AddressType::P2wpkh),
|
|
|
|
32 => Some(AddressType::P2wsh),
|
2022-11-02 22:36:37 +00:00
|
|
|
_ => unreachable!(
|
|
|
|
"Address creation invariant violation: invalid program length"
|
|
|
|
),
|
2022-12-19 15:15:20 +00:00
|
|
|
},
|
2023-01-19 23:30:17 +00:00
|
|
|
WitnessVersion::V1 if prog.program().len() == 32 => Some(AddressType::P2tr),
|
2022-12-19 15:15:20 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
/// Format the address for the usage by `Debug` and `Display` implementations.
|
|
|
|
fn fmt_internal(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2023-03-30 05:12:30 +00:00
|
|
|
let p2pkh_prefix = match self.network() {
|
2022-12-19 15:15:20 +00:00
|
|
|
Network::Bitcoin => PUBKEY_ADDRESS_PREFIX_MAIN,
|
|
|
|
Network::Testnet | Network::Signet | Network::Regtest => PUBKEY_ADDRESS_PREFIX_TEST,
|
|
|
|
};
|
2023-03-30 05:12:30 +00:00
|
|
|
let p2sh_prefix = match self.network() {
|
2022-12-19 15:15:20 +00:00
|
|
|
Network::Bitcoin => SCRIPT_ADDRESS_PREFIX_MAIN,
|
|
|
|
Network::Testnet | Network::Signet | Network::Regtest => SCRIPT_ADDRESS_PREFIX_TEST,
|
|
|
|
};
|
2023-03-30 05:12:30 +00:00
|
|
|
let bech32_hrp = match self.network() {
|
2022-12-19 15:15:20 +00:00
|
|
|
Network::Bitcoin => "bc",
|
|
|
|
Network::Testnet | Network::Signet => "tb",
|
|
|
|
Network::Regtest => "bcrt",
|
|
|
|
};
|
|
|
|
let encoding =
|
2023-03-30 05:12:30 +00:00
|
|
|
AddressEncoding { payload: self.payload(), p2pkh_prefix, p2sh_prefix, bech32_hrp };
|
2022-12-19 15:15:20 +00:00
|
|
|
|
|
|
|
use fmt::Display;
|
|
|
|
|
|
|
|
encoding.fmt(fmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new address from given components, infering the network validation
|
|
|
|
/// marker type of the address.
|
|
|
|
#[inline]
|
2023-03-30 05:12:30 +00:00
|
|
|
pub fn new(network: Network, payload: Payload) -> Self {
|
|
|
|
Self(AddressInner { network, payload }, PhantomData)
|
2022-12-19 15:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Methods and functions that can be called only on `Address<NetworkChecked>`.
|
2015-10-14 20:29:19 +00:00
|
|
|
impl Address {
|
2021-11-18 22:37:34 +00:00
|
|
|
/// Creates a pay to (compressed) public key hash address from a public key.
|
|
|
|
///
|
|
|
|
/// This is the preferred non-witness type address.
|
2015-10-14 20:29:19 +00:00
|
|
|
#[inline]
|
2022-01-08 12:59:43 +00:00
|
|
|
pub fn p2pkh(pk: &PublicKey, network: Network) -> Address {
|
2022-12-19 15:15:20 +00:00
|
|
|
Address::new(network, Payload::p2pkh(pk))
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 22:37:34 +00:00
|
|
|
/// Creates a pay to script hash P2SH address from a script.
|
|
|
|
///
|
|
|
|
/// This address type was introduced with BIP16 and is the popular type to implement multi-sig
|
|
|
|
/// these days.
|
2018-03-14 03:53:03 +00:00
|
|
|
#[inline]
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn p2sh(script: &Script, network: Network) -> Result<Address, Error> {
|
2022-12-19 15:15:20 +00:00
|
|
|
Ok(Address::new(network, Payload::p2sh(script)?))
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 22:37:34 +00:00
|
|
|
/// Creates a witness pay to public key address from a public key.
|
2020-05-23 22:20:46 +00:00
|
|
|
///
|
2021-11-18 22:37:34 +00:00
|
|
|
/// This is the native segwit address type for an output redeemable with a single signature.
|
2020-05-23 22:20:46 +00:00
|
|
|
///
|
2021-11-16 21:38:00 +00:00
|
|
|
/// # Errors
|
|
|
|
/// Will only return an error if an uncompressed public key is provided.
|
2022-01-08 12:59:43 +00:00
|
|
|
pub fn p2wpkh(pk: &PublicKey, network: Network) -> Result<Address, Error> {
|
2022-12-19 15:15:20 +00:00
|
|
|
Ok(Address::new(network, Payload::p2wpkh(pk)?))
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 22:37:34 +00:00
|
|
|
/// Creates a pay to script address that embeds a witness pay to public key.
|
|
|
|
///
|
|
|
|
/// This is a segwit address type that looks familiar (as p2sh) to legacy clients.
|
2020-05-23 22:20:46 +00:00
|
|
|
///
|
2021-11-16 21:38:00 +00:00
|
|
|
/// # Errors
|
|
|
|
/// Will only return an Error if an uncompressed public key is provided.
|
2022-01-08 12:59:43 +00:00
|
|
|
pub fn p2shwpkh(pk: &PublicKey, network: Network) -> Result<Address, Error> {
|
2022-12-19 15:15:20 +00:00
|
|
|
Ok(Address::new(network, Payload::p2shwpkh(pk)?))
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Creates a witness pay to script hash address.
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn p2wsh(script: &Script, network: Network) -> Address {
|
2022-12-19 15:15:20 +00:00
|
|
|
Address::new(network, Payload::p2wsh(script))
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 22:37:34 +00:00
|
|
|
/// Creates a pay to script address that embeds a witness pay to script hash address.
|
|
|
|
///
|
|
|
|
/// This is a segwit address type that looks familiar (as p2sh) to legacy clients.
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn p2shwsh(script: &Script, network: Network) -> Address {
|
2022-12-19 15:15:20 +00:00
|
|
|
Address::new(network, Payload::p2shwsh(script))
|
2015-10-15 03:21:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Creates a pay to taproot address from an untweaked key.
|
2021-11-07 06:11:43 +00:00
|
|
|
pub fn p2tr<C: Verification>(
|
2021-11-12 21:36:31 +00:00
|
|
|
secp: &Secp256k1<C>,
|
2021-11-14 12:04:28 +00:00
|
|
|
internal_key: UntweakedPublicKey,
|
2022-12-15 15:54:32 +00:00
|
|
|
merkle_root: Option<TapNodeHash>,
|
2022-08-05 03:40:26 +00:00
|
|
|
network: Network,
|
2021-11-07 06:11:43 +00:00
|
|
|
) -> Address {
|
2022-12-19 15:15:20 +00:00
|
|
|
Address::new(network, Payload::p2tr(secp, internal_key, merkle_root))
|
2021-11-07 06:11:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Creates a pay to taproot address from a pre-tweaked output key.
|
2021-11-07 06:11:43 +00:00
|
|
|
///
|
2021-11-16 21:38:00 +00:00
|
|
|
/// This method is not recommended for use, [`Address::p2tr()`] should be used where possible.
|
2022-01-24 00:31:39 +00:00
|
|
|
pub fn p2tr_tweaked(output_key: TweakedPublicKey, network: Network) -> Address {
|
2022-12-19 15:15:20 +00:00
|
|
|
Address::new(network, Payload::p2tr_tweaked(output_key))
|
2021-01-30 20:39:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Gets the address type of the address.
|
|
|
|
///
|
|
|
|
/// # Returns
|
2021-01-30 19:50:04 +00:00
|
|
|
/// None if unknown, non-standard or related to the future witness version.
|
2022-12-19 15:15:20 +00:00
|
|
|
#[inline]
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn address_type(&self) -> Option<AddressType> { self.address_type_internal() }
|
2018-03-14 03:53:03 +00:00
|
|
|
|
2023-01-19 22:50:32 +00:00
|
|
|
/// Checks whether or not the address is following Bitcoin standardness rules when
|
2023-01-19 23:30:17 +00:00
|
|
|
/// *spending* from this address. *NOT* to be called by senders.
|
|
|
|
///
|
|
|
|
/// <details>
|
|
|
|
/// <summary>Spending Standardness</summary>
|
|
|
|
///
|
|
|
|
/// For forward compatibility, the senders must send to any [`Address`]. Receivers
|
|
|
|
/// can use this method to check whether or not they can spend from this address.
|
2023-01-19 22:50:32 +00:00
|
|
|
///
|
|
|
|
/// SegWit addresses with unassigned witness versions or non-standard program sizes are
|
|
|
|
/// considered non-standard.
|
2023-01-19 23:30:17 +00:00
|
|
|
/// </details>
|
|
|
|
///
|
2023-01-19 22:50:32 +00:00
|
|
|
pub fn is_spend_standard(&self) -> bool { self.address_type().is_some() }
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Checks whether or not the address is following Bitcoin standardness rules.
|
2019-07-16 20:31:49 +00:00
|
|
|
///
|
2021-11-16 21:38:00 +00:00
|
|
|
/// SegWit addresses with unassigned witness versions or non-standard program sizes are
|
|
|
|
/// considered non-standard.
|
2023-01-19 22:50:32 +00:00
|
|
|
#[deprecated(since = "0.30.0", note = "Use Address::is_spend_standard instead")]
|
2022-08-05 03:40:26 +00:00
|
|
|
pub fn is_standard(&self) -> bool { self.address_type().is_some() }
|
2019-07-16 20:31:49 +00:00
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Constructs an [`Address`] from an output script (`scriptPubkey`).
|
2023-01-20 01:33:56 +00:00
|
|
|
pub fn from_script(script: &Script, network: Network) -> Result<Address, Error> {
|
2022-12-19 15:15:20 +00:00
|
|
|
Ok(Address::new(network, Payload::from_script(script)?))
|
2019-04-24 07:02:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Generates a script pubkey spending to this address.
|
2023-03-30 05:12:30 +00:00
|
|
|
pub fn script_pubkey(&self) -> ScriptBuf { self.payload().script_pubkey() }
|
2021-03-22 12:58:59 +00:00
|
|
|
|
2021-04-15 08:40:57 +00:00
|
|
|
/// Creates a URI string *bitcoin:address* optimized to be encoded in QR codes.
|
|
|
|
///
|
|
|
|
/// If the address is bech32, both the schema and the address become uppercase.
|
|
|
|
/// If the address is base58, the schema is lowercase and the address is left mixed case.
|
2021-04-09 16:24:27 +00:00
|
|
|
///
|
2021-03-23 08:40:19 +00:00
|
|
|
/// Quoting BIP 173 "inside QR codes uppercase SHOULD be used, as those permit the use of
|
|
|
|
/// alphanumeric mode, which is 45% more compact than the normal byte mode."
|
2023-03-15 05:45:58 +00:00
|
|
|
///
|
|
|
|
/// Note however that despite BIP21 explicitly stating that the `bitcoin:` prefix should be
|
|
|
|
/// parsed as case-insensitive many wallets got this wrong and don't parse correctly.
|
|
|
|
/// [See compatibility table.](https://github.com/btcpayserver/btcpayserver/issues/2110)
|
|
|
|
///
|
|
|
|
/// If you want to avoid allocation you can use alternate display instead:
|
|
|
|
/// ```
|
|
|
|
/// # use core::fmt::Write;
|
|
|
|
/// # const ADDRESS: &str = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
|
|
|
|
/// # let address = ADDRESS.parse::<bitcoin::Address<_>>().unwrap().assume_checked();
|
|
|
|
/// # let mut writer = String::new();
|
|
|
|
/// # // magic trick to make error handling look better
|
|
|
|
/// # (|| -> Result<(), core::fmt::Error> {
|
|
|
|
///
|
|
|
|
/// write!(writer, "{:#}", address)?;
|
|
|
|
///
|
|
|
|
/// # Ok(())
|
|
|
|
/// # })().unwrap();
|
|
|
|
/// # assert_eq!(writer, ADDRESS);
|
|
|
|
/// ```
|
2021-04-15 08:40:57 +00:00
|
|
|
pub fn to_qr_uri(&self) -> String {
|
2023-03-30 05:12:30 +00:00
|
|
|
let schema = match self.payload() {
|
2021-04-15 08:40:57 +00:00
|
|
|
Payload::WitnessProgram { .. } => "BITCOIN",
|
|
|
|
_ => "bitcoin",
|
|
|
|
};
|
|
|
|
format!("{}:{:#}", schema, self)
|
2021-03-22 12:58:59 +00:00
|
|
|
}
|
2021-06-14 18:01:22 +00:00
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
/// Returns true if the given pubkey is directly related to the address payload.
|
|
|
|
///
|
|
|
|
/// This is determined by directly comparing the address payload with either the
|
|
|
|
/// hash of the given public key or the segwit redeem hash generated from the
|
|
|
|
/// given key. For taproot addresses, the supplied key is assumed to be tweaked
|
|
|
|
pub fn is_related_to_pubkey(&self, pubkey: &PublicKey) -> bool {
|
|
|
|
let pubkey_hash = pubkey.pubkey_hash();
|
2023-03-30 05:12:30 +00:00
|
|
|
let payload = self.payload().inner_prog_as_bytes();
|
2022-12-19 15:15:20 +00:00
|
|
|
let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner);
|
|
|
|
|
2023-01-28 22:47:24 +00:00
|
|
|
(*pubkey_hash.as_byte_array() == *payload)
|
2022-12-19 15:15:20 +00:00
|
|
|
|| (xonly_pubkey.serialize() == *payload)
|
2023-01-28 22:47:24 +00:00
|
|
|
|| (*segwit_redeem_hash(&pubkey_hash).as_byte_array() == *payload)
|
2022-12-19 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the supplied xonly public key can be used to derive the address.
|
|
|
|
///
|
|
|
|
/// This will only work for Taproot addresses. The Public Key is
|
|
|
|
/// assumed to have already been tweaked.
|
|
|
|
pub fn is_related_to_xonly_pubkey(&self, xonly_pubkey: &XOnlyPublicKey) -> bool {
|
2023-03-30 05:12:30 +00:00
|
|
|
let payload = self.payload().inner_prog_as_bytes();
|
2022-12-19 15:15:20 +00:00
|
|
|
payload == xonly_pubkey.serialize()
|
|
|
|
}
|
2023-02-23 20:52:15 +00:00
|
|
|
|
|
|
|
/// Returns true if the address creates a particular script
|
|
|
|
/// This function doesn't make any allocations.
|
|
|
|
pub fn matches_script_pubkey(&self, script_pubkey: &Script) -> bool {
|
2023-03-30 05:12:30 +00:00
|
|
|
self.payload().matches_script_pubkey(script_pubkey)
|
2023-02-23 20:52:15 +00:00
|
|
|
}
|
2022-12-19 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Methods that can be called only on `Address<NetworkUnchecked>`.
|
|
|
|
impl Address<NetworkUnchecked> {
|
2023-03-30 05:12:30 +00:00
|
|
|
/// Returns a reference to the checked address.
|
|
|
|
/// This function is dangerous in case the address is not a valid checked address.
|
|
|
|
pub fn assume_checked_ref(&self) -> &Address {
|
|
|
|
unsafe { &*(self as *const Address<NetworkUnchecked> as *const Address) }
|
|
|
|
}
|
2021-06-14 18:01:22 +00:00
|
|
|
/// Parsed addresses do not always have *one* network. The problem is that legacy testnet,
|
|
|
|
/// regtest and signet addresse use the same prefix instead of multiple different ones. When
|
|
|
|
/// parsing, such addresses are always assumed to be testnet addresses (the same is true for
|
|
|
|
/// bech32 signet addresses). So if one wants to check if an address belongs to a certain
|
|
|
|
/// network a simple comparison is not enough anymore. Instead this function can be used.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use bitcoin::{Address, Network};
|
2022-12-19 15:15:20 +00:00
|
|
|
/// use bitcoin::address::NetworkUnchecked;
|
2021-06-14 18:01:22 +00:00
|
|
|
///
|
2022-12-19 15:15:20 +00:00
|
|
|
/// let address: Address<NetworkUnchecked> = "2N83imGV3gPwBzKJQvWJ7cRUY2SpUyU6A5e".parse().unwrap();
|
2021-06-14 18:01:22 +00:00
|
|
|
/// assert!(address.is_valid_for_network(Network::Testnet));
|
|
|
|
/// assert!(address.is_valid_for_network(Network::Regtest));
|
|
|
|
/// assert!(address.is_valid_for_network(Network::Signet));
|
|
|
|
///
|
|
|
|
/// assert_eq!(address.is_valid_for_network(Network::Bitcoin), false);
|
|
|
|
///
|
2022-12-19 15:15:20 +00:00
|
|
|
/// let address: Address<NetworkUnchecked> = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse().unwrap();
|
2021-06-14 18:01:22 +00:00
|
|
|
/// assert!(address.is_valid_for_network(Network::Bitcoin));
|
|
|
|
/// assert_eq!(address.is_valid_for_network(Network::Testnet), false);
|
|
|
|
/// ```
|
|
|
|
pub fn is_valid_for_network(&self, network: Network) -> bool {
|
2023-04-02 14:25:09 +00:00
|
|
|
let is_legacy = matches!(
|
|
|
|
self.address_type_internal(),
|
|
|
|
Some(AddressType::P2pkh) | Some(AddressType::P2sh)
|
|
|
|
);
|
2021-06-14 18:01:22 +00:00
|
|
|
|
2023-03-30 05:12:30 +00:00
|
|
|
match (self.network(), network) {
|
|
|
|
(a, b) if *a == b => true,
|
2021-06-14 18:01:22 +00:00
|
|
|
(Network::Bitcoin, _) | (_, Network::Bitcoin) => false,
|
|
|
|
(Network::Regtest, _) | (_, Network::Regtest) if !is_legacy => false,
|
2022-08-05 03:40:26 +00:00
|
|
|
(Network::Testnet, _) | (Network::Regtest, _) | (Network::Signet, _) => true,
|
2021-06-14 18:01:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 19:12:16 +00:00
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
/// Checks whether network of this address is as required.
|
2022-02-20 19:12:16 +00:00
|
|
|
///
|
2022-12-19 15:15:20 +00:00
|
|
|
/// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses)
|
|
|
|
/// on [`Address`].
|
|
|
|
#[inline]
|
|
|
|
pub fn require_network(self, required: Network) -> Result<Address, Error> {
|
|
|
|
if self.is_valid_for_network(required) {
|
|
|
|
Ok(self.assume_checked())
|
|
|
|
} else {
|
2023-03-30 05:12:30 +00:00
|
|
|
Err(Error::NetworkValidation { found: *self.network(), required, address: self })
|
2022-12-19 15:15:20 +00:00
|
|
|
}
|
2022-02-20 19:12:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
/// Marks, without any additional checks, network of this address as checked.
|
2022-02-20 19:12:16 +00:00
|
|
|
///
|
2022-12-19 15:15:20 +00:00
|
|
|
/// Improper use of this method may lead to loss of funds. Reader will most likely prefer
|
|
|
|
/// [`require_network`](Address<NetworkUnchecked>::require_network) as a safe variant.
|
|
|
|
/// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses)
|
|
|
|
/// on [`Address`].
|
|
|
|
#[inline]
|
2023-03-30 05:12:30 +00:00
|
|
|
pub fn assume_checked(self) -> Address {
|
|
|
|
let (network, payload) = self.into_parts();
|
|
|
|
Address::new(network, payload)
|
|
|
|
}
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 06:50:11 +00:00
|
|
|
// For NetworkUnchecked , it compare Addresses and if network and payload matches then return true.
|
|
|
|
impl PartialEq<Address<NetworkUnchecked>> for Address {
|
|
|
|
fn eq(&self, other: &Address<NetworkUnchecked>) -> bool {
|
2023-03-30 05:12:30 +00:00
|
|
|
self.network() == other.network() && self.payload() == other.payload()
|
2023-03-28 06:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<Address> for Address<NetworkUnchecked> {
|
|
|
|
fn eq(&self, other: &Address) -> bool { other == self }
|
|
|
|
}
|
|
|
|
|
2023-01-25 23:28:40 +00:00
|
|
|
impl From<Address> for script::ScriptBuf {
|
2022-11-02 22:36:37 +00:00
|
|
|
fn from(a: Address) -> Self { a.script_pubkey() }
|
2023-01-25 23:28:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 08:40:57 +00:00
|
|
|
// Alternate formatting `{:#}` is used to return uppercase version of bech32 addresses which should
|
2021-11-16 21:38:00 +00:00
|
|
|
// be used in QR codes, see [`Address::to_qr_uri`].
|
2021-03-22 12:42:32 +00:00
|
|
|
impl fmt::Display for Address {
|
2022-12-19 15:15:20 +00:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.fmt_internal(fmt) }
|
|
|
|
}
|
|
|
|
|
2023-01-22 08:23:10 +00:00
|
|
|
impl<V: NetworkValidation> fmt::Debug for Address<V> {
|
2022-12-19 15:15:20 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-01-22 08:23:10 +00:00
|
|
|
if V::IS_CHECKED {
|
|
|
|
self.fmt_internal(f)
|
|
|
|
} else {
|
|
|
|
write!(f, "Address<NetworkUnchecked>(")?;
|
|
|
|
self.fmt_internal(f)?;
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:58:35 +00:00
|
|
|
struct UpperWriter<W: fmt::Write>(W);
|
2021-04-07 14:56:54 +00:00
|
|
|
|
2021-04-09 15:58:35 +00:00
|
|
|
impl<W: fmt::Write> fmt::Write for UpperWriter<W> {
|
2021-04-07 14:56:54 +00:00
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
2021-04-09 15:58:35 +00:00
|
|
|
for c in s.chars() {
|
|
|
|
self.0.write_char(c.to_ascii_uppercase())?;
|
2021-04-07 14:56:54 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:38:00 +00:00
|
|
|
/// Extracts the bech32 prefix.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// The input slice if no prefix is found.
|
2019-04-24 14:17:43 +00:00
|
|
|
fn find_bech32_prefix(bech32: &str) -> &str {
|
|
|
|
// Split at the last occurrence of the separator character '1'.
|
2019-08-05 19:41:07 +00:00
|
|
|
match bech32.rfind('1') {
|
2019-04-24 14:17:43 +00:00
|
|
|
None => bech32,
|
|
|
|
Some(sep) => bech32.split_at(sep).0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 08:23:10 +00:00
|
|
|
/// Address can be parsed only with `NetworkUnchecked`.
|
2022-12-19 15:15:20 +00:00
|
|
|
impl FromStr for Address<NetworkUnchecked> {
|
2019-04-24 14:17:43 +00:00
|
|
|
type Err = Error;
|
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
fn from_str(s: &str) -> Result<Address<NetworkUnchecked>, Error> {
|
2019-04-24 14:17:43 +00:00
|
|
|
// try bech32
|
|
|
|
let bech32_network = match find_bech32_prefix(s) {
|
|
|
|
// note that upper or lowercase is allowed but NOT mixed case
|
|
|
|
"bc" | "BC" => Some(Network::Bitcoin),
|
2019-07-10 04:53:53 +00:00
|
|
|
"tb" | "TB" => Some(Network::Testnet), // this may also be signet
|
2019-04-24 14:17:43 +00:00
|
|
|
"bcrt" | "BCRT" => Some(Network::Regtest),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(network) = bech32_network {
|
|
|
|
// decode as bech32
|
2021-05-04 01:34:01 +00:00
|
|
|
let (_, payload, variant) = bech32::decode(s)?;
|
2019-08-05 19:41:07 +00:00
|
|
|
if payload.is_empty() {
|
2019-04-24 14:17:43 +00:00
|
|
|
return Err(Error::EmptyBech32Payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the script version and program (converted from 5-bit to 8-bit)
|
2021-01-30 19:50:04 +00:00
|
|
|
let (version, program): (WitnessVersion, Vec<u8>) = {
|
2019-04-24 14:17:43 +00:00
|
|
|
let (v, p5) = payload.split_at(1);
|
2022-05-25 06:35:34 +00:00
|
|
|
(WitnessVersion::try_from(v[0])?, bech32::FromBase32::from_base32(p5)?)
|
2018-03-09 20:27:13 +00:00
|
|
|
};
|
2019-04-24 14:17:43 +00:00
|
|
|
|
2023-01-19 23:30:17 +00:00
|
|
|
let witness_program = WitnessProgram::new(version, program)?;
|
2019-04-24 14:17:43 +00:00
|
|
|
|
2021-02-18 04:10:07 +00:00
|
|
|
// Encoding check
|
2021-09-09 08:19:17 +00:00
|
|
|
let expected = version.bech32_variant();
|
|
|
|
if expected != variant {
|
|
|
|
return Err(Error::InvalidBech32Variant { expected, found: variant });
|
2021-05-04 01:34:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 23:30:17 +00:00
|
|
|
return Ok(Address::new(network, Payload::WitnessProgram(witness_program)));
|
2018-03-09 20:27:13 +00:00
|
|
|
}
|
2018-03-09 17:53:20 +00:00
|
|
|
|
2019-04-24 14:17:43 +00:00
|
|
|
// Base58
|
2018-05-14 15:07:30 +00:00
|
|
|
if s.len() > 50 {
|
2019-04-24 14:17:43 +00:00
|
|
|
return Err(Error::Base58(base58::Error::InvalidLength(s.len() * 11 / 15)));
|
2018-05-14 15:07:30 +00:00
|
|
|
}
|
2022-09-13 01:40:49 +00:00
|
|
|
let data = base58::decode_check(s)?;
|
2015-10-14 20:29:19 +00:00
|
|
|
if data.len() != 21 {
|
2019-04-24 14:17:43 +00:00
|
|
|
return Err(Error::Base58(base58::Error::InvalidLength(data.len())));
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:27:13 +00:00
|
|
|
let (network, payload) = match data[0] {
|
2022-08-05 03:40:26 +00:00
|
|
|
PUBKEY_ADDRESS_PREFIX_MAIN =>
|
|
|
|
(Network::Bitcoin, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap())),
|
|
|
|
SCRIPT_ADDRESS_PREFIX_MAIN =>
|
|
|
|
(Network::Bitcoin, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap())),
|
|
|
|
PUBKEY_ADDRESS_PREFIX_TEST =>
|
|
|
|
(Network::Testnet, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap())),
|
|
|
|
SCRIPT_ADDRESS_PREFIX_TEST =>
|
|
|
|
(Network::Testnet, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap())),
|
2020-11-18 13:05:55 +00:00
|
|
|
x => return Err(Error::Base58(base58::Error::InvalidAddressVersion(x))),
|
2015-10-14 20:29:19 +00:00
|
|
|
};
|
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
Ok(Address::new(network, payload))
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 19:12:16 +00:00
|
|
|
/// Convert a byte array of a pubkey hash into a segwit redeem hash
|
2022-12-15 19:52:07 +00:00
|
|
|
fn segwit_redeem_hash(pubkey_hash: &PubkeyHash) -> crate::hashes::hash160::Hash {
|
2022-02-20 19:12:16 +00:00
|
|
|
let mut sha_engine = sha256::Hash::engine();
|
|
|
|
sha_engine.input(&[0, 20]);
|
2022-12-15 19:52:07 +00:00
|
|
|
sha_engine.input(pubkey_hash.as_ref());
|
2022-05-02 22:13:57 +00:00
|
|
|
crate::hashes::hash160::Hash::from_engine(sha_engine)
|
2022-02-20 19:12:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 20:29:19 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-09 10:40:41 +00:00
|
|
|
use core::str::FromStr;
|
2018-03-09 17:53:20 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
use hex_lit::hex;
|
2022-08-05 03:40:26 +00:00
|
|
|
use secp256k1::XOnlyPublicKey;
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
use super::*;
|
2022-11-08 00:36:52 +00:00
|
|
|
use crate::crypto::key::PublicKey;
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::network::constants::Network::{Bitcoin, Testnet};
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2019-04-24 06:55:51 +00:00
|
|
|
fn roundtrips(addr: &Address) {
|
|
|
|
assert_eq!(
|
2022-12-19 15:15:20 +00:00
|
|
|
Address::from_str(&addr.to_string()).unwrap().assume_checked(),
|
2019-07-26 16:49:21 +00:00
|
|
|
*addr,
|
|
|
|
"string round-trip failed for {}",
|
|
|
|
addr,
|
2019-04-24 06:55:51 +00:00
|
|
|
);
|
2019-04-24 07:02:05 +00:00
|
|
|
assert_eq!(
|
2023-03-30 05:12:30 +00:00
|
|
|
Address::from_script(&addr.script_pubkey(), *addr.network()).as_ref(),
|
2022-06-05 14:49:26 +00:00
|
|
|
Ok(addr),
|
2019-07-26 16:49:21 +00:00
|
|
|
"script round-trip failed for {}",
|
|
|
|
addr,
|
2019-04-24 07:02:05 +00:00
|
|
|
);
|
2022-09-01 06:08:12 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
{
|
|
|
|
let ser = serde_json::to_string(addr).expect("failed to serialize address");
|
2022-11-02 22:36:37 +00:00
|
|
|
let back: Address<NetworkUnchecked> =
|
|
|
|
serde_json::from_str(&ser).expect("failed to deserialize address");
|
2022-12-19 15:15:20 +00:00
|
|
|
assert_eq!(back.assume_checked(), *addr, "serde round-trip failed for {}", addr)
|
2022-09-01 06:08:12 +00:00
|
|
|
}
|
2019-04-24 06:55:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 20:29:19 +00:00
|
|
|
#[test]
|
|
|
|
fn test_p2pkh_address_58() {
|
2022-11-02 22:36:37 +00:00
|
|
|
let addr = Address::new(
|
|
|
|
Bitcoin,
|
|
|
|
Payload::PubkeyHash("162c5ea71c0b23f5b9022ef047c4a86470a5b070".parse().unwrap()),
|
|
|
|
);
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2019-07-26 16:49:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
addr.script_pubkey(),
|
2023-01-20 00:24:48 +00:00
|
|
|
ScriptBuf::from_hex("76a914162c5ea71c0b23f5b9022ef047c4a86470a5b07088ac").unwrap()
|
2019-07-26 16:49:21 +00:00
|
|
|
);
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(&addr.to_string(), "132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM");
|
2019-07-16 20:31:49 +00:00
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2pkh));
|
2019-04-24 06:55:51 +00:00
|
|
|
roundtrips(&addr);
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_p2pkh_from_key() {
|
2023-01-20 00:24:48 +00:00
|
|
|
let key = "048d5141948c1702e8c95f438815794b87f706a8d4cd2bffad1dc1570971032c9b6042a0431ded2478b5c9cf2d81c124a5e57347a3c63ef0e7716cf54d613ba183".parse::<PublicKey>().unwrap();
|
2018-11-16 04:21:41 +00:00
|
|
|
let addr = Address::p2pkh(&key, Bitcoin);
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(&addr.to_string(), "1QJVDzdqb1VpbDK7uDeyVXy9mR27CJiyhY");
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
let key = "03df154ebfcf29d29cc10d5c2565018bce2d9edbab267c31d2caf44a63056cf99f"
|
|
|
|
.parse::<PublicKey>()
|
|
|
|
.unwrap();
|
2018-03-14 03:53:03 +00:00
|
|
|
let addr = Address::p2pkh(&key, Testnet);
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(&addr.to_string(), "mqkhEMH6NCeYjFybv7pvFC22MFeaNT9AQC");
|
2019-07-16 20:31:49 +00:00
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2pkh));
|
2019-04-24 06:55:51 +00:00
|
|
|
roundtrips(&addr);
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_p2sh_address_58() {
|
2022-11-02 22:36:37 +00:00
|
|
|
let addr = Address::new(
|
|
|
|
Bitcoin,
|
|
|
|
Payload::ScriptHash("162c5ea71c0b23f5b9022ef047c4a86470a5b070".parse().unwrap()),
|
|
|
|
);
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2019-07-26 16:49:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
addr.script_pubkey(),
|
2023-01-20 00:24:48 +00:00
|
|
|
ScriptBuf::from_hex("a914162c5ea71c0b23f5b9022ef047c4a86470a5b07087").unwrap(),
|
2019-07-26 16:49:21 +00:00
|
|
|
);
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(&addr.to_string(), "33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k");
|
2019-07-16 20:31:49 +00:00
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2sh));
|
2019-04-24 06:55:51 +00:00
|
|
|
roundtrips(&addr);
|
2015-10-14 20:29:19 +00:00
|
|
|
}
|
2015-10-15 00:25:43 +00:00
|
|
|
|
2015-10-15 03:21:09 +00:00
|
|
|
#[test]
|
|
|
|
fn test_p2sh_parse() {
|
2023-01-20 00:24:48 +00:00
|
|
|
let script = ScriptBuf::from_hex("552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae").unwrap();
|
2021-09-15 10:46:20 +00:00
|
|
|
let addr = Address::p2sh(&script, Testnet).unwrap();
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(&addr.to_string(), "2N3zXjbwdTcPsJiy8sUK9FhWJhqQCxA8Jjr");
|
2019-07-16 20:31:49 +00:00
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2sh));
|
2019-04-24 06:55:51 +00:00
|
|
|
roundtrips(&addr);
|
2018-03-09 20:27:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 10:46:20 +00:00
|
|
|
#[test]
|
2022-08-05 03:40:26 +00:00
|
|
|
fn test_p2sh_parse_for_large_script() {
|
2023-01-20 00:24:48 +00:00
|
|
|
let script = ScriptBuf::from_hex("552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae12123122313123123ac1231231231231313123131231231231313212313213123123552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae12123122313123123ac1231231231231313123131231231231313212313213123123552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae12123122313123123ac1231231231231313123131231231231313212313213123123").unwrap();
|
2021-09-15 10:46:20 +00:00
|
|
|
assert_eq!(Address::p2sh(&script, Testnet), Err(Error::ExcessiveScriptSize));
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:53:03 +00:00
|
|
|
#[test]
|
2019-07-26 16:49:21 +00:00
|
|
|
fn test_p2wpkh() {
|
2018-03-14 03:53:03 +00:00
|
|
|
// stolen from Bitcoin transaction: b3c8c2b6cfc335abbcb2c7823a8453f55d64b2b5125a9a61e8737230cdb8ce20
|
2022-11-02 22:36:37 +00:00
|
|
|
let mut key = "033bc8c83c52df5712229a2f72206d90192366c36428cb0c12b6af98324d97bfbc"
|
|
|
|
.parse::<PublicKey>()
|
|
|
|
.unwrap();
|
2020-05-23 22:20:46 +00:00
|
|
|
let addr = Address::p2wpkh(&key, Bitcoin).unwrap();
|
2018-03-14 03:53:03 +00:00
|
|
|
assert_eq!(&addr.to_string(), "bc1qvzvkjn4q3nszqxrv3nraga2r822xjty3ykvkuw");
|
2019-07-16 20:31:49 +00:00
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2wpkh));
|
2019-04-24 06:55:51 +00:00
|
|
|
roundtrips(&addr);
|
2020-05-23 22:20:46 +00:00
|
|
|
|
|
|
|
// Test uncompressed pubkey
|
|
|
|
key.compressed = false;
|
|
|
|
assert_eq!(Address::p2wpkh(&key, Bitcoin), Err(Error::UncompressedPubkey));
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-07-26 16:49:21 +00:00
|
|
|
fn test_p2wsh() {
|
2018-03-14 03:53:03 +00:00
|
|
|
// stolen from Bitcoin transaction 5df912fda4becb1c29e928bec8d64d93e9ba8efa9b5b405bd683c86fd2c65667
|
2023-01-20 00:24:48 +00:00
|
|
|
let script = ScriptBuf::from_hex("52210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae").unwrap();
|
2018-03-14 03:53:03 +00:00
|
|
|
let addr = Address::p2wsh(&script, Bitcoin);
|
2019-07-26 16:49:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
&addr.to_string(),
|
|
|
|
"bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"
|
|
|
|
);
|
2019-07-16 20:31:49 +00:00
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2wsh));
|
2019-04-24 06:55:51 +00:00
|
|
|
roundtrips(&addr);
|
2018-03-14 03:53:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:00:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_p2shwpkh() {
|
|
|
|
// stolen from Bitcoin transaction: ad3fd9c6b52e752ba21425435ff3dd361d6ac271531fc1d2144843a9f550ad01
|
2022-11-02 22:36:37 +00:00
|
|
|
let mut key = "026c468be64d22761c30cd2f12cbc7de255d592d7904b1bab07236897cc4c2e766"
|
|
|
|
.parse::<PublicKey>()
|
|
|
|
.unwrap();
|
2020-05-23 22:20:46 +00:00
|
|
|
let addr = Address::p2shwpkh(&key, Bitcoin).unwrap();
|
2019-08-13 12:00:42 +00:00
|
|
|
assert_eq!(&addr.to_string(), "3QBRmWNqqBGme9er7fMkGqtZtp4gjMFxhE");
|
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2sh));
|
|
|
|
roundtrips(&addr);
|
2020-05-23 22:20:46 +00:00
|
|
|
|
|
|
|
// Test uncompressed pubkey
|
|
|
|
key.compressed = false;
|
|
|
|
assert_eq!(Address::p2wpkh(&key, Bitcoin), Err(Error::UncompressedPubkey));
|
2019-08-13 12:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_p2shwsh() {
|
|
|
|
// stolen from Bitcoin transaction f9ee2be4df05041d0e0a35d7caa3157495ca4f93b233234c9967b6901dacf7a9
|
2023-01-20 00:24:48 +00:00
|
|
|
let script = ScriptBuf::from_hex("522103e5529d8eaa3d559903adb2e881eb06c86ac2574ffa503c45f4e942e2a693b33e2102e5f10fcdcdbab211e0af6a481f5532536ec61a5fdbf7183770cf8680fe729d8152ae").unwrap();
|
2019-08-13 12:00:42 +00:00
|
|
|
let addr = Address::p2shwsh(&script, Bitcoin);
|
|
|
|
assert_eq!(&addr.to_string(), "36EqgNnsWW94SreZgBWc1ANC6wpFZwirHr");
|
|
|
|
assert_eq!(addr.address_type(), Some(AddressType::P2sh));
|
|
|
|
roundtrips(&addr);
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:02:05 +00:00
|
|
|
#[test]
|
|
|
|
fn test_non_existent_segwit_version() {
|
|
|
|
// 40-byte program
|
2022-11-02 22:36:37 +00:00
|
|
|
let program = hex!(
|
|
|
|
"654f6ea368e0acdfd92976b7c2103a1b26313f430654f6ea368e0acdfd92976b7c2103a1b26313f4"
|
|
|
|
);
|
2023-02-18 10:21:13 +00:00
|
|
|
let witness_prog = WitnessProgram::new(WitnessVersion::V13, program.to_vec()).unwrap();
|
2023-01-19 23:30:17 +00:00
|
|
|
let addr = Address::new(Bitcoin, Payload::WitnessProgram(witness_prog));
|
2019-04-24 07:02:05 +00:00
|
|
|
roundtrips(&addr);
|
|
|
|
}
|
|
|
|
|
2023-01-22 08:23:10 +00:00
|
|
|
#[test]
|
|
|
|
fn test_address_debug() {
|
|
|
|
// This is not really testing output of Debug but the ability and proper functioning
|
|
|
|
// of Debug derivation on structs generic in NetworkValidation.
|
2022-11-02 22:36:37 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
#[allow(unused)]
|
|
|
|
struct Test<V: NetworkValidation> {
|
|
|
|
address: Address<V>,
|
|
|
|
}
|
2023-01-22 08:23:10 +00:00
|
|
|
|
|
|
|
let addr_str = "33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k";
|
|
|
|
let unchecked = Address::from_str(addr_str).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:?}", Test { address: unchecked.clone() }),
|
2022-11-02 22:36:37 +00:00
|
|
|
format!("Test {{ address: Address<NetworkUnchecked>({}) }}", addr_str)
|
|
|
|
);
|
2023-01-22 08:23:10 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:?}", Test { address: unchecked.assume_checked() }),
|
2022-11-02 22:36:37 +00:00
|
|
|
format!("Test {{ address: {} }}", addr_str)
|
|
|
|
);
|
2023-01-22 08:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 15:58:19 +00:00
|
|
|
#[test]
|
|
|
|
fn test_address_type() {
|
|
|
|
let addresses = [
|
|
|
|
("1QJVDzdqb1VpbDK7uDeyVXy9mR27CJiyhY", Some(AddressType::P2pkh)),
|
|
|
|
("33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k", Some(AddressType::P2sh)),
|
|
|
|
("bc1qvzvkjn4q3nszqxrv3nraga2r822xjty3ykvkuw", Some(AddressType::P2wpkh)),
|
2022-08-05 03:40:26 +00:00
|
|
|
(
|
|
|
|
"bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej",
|
|
|
|
Some(AddressType::P2wsh),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr",
|
|
|
|
Some(AddressType::P2tr),
|
|
|
|
),
|
2021-10-27 15:58:19 +00:00
|
|
|
// Related to future extensions, addresses are valid but have no type
|
|
|
|
// segwit v1 and len != 32
|
|
|
|
("bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y", None),
|
|
|
|
// segwit v2
|
|
|
|
("bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs", None),
|
|
|
|
];
|
|
|
|
for (address, expected_type) in &addresses {
|
2022-12-19 15:15:20 +00:00
|
|
|
let addr = Address::from_str(address)
|
|
|
|
.unwrap()
|
|
|
|
.require_network(Network::Bitcoin)
|
|
|
|
.expect("mainnet");
|
2021-10-27 15:58:19 +00:00
|
|
|
assert_eq!(&addr.address_type(), expected_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:27:13 +00:00
|
|
|
#[test]
|
2021-06-16 06:43:18 +00:00
|
|
|
fn test_bip173_350_vectors() {
|
|
|
|
// Test vectors valid under both BIP-173 and BIP-350
|
2019-07-26 16:48:43 +00:00
|
|
|
let valid_vectors = [
|
|
|
|
("BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4", "0014751e76e8199196d454941c45d1b3a323f1433bd6"),
|
|
|
|
("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7", "00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"),
|
2021-05-04 01:34:01 +00:00
|
|
|
("bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y", "5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6"),
|
|
|
|
("BC1SW50QGDZ25J", "6002751e"),
|
|
|
|
("bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs", "5210751e76e8199196d454941c45d1b3a323"),
|
2019-07-26 16:48:43 +00:00
|
|
|
("tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy", "0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"),
|
2021-06-16 06:43:18 +00:00
|
|
|
("tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c", "5120000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"),
|
|
|
|
("bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0", "512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
|
2019-07-26 16:48:43 +00:00
|
|
|
];
|
|
|
|
for vector in &valid_vectors {
|
2022-12-19 15:15:20 +00:00
|
|
|
let addr: Address = vector.0.parse::<Address<_>>().unwrap().assume_checked();
|
2023-01-07 15:39:11 +00:00
|
|
|
assert_eq!(&addr.script_pubkey().to_hex_string(), vector.1);
|
2019-07-26 16:48:43 +00:00
|
|
|
roundtrips(&addr);
|
|
|
|
}
|
2018-03-09 20:27:13 +00:00
|
|
|
|
2019-07-26 16:48:43 +00:00
|
|
|
let invalid_vectors = [
|
2021-06-16 06:43:18 +00:00
|
|
|
// 1. BIP-350 test vectors
|
|
|
|
// Invalid human-readable part
|
|
|
|
"tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut",
|
|
|
|
// Invalid checksums (Bech32 instead of Bech32m):
|
|
|
|
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd",
|
|
|
|
"tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf",
|
|
|
|
"BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL",
|
|
|
|
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh",
|
|
|
|
"tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47",
|
|
|
|
// Invalid character in checksum
|
|
|
|
"bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4",
|
|
|
|
// Invalid witness version
|
|
|
|
"BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R",
|
|
|
|
// Invalid program length (1 byte)
|
|
|
|
"bc1pw5dgrnzv",
|
|
|
|
// Invalid program length (41 bytes)
|
|
|
|
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav",
|
|
|
|
// Invalid program length for witness version 0 (per BIP141)
|
|
|
|
"BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
|
|
|
|
// Mixed case
|
|
|
|
"tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq47Zagq",
|
|
|
|
// zero padding of more than 4 bits
|
|
|
|
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf",
|
|
|
|
// Non-zero padding in 8-to-5 conversion
|
|
|
|
"tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j",
|
|
|
|
// Empty data section
|
|
|
|
"bc1gmk9yu",
|
|
|
|
// 2. BIP-173 test vectors
|
|
|
|
// Invalid human-readable part
|
2019-07-26 16:48:43 +00:00
|
|
|
"tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Invalid checksum
|
2019-07-26 16:48:43 +00:00
|
|
|
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Invalid witness version
|
2019-07-26 16:48:43 +00:00
|
|
|
"BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Invalid program length
|
2019-07-26 16:48:43 +00:00
|
|
|
"bc1rw5uspcuh",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Invalid program length
|
2019-07-26 16:48:43 +00:00
|
|
|
"bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Invalid program length for witness version 0 (per BIP141)
|
2019-07-26 16:48:43 +00:00
|
|
|
"BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Mixed case
|
2019-07-26 16:48:43 +00:00
|
|
|
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7",
|
2021-06-16 06:43:18 +00:00
|
|
|
// zero padding of more than 4 bits
|
2019-07-26 16:48:43 +00:00
|
|
|
"bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Non-zero padding in 8-to-5 conversion
|
2019-07-26 16:48:43 +00:00
|
|
|
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv",
|
2021-06-16 06:43:18 +00:00
|
|
|
// Final test for empty data section is the same as above in BIP-350
|
|
|
|
|
|
|
|
// 3. BIP-173 valid test vectors obsolete by BIP-350
|
|
|
|
"bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx",
|
|
|
|
"BC1SW50QA3JX3S",
|
|
|
|
"bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj",
|
2019-07-26 16:48:43 +00:00
|
|
|
];
|
|
|
|
for vector in &invalid_vectors {
|
2022-12-19 15:15:20 +00:00
|
|
|
assert!(vector.parse::<Address<_>>().is_err());
|
2019-07-26 16:48:43 +00:00
|
|
|
}
|
2015-10-15 03:21:09 +00:00
|
|
|
}
|
2015-10-14 20:29:19 +00:00
|
|
|
|
2018-08-30 19:01:54 +00:00
|
|
|
#[test]
|
2019-03-26 16:05:51 +00:00
|
|
|
#[cfg(feature = "serde")]
|
2018-08-30 19:01:54 +00:00
|
|
|
fn test_json_serialize() {
|
2019-03-26 16:05:51 +00:00
|
|
|
use serde_json;
|
2018-08-30 19:01:54 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
let addr =
|
|
|
|
Address::from_str("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM").unwrap().assume_checked();
|
2019-03-26 16:05:51 +00:00
|
|
|
let json = serde_json::to_value(&addr).unwrap();
|
2019-07-26 16:49:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
json,
|
|
|
|
serde_json::Value::String("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM".to_owned())
|
|
|
|
);
|
2022-12-19 15:15:20 +00:00
|
|
|
let into: Address = serde_json::from_value::<Address<_>>(json).unwrap().assume_checked();
|
2018-08-30 19:01:54 +00:00
|
|
|
assert_eq!(addr.to_string(), into.to_string());
|
|
|
|
assert_eq!(
|
|
|
|
into.script_pubkey(),
|
2023-01-20 00:24:48 +00:00
|
|
|
ScriptBuf::from_hex("76a914162c5ea71c0b23f5b9022ef047c4a86470a5b07088ac").unwrap()
|
2018-08-30 19:01:54 +00:00
|
|
|
);
|
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
let addr =
|
|
|
|
Address::from_str("33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k").unwrap().assume_checked();
|
2019-03-26 16:05:51 +00:00
|
|
|
let json = serde_json::to_value(&addr).unwrap();
|
2019-07-26 16:49:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
json,
|
|
|
|
serde_json::Value::String("33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k".to_owned())
|
|
|
|
);
|
2022-12-19 15:15:20 +00:00
|
|
|
let into: Address = serde_json::from_value::<Address<_>>(json).unwrap().assume_checked();
|
2018-08-30 19:01:54 +00:00
|
|
|
assert_eq!(addr.to_string(), into.to_string());
|
|
|
|
assert_eq!(
|
|
|
|
into.script_pubkey(),
|
2023-01-20 00:24:48 +00:00
|
|
|
ScriptBuf::from_hex("a914162c5ea71c0b23f5b9022ef047c4a86470a5b07087").unwrap()
|
2018-08-30 19:01:54 +00:00
|
|
|
);
|
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
let addr: Address<NetworkUnchecked> =
|
2019-07-26 16:49:21 +00:00
|
|
|
Address::from_str("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7")
|
|
|
|
.unwrap();
|
2022-12-19 15:15:20 +00:00
|
|
|
let json = serde_json::to_value(addr).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
json,
|
|
|
|
serde_json::Value::String(
|
|
|
|
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7".to_owned()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
let addr =
|
|
|
|
Address::from_str("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7")
|
2022-11-02 22:36:37 +00:00
|
|
|
.unwrap()
|
|
|
|
.assume_checked();
|
2019-03-26 16:05:51 +00:00
|
|
|
let json = serde_json::to_value(&addr).unwrap();
|
2019-07-26 16:49:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
json,
|
|
|
|
serde_json::Value::String(
|
|
|
|
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7".to_owned()
|
|
|
|
)
|
|
|
|
);
|
2022-12-19 15:15:20 +00:00
|
|
|
let into: Address = serde_json::from_value::<Address<_>>(json).unwrap().assume_checked();
|
2018-08-30 19:01:54 +00:00
|
|
|
assert_eq!(addr.to_string(), into.to_string());
|
|
|
|
assert_eq!(
|
|
|
|
into.script_pubkey(),
|
2022-11-02 22:36:37 +00:00
|
|
|
ScriptBuf::from_hex(
|
|
|
|
"00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"
|
|
|
|
)
|
|
|
|
.unwrap()
|
2018-08-30 19:01:54 +00:00
|
|
|
);
|
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
let addr = Address::from_str("bcrt1q2nfxmhd4n3c8834pj72xagvyr9gl57n5r94fsl")
|
|
|
|
.unwrap()
|
|
|
|
.assume_checked();
|
2019-03-26 16:05:51 +00:00
|
|
|
let json = serde_json::to_value(&addr).unwrap();
|
2019-07-26 16:49:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
json,
|
|
|
|
serde_json::Value::String("bcrt1q2nfxmhd4n3c8834pj72xagvyr9gl57n5r94fsl".to_owned())
|
|
|
|
);
|
2022-12-19 15:15:20 +00:00
|
|
|
let into: Address = serde_json::from_value::<Address<_>>(json).unwrap().assume_checked();
|
2018-08-30 19:01:54 +00:00
|
|
|
assert_eq!(addr.to_string(), into.to_string());
|
|
|
|
assert_eq!(
|
|
|
|
into.script_pubkey(),
|
2023-01-20 00:24:48 +00:00
|
|
|
ScriptBuf::from_hex("001454d26dddb59c7073c6a197946ea1841951fa7a74").unwrap()
|
2018-08-30 19:01:54 +00:00
|
|
|
);
|
|
|
|
}
|
2021-03-22 12:58:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qr_string() {
|
2022-08-05 03:40:26 +00:00
|
|
|
for el in
|
|
|
|
["132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM", "33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k"].iter()
|
|
|
|
{
|
2022-11-02 22:36:37 +00:00
|
|
|
let addr =
|
|
|
|
Address::from_str(el).unwrap().require_network(Network::Bitcoin).expect("mainnet");
|
2021-04-15 08:40:57 +00:00
|
|
|
assert_eq!(addr.to_qr_uri(), format!("bitcoin:{}", el));
|
2021-03-22 12:58:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
for el in [
|
|
|
|
"bcrt1q2nfxmhd4n3c8834pj72xagvyr9gl57n5r94fsl",
|
|
|
|
"bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej",
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
{
|
2022-12-19 15:15:20 +00:00
|
|
|
let addr = Address::from_str(el).unwrap().assume_checked();
|
2022-08-05 03:40:26 +00:00
|
|
|
assert_eq!(addr.to_qr_uri(), format!("BITCOIN:{}", el.to_ascii_uppercase()));
|
2021-03-22 12:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 18:01:22 +00:00
|
|
|
#[test]
|
|
|
|
fn test_valid_networks() {
|
|
|
|
let legacy_payload = &[
|
2022-06-07 02:39:25 +00:00
|
|
|
Payload::PubkeyHash(PubkeyHash::all_zeros()),
|
2022-08-05 03:40:26 +00:00
|
|
|
Payload::ScriptHash(ScriptHash::all_zeros()),
|
2021-06-14 18:01:22 +00:00
|
|
|
];
|
2022-08-05 03:40:26 +00:00
|
|
|
let segwit_payload = (0..=16)
|
2022-11-02 22:36:37 +00:00
|
|
|
.map(|version| {
|
|
|
|
Payload::WitnessProgram(
|
|
|
|
WitnessProgram::new(
|
|
|
|
WitnessVersion::try_from(version).unwrap(),
|
|
|
|
vec![0xab; 32], // Choose 32 to make test case valid for all witness versions(including v0)
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
})
|
2022-08-05 03:40:26 +00:00
|
|
|
.collect::<Vec<_>>();
|
2021-06-14 18:01:22 +00:00
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
const LEGACY_EQUIVALENCE_CLASSES: &[&[Network]] =
|
|
|
|
&[&[Network::Bitcoin], &[Network::Testnet, Network::Regtest, Network::Signet]];
|
|
|
|
const SEGWIT_EQUIVALENCE_CLASSES: &[&[Network]] =
|
|
|
|
&[&[Network::Bitcoin], &[Network::Regtest], &[Network::Testnet, Network::Signet]];
|
2021-06-14 18:01:22 +00:00
|
|
|
|
|
|
|
fn test_addr_type(payloads: &[Payload], equivalence_classes: &[&[Network]]) {
|
|
|
|
for pl in payloads {
|
2022-06-07 05:13:03 +00:00
|
|
|
for addr_net in equivalence_classes.iter().flat_map(|ec| ec.iter()) {
|
2022-08-05 03:40:26 +00:00
|
|
|
for valid_net in equivalence_classes
|
|
|
|
.iter()
|
2021-06-14 18:01:22 +00:00
|
|
|
.filter(|ec| ec.contains(addr_net))
|
2022-06-07 05:13:03 +00:00
|
|
|
.flat_map(|ec| ec.iter())
|
2021-06-14 18:01:22 +00:00
|
|
|
{
|
2022-12-19 15:15:20 +00:00
|
|
|
let addr = Address::new(*addr_net, pl.clone());
|
2021-06-14 18:01:22 +00:00
|
|
|
assert!(addr.is_valid_for_network(*valid_net));
|
|
|
|
}
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
for invalid_net in equivalence_classes
|
|
|
|
.iter()
|
2021-06-14 18:01:22 +00:00
|
|
|
.filter(|ec| !ec.contains(addr_net))
|
2022-06-07 05:13:03 +00:00
|
|
|
.flat_map(|ec| ec.iter())
|
2021-06-14 18:01:22 +00:00
|
|
|
{
|
2022-12-19 15:15:20 +00:00
|
|
|
let addr = Address::new(*addr_net, pl.clone());
|
2021-06-14 18:01:22 +00:00
|
|
|
assert!(!addr.is_valid_for_network(*invalid_net));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test_addr_type(legacy_payload, LEGACY_EQUIVALENCE_CLASSES);
|
|
|
|
test_addr_type(&segwit_payload, SEGWIT_EQUIVALENCE_CLASSES);
|
|
|
|
}
|
2021-11-07 06:11:43 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-01-24 00:26:29 +00:00
|
|
|
fn p2tr_from_untweaked() {
|
2021-11-07 06:11:43 +00:00
|
|
|
//Test case from BIP-086
|
2022-08-05 03:40:26 +00:00
|
|
|
let internal_key = XOnlyPublicKey::from_str(
|
|
|
|
"cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-12 21:36:31 +00:00
|
|
|
let secp = Secp256k1::verification_only();
|
|
|
|
let address = Address::p2tr(&secp, internal_key, None, Network::Bitcoin);
|
2022-08-05 03:40:26 +00:00
|
|
|
assert_eq!(
|
|
|
|
address.to_string(),
|
|
|
|
"bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr"
|
|
|
|
);
|
2021-10-27 15:58:19 +00:00
|
|
|
assert_eq!(address.address_type(), Some(AddressType::P2tr));
|
|
|
|
roundtrips(&address);
|
2021-11-07 06:11:43 +00:00
|
|
|
}
|
2022-02-20 19:12:16 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_related_to_pubkey_p2wpkh() {
|
|
|
|
let address_string = "bc1qhvd6suvqzjcu9pxjhrwhtrlj85ny3n2mqql5w4";
|
2022-12-19 15:15:20 +00:00
|
|
|
let address = Address::from_str(address_string)
|
|
|
|
.expect("address")
|
|
|
|
.require_network(Network::Bitcoin)
|
|
|
|
.expect("mainnet");
|
2022-02-20 19:12:16 +00:00
|
|
|
|
|
|
|
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
|
|
|
|
let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey");
|
|
|
|
|
|
|
|
let result = address.is_related_to_pubkey(&pubkey);
|
|
|
|
assert!(result);
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
let unused_pubkey = PublicKey::from_str(
|
|
|
|
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
|
|
|
|
)
|
|
|
|
.expect("pubkey");
|
2022-02-20 19:12:16 +00:00
|
|
|
assert!(!address.is_related_to_pubkey(&unused_pubkey))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_related_to_pubkey_p2shwpkh() {
|
|
|
|
let address_string = "3EZQk4F8GURH5sqVMLTFisD17yNeKa7Dfs";
|
2022-12-19 15:15:20 +00:00
|
|
|
let address = Address::from_str(address_string)
|
|
|
|
.expect("address")
|
|
|
|
.require_network(Network::Bitcoin)
|
|
|
|
.expect("mainnet");
|
2022-02-20 19:12:16 +00:00
|
|
|
|
|
|
|
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
|
|
|
|
let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey");
|
|
|
|
|
|
|
|
let result = address.is_related_to_pubkey(&pubkey);
|
|
|
|
assert!(result);
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
let unused_pubkey = PublicKey::from_str(
|
|
|
|
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
|
|
|
|
)
|
|
|
|
.expect("pubkey");
|
2022-02-20 19:12:16 +00:00
|
|
|
assert!(!address.is_related_to_pubkey(&unused_pubkey))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_related_to_pubkey_p2pkh() {
|
|
|
|
let address_string = "1J4LVanjHMu3JkXbVrahNuQCTGCRRgfWWx";
|
2022-12-19 15:15:20 +00:00
|
|
|
let address = Address::from_str(address_string)
|
|
|
|
.expect("address")
|
|
|
|
.require_network(Network::Bitcoin)
|
|
|
|
.expect("mainnet");
|
2022-02-20 19:12:16 +00:00
|
|
|
|
|
|
|
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
|
|
|
|
let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey");
|
|
|
|
|
|
|
|
let result = address.is_related_to_pubkey(&pubkey);
|
|
|
|
assert!(result);
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
let unused_pubkey = PublicKey::from_str(
|
|
|
|
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
|
|
|
|
)
|
|
|
|
.expect("pubkey");
|
2022-02-20 19:12:16 +00:00
|
|
|
assert!(!address.is_related_to_pubkey(&unused_pubkey))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_related_to_pubkey_p2pkh_uncompressed_key() {
|
|
|
|
let address_string = "msvS7KzhReCDpQEJaV2hmGNvuQqVUDuC6p";
|
2022-12-19 15:15:20 +00:00
|
|
|
let address = Address::from_str(address_string)
|
|
|
|
.expect("address")
|
|
|
|
.require_network(Network::Testnet)
|
|
|
|
.expect("testnet");
|
2022-02-20 19:12:16 +00:00
|
|
|
|
|
|
|
let pubkey_string = "04e96e22004e3db93530de27ccddfdf1463975d2138ac018fc3e7ba1a2e5e0aad8e424d0b55e2436eb1d0dcd5cb2b8bcc6d53412c22f358de57803a6a655fbbd04";
|
|
|
|
let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey");
|
|
|
|
|
|
|
|
let result = address.is_related_to_pubkey(&pubkey);
|
|
|
|
assert!(result);
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
let unused_pubkey = PublicKey::from_str(
|
|
|
|
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
|
|
|
|
)
|
|
|
|
.expect("pubkey");
|
2022-02-20 19:12:16 +00:00
|
|
|
assert!(!address.is_related_to_pubkey(&unused_pubkey))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-08-05 03:40:26 +00:00
|
|
|
fn test_is_related_to_pubkey_p2tr() {
|
2022-02-20 19:12:16 +00:00
|
|
|
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
|
|
|
|
let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey");
|
|
|
|
let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner);
|
|
|
|
let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(xonly_pubkey);
|
|
|
|
let address = Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin);
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
assert_eq!(
|
|
|
|
address,
|
|
|
|
Address::from_str("bc1pgllnmtxs0g058qz7c6qgaqq4qknwrqj9z7rqn9e2dzhmcfmhlu4sfadf5e")
|
|
|
|
.expect("address")
|
2022-12-19 15:15:20 +00:00
|
|
|
.require_network(Network::Bitcoin)
|
|
|
|
.expect("mainnet")
|
2022-08-05 03:40:26 +00:00
|
|
|
);
|
2022-02-20 19:12:16 +00:00
|
|
|
|
|
|
|
let result = address.is_related_to_pubkey(&pubkey);
|
|
|
|
assert!(result);
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
let unused_pubkey = PublicKey::from_str(
|
|
|
|
"02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c",
|
|
|
|
)
|
|
|
|
.expect("pubkey");
|
2022-02-20 19:12:16 +00:00
|
|
|
assert!(!address.is_related_to_pubkey(&unused_pubkey));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-08-05 03:40:26 +00:00
|
|
|
fn test_is_related_to_xonly_pubkey() {
|
2022-02-20 19:12:16 +00:00
|
|
|
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
|
|
|
|
let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey");
|
|
|
|
let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner);
|
|
|
|
let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(xonly_pubkey);
|
|
|
|
let address = Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin);
|
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
assert_eq!(
|
|
|
|
address,
|
|
|
|
Address::from_str("bc1pgllnmtxs0g058qz7c6qgaqq4qknwrqj9z7rqn9e2dzhmcfmhlu4sfadf5e")
|
|
|
|
.expect("address")
|
2022-12-19 15:15:20 +00:00
|
|
|
.require_network(Network::Bitcoin)
|
|
|
|
.expect("mainnet")
|
2022-08-05 03:40:26 +00:00
|
|
|
);
|
2022-02-20 19:12:16 +00:00
|
|
|
|
|
|
|
let result = address.is_related_to_xonly_pubkey(&xonly_pubkey);
|
|
|
|
assert!(result);
|
|
|
|
}
|
2022-05-28 10:27:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fail_address_from_script() {
|
2023-01-20 00:24:48 +00:00
|
|
|
let bad_p2wpkh = ScriptBuf::from_hex("0014dbc5b0a8f9d4353b4b54c3db48846bb15abfec").unwrap();
|
2022-11-02 22:36:37 +00:00
|
|
|
let bad_p2wsh = ScriptBuf::from_hex(
|
|
|
|
"00202d4fa2eb233d008cc83206fa2f4f2e60199000f5b857a835e3172323385623",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let invalid_segwitv0_script =
|
|
|
|
ScriptBuf::from_hex("001161458e330389cd0437ee9fe3641d70cc18").unwrap();
|
2022-06-05 14:49:26 +00:00
|
|
|
let expected = Err(Error::UnrecognizedScript);
|
2022-05-28 10:27:18 +00:00
|
|
|
|
2022-06-05 14:49:26 +00:00
|
|
|
assert_eq!(Address::from_script(&bad_p2wpkh, Network::Bitcoin), expected);
|
|
|
|
assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), expected);
|
2022-08-05 03:40:26 +00:00
|
|
|
assert_eq!(
|
|
|
|
Address::from_script(&invalid_segwitv0_script, Network::Bitcoin),
|
|
|
|
Err(Error::InvalidSegwitV0ProgramLength(17))
|
|
|
|
);
|
2022-05-28 10:27:18 +00:00
|
|
|
}
|
2022-07-10 21:25:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn valid_address_parses_correctly() {
|
|
|
|
let addr = AddressType::from_str("p2tr").expect("false negative while parsing address");
|
|
|
|
assert_eq!(addr, AddressType::P2tr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_address_parses_error() {
|
|
|
|
let got = AddressType::from_str("invalid");
|
|
|
|
let want = Err(Error::UnknownAddressType("invalid".to_string()));
|
|
|
|
assert_eq!(got, want);
|
|
|
|
}
|
2023-02-23 20:52:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_matches_script_pubkey() {
|
|
|
|
let addresses = [
|
|
|
|
"1QJVDzdqb1VpbDK7uDeyVXy9mR27CJiyhY",
|
|
|
|
"1J4LVanjHMu3JkXbVrahNuQCTGCRRgfWWx",
|
|
|
|
"33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k",
|
|
|
|
"3QBRmWNqqBGme9er7fMkGqtZtp4gjMFxhE",
|
|
|
|
"bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs",
|
|
|
|
"bc1qvzvkjn4q3nszqxrv3nraga2r822xjty3ykvkuw",
|
|
|
|
"bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr",
|
|
|
|
"bc1pgllnmtxs0g058qz7c6qgaqq4qknwrqj9z7rqn9e2dzhmcfmhlu4sfadf5e",
|
|
|
|
];
|
|
|
|
for addr in &addresses {
|
2022-11-02 22:36:37 +00:00
|
|
|
let addr = Address::from_str(addr).unwrap().require_network(Network::Bitcoin).unwrap();
|
2023-02-23 20:52:15 +00:00
|
|
|
for another in &addresses {
|
2022-11-02 22:36:37 +00:00
|
|
|
let another =
|
|
|
|
Address::from_str(another).unwrap().require_network(Network::Bitcoin).unwrap();
|
2023-02-23 20:52:15 +00:00
|
|
|
assert_eq!(addr.matches_script_pubkey(&another.script_pubkey()), addr == another);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 19:01:54 +00:00
|
|
|
}
|