Skip to content

Commit

Permalink
feat: add non-sequential AddressHashMode for multi-sig
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Jul 1, 2024
1 parent 9e2aa26 commit 7e533bd
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 125 deletions.
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,12 @@ export enum TxSpendingConditionSingleSigHashMode {
export enum TxSpendingConditionMultiSigHashMode {
/** hash160(multisig-redeem-script), same as bitcoin's multisig p2sh */
P2SH = 0x01,
/** hash160(multisig-redeem-script), same as bitcoin's multisig p2sh (non-sequential signing) */
P2SHNonSequential = 0x05,
/** hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh */
P2WSH = 0x03,
/** hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh (non-sequential signing) */
P2WSHNonSequential = 0x07,
}

export enum ClarityVersion {
Expand Down
12 changes: 10 additions & 2 deletions src/address/stacks_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ impl StacksAddress {
pub enum AddressHashMode {
// serialization modes for public keys to addresses.
// We support four different modes due to legacy compatibility with Stacks v1 addresses:
SerializeP2PKH = 0x00, // hash160(public-key), same as bitcoin's p2pkh
SerializeP2SH = 0x01, // hash160(multisig-redeem-script), same as bitcoin's multisig p2sh
SerializeP2PKH = 0x00, // hash160(public-key), same as bitcoin's p2pkh
SerializeP2SH = 0x01, // hash160(multisig-redeem-script), same as bitcoin's multisig p2sh
SerializeP2SHNonSequential = 0x05, // hash160(multisig-redeem-script), same as bitcoin's multisig p2sh (non-sequential signing)
SerializeP2WPKH = 0x02, // hash160(segwit-program-00(p2pkh)), same as bitcoin's p2sh-p2wpkh
SerializeP2WSH = 0x03, // hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh
SerializeP2WSHNonSequential = 0x07, // hash160(segwit-program-00(public-keys)), same as bitcoin's p2sh-p2wsh (non-sequential signing)
}

impl AddressHashMode {
Expand All @@ -70,10 +72,16 @@ impl TryFrom<u8> for AddressHashMode {
match value {
x if x == AddressHashMode::SerializeP2PKH as u8 => Ok(AddressHashMode::SerializeP2PKH),
x if x == AddressHashMode::SerializeP2SH as u8 => Ok(AddressHashMode::SerializeP2SH),
x if x == AddressHashMode::SerializeP2SHNonSequential as u8 => {
Ok(AddressHashMode::SerializeP2SHNonSequential)
}
x if x == AddressHashMode::SerializeP2WPKH as u8 => {
Ok(AddressHashMode::SerializeP2WPKH)
}
x if x == AddressHashMode::SerializeP2WSH as u8 => Ok(AddressHashMode::SerializeP2WSH),
x if x == AddressHashMode::SerializeP2WSHNonSequential as u8 => {
Ok(AddressHashMode::SerializeP2WSHNonSequential)
}
_ => Err(format!("Invalid version {}", value)),
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/stacks_tx/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,13 @@ impl MultisigHashMode {
pub fn from_u8(n: u8) -> Option<MultisigHashMode> {
match n {
x if x == MultisigHashMode::P2SH as u8 => Some(MultisigHashMode::P2SH),
x if x == MultisigHashMode::P2SHNonSequential as u8 => {
Some(MultisigHashMode::P2SHNonSequential)
}
x if x == MultisigHashMode::P2WSH as u8 => Some(MultisigHashMode::P2WSH),
x if x == MultisigHashMode::P2WSHNonSequential as u8 => {
Some(MultisigHashMode::P2WSHNonSequential)
}
_ => None,
}
}
Expand Down Expand Up @@ -632,7 +638,9 @@ pub struct SinglesigSpendingCondition {
#[derive(PartialEq, Copy, Clone)]
pub enum MultisigHashMode {
P2SH = 0x01,
P2SHNonSequential = 0x05,
P2WSH = 0x03,
P2WSHNonSequential = 0x07,
}

#[repr(u8)]
Expand Down
Loading

0 comments on commit 7e533bd

Please sign in to comment.