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 WALLY_SCRIPT_TYPE_ script type.
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:
  • bytes – Bytes to create a scriptPubkey for.
  • bytes_len – The length of bytes in bytes. If WALLY_SCRIPT_HASH160 is given in flags, bytes is a public key to hash160 before creating the P2PKH, and bytes_len must be EC_PUBLIC_KEY_LEN or EC_PUBLIC_KEY_UNCOMPRESSED_LEN. Otherwise, bytes_len must be HASH160_LEN and bytes must contain the hash160 to use.
  • flagsWALLY_SCRIPT_HASH160 or 0.
  • bytes_out – Destination for the resulting scriptPubkey.
  • len – 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_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.
  • 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 – 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_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_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 – The length of bytes_out in bytes. Passing WALLY_SCRIPTPUBKEY_OP_RETURN_MAX_LEN will ensure there is always enough room for the resulting scriptPubkey.
  • 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.
  • bytes_len – Length of bytes in bytes.
  • flagsWALLY_SCRIPT_HASH160 or 0.
  • bytes_out – Destination for the resulting scriptPubkey.
  • len – The length of bytes_out in bytes. If WALLY_SCRIPT_HASH160 is given, bytes is a script to hash160 before creating the P2SH. Otherwise, bytes_len must be HASH160_LEN and bytes must contain the hash160 to use.
  • 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>)))”. :return: See Variable Length Output Buffers

int wally_scriptpubkey_csv_2of3_then_2_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 2of3 multisig with two key recovery scriptPubkey.

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

Parameters:
  • bytes – Compressed public keys to create a scriptPubkey from. The second and third keys given will be used as the recovery keys.
  • bytes_len – Length of bytes in bytes. Must 3 * EC_PUBLIC_KEY_LEN.
  • csv_blocks – The number of blocks before the recovery keys 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_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:
  • bytes – Script or hash bytes to create a witness program from.
  • bytes_len – Length of bytes in bytes. Must be HASH160_LEN or SHA256_LEN if neither WALLY_SCRIPT_HASH160 or WALLY_SCRIPT_SHA256 is given.
  • 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 – 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_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 – 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_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