WIP: lib/btc-wallet-store refactored

This commit is contained in:
timlucmiptev 2020-11-11 14:30:22 +02:00 committed by ixv
parent 61b9c8a508
commit 001f42c949
4 changed files with 63 additions and 62 deletions

View File

@ -41,7 +41,7 @@ Incoming data:
- responses from `btc-provider`
- connectivity status from `btc-provider`
- address lookup requests from `btc-wallet-store`
- newlyy generated/watched addresses from `btc-wallet-store`
- newly generated/watched addresses from `btc-wallet-store`
Outgoing data:
- pokes `btc-wallet-store` with address updates

View File

@ -12,34 +12,31 @@ absurd sick rose mask magnet know slide spell rent casual someone grant giant in
=bl -build-file %/lib/btc-wallet-store/hoon
```
### add
```
:btc-wallet-store|action [%add-wallet xpub ~ ~]
```
### get address at indices
```
=walt1 (from-xpub:walt:bl xpub1 ~ ~)
(get-address:walt1 %0 0)
=walt1 (from-xpub:bl xpub1 ~ ~)
(~(mk-address wad:bl walt1 %0) 0)
:: gives [%bech32 'bc1q0adfj7ur750hjwufzzlfj5ytqzsnd7fz9fjuzc']
```
### update address data
```
=walt1 (from-xpub:walt:bl xpub1 ~ ~)
(~(mk-address walt1 %0) 0)
=walt1 (from-xpub:bl xpub1 ~ ~)
(~(mk-address wad:bl walt1 %0) 0)
:: gives bc1q0adfj7ur750hjwufzzlfj5ytqzsnd7fz9fjuzc
=walt1 (~(insert-address walt1 %0) [%bech32 'bc1q0adfj7ur750hjwufzzlfj5ytqzsnd7fz9fjuzc'] [%0 0 ~])
nixt.st.walt1
=walt1 (~(watch-address wad:bl walt1 %0) [%bech32 'bc1q0adfj7ur750hjwufzzlfj5ytqzsnd7fz9fjuzc'] [%0 0 ~])
nixt.walt1
:: gives [p=1 q=0] (nixt updated since it was 0)
=walt1 (~(insert-address walt1 %0) [%bech32 'bc1qa5jcdww8u8493zttjjf6q5wu89e6knpvmsh7x4'] [%0 2 ~])
nixt.st.walt1
=walt1 (~(watch-address wad:bl walt1 %0) [%bech32 'bc1qa5jcdww8u8493zttjjf6q5wu89e6knpvmsh7x4'] [%0 2 ~])
nixt.walt1=walt1 (~(watch-address wad:bl walt1 %0) [%bech32 'bc1qa5jcdww8u8493zttjjf6q5wu89e6knpvmsh7x4'] [%0 2 ~])
nixt.walt1
:: gives [p=1 q=0] (no update)
=walt1 (~(insert-address walt1 %0) [%bech32 'bc1qvqrdh8suyv63ntaa0d7hmcamavv8283sngh6e5'] [%0 1 ~])
nixt.st.walt1
=walt1 (~(watch-address wad:bl walt1 %0) [%bech32 'bc1qvqrdh8suyv63ntaa0d7hmcamavv8283sngh6e5'] [%0 1 ~])
nixt.walt1
:: gives [p=3 q=0] (skips index 2, since already a used address there)
=walt1 (insert-address:walt1 [%bech32 'bc1qvqrdh8suyv63ntaa0d7hmcamavv8283sngh6e5'] [%0 4 ~])
:: gives error, because address is inserted at the wrong index
=walt1 (~(watch-address wad:bl walt1 %0) [%bech32 'bc1qvqrdh8suyv63ntaa0d7hmcamavv8283sngh6e5'] [%0 4 ~])
:: gives error, because address is inserted at index that doesn't match it
```
### generate new address (for receiving payment)

View File

@ -6,14 +6,6 @@
=+ ecc=secp256k1
|%
++ default-max-gap 20
:: 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
:: scanned: whether the wallet's addresses have been checked for prior activity
:: scan-to
:: max-gap
::
::
++ hash-xpub
|= [=xpub:btc =chyg =idx]
@ -22,37 +14,36 @@
=/ dat=@ (cat 3 xpub chygidx)
%- ripemd-160:ripemd:crypto
[(met 3 dat) dat]
++ walt
=| st=[=wilt =bipt =wach =nixt scanned=? scan-to=scon max-gap=@]
|_ =chyg
+* this .
::
++ from-xpub
|= [=xpub:btc scan-to=(unit scon) max-gap=(unit @)]
^- _this
%= this
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)
^- walt
:* (from-extended:bip32 (trip xpub))
(xpub-type:btc xpub)
*wach
[0 0]
%.n
(fall scan-to *scon)
(fall max-gap default-max-gap)
==
:: wad: door for processing walts (wallets)
:: parameterized on a walt and it's chyg account
::
++ wad
|_ [w=walt =chyg]
++ mk-address
|= =idx
^- address:btc
=/ pubkey=@ux
%- compress-point:ecc
pub:(derive-public:(derive-public:wilt.st (@ chyg)) idx)
?: ?=(%bip84 bipt.st)
pub:(derive-public:(derive-public:wilt.w (@ chyg)) idx)
?: ?=(%bip84 bipt.w)
(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)
^- (pair address:btc walt)
=/ addr (mk-address nixt-idx)
:- addr
(watch-address addr [chyg nixt-idx *(set utxo:btc)])
@ -60,29 +51,29 @@
::
++ watch-address
|= [a=address:btc =addi]
^- _this
^- walt
?> =(chyg chyg.addi)
?> =(a (mk-address idx.addi))
=? nixt.st (is-nixt addi)
=? nixt.w (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
w(wach (~(put by wach.w) a addi))
:: update an address if it's in wach map
::
++ update-address
|= [a=address:btc utxos=(set utxo:btc)]
^- _this
^- walt
=/ adi=(unit addi)
(~(get by wach.st) a)
?~ adi this
this(wach.st (~(put by wach.st) a u.adi(utxos utxos)))
(~(get by wach.w) a)
?~ adi w
w(wach (~(put by wach.w) a u.adi(utxos utxos)))
::
++ is-nixt
|= =addi ^- ?
?: ?=(%0 chyg.addi)
=(idx.addi p.nixt.st)
=(idx.addi q.nixt.st)
=(idx.addi p.nixt.w)
=(idx.addi q.nixt.w)
++ nixt-idx
?:(?=(%0 chyg) p.nixt.st q.nixt.st)
?:(?=(%0 chyg) p.nixt.w q.nixt.w)
:: Returns: the prior idx in the account
:: nixt with account idx bumped
:: Increments idx until an unwatched address is found
@ -93,13 +84,13 @@
:- nixt-idx
=/ new-idx=idx +(nixt-idx)
|- ?> (lte new-idx max-index)
?. (~(has by wach.st) (mk-address new-idx))
?. (~(has by wach.w) (mk-address new-idx))
(set-nixt new-idx)
$(new-idx +(new-idx))
::
++ set-nixt
|= idx=@ ^- nixt
?:(?=(%0 chyg) [idx q.nixt.st] [p.nixt.st idx])
?:(?=(%0 chyg) [idx q.nixt.w] [p.nixt.w idx])
--
--
--

View File

@ -17,7 +17,6 @@
:: scon: indices to initially scan to in (non-)change accounts
:: defaults to 2^32-1 (i.e. all the addresses, ~4B)
:: wilt: stores xpub; copulates with thousands of indices to form addresses
:: walt: wallet metadata
::
+$ chyg $?(%0 %1)
+$ idx @
@ -26,6 +25,20 @@
+$ 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
::
+$ walt
$: =wilt
=bipt
=wach
=nixt
scanned=?
scan-to=scon
max-gap=@
==
:: todo: Set of indices; empty it out until none are left--means scanning of that batch is done
:: start: index this batch started scanning from
::