BSV.Transaction.P2MPKH (bsv_sdk v1.4.0)

Copy Markdown View Source

Pay-to-Multiple-Public-Key-Hash (P2MPKH) signing template.

Extends P2PKH to support m-of-n multisig ownership. The locking script stores only the HASH160 of a raw multisig script (the "MPKH") — the full multisig script remains hidden until spending.

MultisigScript

A multisig script is represented as a map:

%{threshold: 2, public_keys: [pk1, pk2, pk3]}

where each public key is a 33-byte compressed SEC1 binary.

Standalone locking script (bare multisig)

OP_m <pk1> <pk2>  <pkN> OP_n OP_CHECKMULTISIG

Standalone unlocking script

OP_0 <sig1> <sig2>  <sigM>

STAS unlocking script (P2MPKH mode)

<sig1> <sig2>  <sigM> <serialized_multisig_script>

STAS handles HASH160 verification and OP_CHECKMULTISIG internally.

Summary

Functions

Estimated unlocking script length: 1 (OP_0) + m * 73.

Parse a multisig script from raw bytes.

Create a bare multisig locking script.

Compute the MPKH — 20-byte HASH160 of the serialized multisig script.

Create a new multisig script map.

Sign a transaction input producing a bare multisig unlocking script.

Serialize a multisig script to raw bytes.

Create a P2MPKH unlocker for bare multisig.

Types

multisig_script()

@type multisig_script() :: %{threshold: pos_integer(), public_keys: [binary()]}

t()

@type t() :: %BSV.Transaction.P2MPKH{
  multisig: multisig_script(),
  private_keys: [BSV.PrivateKey.t()],
  sighash_flag: non_neg_integer()
}

Functions

estimate_length(p2_mpkh, _, _)

Estimated unlocking script length: 1 (OP_0) + m * 73.

from_script_bytes(arg1)

@spec from_script_bytes(binary()) :: {:ok, multisig_script()} | {:error, term()}

Parse a multisig script from raw bytes.

Expects: OP_m <pk1_33bytes> … <pkN_33bytes> OP_n OP_CHECKMULTISIG

lock(ms)

@spec lock(multisig_script()) :: {:ok, BSV.Script.t()} | {:error, term()}

Create a bare multisig locking script.

Produces: OP_m <pk1> … <pkN> OP_n OP_CHECKMULTISIG

mpkh(ms)

@spec mpkh(multisig_script()) :: <<_::160>>

Compute the MPKH — 20-byte HASH160 of the serialized multisig script.

new_multisig(threshold, public_keys)

@spec new_multisig(pos_integer(), [binary()]) ::
  {:ok, multisig_script()} | {:error, term()}

Create a new multisig script map.

Arguments

  • threshold — minimum signatures required (m)
  • public_keys — list of 33-byte compressed public keys

Returns

{:ok, multisig_script()} or {:error, reason}

sign(p2_mpkh, tx, input_index)

Sign a transaction input producing a bare multisig unlocking script.

Produces: OP_0 <sig1> <sig2> … <sigM>

to_script_bytes(map)

@spec to_script_bytes(multisig_script()) :: binary()

Serialize a multisig script to raw bytes.

Produces: OP_m <pk1> <pk2> … <pkN> OP_n OP_CHECKMULTISIG

unlock(private_keys, multisig, opts \\ [])

@spec unlock([BSV.PrivateKey.t()], multisig_script(), keyword()) ::
  {:ok, t()} | {:error, term()}

Create a P2MPKH unlocker for bare multisig.

Arguments

  • private_keys — the m private keys satisfying the threshold
  • multisig — the full multisig script
  • opts:sighash_flag (default 0x41)