Psbt Functions

int wally_map_init_alloc(size_t allocation_len, struct wally_map **output)

Allocate and initialize a new map.

Parameters:
  • allocation_len – The number of items to allocate.
  • output – Destination for the new map.
Returns:

See Error Codes

int wally_map_free(struct wally_map *map_in)

Free a map allocated by wally_map_init_alloc.

Parameters:
  • map_in – The map to free.
Returns:

See Error Codes

int wally_map_find(const struct wally_map *map_in, const unsigned char *key, size_t key_len, size_t *written)

Find an item in a map.

Parameters:
  • map_in – The map to find key in.
  • key – The key to find.
  • key_len – Length of key in bytes.
  • written – On success, set to zero if the item is not found, otherwise the index of the item plus one.
Returns:

See Error Codes

int wally_map_add(struct wally_map *map_in, const unsigned char *key, size_t key_len, const unsigned char *value, size_t value_len)

Add an item to a map.

Parameters:
  • map_in – The map to add to.
  • key – The key to add.
  • key_len – Length of key in bytes.
  • value – The value to add.
  • value_len – Length of value in bytes.
Returns:

See Error Codes

int wally_map_add_keypath_item(struct wally_map *map_in, const unsigned char *pub_key, size_t pub_key_len, const unsigned char *fingerprint, size_t fingerprint_len, const uint32_t *child_path, size_t child_path_len)

Convert and add a pubkey/keypath to a map.

Parameters:
  • map_in – The map to add to.
  • pub_key – The pubkey to add.
  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_UNCOMPRESSED_LEN or EC_PUBLIC_KEY_LEN.
  • fingerprint – The master key fingerprint for the pubkey.
  • fingerprint_len – Length of fingerprint in bytes. Must be BIP32_KEY_FINGERPRINT_LEN.
  • child_path – The BIP32 derivation path for the pubkey.
  • child_path_len – The number of items in child_path.
Returns:

See Error Codes

int wally_map_sort(struct wally_map *map_in, uint32_t flags)

Sort the items in a map.

Parameters:
  • map_in – The map to sort.
  • flags – Flags controlling sorting. Must be 0.
Returns:

See Error Codes

int wally_psbt_input_is_finalized(const struct wally_psbt_input *input, size_t *written)

Determine if a PSBT input is finalized.

Parameters:
  • input – The input to check.
  • written – On success, set to one if the input is finalized, otherwise zero.
Returns:

See Error Codes

int wally_psbt_input_set_utxo(struct wally_psbt_input *input, const struct wally_tx *utxo)

Set the utxo in an input.

Parameters:
  • input – The input to update.
  • utxo – The (non witness) utxo for this input if it exists.
Returns:

See Error Codes

int wally_psbt_input_set_witness_utxo(struct wally_psbt_input *input, const struct wally_tx_output *witness_utxo)

Set the witness_utxo in an input.

Parameters:
  • input – The input to update.
  • witness_utxo – The witness utxo for this input if it exists.
Returns:

See Error Codes

int wally_psbt_input_set_redeem_script(struct wally_psbt_input *input, const unsigned char *script, size_t script_len)

Set the redeem_script in an input.

Parameters:
  • input – The input to update.
  • script – The redeem script for this input.
  • script_len – Length of script in bytes.
Returns:

See Error Codes

int wally_psbt_input_set_witness_script(struct wally_psbt_input *input, const unsigned char *script, size_t script_len)

Set the witness_script in an input.

Parameters:
  • input – The input to update.
  • script – The witness script for this input.
  • script_len – Length of script in bytes.
Returns:

See Error Codes

int wally_psbt_input_set_final_scriptsig(struct wally_psbt_input *input, const unsigned char *final_scriptsig, size_t final_scriptsig_len)

Set the final_scriptsig in an input.

Parameters:
  • input – The input to update.
  • final_scriptsig – The scriptSig for this input.
  • final_scriptsig_len – Length of final_scriptsig in bytes.
Returns:

See Error Codes

int wally_psbt_input_set_final_witness(struct wally_psbt_input *input, const struct wally_tx_witness_stack *final_witness)

Set the final_witness in an input.

Parameters:
  • input – The input to update.
  • final_witness – The witness stack for the input, or NULL if no witness is present.
Returns:

See Error Codes

int wally_psbt_input_set_keypaths(struct wally_psbt_input *input, const struct wally_map *map_in)

Set the keypaths in an input.

Parameters:
  • input – The input to update.
  • map_in – The HD keypaths for this input.
Returns:

See Error Codes

int wally_psbt_input_find_keypath(struct wally_psbt_input *input, const unsigned char *pub_key, size_t pub_key_len, size_t *written)

Find a keypath matching a pubkey in an input.

Parameters:
  • input – The input to search in.
  • pub_key – The pubkey to find.
  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_UNCOMPRESSED_LEN or EC_PUBLIC_KEY_LEN.
  • written – On success, set to zero if the item is not found, otherwise the index of the item plus one.
Returns:

See Error Codes

int wally_psbt_input_add_keypath_item(struct wally_psbt_input *input, const unsigned char *pub_key, size_t pub_key_len, const unsigned char *fingerprint, size_t fingerprint_len, const uint32_t *child_path, size_t child_path_len)

Convert and add a pubkey/keypath to an input.

Parameters:
  • input – The input to add to.
  • pub_key – The pubkey to add.
  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_UNCOMPRESSED_LEN or EC_PUBLIC_KEY_LEN.
  • fingerprint – The master key fingerprint for the pubkey.
  • fingerprint_len – Length of fingerprint in bytes. Must be BIP32_KEY_FINGERPRINT_LEN.
  • child_path – The BIP32 derivation path for the pubkey.
  • child_path_len – The number of items in child_path.
Returns:

See Error Codes

int wally_psbt_input_set_signatures(struct wally_psbt_input *input, const struct wally_map *map_in)

Set the partial signatures in an input.

Parameters:
  • input – The input to update.
  • map_in – The partial signatures for this input.
Returns:

See Error Codes

int wally_psbt_input_find_signature(struct wally_psbt_input *input, const unsigned char *pub_key, size_t pub_key_len, size_t *written)

Find a partial signature matching a pubkey in an input.

Parameters:
  • input – The input to search in.
  • pub_key – The pubkey to find.
  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_UNCOMPRESSED_LEN or EC_PUBLIC_KEY_LEN.
  • written – On success, set to zero if the item is not found, otherwise the index of the item plus one.
Returns:

See Error Codes

int wally_psbt_input_add_signature(struct wally_psbt_input *input, const unsigned char *pub_key, size_t pub_key_len, const unsigned char *sig, size_t sig_len)

Add a pubkey/partial signature item to an input.

Parameters:
  • input – The input to add the partial signature to.
  • pub_key – The pubkey to find.
  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_UNCOMPRESSED_LEN or EC_PUBLIC_KEY_LEN.
  • sig – The DER-encoded signature plus sighash byte to add.
  • sig_len – The length of sig in bytes.
Returns:

See Error Codes

int wally_psbt_input_set_unknowns(struct wally_psbt_input *input, const struct wally_map *map_in)

Set the unknown values in an input.

Parameters:
  • input – The input to update.
  • map_in – The unknown key value pairs for this input.
Returns:

See Error Codes

int wally_psbt_input_find_unknown(struct wally_psbt_input *input, const unsigned char *key, size_t key_len, size_t *written)

Find an unknown item matching a key in an input.

Parameters:
  • input – The input to search in.
  • key – The key to find.
  • key_len – Length of key in bytes.
  • written – On success, set to zero if the item is not found, otherwise the index of the item plus one.
Returns:

See Error Codes

int wally_psbt_input_set_sighash(struct wally_psbt_input *input, uint32_t sighash)

Set the sighash type in an input.

Parameters:
  • input – The input to update.
  • sighash – The sighash type for this input.
Returns:

See Error Codes

int wally_psbt_output_set_redeem_script(struct wally_psbt_output *output, const unsigned char *script, size_t script_len)

Set the redeem_script in an output.

Parameters:
  • output – The input to update.
  • script – The redeem script for this output.
  • script_len – Length of script in bytes.
Returns:

See Error Codes

int wally_psbt_output_set_witness_script(struct wally_psbt_output *output, const unsigned char *script, size_t script_len)

Set the witness_script in an output.

Parameters:
  • output – The output to update.
  • script – The witness script for this output.
  • script_len – Length of script in bytes.
Returns:

See Error Codes

int wally_psbt_output_set_keypaths(struct wally_psbt_output *output, const struct wally_map *map_in)

Set the keypaths in an output.

Parameters:
  • output – The output to update.
  • map_in – The HD keypaths for this output.
Returns:

See Error Codes

int wally_psbt_output_find_keypath(struct wally_psbt_output *output, const unsigned char *pub_key, size_t pub_key_len, size_t *written)

Find a keypath matching a pubkey in an output.

Parameters:
  • output – The output to search in.
  • pub_key – The pubkey to find.
  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_UNCOMPRESSED_LEN or EC_PUBLIC_KEY_LEN.
  • written – On success, set to zero if the item is not found, otherwise the index of the item plus one.
Returns:

See Error Codes

int wally_psbt_output_add_keypath_item(struct wally_psbt_output *output, const unsigned char *pub_key, size_t pub_key_len, const unsigned char *fingerprint, size_t fingerprint_len, const uint32_t *child_path, size_t child_path_len)

Convert and add a pubkey/keypath to an output.

Parameters:
  • output – The output to add to.
  • pub_key – The pubkey to add.
  • pub_key_len – Length of pub_key in bytes. Must be EC_PUBLIC_KEY_UNCOMPRESSED_LEN or EC_PUBLIC_KEY_LEN.
  • fingerprint – The master key fingerprint for the pubkey.
  • fingerprint_len – Length of fingerprint in bytes. Must be BIP32_KEY_FINGERPRINT_LEN.
  • child_path – The BIP32 derivation path for the pubkey.
  • child_path_len – The number of items in child_path.
Returns:

See Error Codes

int wally_psbt_output_set_unknowns(struct wally_psbt_output *output, const struct wally_map *map_in)

Set the unknown map in an output.

Parameters:
  • output – The output to update.
  • map_in – The unknown key value pairs for this output.
Returns:

See Error Codes

int wally_psbt_output_find_unknown(struct wally_psbt_output *output, const unsigned char *key, size_t key_len, size_t *written)

Find an unknown item matching a key in an output.

Parameters:
  • output – The output to search in.
  • key – The key to find.
  • key_len – Length of key in bytes.
  • written – On success, set to zero if the item is not found, otherwise the index of the item plus one.
Returns:

See Error Codes

int wally_psbt_init_alloc(uint32_t version, size_t inputs_allocation_len, size_t outputs_allocation_len, size_t global_unknowns_allocation_len, struct wally_psbt **output)

Allocate and initialize a new PSBT.

Parameters:
  • version – The version of the PSBT. Must be 0.
  • inputs_allocation_len – The number of inputs to pre-allocate space for.
  • outputs_allocation_len – The number of outputs to pre-allocate space for.
  • global_unknowns_allocation_len – The number of global unknowns to allocate space for.
  • output – Destination for the resulting PSBT output.
Returns:

See Error Codes

int wally_psbt_free(struct wally_psbt *psbt)

Free a PSBT allocated by wally_psbt_init_alloc.

Parameters:
  • psbt – The PSBT to free.
Returns:

See Error Codes

int wally_psbt_is_finalized(const struct wally_psbt *psbt, size_t *written)

Determine if all PSBT inputs are finalized.

Parameters:
  • psbt – The PSBT to check.
  • written – On success, set to one if the PSBT is finalized, otherwise zero.
Returns:

See Error Codes

int wally_psbt_set_global_tx(struct wally_psbt *psbt, const struct wally_tx *tx)

Set the global transaction for a PSBT.

Parameters:
  • psbt – The PSBT to set the transaction for.
  • tx – The transaction to set.

The global transaction can only be set on a newly created PSBT. After this call completes the PSBT will have empty inputs and outputs for each input and output in the transaction tx given. :return: See Error Codes

int wally_psbt_add_input_at(struct wally_psbt *psbt, uint32_t index, uint32_t flags, const struct wally_tx_input *input)

Add a transaction input to PBST at a given position.

Parameters:
  • psbt – The PSBT to add the input to.
  • index – The zero-based index of the position to add the input at.
  • flags – Flags controlling input insertion. Must be 0 or WALLY_PSBT_FLAG_NON_FINAL.
  • input – The transaction input to add.
Returns:

See Error Codes

int wally_psbt_remove_input(struct wally_psbt *psbt, uint32_t index)

Remove a transaction input from a PBST.

Parameters:
  • psbt – The PSBT to remove the input from.
  • index – The zero-based index of the input to remove.
Returns:

See Error Codes

int wally_psbt_add_output_at(struct wally_psbt *psbt, uint32_t index, uint32_t flags, const struct wally_tx_output *output)

Add a transaction output to PBST at a given position.

Parameters:
  • psbt – The PSBT to add the output to.
  • index – The zero-based index of the position to add the output at.
  • flags – Flags controlling output insertion. Must be 0.
  • output – The transaction output to add.
Returns:

See Error Codes

int wally_psbt_remove_output(struct wally_psbt *psbt, uint32_t index)

Remove a transaction output from a PBST.

Parameters:
  • psbt – The PSBT to remove the output from.
  • index – The zero-based index of the output to remove.
Returns:

See Error Codes

int wally_psbt_from_bytes(const unsigned char *bytes, size_t bytes_len, struct wally_psbt **output)

Create a PSBT from its serialized bytes.

Parameters:
  • bytes – Bytes to create the PSBT from.
  • bytes_len – Length of bytes in bytes.
  • output – Destination for the resulting PSBT.
Returns:

See Error Codes

int wally_psbt_get_length(const struct wally_psbt *psbt, uint32_t flags, size_t *written)

Get the length of a PSBT when serialized to bytes.

Parameters:
  • psbt – the PSBT.
  • flags – Flags controlling length determination. Must be 0.
  • written – Destination for the length in bytes when serialized.
Returns:

See Error Codes

int wally_psbt_to_bytes(const struct wally_psbt *psbt, uint32_t flags, unsigned char *bytes_out, size_t len, size_t *written)

Serialize a PSBT to bytes.

Parameters:
  • psbt – the PSBT to serialize.
  • flags – Flags controlling serialization. Must be 0.
  • bytes_out – Bytes to create the transaction from.
  • len – Length of bytes in bytes (use wally_psbt_get_length).
  • written – number of bytes written to bytes_out.
Returns:

See Variable Length Output Buffers

int wally_psbt_from_base64(const char *base64, struct wally_psbt **output)

Create a PSBT from its serialized base64 string.

Parameters:
  • base64 – Base64 string to create the PSBT from.
  • output – Destination for the resulting PSBT.
Returns:

See Error Codes

int wally_psbt_to_base64(const struct wally_psbt *psbt, uint32_t flags, char **output)

Serialize a PSBT to a base64 string.

Parameters:
  • psbt – the PSBT to serialize.
  • flags – Flags controlling serialization. Must be 0.
  • output – Destination for the resulting serialized PSBT.
Returns:

See Error Codes

int wally_psbt_combine(struct wally_psbt *psbt, const struct wally_psbt *src)

Combine the metadata from a source PSBT into another PSBT.

Parameters:
  • psbt – the PSBT to combine into.
  • source – the PSBT to copy data from.
Returns:

See Error Codes

int wally_psbt_clone_alloc(const struct wally_psbt *psbt, uint32_t flags, struct wally_psbt **output)

Clone a PSBT into a newly allocated copy.

Parameters:
  • psbt – the PSBT to clone.
  • flags – Flags controlling PSBT creation. Must be 0.
  • output – Destination for the resulting cloned PSBT.
Returns:

See Error Codes

int wally_psbt_sign(struct wally_psbt *psbt, const unsigned char *key, size_t key_len, uint32_t flags)

Sign a PSBT using the simple signer algorithm.

Parameters:
  • psbt – PSBT to sign. Directly modifies this PSBT.
  • key – Private key to sign PSBT with.
  • key_len – Length of key in bytes. Must be EC_PRIVATE_KEY_LEN.
  • flags – Flags controlling sigining. Must be 0 or EC_FLAG_GRIND_R.

Note

See https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#simple-signer-algorithm for a description of the simple signer algorithm.

Returns:See Error Codes
int wally_psbt_finalize(struct wally_psbt *psbt)

Finalize a PSBT.

Parameters:
  • psbt – PSBT to finalize. Directly modifies this PSBT.
Returns:

See Error Codes

int wally_psbt_extract(const struct wally_psbt *psbt, struct wally_tx **output)

Extract a network transaction from a finalized PSBT.

Parameters:
  • psbt – PSBT to extract from.
  • output – Destination for the resulting transaction.
Returns:

See Error Codes

int wally_psbt_is_elements(const struct wally_psbt *psbt, size_t *written)

Determine if a PSBT is an elements PSBT.

Parameters:
  • psbt – The PSBT to check.
  • written – 1 if the PSBT is an elements PSBT, otherwise 0.
Returns:

See Error Codes

int wally_psbt_elements_init_alloc(uint32_t version, size_t inputs_allocation_len, size_t outputs_allocation_len, size_t global_unknowns_allocation_len, struct wally_psbt **output)

Allocate and initialize a new elements PSBT.

Parameters:
  • version – The version of the PSBT. Must be 0.
  • inputs_allocation_len – The number of inputs to pre-allocate space for.
  • outputs_allocation_len – The number of outputs to pre-allocate space for.
  • global_unknowns_allocation_len – The number of global unknowns to allocate space for.
  • output – Destination for the resulting PSBT output.
Returns:

See Error Codes

int wally_psbt_input_set_value(struct wally_psbt_input *input, uint64_t value)

Set the value in an elements input.

Parameters:
  • input – The input to update.
  • value – The value for this input.
Returns:

See Error Codes

int wally_psbt_input_clear_value(struct wally_psbt_input *input)

Clear the value in an elements input.

Parameters:
  • input – The input to update.
Returns:

See Error Codes

int wally_psbt_input_set_vbf(struct wally_psbt_input *input, const unsigned char *vbf, size_t vbf_len)

Set the value blinding factor in an elements input.

Parameters:
  • input – The input to update.
  • vbf – The value blinding factor.
  • vbf_len – Length of vbf. Must be BLINDING_FACTOR_LEN.
Returns:

See Error Codes

int wally_psbt_input_set_asset(struct wally_psbt_input *input, const unsigned char *asset, size_t asset_len)

Set the asset in an elements input.

Parameters:
  • input – The input to update.
  • asset – The asset for this input.
  • asset_len – Length of asset in bytes.
Returns:

See Error Codes

int wally_psbt_input_set_abf(struct wally_psbt_input *input, const unsigned char *abf, size_t abf_len)

Set the asset blinding factor in an elements input

Parameters:
  • input – The input to update.
  • abf – The asset blinding factor.
  • abf_len – Length of abf in bytes. Must be BLINDING_FACTOR_LEN.
Returns:

See Error Codes

int wally_psbt_input_set_pegin_tx(struct wally_psbt_input *input, const struct wally_tx *pegin_tx)

Set the peg in tx in an input.

Parameters:
  • input – The input to update.
  • pegin_tx – The peg in tx for this input if it exists.
Returns:

See Error Codes

int wally_psbt_input_set_txoutproof(struct wally_psbt_input *input, const unsigned char *proof, size_t proof_len)

Set the txout proof in an elements input.

Parameters:
  • input – The input to update.
  • proof – The txout proof for this input.
  • proof_len – Length of proof in bytes.
Returns:

See Error Codes

int wally_psbt_input_set_genesis_blockhash(struct wally_psbt_input *input, const unsigned char *genesis_blockhash, size_t genesis_blockhash_len)

Set the genesis hash in an elements input.

Parameters:
  • input – The input to update.
  • genesis_blockhash – The genesis hash for this input.
  • genesis_blockhash_len – Length of genesis_blockhash in bytes. Must be SHA256_LEN.
Returns:

See Error Codes

int wally_psbt_input_set_claim_script(struct wally_psbt_input *input, const unsigned char *script, size_t script_len)

Set the claim script in an elements input.

Parameters:
  • input – The input to update.
  • script – The claim script for this input.
  • script_len – Length of script in bytes.
Returns:

See Error Codes

int wally_psbt_output_set_blinding_pubkey(struct wally_psbt_output *output, const unsigned char *pub_key, size_t pub_key_len)

Set the blinding pubkey in an elements output.

Parameters:
  • output – The output to update.
  • pub_key – The blinding pubkey for this output.
  • pub_key_len – Length of pub_key in bytes.
Returns:

See Error Codes

int wally_psbt_output_set_value_commitment(struct wally_psbt_output *output, const unsigned char *commitment, size_t commitment_len)

Set the value commitment in an elements output.

Parameters:
  • output – The output to update.
  • commitment – The value commitment for this output.
  • commitment_len – Length of commitment in bytes.
Returns:

See Error Codes

int wally_psbt_output_set_vbf(struct wally_psbt_output *output, const unsigned char *vbf, size_t vbf_len)

Set the value blinding factor in an elements output.

Parameters:
  • output – The output to update.
  • vbf – The value blinding factor.
  • vbf_len – Length of vbf. Must be BLINDING_FACTOR_LEN.
Returns:

See Error Codes

int wally_psbt_output_set_asset_commitment(struct wally_psbt_output *output, const unsigned char *commitment, size_t commitment_len)

Set the asset commitment in an elements output.

Parameters:
  • output – The output to update.
  • commitment – The asset commitment for this output.
  • commitment_len – Length of commitment in bytes.
Returns:

See Error Codes

int wally_psbt_output_set_abf(struct wally_psbt_output *output, const unsigned char *abf, size_t abf_len)

Set the asset blinding factor in an elements output.

Parameters:
  • output – The output to update.
  • abf – The asset blinding factor.
  • abf_len – Length of abf in bytes. Must be BLINDING_FACTOR_LEN.
Returns:

See Error Codes

int wally_psbt_output_set_nonce(struct wally_psbt_output *output, const unsigned char *nonce, size_t nonce_len)

Set the nonce commitment in an elements output.

Parameters:
  • output – The output to update.
  • nonce – The commitment used to create the nonce (with the blinding key) for the range proof.
  • nonce_len – Size of nonce in bytes. Must be WALLY_TX_ASSET_CT_NONCE_LEN.
Returns:

See Error Codes

int wally_psbt_output_set_rangeproof(struct wally_psbt_output *output, const unsigned char *proof, size_t proof_len)

Set the range proof in an elements output.

Parameters:
  • output – The output to update.
  • proof – The range proof for this output.
  • proof_len – Length of proof in bytes.
Returns:

See Error Codes

int wally_psbt_output_set_surjectionproof(struct wally_psbt_output *output, const unsigned char *proof, size_t proof_len)

Set the surjection proof in an elements output.

Parameters:
  • output – The output to update.
  • proof – The surjection proof for this output.
  • proof_len – Length of proof in bytes.
Returns:

See Error Codes