CashScript / cashscript

⚖️ Easily write and interact with Bitcoin Cash smart contracts
https://cashscript.org
MIT License
115 stars 80 forks source link

Manual UTXO selection #44

Closed rkalis closed 4 years ago

rkalis commented 4 years ago

Some covenant contracts are reliant on having specific UTXOs as inputs. It should be possible to optionally provide the UTXOs to the transaction manually. For this it should also be possible to retrieve all UTXOs that belong to a contract instance (instance.getUTXOs()).

rkalis commented 4 years ago

This issue has two parts: the first is exposing a contract instance's UTXOs, and the second is allowing these UTXOs to be used as inputs for a transaction.

  1. Add function to retrieve an instance's UTXOs.

    instance.getUtxos(excludeUnconfirmed?: boolean): Promise<Utxo[]>

    This function should be similar to the getBalance function, but instead of balance, it should use bitbox to return the UTXOs + unconfirmed UTXOs. We could add an excludeUnconfirmed flag too.

  2. Add an optional inputs: Utxo[] field in TxOptions.

  3. If this field is used, pass in the inputs into the getInputsAndOutputs function, similar to options.fee.

  4. If this field is used, replace the UTXO selection algorithm inside the getInputsAndOutputs function. Note that fee and change calculation should still be performed regularly.

  5. Test the new functionality. Currently much of the SDK is tested through end-to-end testing on testnet, since it's difficult to test transactions locally. A few failure cases should be tested (e.g. the manual UTXOs are too small for the transaction), and then a success case should be tested (can use any existing contract, like P2PKH). Ideally this test would parse the resulting transaction object to check that the correct UTXOs are used.