shrub/lib/btc-wallet-store.hoon

99 lines
2.6 KiB
Plaintext
Raw Normal View History

2020-10-31 14:25:32 +03:00
::
::
2020-10-30 14:45:38 +03:00
/- *btc-wallet-store
/+ bip32, btc
=, secp:crypto
=+ ecc=secp256k1
|%
++ default-max-gap 20
2020-11-02 10:21:48 +03:00
:: walt: door parameterized on chyg (the change account to use for current operation)
:: to access state, use VARNAME.st.WALTNAME, e.g. nixt.st.walt1
::
:: wach: map of watched addresses
2020-10-30 14:45:38 +03:00
:: scanned: whether the wallet's addresses have been checked for prior activity
:: scan-to
:: max-gap
::
2020-11-02 10:21:48 +03:00
::
2020-10-30 14:45:38 +03:00
++ walt
2020-11-02 10:21:48 +03:00
=| st=[=wilt =bipt =wach =nixt scanned=? scan-to=scon max-gap=@]
|_ =chyg
2020-10-31 12:41:00 +03:00
+* this .
::
2020-10-30 14:45:38 +03:00
++ from-xpub
2020-10-31 14:25:32 +03:00
|= [=xpub:btc scan-to=(unit scon) max-gap=(unit @)]
2020-10-31 12:41:00 +03:00
^- _this
%= this
2020-11-02 10:21:48 +03:00
wilt.st (from-extended:bip32 (trip xpub))
bipt.st (xpub-type:btc xpub)
wach.st *wach
nixt.st [0 0]
scanned.st %.n
scan-to.st (fall scan-to *scon)
max-gap.st (fall max-gap default-max-gap)
2020-10-30 14:45:38 +03:00
==
2020-10-31 18:03:00 +03:00
::
2020-10-31 14:25:32 +03:00
++ mk-address
2020-11-02 10:21:48 +03:00
|= =idx
2020-10-30 14:45:38 +03:00
^- address:btc
=/ pubkey=@ux
%- compress-point:ecc
2020-11-02 10:21:48 +03:00
pub:(derive-public:(derive-public:wilt.st (@ chyg)) idx)
?: ?=(%bip84 bipt.st)
2020-10-30 14:45:38 +03:00
(need (encode-pubkey:bech32:btc %main pubkey))
~|("legacy addresses not supported yet " !!)
:: generates and watches the next available address
::
++ gen-address
^- (pair address:btc _this)
=/ addr (mk-address nixt-idx)
:- addr
(watch-address addr [chyg nixt-idx *(set utxo:btc)])
2020-10-31 14:25:32 +03:00
:: insert a new address; update "nixt" free address if this one was it
::
++ watch-address
2020-10-31 14:25:32 +03:00
|= [a=address:btc =addi]
^- _this
2020-11-02 10:21:48 +03:00
?> =(chyg chyg.addi)
?> =(a (mk-address idx.addi))
=? nixt.st (is-nixt addi)
new:bump-nixt
this(wach.st (~(put by wach.st) a addi))
:: update an address if it's in our wach map
2020-10-31 12:41:00 +03:00
::
++ update-address
2020-10-31 14:25:32 +03:00
|= [a=address:btc utxos=(set utxo:btc)]
2020-10-31 12:41:00 +03:00
^- _this
=/ adi=(unit addi)
2020-11-02 10:21:48 +03:00
(~(get by wach.st) a)
2020-10-31 12:41:00 +03:00
?~ adi this
2020-11-02 10:21:48 +03:00
this(wach.st (~(put by wach.st) a u.adi(utxos utxos)))
2020-10-31 14:25:32 +03:00
::
++ is-nixt
2020-11-02 10:21:48 +03:00
|= =addi ^- ?
?: ?=(%0 chyg.addi)
=(idx.addi p.nixt.st)
=(idx.addi q.nixt.st)
2020-11-02 13:37:55 +03:00
++ nixt-idx
?:(?=(%0 chyg) p.nixt.st q.nixt.st)
2020-11-02 10:21:48 +03:00
:: Returns: the prior idx in the account
:: nixt with account idx bumped
2020-11-02 13:37:55 +03:00
:: Increments idx until an unwatched address is found
2020-11-02 10:21:48 +03:00
:: Crashes if max-index is passed
2020-10-31 12:41:00 +03:00
::
2020-10-31 14:25:32 +03:00
++ bump-nixt
2020-11-02 10:21:48 +03:00
|^ ^- [old=idx new=nixt]
2020-11-02 13:37:55 +03:00
:- nixt-idx
=/ new-idx=idx +(nixt-idx)
2020-10-31 14:25:32 +03:00
|- ?> (lte new-idx max-index)
2020-11-02 10:21:48 +03:00
?. (~(has by wach.st) (mk-address new-idx))
(set-nixt new-idx)
2020-10-31 14:25:32 +03:00
$(new-idx +(new-idx))
2020-11-02 10:21:48 +03:00
::
++ set-nixt
|= idx=@ ^- nixt
?:(?=(%0 chyg) [idx q.nixt.st] [p.nixt.st idx])
--
2020-10-30 14:45:38 +03:00
--
--