Introduction

In order to avoid an expensive and error-prone consensus process, it is mandatory for wallets to be able to construct withdrawal transactions in a deterministic manner, assuming they are given the withdrawal requests in a deterministic order. Once this is accomplished, then it only becomes necessary for wallets to share signatures among themselves because they know they all signed the exact same bitcoin transaction.

Procedure

Overview

Initial Conditions

The startwithdrawal API call has been received by the wallet, and all the arguments have been checked for errors.

Sequence

The basic flow of the procedure is:

Gather accounting information from the API call arguments

Validate and sort the output list

Start a new transactions

Add outputs to the transaction until either all outputs are added or all inputs are used.

Make sure all generated transactions are ready for signing.

Create a list of all signatures which the wallet is capable of generating.

Create a status list containing the deposition of every output the caller specified.

Return the status list and signature list to the caller.

Each step in this chart is more fully described below.

Preparation

Initial Conditions

The startwithdrawal API call has been received by the wallet, and all the arguments have been checked for errors.

Sequence

The algorithm must keep track of the next change address to be used. The initial value is supplied as the changestart argument. Any time a change output is allocated, the index value of the address identifier is incremented, and if a change output is remove from a transaction the index value is decremented. The final value will be returned to the caller as the nextchangestart value in the withdrawal status list.

Prepare an empty list for holding transactions as they are constructed and before they are signed.

Prepare an empty array to hold the transaction signatures which will be returned to the caller as the signatures value.

Prepare an withdrawal status object.

roundID: copied from roundID argument

nextinputstart: nil

nextchangestart: nil

fees: 0

outputs: should contain an entry for every output passed:

outBailmentID: copied from outBailmentID entry in outputs argument

status: empty string

transactions: nil

Output List Initialization

Initial Conditions

The startwithdrawal API call has been received by the wallet, and all the arguments have been checked for errors.

Sequence

Pass the inputstart, inputstop, and dustthreshold parameters to the input selection algorithm to obtain an ordered list of eligible inputs.

Perform a first pass check to determine if the requested outputs exceed the size of the eligible inputs. Note that this check is only definitive in the case where the output sum is greater than the input sum. At this stage of the algorithm, the input sum exceeding the output sum does not prove that all outputs will be successfully created.

Take the sum of both lists.

If the sum of the outputs exceeds the sum of the inputs, remove outputs in descending size order until this is no longer true. Note this may result in the output list being an empty set.

If the output list is non-empty, sort it by outBailmentID

Move both the input lists and outputs lists into separate stacks by pushing them in reverse list order. The first input and first output should be on the top of their respective stacks.

Initialize New Transaction

Initial Conditions

There are no pending transactions to in the process of being constructed, either because this is the first transaction of the algorithm or because a previous pending transaction has been moved to the finished transaction list.

The input stack contains at least one input.

The output stack contains at least one output.

Sequence

Create a minimal transaction skeleton with a header and one change output.

After creating the transaction skeleton, and after any action which alters it, the new transaction size (bytes) and minimum required transaction fee must be recalculated.

Add Next Output

Initial Conditions

The output stack contains at least one output.

There is a transaction in progress which is ready to accept new outputs. This transaction is either empty or it contains 1 or more outputs.

If the transaction already contains outputs, then it also contains sufficient inputs to cover those outputs and any required transaction fees without exceeding size limits. This is because it must have already successfully passed through this procedure before.

Sequence

Validate the output address. If it is invalid, update the withdrawal status list and from this procedure. If it is valid, add it to the transaction

Check that the transaction does not exceed size limits. If it does, follow the Oversize Transaction procedure.

Compare the sum of the inputs to the sum of the inputs and required transaction fee to determine if more inputs are required.

If no more inputs are required, then update the status for the output's associated outBailmentID to "success". The rest of the status data for this outBailmentID will be filled in at a later stage of the algorithm.

If more inputs are required, then check to see if any more are available.

According to the initial conditions, the input stack can not be empty on the first run through this loop.

If the input stack is empty at this point, then the output has added at least one input beyond what was needed to satisfy the prior output (if there was a prior output)

If the input stack is empty, then the output can only be partially fulfilled. Follow the Split Output procedure.