argonprotocol / mainchain

The Argon is an inflation-proof stablecoin powered by Bitcoin
https://argonprotocol.org
MIT No Attribution
4 stars 1 forks source link

Bitcoin cli #136

Closed blakebyrnes closed 3 months ago

blakebyrnes commented 3 months ago

blakebyrnes Authored by blakebyrnes Merged

This PR was created to ensure a human can bond bitcoins, and similarly, operate a vault. Without building an entire bitcoin wallet and app, which is probably the ultimate goal, the attempt was to ensure there's at least a compiled binary that can have a list of instructions to follow. Another goal of this effort was to maintain the possibility of signing with hardware wallets.

To that end, a cli was created with commands that can be used by both bitcoin bonders and vault operators. The cli ouputs psbt bytes for bitcoin, which are then expected to be pasted into tools like Electrum or the bitcoin cli. The electrum client (or another miniscript/bitcoin-core built wallet) would then have the psbt pasted in to sign (walletprocesspsbt for bitcoin-core) and submit the transaction (finalizepsbt and submitrawtransaction).

The commands to bond a bitcoin, create a vault, submit a cosigned vault signature, all output links that can be used to navigate to the polkadot.js apps and submit the final transaction. The apps supports using ledgers, polkadot vault and other offline wallets. The commands to add funding and modify terms of a vault are expected to simply use the polkadot ui. The main reason we have this ux for other apis is that the percent and milligon based fields are challenging to input. To that end, I have added a utils section of the commands that allow a user to calculate the values of FixedU128 (percents) for values like 1.5%.

Not covered

This cli does NOT currently have any mechanism for a vault operator to track all the bonds created against it so that it knows which ones need to be claimed after expiration.

Miniscript

The other major change in here was that I converted our bitcoin script to miniscript. The policy is:

or(
        thresh(2, pk({vault_pubkey}), pk({owner_pubkey}), after({open_claim_height})),
    and(pk({vault_claim_pubkey}), after({vault_claim_height}))
)

This necessitated creating a secondary "vault claim pubkey" that it will need to use to claim a bitcoin in the case where a bitcoin bonder doesn't ever unlock it. The miniscript seems to have allowed a script that bitcoin core will sign.

Cli commands:

./target/debug/ulx-bitcoin-cli
A cli used to bond bitcoins, create and manage Vaults

Usage: ulx-bitcoin-cli [OPTIONS] <COMMAND>

Commands:
  vault  List, create and manage vaults
  bond   Create, unlock and monitor bonds
  utils  Utilities for working with Bitcoin and Ulixee primitives
  help   Print this message or the help of the given subcommand(s)
./target/debug/ulx-bitcoin-cli vault
List, create and manage vaults

Usage: ulx-bitcoin-cli vault [OPTIONS] <COMMAND>

Commands:
  list            Show vaults that can support the given amount of btc
  create          Create a new vault
  pending-unlock  List pending unlock requests (vault claim, cosign)
  help            Print this message or the help of the given subcommand(s)
./target/debug/ulx-bitcoin-cli bond
Create, unlock and monitor bonds

Usage: ulx-bitcoin-cli bond [OPTIONS] <COMMAND>

Commands:
  apply                Create a bond application
  create-psbt          Create a partially signed bitcoin transaction to fund this bitcoin bond
  get                  Show the current state of the bond
  request-unlock       Helps create an unlock request
  vault-cosign-psbt    Create the vault side of this unlock request
  vault-cosign-submit  Submit a cosignature to the blockchain
  owner-cosign-psbt    Create an unlock psbt to submit to bitcoin
  claim-utxo-psbt      Create a psbt to claim back the utxo
  help                 Print this message or the help of the given subcommand(s)
./target/debug/ulx-bitcoin-cli utils
Utilities for working with Bitcoin and Ulixee primitives

Usage: ulx-bitcoin-cli utils [OPTIONS] <COMMAND>

Commands:
  to-fixed     Translate a percent into a FixedU128 for the polkadot.js apps
  from-fixed   Convert a FixedU128 to a readable percent
  encode-xpub  XPub to bytes
  help         Print this message or the help of the given subcommand(s)