BSV.Contract.P2RPH (bsv_sdk v2.0.1)

Copy Markdown View Source

Pay to R-Puzzle Hash contract.

P2RPH scripts lock Bitcoin to a hash puzzle based on the R value of an ECDSA signature. The funds can be unlocked with knowledge of the corresponding K value (the ECDSA nonce), allowing the spending party to sign with any key pair.

Lock parameters

  • :r_hash — 20-byte HASH160 of the R value (preferred)
  • :r — raw R value binary (will be HASH160'd automatically)

Unlock parameters

  • :privkey — a BSV.PrivateKey.t() (any key pair works)
  • :k — the K value (ECDSA nonce) as a 32-byte binary
  • :pubkey — the corresponding compressed public key binary

How it works

The locking script extracts the R value from a signature on the stack, hashes it, and compares against the committed hash. Two signatures are required: one with the known K value (proving knowledge of K), and a standard signature from any key pair.

Example

k = BSV.Contract.P2RPH.generate_k()
r = BSV.Contract.P2RPH.get_r(k)

# Lock
contract = P2RPH.lock(1000, %{r: r})

# Unlock (with any keypair)
contract = P2RPH.unlock(utxo, %{
  privkey: my_privkey,
  k: k,
  pubkey: my_pubkey_bin
})

Summary

Functions

Generate a random K value (32-byte binary).

Compute the R value (compressed point x-coordinate) from a K value. Returns the R value as a binary, with a leading 0x00 byte if the high bit is set.

Create a locking contract with the given satoshis and parameters.

Create an unlocking contract with the given UTXO info and parameters.

Functions

generate_k()

@spec generate_k() :: binary()

Generate a random K value (32-byte binary).

get_r(arg)

@spec get_r(binary()) :: binary()

Compute the R value (compressed point x-coordinate) from a K value. Returns the R value as a binary, with a leading 0x00 byte if the high bit is set.

lock(satoshis, params, opts \\ [])

@spec lock(non_neg_integer(), map(), keyword()) :: BSV.Contract.t()

Create a locking contract with the given satoshis and parameters.

unlock(utxo_info, params, opts \\ [])

@spec unlock(map(), map(), keyword()) :: BSV.Contract.t()

Create an unlocking contract with the given UTXO info and parameters.