Script Functions

int wally_scriptpubkey_get_type(const unsigned char *bytes, size_t bytes_len, size_t *written)

Determine the type of a scriptPubkey script.

Parameters:
  • bytes – Bytes of the scriptPubkey.

  • bytes_len – Length of bytes in bytes.

  • written – Destination for the script type, one of the Script type constants.

Returns:

See Error Codes

int wally_scriptpubkey_p2pkh_from_bytes(const unsigned char *bytes, size_t bytes_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a P2PKH scriptPubkey.

Parameters:
Returns:

See Variable Length Output Buffers

int wally_scriptsig_p2pkh_from_sig(const unsigned char *pub_key, size_t pub_key_len, const unsigned char *sig, size_t sig_len, uint32_t sighash, unsigned char *bytes_out, size_t len, size_t *written)

Create a P2PKH scriptSig from a pubkey and compact signature.

This function creates the scriptSig by converting sig to DER encoding, appending the given sighash, then calling wally_scriptsig_p2pkh_from_der.

Parameters:
  • pub_key – The public key to create a scriptSig with.

  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_LEN or EC_PUBLIC_KEY_UNCOMPRESSED_LEN.

  • sig – The compact signature to create a scriptSig with.

  • sig_len – The length of sig in bytes. Must be EC_SIGNATURE_LEN.

  • sighashWALLY_SIGHASH_ flags specifying the type of signature desired.

  • bytes_out – Destination for the resulting scriptSig.

  • len – The length of bytes_out in bytes.

  • len – Size of bytes_out. Passing WALLY_SCRIPTSIG_P2PKH_MAX_LEN will ensure the buffer is large enough.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_witness_p2wpkh_from_sig(const unsigned char *pub_key, size_t pub_key_len, const unsigned char *sig, size_t sig_len, uint32_t sighash, struct wally_tx_witness_stack **witness)

Create a P2WPKH witness from a pubkey and compact signature.

Parameters:
  • pub_key – The public key to create a witness with.

  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_LEN or EC_PUBLIC_KEY_UNCOMPRESSED_LEN.

  • sig – The compact signature to create a witness with.

  • sig_len – The length of sig in bytes. Must be EC_SIGNATURE_LEN.

  • sighashWALLY_SIGHASH_ flags specifying the type of signature desired.

  • witness – Destination for the newly created witness.

Returns:

See Error Codes

int wally_scriptsig_p2pkh_from_der(const unsigned char *pub_key, size_t pub_key_len, const unsigned char *sig, size_t sig_len, unsigned char *bytes_out, size_t len, size_t *written)

Create a P2PKH scriptSig from a pubkey and DER signature plus sighash.

Parameters:
  • pub_key – The public key to create a scriptSig with.

  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_LEN EC_PUBLIC_KEY_UNCOMPRESSED_LEN.

  • sig – The DER encoded signature to create a scriptSig, with the sighash byte appended to it.

  • sig_len – The length of sig in bytes.

  • bytes_out – Destination for the resulting scriptSig.

  • len – Size of bytes_out. Passing WALLY_SCRIPTSIG_P2PKH_MAX_LEN will ensure the buffer is large enough.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_witness_p2wpkh_from_der(const unsigned char *pub_key, size_t pub_key_len, const unsigned char *sig, size_t sig_len, struct wally_tx_witness_stack **witness)

Create a P2WPKH witness from a pubkey and DER signature plus sighash.

Parameters:
  • pub_key – The public key to create a witness with.

  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_LEN EC_PUBLIC_KEY_UNCOMPRESSED_LEN.

  • sig – The DER encoded signature to create a witness, with the sighash byte appended to it.

  • sig_len – The length of sig in bytes.

  • witness – Destination for the newly created witness.

Returns:

See Error Codes

int wally_witness_p2tr_from_sig(const unsigned char *sig, size_t sig_len, struct wally_tx_witness_stack **witness)

Create a P2TR keyspend witness from a BIP340 signature plus optional sighash.

Parameters:
  • sig – The BIP340-encoded keyspend signature, including a sighash byte for non WALLY_SIGHASH_DEFAULT sighashes.

  • sig_len – The length of sig in bytes. Must be 64 or 65.

  • witness – Destination for the newly created witness.

Returns:

See Error Codes

int wally_scriptpubkey_op_return_from_bytes(const unsigned char *bytes, size_t bytes_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create an OP_RETURN scriptPubkey.

Parameters:
  • bytes – Bytes to create a scriptPubkey for.

  • bytes_len – Length of bytes in bytes. Must be less than or equal to WALLY_MAX_OP_RETURN_LEN.

  • flags – Currently unused, must be 0.

  • bytes_out – Destination for the resulting scriptPubkey.

  • len – Size of bytes_out. Passing WALLY_SCRIPTPUBKEY_OP_RETURN_MAX_LEN will ensure the buffer is large enough.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_scriptpubkey_p2sh_from_bytes(const unsigned char *bytes, size_t bytes_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a P2SH scriptPubkey.

Parameters:
  • bytes – Bytes to create a scriptPubkey for. If WALLY_SCRIPT_HASH160 is given, bytes is a redeem script to hash160 before creating the P2SH. Otherwise, bytes_len must be HASH160_LEN and bytes must contain the hash160 to use.

  • bytes_len – Length of bytes in bytes.

  • flagsWALLY_SCRIPT_HASH160 or 0.

  • bytes_out – Destination for the resulting scriptPubkey.

  • len – Size of bytes_out. Passing WALLY_SCRIPTPUBKEY_P2SH_LEN will ensure the buffer is large enough.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_scriptpubkey_multisig_from_bytes(const unsigned char *bytes, size_t bytes_len, uint32_t threshold, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a multisig scriptPubkey.

Parameters:
  • bytes – Compressed public keys to create a scriptPubkey from.

  • bytes_len – Length of bytes in bytes. Must be a multiple of EC_PUBLIC_KEY_LEN.

  • threshold – The number of signatures that must match to satisfy the script.

  • flags – Must be WALLY_SCRIPT_MULTISIG_SORTED for BIP67 sorting or 0.

  • bytes_out – Destination for the resulting scriptPubkey.

  • len – The length of bytes_out in bytes.

  • written – Destination for the number of bytes written to bytes_out.

Note

A maximum of 15 keys are allowed to be passed.

Returns:

See Variable Length Output Buffers

int wally_scriptsig_multisig_from_bytes(const unsigned char *script, size_t script_len, const unsigned char *bytes, size_t bytes_len, const uint32_t *sighash, size_t sighash_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a multisig scriptSig.

Parameters:
  • script – The redeem script this scriptSig provides signatures for.

  • script_len – The length of script in bytes.

  • bytes – Compact signatures to place in the scriptSig.

  • bytes_len – Length of bytes in bytes. Must be a multiple of EC_SIGNATURE_LEN.

  • sighashWALLY_SIGHASH_ flags for each signature in bytes.

  • sighash_len – The number of sighash flags in sighash.

  • flags – Must be zero.

  • bytes_out – Destination for the resulting scriptSig.

  • len – The length of bytes_out in bytes.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_witness_multisig_from_bytes(const unsigned char *script, size_t script_len, const unsigned char *bytes, size_t bytes_len, const uint32_t *sighash, size_t sighash_len, uint32_t flags, struct wally_tx_witness_stack **witness)

Create a multisig scriptWitness.

Parameters:
  • script – The witness script this scriptWitness provides signatures for.

  • script_len – The length of script in bytes.

  • bytes – Compact signatures to place in the scriptWitness.

  • bytes_len – Length of bytes in bytes. Must be a multiple of EC_SIGNATURE_LEN.

  • sighashWALLY_SIGHASH_ flags for each signature in bytes.

  • sighash_len – The number of sighash flags in sighash.

  • flags – Must be zero.

  • witness – Destination for newly allocated witness.

Returns:

See Error Codes

int wally_scriptpubkey_csv_2of2_then_1_from_bytes(const unsigned char *bytes, size_t bytes_len, uint32_t csv_blocks, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a CSV 2of2 multisig with a single key recovery scriptPubkey.

The resulting output can be spent at any time with both of the two keys given, and by the last (recovery) key alone, csv_blocks after the output confirms.

Parameters:
  • bytes – Compressed public keys to create a scriptPubkey from. The second key given will be used as the recovery key.

  • bytes_len – Length of bytes in bytes. Must 2 * EC_PUBLIC_KEY_LEN.

  • csv_blocks – The number of blocks before the recovery key can be used. Must be between 17 and 65536.

  • flags – Must be zero.

  • bytes_out – Destination for the resulting scriptPubkey.

  • len – The length of bytes_out in bytes.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_scriptpubkey_csv_2of2_then_1_from_bytes_opt(const unsigned char *bytes, size_t bytes_len, uint32_t csv_blocks, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create an optimised CSV 2of2 multisig with a single key recovery scriptPubkey.

Works like wally_scriptpubkey_csv_2of2_then_1_from_bytes but produces a script that is smaller and compatible with the miniscript expression “and(pk(key_user),or(99@pk(key_service),older(<csv_blocks>)))”.

Returns:

See Variable Length Output Buffers

int wally_script_push_from_bytes(const unsigned char *bytes, size_t bytes_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a bitcoin script that pushes data to the stack.

Parameters:
  • bytes – Bytes to create a push script for.

  • bytes_len – Length of bytes in bytes.

  • flagsWALLY_SCRIPT_HASH160 or WALLY_SCRIPT_SHA256 to hash bytes before pushing it.

  • bytes_out – Destination for the resulting push script.

  • len – The length of bytes_out in bytes.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_varint_get_length(uint64_t value, size_t *written)

Get the length of an integer serialized to a varint.

Parameters:
  • value – The integer value to be find the length of.

  • written – Destination for the length of the integer when serialized.

Returns:

See Error Codes

int wally_varint_to_bytes(uint64_t value, unsigned char *bytes_out, size_t len, size_t *written)

Serialize an integer to a buffer as a varint.

Parameters:
  • value – The integer value to be serialized.

  • bytes_out – Destination for the resulting serialized varint.

  • len – The length of bytes_out in bytes. Must be at least wally_varint_get_length(value).

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_varbuff_get_length(const unsigned char *bytes, size_t bytes_len, size_t *written)

Get the length of a buffer serialized to a varbuff (varint size, followed by the buffer).

Parameters:
  • bytes – The buffer to get the length of.

  • bytes_len – Length of bytes in bytes.

  • written – Destination for the length of the buffer when serialized.

Returns:

See Error Codes

int wally_varbuff_to_bytes(const unsigned char *bytes, size_t bytes_len, unsigned char *bytes_out, size_t len, size_t *written)

Serialize a buffer to a varbuff (varint size, followed by the buffer).

Parameters:
  • bytes – The buffer to be serialized.

  • bytes_len – Length of bytes in bytes.

  • bytes_out – Destination for the resulting serialized varbuff.

  • len – The length of bytes_out in bytes. Must be at least wally_varbuff_get_length(bytes, bytes_len).

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_witness_program_from_bytes(const unsigned char *bytes, size_t bytes_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a segwit witness program from a script or hash.

Parameters:
Returns:

See Variable Length Output Buffers

int wally_witness_program_from_bytes_and_version(const unsigned char *bytes, size_t bytes_len, uint32_t version, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a segwit witness program from a script or hash using witness version.

Parameters:
  • bytes – Script or hash bytes to create a witness program from.

  • bytes_len – Length of bytes in bytes.

  • version – Witness version to create a witness program from. Specify a value of 16 or less.

  • flagsWALLY_SCRIPT_HASH160 or WALLY_SCRIPT_SHA256 to hash the input script before using it. WALLY_SCRIPT_AS_PUSH to generate a push of the generated script as used for the scriptSig in p2sh-p2wpkh and p2sh-p2wsh.

  • bytes_out – Destination for the resulting witness program.

  • len – Size of bytes_out. Passing WALLY_WITNESSSCRIPT_MAX_LEN will ensure the buffer is large enough.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_elements_pegout_script_size(size_t genesis_blockhash_len, size_t mainchain_script_len, size_t sub_pubkey_len, size_t whitelistproof_len, size_t *written)

Get the pegout script size.

Parameters:
  • genesis_blockhash_len – Length of genesis_blockhash in bytes. Must be SHA256_LEN.

  • mainchain_script_len – Length of mainchain_script in bytes.

  • sub_pubkey_len – Length of sub_pubkey in bytes. Must be EC_PUBLIC_KEY_LEN.

  • whitelistproof_len – The length of whitelistproof in bytes.

  • written – Destination for the number of bytes required to hold the pegout script.

Returns:

See Error Codes

int wally_elements_pegout_script_from_bytes(const unsigned char *genesis_blockhash, size_t genesis_blockhash_len, const unsigned char *mainchain_script, size_t mainchain_script_len, const unsigned char *sub_pubkey, size_t sub_pubkey_len, const unsigned char *whitelistproof, size_t whitelistproof_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a pegout script.

Parameters:
  • genesis_blockhash – The genesis blockhash of the parent chain.

  • genesis_blockhash_len – Length of genesis_blockhash in bytes. Must be SHA256_LEN.

  • mainchain_script – The parent chain script.

  • mainchain_script_len – Length of mainchain_script in bytes.

  • sub_pubkey – The whitelisted public key.

  • sub_pubkey_len – Length of sub_pubkey in bytes. Must be EC_PUBLIC_KEY_LEN.

  • whitelistproof – The whitelist proof.

  • whitelistproof_len – The length of whitelistproof in bytes.

  • flags – Must be zero.

  • bytes_out – Destination for the resulting pegout script.

  • len – The length of bytes_out in bytes.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

int wally_elements_pegin_contract_script_from_bytes(const unsigned char *redeem_script, size_t redeem_script_len, const unsigned char *script, size_t script_len, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Create a script for P2CH pegin transactions.

Parameters:
  • redeem_script – The federation redeem script.

  • redeem_script_len – Length of redeem_script in bytes.

  • script – The claim script.

  • script_len – Length of script in bytes.

  • flags – Must be zero.

  • bytes_out – Destination for the resulting script.

  • len – Length of bytes_out in bytes.

  • written – Destination for the number of bytes written to bytes_out.

Returns:

See Variable Length Output Buffers

Script Constants

Script type constants

WALLY_SCRIPT_TYPE_UNKNOWN
WALLY_SCRIPT_TYPE_OP_RETURN
WALLY_SCRIPT_TYPE_P2PKH
WALLY_SCRIPT_TYPE_P2SH
WALLY_SCRIPT_TYPE_P2WPKH
WALLY_SCRIPT_TYPE_P2WSH
WALLY_SCRIPT_TYPE_MULTISIG
WALLY_SCRIPT_TYPE_P2TR
WALLY_SCRIPT_TYPE_CSV2OF2_1
WALLY_SCRIPT_TYPE_CSV2OF2_1_OPT
WALLY_SCRIPTPUBKEY_P2PKH_LEN

OP_DUP OP_HASH160 [HASH160] OP_EQUALVERIFY OP_CHECKSIG

WALLY_SCRIPTPUBKEY_P2SH_LEN

OP_HASH160 [HASH160] OP_EQUAL

WALLY_SCRIPTPUBKEY_P2WPKH_LEN

OP_0 [HASH160]

WALLY_SCRIPTPUBKEY_P2WSH_LEN

OP_0 [SHA256]

WALLY_SCRIPTPUBKEY_P2TR_LEN

OP_1 [X-ONLY-PUBKEY]

WALLY_SCRIPTPUBKEY_OP_RETURN_MAX_LEN

OP_RETURN [80 bytes of data]

WALLY_MAX_OP_RETURN_LEN
WALLY_SCRIPTSIG_P2PKH_MAX_LEN

[SIG+SIGHASH] [PUBKEY]

WALLY_SCRIPTSIG_MAX_LEN

MAX_SCRIPT_ELEMENT_SIZE from core

WALLY_WITNESSSCRIPT_MAX_LEN

(PUSH OF)0 [Up to 40 bytes of data]

WALLY_SCRIPT_VARINT_MAX_SIZE
WALLY_SCRIPT_HASH160

hash160 input bytes before using them

WALLY_SCRIPT_SHA256

sha256 input bytes before using them

WALLY_SCRIPT_AS_PUSH

Return a push of the generated script

WALLY_SCRIPT_MULTISIG_SORTED

Sort public keys (BIP67)

OP_0
OP_FALSE
OP_PUSHDATA1
OP_PUSHDATA2
OP_PUSHDATA4
OP_1NEGATE
OP_RESERVED
OP_1
OP_TRUE
OP_2
OP_3
OP_4
OP_5
OP_6
OP_7
OP_8
OP_9
OP_10
OP_11
OP_12
OP_13
OP_14
OP_15
OP_16
OP_NOP
OP_VER
OP_IF
OP_NOTIF
OP_VERIF
OP_VERNOTIF
OP_ELSE
OP_ENDIF
OP_VERIFY
OP_RETURN
OP_TOALTSTACK
OP_FROMALTSTACK
OP_2DROP
OP_2DUP
OP_3DUP
OP_2OVER
OP_2ROT
OP_2SWAP
OP_IFDUP
OP_DEPTH
OP_DROP
OP_DUP
OP_NIP
OP_OVER
OP_PICK
OP_ROLL
OP_ROT
OP_SWAP
OP_TUCK
OP_CAT
OP_SUBSTR
OP_LEFT
OP_RIGHT
OP_SIZE
OP_INVERT
OP_AND
OP_OR
OP_XOR
OP_EQUAL
OP_EQUALVERIFY
OP_RESERVED1
OP_RESERVED2
OP_1ADD
OP_1SUB
OP_2MUL
OP_2DIV
OP_NEGATE
OP_ABS
OP_NOT
OP_0NOTEQUAL
OP_ADD
OP_SUB
OP_MUL
OP_DIV
OP_MOD
OP_LSHIFT
OP_RSHIFT
OP_BOOLAND
OP_BOOLOR
OP_NUMEQUAL
OP_NUMEQUALVERIFY
OP_NUMNOTEQUAL
OP_LESSTHAN
OP_GREATERTHAN
OP_LESSTHANOREQUAL
OP_GREATERTHANOREQUAL
OP_MIN
OP_MAX
OP_WITHIN
OP_RIPEMD160
OP_SHA1
OP_SHA256
OP_HASH160
OP_HASH256
OP_CODESEPARATOR
OP_CHECKSIG
OP_CHECKSIGVERIFY
OP_CHECKMULTISIG
OP_CHECKMULTISIGVERIFY
OP_NOP1
OP_CHECKLOCKTIMEVERIFY
OP_NOP2
OP_CHECKSEQUENCEVERIFY
OP_NOP3
OP_NOP4
OP_NOP5
OP_NOP6
OP_NOP7
OP_NOP8
OP_NOP9
OP_NOP10
OP_INVALIDOPCODE