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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int

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 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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int

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 non-zero and less than 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:

WALLY_OK or an error code.

Return type:

int

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 non-zero and less than 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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int

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:

WALLY_OK or an error code.

Return type:

int