urbit/pkg/arvo/sur/btc-wallet.hoon

151 lines
4.3 KiB
Plaintext
Raw Normal View History

2021-02-18 15:13:06 +03:00
/- *bitcoin, bp=btc-provider
2021-02-12 17:37:21 +03:00
/+ bip32
|%
+$ params [batch-size=@ud fam-limit=@ud piym-limit=@ud]
2021-04-20 05:45:34 +03:00
+$ provider [host=ship connected=?]
2021-02-12 17:37:21 +03:00
+$ block @ud
+$ btc-state [=block fee=(unit sats) t=@da]
2021-05-17 05:31:41 +03:00
+$ payment [pend=(unit txid) =xpub =address payer=ship value=sats note=(unit @t)]
+$ piym
$: ps=(map ship payment)
pend=(map txid payment)
num-fam=(map ship @ud)
==
+$ poym [txbu=(unit txbu) note=(unit @t)]
2021-02-12 17:37:21 +03:00
::
2021-02-18 15:13:06 +03:00
:: command: run from the CLI or as API calls by our ship
::
2021-02-12 17:37:21 +03:00
+$ command
$% [%set-provider provider=(unit ship)]
2021-04-02 01:33:27 +03:00
[%check-provider provider=ship]
[%check-payee payee=ship]
2021-02-12 17:37:21 +03:00
[%set-current-wallet =xpub]
2021-02-12 20:43:25 +03:00
[%add-wallet =xpub =fprint scan-to=(unit scon) max-gap=(unit @ud) confs=(unit @ud)]
2021-02-12 17:37:21 +03:00
[%delete-wallet =xpub]
2021-05-17 05:31:41 +03:00
[%init-payment-external =address value=sats feyb=sats note=(unit @t)]
[%init-payment payee=ship value=sats feyb=sats note=(unit @t)]
2021-02-12 17:37:21 +03:00
[%broadcast-tx txhex=cord]
2021-04-20 05:45:34 +03:00
[%gen-new-address ~]
2021-02-12 17:37:21 +03:00
==
2021-02-18 15:13:06 +03:00
:: action: how peers poke us
::
2021-02-12 17:37:21 +03:00
+$ action
2021-05-17 05:31:41 +03:00
$% [%gen-pay-address value=sats note=(unit @t)]
2021-03-01 12:13:07 +03:00
[%give-pay-address =address value=sats]
2021-02-12 17:37:21 +03:00
[%expect-payment =txid value=sats]
==
2021-02-18 15:13:06 +03:00
:: internal: actions that simply make the state machine more explicit
::
+$ internal
$% [%add-poym-raw-txi =txid rawtx=hexb]
[%close-pym ti=info:tx]
[%fail-broadcast-tx =txid]
[%succeed-broadcast-tx =txid]
==
2021-02-12 17:37:21 +03:00
::
:: Wallet Types
::
:: nixt: next indices to generate addresses from (non-change/change)
:: addi: HD path along with UTXOs
2021-02-15 11:23:56 +03:00
:: wach: map for watched addresses.
2021-02-12 17:37:21 +03:00
:: Membership implies the address is known by outside parties or had prior activity
:: scon: indices to initially scan to in (non-)change accounts
:: defaults to 2^32-1 (i.e. all the addresses, ~4B)
:: wilt: copulates with thousands of indices to form addresses
::
++ max-index (dec (pow 2 32))
+$ nixt (pair idx idx)
+$ addi [used=? =chyg =idx utxos=(set utxo)]
+$ wach (map address addi)
+$ scon $~([max-index max-index] (pair idx idx))
+$ wilt _bip32
::
:: walt: wallet datastructure
:: scanned: whether the wallet's addresses have been checked for prior activity
:: scan-to
:: max-gap: maximum number of consec blank addresses before wallet stops scanning
:: confs: confirmations required (after this is hit for an address, wallet stops refreshing it)
::
+$ walt
$: =xpub
=network
=fprint
=wilt
=bipt
=wach
=nixt
scanned=?
scan-to=scon
max-gap=@ud
confs=@ud
==
:: batch: indexes to scan for a given chyg
:: scans: all scans underway (batches)
::
+$ batch [todo=(set idx) endpoint=idx has-used=?]
+$ scans (map [xpub chyg] batch)
::
:: insel: a selected utxo for input to a transaction
:: pmet: optional payment metadata
:: feyb: fee per byte in sats
:: txi/txo: input/output for a transaction being built
:: - txo has an hdkey if it's a change account
:: - by convention, first output of txo is to the payee, if one is present
:: txbu: tx builder -- all information needed to make a transaction for signing
::
+$ insel [=utxo =chyg =idx]
+$ feyb sats
2021-03-01 12:13:07 +03:00
+$ txi [=utxo rawtx=(unit hexb) =hdkey]
2021-02-12 17:37:21 +03:00
+$ txo [=address value=sats hk=(unit hdkey)]
+$ txbu
$: =xpub
payee=(unit ship)
=vbytes
txis=(list txi)
txos=(list txo)
signed-tx=(unit hexb)
2021-02-12 17:37:21 +03:00
==
:: hest: an entry in the history log
::
+$ hest
$: =xpub
=txid
confs=@ud
recvd=(unit @da)
inputs=(list [=val:tx s=(unit ship)])
outputs=(list [=val:tx s=(unit ship)])
2021-05-17 05:31:41 +03:00
note=(unit @t)
2021-02-12 17:37:21 +03:00
==
+$ history (map txid hest)
2021-05-12 17:06:18 +03:00
::
+$ error
$? %cant-pay-ourselves
%no-comets
2021-05-14 15:30:56 +03:00
%no-dust
2021-05-12 17:06:18 +03:00
%tx-being-signed
%insufficient-balance
==
2021-04-16 17:11:03 +03:00
:: data to send to the frontend
::
+$ update
$% $: %initial
provider=(unit provider)
wallet=(unit xpub)
balance=(unit [confirmed=sats unconfirmed=sats])
2021-04-16 17:11:03 +03:00
=history
=btc-state
2021-04-20 05:45:34 +03:00
address=(unit address)
2021-04-16 17:11:03 +03:00
==
[%change-provider provider=(unit provider)]
2021-04-23 23:18:20 +03:00
[%change-wallet wallet=(unit xpub) balance=(unit [p=sats q=sats]) =history]
2021-04-16 17:11:03 +03:00
[%psbt pb=@t]
[%btc-state =btc-state]
[%new-tx =hest]
[%cancel-tx =txid]
2021-04-20 05:45:34 +03:00
[%new-address =address]
[%balance balance=(unit [confirmed=sats unconfirmed=sats])]
2021-05-12 17:06:18 +03:00
[%error =error]
2021-04-16 17:11:03 +03:00
==
2021-04-25 21:52:04 +03:00
::
2021-02-12 17:37:21 +03:00
--