WIP: refactor to monolith, testing now

This commit is contained in:
timlucmiptev 2021-02-12 18:37:21 +04:00 committed by ixv
parent ff2115bf0f
commit c7f3042740
15 changed files with 981 additions and 153 deletions

View File

@ -150,7 +150,6 @@
?~ def-wallet state
?. =(u.def-wallet xpub.comm) state
state(def-wallet ~)
:: TODO if wallet is xpub.comm, clear
::
:: %req-pay-address
:: overwrites any payment being built currently
@ -288,7 +287,7 @@
::
%ret-pay-address
?: =(src.bowl our.bowl) ~|("Can't pay ourselves" !!)
?: broadcasting ~|("Broadcasting a transaction" !!)
?: is-broadcasting ~|("Broadcasting a transaction" !!)
?~ def-wallet ~|("btc-wallet-hook: no def(ault)-wallet set" !!)
=+ feyb=(~(gut by feybs) src.bowl ?~(fee.btc-state 100 u.fee.btc-state))
?> =(payer.act our.bowl)
@ -470,7 +469,8 @@
::
++ poym-to-history
|= ti=info:tx
|^ ^- (quip card _state)
^- (quip card _state)
|^
?~ poym `state
?~ sitx.u.poym `state
?. (poym-has-txid txid.ti)
@ -496,7 +496,6 @@
:: - returns card that adds hest to wallet-store history
::
++ piym-to-history
:: TODO: delete prints
|= ti=info:tx
|^ ^- (quip card _state)
=+ pay=(~(get by pend-piym) txid.ti)
@ -602,44 +601,26 @@
++ retry-poym
^- (list card)
?~ poym ~
?~ prov ~|("prov not set" !!)
=* host host.u.prov
%+ weld
?~ sitx.u.poym ~
~[(poke-provider host [%broadcast-tx u.sitx.u.poym])]
~[(poke-provider [%broadcast-tx u.sitx.u.poym])]
%+ turn txis.u.poym
|= =txi:bws
(get-raw-tx host txid.utxo.txi)
|= =txi
(poke-provider [%raw-tx txid])
:: +retry-pend-piym: check whether txids in pend-piym are in mempool
::
++ retry-pend-piym
^- (list card)
?~ prov ~|("provider not set" !!)
%+ turn ~(tap in ~(key by pend-piym))
|=(txid=hexb (poke-provider host.u.prov [%tx-info txid]))
|=(=txid (poke-provider [%tx-info txid]))
::
++ get-raw-tx
|= [host=ship txid=hexb]
^- card
(poke-provider host [%raw-tx txid])
::
++ poke-provider
|= [host=ship act=action:bp]
^- card
:* %pass /[(scot %da now.bowl)] %agent [host %btc-provider]
%poke %btc-provider-action !>([act])
==
::
++ broadcasting
^- ?
?~ poym %.n
?~ sitx.u.poym %.n
%.y
::
++ provider-connected
^- ?
?~ prov %.n
connected.u.prov
::
++ poke-hook
|= [target=ship act=action]

View File

@ -7,9 +7,7 @@
:: - /requests: to request data about addresses
:: - /updates: new data about one of our addresses
::
:: Scrys
:: x/scanned: (list xpub) of all scanned wallets
:: x/balance/xpub: balance (in sats) of wallet
::
/- *btc-wallet-store
/+ dbug, default-agent, *btc-wallet-store, btc, bip32
@ -114,11 +112,6 @@
::
%delete-wallet
`state(walts (~(del by walts) xpub.act))
::
:: TODO
:: if blank address we're watching gets a value
:: "blank" = unused
:: send a %tx-info request--txinfo handles history stuff
::
%address-info
(update-address +.act)
@ -235,8 +228,9 @@
==
:- (req-scan ~[req-pax] newb xpub chyg)
newb
:: +del-scanned: delete scanned idxs
::
++ iter-scan
++ del-scanned
|= [b=batch =xpub =chyg to-delete=idx]
^- ^scans
%+ ~(put by scans) [xpub chyg]
@ -292,7 +286,7 @@
=+ b=(~(get by scans) [xpub chyg])
?~ b [(more-info u.w) state]
=. scans
(iter-scan u.b(has-used ?|(used has-used.u.b)) xpub chyg idx)
(del-scanned u.b(has-used ?|(used has-used.u.b)) xpub chyg idx)
?: empty:(scan-status xpub chyg)
=^ cards state (run-scan xpub)
[(weld (more-info u.w) cards) state]
@ -421,27 +415,9 @@
==
==
::
++ scanned-wallets
^- (list xpub)
%+ murn ~(tap by walts)
|= [=xpub:btc w=walt]
^- (unit xpub:btc)
?: scanned.w `xpub ~
::
++ balance
|= =xpub:btc
^- sats:btc
=/ w (~(get by walts) xpub)
?~ w ~|("btc-wallet-store: non-existent xpub" !!)
=/ values=(list sats:btc)
%+ turn ~(val by wach.u.w)
|= =addi ^- sats:btc
%+ roll
%+ turn ~(tap by utxos.addi)
|=(=utxo:btc value.utxo)
add
(roll values add)
::
:: REMOVED
:: scanned-wallets
:: balance
++ send-req
|= [pax=(list path) req=request] ^- card
:: ~& >> "send-req: {<chyg.req>}, {<idx.req>}"

817
app/btc-wallet.hoon Normal file
View File

@ -0,0 +1,817 @@
:: btc-wallet
::
:: Scrys
:: x/scanned: (list xpub) of all scanned wallets
:: x/balance/xpub: balance (in sats) of wallet
/- *btc-wallet
/+ dbug, default-agent, *btc-wallet, bp=btc-provider, *btc, bip32
|%
++ defaults
|%
++ params
:* batch-size=20
fam-limit=10
piym-limit=3
==
++ confs 6
--
::
+$ versioned-state
$% state-0
==
:: batch-size: how many addresses to send out at once for checking
:: last-block: most recent block seen by the store
::
+$ state-0
$: %0
prov=(unit provider)
walts=(map xpub:btc walt)
=btc-state
=history
curr-xpub=(unit xpub)
=scans
=params
feybs=(map ship sats)
=piym
=poym
=pend-piym
==
::
+$ card card:agent:gall
::
--
=| state-0
=* state -
%- agent:dbug
^- agent:gall
=<
|_ =bowl:gall
+* this .
def ~(. (default-agent this %|) bowl)
hc ~(. +> bowl)
::
++ on-init
^- (quip card _this)
~& > '%btc-wallet initialized'
:- ~
%_ this
state
:* %0
~
*(map xpub:btc walt)
*^btc-state
*^history
~
*^scans
defaults:params
*(map ship sats)
*^piym
*^poym
*^pend
==
==
++ on-save
^- vase
!>(state)
++ on-load
|= old-state=vase
^- (quip card _this)
~& > '%btc-wallet recompiled'
`this(state !<(versioned-state old-state))
++ on-poke
|= [=mark =vase]
^- (quip card _this)
?> (team:title our.bowl src.bowl)
=^ cards state
?+ mark (on-poke:def mark vase)
%btc-wallet-action
(handle-action:hc !<(action vase))
%btc-wallet-command
(handle-command:hc !<(action vase))
==
[cards this]
++ on-peek
|= pax=path
^- (unit (unit cage))
?+ pax (on-peek:def pax)
[%x %scanned ~]
``noun+!>(scanned-wallets)
::
[%x %balance @ ~]
``noun+!>((balance:hc (xpub:btc +>-.pax)))
==
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
?+ -.sign (on-agent:def wire sign)
%kick
~& >>> "kicked from prov {<src.bowl>}"
?~ prov `this
?: ?& ?=(%set-provider -.wire)
=(host.u.prov src.bowl)
==
`this(prov ~)
`this
::
%fact
=^ cards state
?+ p.cage.sign `state
%btc-provider-status
(handle-provider-status:hc !<(status:bp q.cage.sign))
::
%btc-provider-update
(handle-provider-update:hc !<(update:bp q.cage.sign))
==
[cards this]
==
::
++ on-watch on-watch:def
++ on-leave on-leave:def
++ on-arvo on-arvo:def
++ on-fail on-fail:def
--
|_ =bowl:gall
++ handle-command
|= comm=command
^- (quip card _state)
?- -.comm
%set-provider
=* sub-card
[%pass /set-provider %agent [provider.comm %btc-provider] %watch /clients]
:_ state(prov [~ provider.comm %.n])
?~ prov ~[sub-card]
:~ [%pass /set-provider %agent [host.u.prov %btc-provider] %leave ~]
sub-card
==
::
%set-current-wallet
?~ (find ~[xpub.comm] scanned-wallets)
`state
`state(curr-xpub `xpub.comm)
::
%add-wallet
=/ w=walt (from-xpub +.comm)
=. walts (~(put by walts) xpub.comm w)
(init-batches xpub.comm (dec max-gap.w))
::
%delete-wallet
=* cw curr-xpub.state
=? cw ?&(?=(^ cw) =(u.cw xpub.comm))
~
`state(walts (~(del by walts) xpub.comm))
::
:: overwrites any payment being built in poym
::
%req-pay-address
~| "Can't pay ourselves; no comets; can't do while tx is being signed"
?< =(src.bowl payee.comm)
?< ?=(%pawn (clan:title payee.comm))
?< is-broadcasting
:_ state(poym ~, feybs (~(put by feybs) payee.comm feyb.comm))
~[(poke-us payee.comm [%gen-pay-address value.comm])]
::
%broadcast-tx
?> =(src.bowl our.bowl)
?~ prov ~|("Provider not connected" !!)
=+ signed=(to-hexb txhex.comm)
=/ tx-match=?
?~ poym %.n
=((get-id:txu (decode:txu signed)) ~(get-txid txb u.poym))
:- ?. tx-match
((slog leaf+"txid didn't match txid in wallet") ~)
~[(poke-provider [%broadcast-tx signed])]
?. tx-match state
?~ poym state
state(sitx.u.poym `signed)
==
::
++ handle-action
|= act=action
^- (quip card _state)
?- -.act
%add-poym-raw-txi
?> =(src.bowl our.bowl)
?~ poym `state
=. txis.u.poym
(update-poym-txis txis.u.poym +.act)
:_ state
=+ pb=~(to-psbt txb u.poym)
?~ pb ~
=+ vb=~(vbytes txb u.poym)
=+ fee=~(fee txb u.poym)
~& >> "{<vb>} vbytes, {<(div fee vb)>} sats/byte, {<fee>} sats fee"
%- (slog [%leaf "PSBT: {<u.pb>}"]~)
~[(send-update [%sign-tx u.poym])]
::
%close-pym
?> =(src.bowl our.bowl)
=^ cards state
?. included.ti.act
`state
?: (~(has by pend-piym) txid.ti.act)
(piym-to-history ti.act)
?: (poym-has-txid txid.ti.act)
(poym-to-history ti.act)
`state
:- cards
(handle-tx-info ti.act)
::
%fail-broadcast-tx
?> =(src.bowl our.bowl)
~& > "%fail-broadcast-tx"
:_ state(poym ~)
~[(send-update [%broadcast-tx-spent-utxos txid.act])]
::
%succeed-broadcast-tx
?> =(src.bowl our.bowl)
~& > "%succeed-broadcast-tx"
:_ %_ state
reqs (~(put by reqs) txid.act [%tx-info 0 txid.act])
==
?~ prov ~
:- (poke-provider host.u.prov [%tx-info txid.act])
?~ poym ~
?~ payee.u.poym ~
:_ ~
%- poke-us
:* u.payee.u.poym
%expect-payment
txid.act
value:(snag 0 txos.u.poym)
==
:: can't pay yourself; comets can't pay (could spam requests)
:: must have default wallet set
:: reuses payment address for ship if exists in piym
::
%gen-pay-address
~| "Can't pay ourselves; no comets"
?< =(src.bowl our.bowl)
?< ?=(%pawn (clan:title src.bowl))
=^ cards state
(reuse-address src.bowl value.act)
?^ cards [cards state]
:: if no reuseable address, call store to generate
::
=+ f=(fam src.bowl)
=+ n=(~(gut by num-fam.piym) f 0)
?~ curr-xpub ~|("btc-wallet-hook: no curr-xpub set" !!)
?: (gte n fam-limit)
~|("More than {<fam-limit>} addresses for moons + planet" !!)
=. state state(num-fam.piym (~(put by num-fam.piym) f +(n)))
=^ addr state
(generate-address u.curr-xpub %0 `[src.bowl value.act])
:- ~[(poke-us payer.act [%recv-pay-address addr value.act])]
state(ps.piym (~(put by ps.piym) src.bowl [~ u.curr-xpub addr src.bowl value.act]))
::
%recv-pay-address
?: =(src.bowl our.bowl) ~|("Can't pay ourselves" !!)
?: is-broadcasting ~|("Broadcasting a transaction" !!)
?~ curr-xpub ~|("btc-wallet-hook: no curr-xpub set" !!)
=+ feyb=(~(gut by feybs) src.bowl ?~(fee.btc-state 100 u.fee.btc-state))
=^ tb=(unit txbu) state
(generate-txbu u.curr-xpub `src.bowl feyb tx ~[[address.act value.act ~]])
:_ state(poym `tb)
?~ tb ~
%+ turn txis.u.tb
|=(=txi (poke-provider txid.utxo.txi))
::
:: %expect-payment
:: - check that payment is in piym
:: - replace pend.payment with incoming txid (lock)
:: - add txid to pend-piym
:: - request tx-info from provider
::
%expect-payment
|^
=+ pay=(~(get by ps.piym) src.bowl)
~| "%expect-payment: matching payment not in piym"
?~ pay !!
?> (piym-matches u.pay)
:_ (update-pend-piym txid.act u.pay(pend `txid.act))
?~ prov ~
~[(poke-provider host.u.prov [%tx-info txid.act])]
::
++ piym-matches
|= p=payment
?& =(payer.p src.bowl)
=(value.p value.act)
==
--
==
::
:: +handle-provider-status: handle connectivity updates from provider
:: - retry pend-piym on any %connected event, since we're checking mempool
:: - if status is %connected, retry all pending address lookups
:: - only retry all if previously disconnected
:: - if block is updated, retry all address reqs
::
++ handle-provider-status
|= s=status:bp
^- (quip card _state)
|^
?~ prov `state
?. =(host.u.prov src.bowl) `state
?- -.s
%new-block
(connected u.prov block.s fee.s `blockhash.s `blockfilter.s)
::
%connected
(connected u.prov block.s fee.s ~ ~)
::
%disconnected
`state(prov `[host.u.prov %.n])
==
::
++ connected
|= $: p=provider
block=@ud
fee=(unit sats)
blockhash=(unit hexb)
blockfilter=(unit hexb)
==
^- (quip card _state)
:_ %_ state
prov `[host.p %.y]
btc-state [block fee now.bowl]
==
?: ?|(?!(connected.p) (lth block.btc-state block))
;:(weld retry-pend-piym retry-addrs retry-txs)
retry-pend-piym
--
::
++ handle-provider-update
|= upd=update:bp
^- (quip card _state)
?. ?=(%.y -.upd) `state
?- -.p.upd
%address-info
(handle-address-info address.p.upd utxos.p.upd used.p.upd)
::
%tx-info
:- ~[(poke-us our.bowl [%close-pym info.p.upd])]
(handle-tx-info info.p.upd)
::
%raw-tx
:_ state
~[(poke-us our.bowl [%add-poym-raw-txi +.p.upd])]
::
%broadcast-tx
?~ poym `state
?. =(~(get-txid txb u.poym) txid.p.upd)
`state
:_ state
?: ?|(broadcast.p.upd included.p.upd)
~[(poke-us our.bowl [%succeed-broadcast-tx txid.p.upd])]
~[(poke-us our.bowl [%fail-broadcast-tx txid.p.upd])]
==
::
++ handle-tx-info
|= ti=info:tx
^- _state
|^
=/ h (~(get by history) txid.ti)
=/ addrs=(set address)
%- sy
%+ turn (weld inputs.ti outputs.ti)
|=(=val:tx address.val)
=/ w (first-matching-wallet ~(tap in addrs))
?~ w state
?~ h :: addresses in wallets, but tx not in history
=. history
%+ ~(put by history) txid.ti
(mk-hest xpub.u.w addrs)
state
?. included.ti
state(history (~(del by history) txid.ti))
%_ state
history
%+ ~(put by history) txid.ti
u.h(confs confs.ti, recvd recvd.ti)
==
::
++ mk-hest
:: has tx-info
|= [=xpub addrs=(set address)]
^- hest
:* xpub
txid.ti
confs.ti
recvd.ti
(turn inputs.ti |=(v=val:tx (our-ship addrs v)))
(turn outputs.ti |=(v=val:tx (our-ship addrs v)))
==
::
++ our-ship
|= [as=(set address:btc) v=val:tx:btc]
^- [=val:tx s=(unit ship)]
[v ?:((~(has in as) address.v) `our.bowl ~)]
::
++ first-matching-wallet
|= addrs=(list address)
^- (unit walt)
|- ?~ addrs ~
=/ am (address-meta i.addrs ~(val by walts))
?^ am `w.u.am
$(addrs t.addrs)
--
::
:: Scan Logic
::
:: +handle-address-info: updates scans and wallet with address info
::
++ handle-address-info
|= [=address utxos=(set utxo) used=?]
^- (quip card _state)
|^
=/ am address-meta
?~ am `state
=/ [w=walt =chyg =idx] u.am
=. walts
%+ ~(put by walts) xpub.w
%+ ~(update-address wad w chyg)
address
[used chyg idx utxos]
:: if the wallet+chyg is being scanned, update the scan batch
:: if not, just get more-info for the address if still being scanned
::
=/ b (~(get by scans) [xpub chyg])
?~ b `state
=. scans
(del-scanned u.b(has-used ?|(used has-used.u.b)) xpub.w chyg idx)
?: empty:(scan-status xpub.w chyg)
(run-scan xpub.w)
`state
::
++ address-meta
^- (unit [walt chyg idx])
=/ ws=(list walt) ~(val by walts)
|-
?~ ws ~
=/ res=(unit [chyg idx])
(address-loc i.ws address)
?^ res [i.ws chyg.u.res idx.u.res]
$(ws t.ws)
--
::
++ req-scan
|= [b=batch =xpub =chyg]
^- (list card)
=/ w=walt (~(got by walts) xpub)
%+ turn ~(tap in todo.b)
|= =idx
%- poke-provider
[%address-info (~(mk-address wad w chyg) idx)]
::
++ scan-status
|= [=xpub =chyg]
^- [empty=? done=?]
=/ b=batch (~(got by scans) [xpub chyg])
=/ empty=? =(0 ~(wyt in todo.b))
:- empty
?&(empty ?!(has-used.b))
::
++ insert-batches
|= [=xpub b0=batch b1=batch]
^- ^scans
=. scans (~(put by scans) [xpub %0] b0)
(~(put by scans) [xpub %1] b1)
::
++ init-batches
|= [=xpub endpoint=idx]
^- (quip card _state)
=/ b=batch
[(sy (gulf 0 endpoint)) endpoint %.n]
:- %+ weld
(req-scan b xpub %0)
(req-scan b xpub %1)
state(scans (insert-batches xpub b b))
:: +bump-batch
:: if the batch is done but the wallet isn't done scanning,
:: returns new address requests and updated batch
::
++ bump-batch
|= [=xpub =chyg]
^- (quip card batch)
=/ b=batch (~(got by scans) xpub chyg)
=/ s (scan-status xpub chyg)
?. ?&(empty.s ?!(done.s))
`b
=/ w=walt (~(got by walts) xpub)
=/ newb=batch
:* (sy (gulf +(endpoint.b) (add endpoint.b max-gap.w)))
(add endpoint.b max-gap.w)
%.n
==
:- (req-scan newb xpub chyg)
newb
:: +del-scanned: delete scanned idxs
::
++ del-scanned
|= [b=batch =xpub =chyg to-delete=idx]
^- ^scans
%+ ~(put by scans) [xpub chyg]
b(todo (~(del in todo.b) to-delete))
:: delete the xpub from scans and set wallet to scanned
::
++ end-scan
|= [=xpub]
^- (quip card _state)
=/ w=walt (~(got by walts) xpub)
=. scans (~(del by scans) [xpub %0])
=. scans (~(del by scans) [xpub %1])
%- (slog leaf+"Scanned xpub {<xpub>}")
`state(walts (~(put by walts) xpub w(scanned %.y)))
:: +check-scan: initiate a scan if one hasn't started
:: check status of scan if one is running
::
++ check-scan
|= =xpub
^- (quip card _state)
=/ s0 (scan-status xpub %0)
=/ s1 (scan-status xpub %1)
?: ?&(empty.s0 done.s0 empty.s1 done.s1)
(end-scan xpub)
=/ [cards0=(list card) batch0=batch]
(bump-batch xpub %0)
=/ [cards1=(list card) batch1=batch]
(bump-batch xpub %1)
:- (weld cards0 cards1)
state(scans (insert-batches xpub batch0 batch1))
::
:: TX and Address Generation
::
++ generate-txbu
|= [=xpub payee=(unit ship) feyb=sats txos=(list txo)]
^- [(unit txbu) _state]
=/ uw (~(get by walts) xpub)
?~ uw
~|("btc-wallet-store: non-existent xpub" !!)
?. scanned.u.uw
~|("btc-wallet-store: wallet not scanned yet" !!)
=/ [tb=(unit txbu) chng=(unit sats)]
%~ with-change sut
[u.uw eny.bowl block.btc-state payee feyb txos]
?~ tb ~&(>>> "btc-wallet-store: insufficient balance" `state)
:: if no change, return txbu; else add change to txbu
::
?~ chng [tb state]
=/ [addr=address:btc =idx w=walt]
~(nixt-address wad u.uw %1)
:- `(~(add-output txb u.tb) addr u.chng `(~(hdkey wad w %1) idx))
state(walts (~(put by walts) xpub w))
::
++ generate-address
|= [=xpub =chyg =pmet]
^- [address _state]
=/ uw=(unit walt) (~(get by walts) xpub)
?~ uw
~|("btc-wallet-store: non-existent xpub" !!)
?. scanned.u.uw
~|("btc-wallet-store: wallet not scanned yet" !!)
=/ [addr=address:btc =idx w=walt]
~(gen-address wad u.uw chyg)
[addr state(walts (~(put by walts) xpub w))]
::
:: Utilities for Incoming/Outgoing Payments
::
:: +reuse-address
:: - if piym already has address for payer,
:: replace address and return to payer
:: - if payment is pending, crash. Shouldn't be getting an address request
::
++ reuse-address
|= [payer=ship value=sats]
^- (quip card _state)
=+ p=(~(get by ps.piym) payer)
?~ p `state
?^ pend.u.p ~|("%gen-address: {<payer>} already has pending payment to us" !!)
=+ newp=u.p(value value)
:_ state(ps.piym (~(put by ps.piym) payer newp))
:~ %+ poke-hook payer
[%ret-pay-address address.newp payer value]
==
::
++ poym-has-txid
|= txid=hexb
^- ?
?~ poym %.n
?~ sitx.u.poym %.n
=(txid (get-id:txu (decode:txu u.sitx.u.poym)))
:: +poym-to-history:
:: - checks whether poym has a signed tx
:: - checks whether the txid matches that signed tx
:: - if not, skip
:: - clears poym
:: - returns card that adds hest to wallet-store history
::
++ poym-to-history
|= ti=info:tx
^- (quip card _state)
|^
?~ poym `state
?~ sitx.u.poym `state
?. (poym-has-txid txid.ti)
`state
=+ vout=(get-vout txos.u.poym)
?~ vout ~|("poym-to-history: poym should always have an output" !!)
:- ~
%= state
poym ~
history
(add-history-entry ti xpub.u.poym our.bowl payee.u.poym u.vout)
==
::
++ get-vout
|= txos=(list txo)
^- (unit @ud)
=| idx=@ud
|- ?~ txos ~
?~ hk.i.txos `idx
$(idx +(idx), txos t.txos)
--
:: +piym-to-history
:: - checks whether txid in pend-piym
:: - checks whether ti has a matching value output to piym
:: - if no match found, just deletes pend-piym with this tx
:: stops peer from spamming txids
:: - returns card that adds hest to wallet-store history
::
++ piym-to-history
|= ti=info:tx
|^ ^- (quip card _state)
=+ pay=(~(get by pend-piym) txid.ti)
?~ pay `state
:: if no matching output in piym, delete from pend-piym to stop DDOS of txids
::
=+ vout=(get-vout value.u.pay)
?~ vout
`(del-pend-piym txid.ti)
=. state (del-all-piym txid.ti payer.u.pay)
`state(history (add-history-entry [ti xpub.u.pay payer.u.pay `our.bowl u.vout]))
::
++ get-vout
|= value=sats
^- (unit @ud)
=| idx=@ud
=+ os=outputs.ti
|- ?~ os ~
?: =(value.i.os value)
`idx
$(os t.os, idx +(idx))
::
::
++ del-pend-piym
|= txid=hexb
^- _state
state(pend-piym (~(del by pend-piym) txid.ti))
::
++ del-all-piym
|= [txid=hexb payer=ship]
^- _state
=+ nf=(~(gut by num-fam.piym) payer 1)
%= state
pend-piym (~(del by pend-piym) txid)
ps.piym (~(del by ps.piym) payer)
num-fam.piym (~(put by num-fam.piym) payer (dec nf))
==
--
::
++ add-history-entry
|= [ti=info:tx =xpub payer=ship payee=(unit ship) vout=@ud]
^- ^history
=/ =hest
:* xpub
txid.ti
confs.ti
recvd.ti
(turn inputs.ti |=(i=val:tx [i `payer]))
%+ turn outputs.ti
|= o=val:tx
?: =(pos.o vout) :: check whether this is the output that went to payee
[o payee]
[o `payer]
==
(~(put by history) txid.hest hest)
:: +fam: planet parent if s is a moon
::
++ fam
|= s=ship
^- ship
?. =(%earl (clan:title s)) s
(sein:title our.bowl now.bowl s)
:: +update-pend-piym
:: - set pend.payment to txid (lock)
:: - add txid to pend-piym
::
++ update-pend-piym
|= [txid=hexb p=payment]
^- _state
?~ pend.p ~|("update-pend-piym: empty pend.payment" !!)
%= state
ps.piym (~(put by ps.piym) payer.p p)
pend-piym (~(put by pend-piym) txid p)
==
::
:: +update-poym-txis:
:: update outgoing payment with a rawtx, if the txid is in poym's txis
::
++ update-poym-txis
|= [txis=(list txi) txid=hexb rt=hexb]
^- (list txi)
=| i=@
|- ?: (gte i (lent txis)) txis
=/ ith=txi (snag i txis)
=? txis =(txid txid.utxo.ith)
(snap txis i `txi`ith(ur `rt))
$(i +(i))
::
::
:: Card Builders and Pokers
::
:: +retry-addrs: get info on addresses with unconfirmed UTXOs
::
++ retry-addrs
^- (list card)
%- zing
%+ turn ~(val by walts)
|= w=walt
^- (list card)
%+ murn ~(tap by wach.w)
|= [a=address ad=addi]
?: %+ levy utxos.ad
|=(u=utxo (gth height.u (sub block.btc-state confs.w)))
~
`(poke-provider [%address-info a])
:: +retry-txs: get info on txs without enough confirmations
::
++ retry-txs
^- (list card)
%+ murn ~(tap by history)
|= [=txid =hest]
=/ w=(unit walt) (~(get by walts) xpub.hest)
=/ nconfs=@ud ?^(w confs.u.w confs:defaults)
?: (gte confs.hest nconfs) ~
`(poke-provider [%tx-info txid])
::
++ retry-poym
^- (list card)
?~ poym ~
%+ weld
?~ sitx.u.poym ~
~[(poke-provider [%broadcast-tx u.sitx.u.poym])]
%+ turn txis.u.poym
|= =txi
(poke-provider [%raw-tx ~(get-txid txb u.poym)])
:: +retry-pend-piym: check whether txids in pend-piym are in mempool
::
++ retry-pend-piym
^- (list card)
%+ turn ~(tap in ~(key by pend-piym))
|=(=txid (poke-provider [%tx-info txid]))
::
++ poke-provider
|= [act=action:bp]
^- card
?~ prov ~|("provider not set" !!)
:* %pass /[(scot %da now.bowl)]
%agent [host.u.prov %btc-provider]
%poke %btc-provider-action !>([act])
==
::
++ poke-us
|= [target=ship act=action]
^- card
:* %pass /[(scot %da now.bowl)] %agent
[target %btc-wallet] %poke
%btc-wallet-action !>(act)
==
::
++ is-broadcasting
^- ?
?~ poym %.n
?=(^ sitx.u.poym)
::
:: Scry Helpers
::
++ scanned-wallets
^- (list xpub)
%+ murn ~(tap by walts)
|= [=xpub w=walt]
^- (unit ^xpub)
?: scanned.w `xpub ~
::
++ balance
|= =xpub
^- (unit sats)
=/ w (~(get by walts) xpub)
?~ w ~
=/ values=(list sats)
%+ turn ~(val by wach.u.w)
|= =addi ^- sats
%+ roll
%+ turn ~(tap by utxos.addi)
|=(=utxo value.utxo)
add
`(roll values add)
::
--

View File

@ -1,12 +0,0 @@
<html>
<head>
</head>
<body>
<script src="/~landscape/js/channel.js"></script>
<script src="/~landscape/js/session.js"></script>
<script src="/~btc-wallet/index.js"></script>
<input type="text" id="msg" name="msg" placeholder="Type message here..." />
<br /><br/ >
<button onClick="sendSubData(document.getElementById('msg').value);">Placeholder</button>
</body>
</html>

View File

@ -1,19 +0,0 @@
window.urb = new Channel();
const sendSubData = (msg) => {
window.urb.poke(window.ship, 'chanel', 'chanel-action',
{'send-sub-data': {'path': '/example', 'msg': msg}},
() => 'sent', (err) => console.log(err));
};
const doShipCalls = () => {
console.log(`window.ship: ${window.ship}`);
window.urb.poke(window.ship, 'chanel', 'chanel-action', {'increase-counter': {step: 40}}, () => console.log("Successful poke"), (err) => console.log(err));
window.urb.poke(window.ship, 'chanel', 'chanel-action', {example: {who: 'timluc-miptev', msg: 'hello world', app: 'chanel', friends: ['zod', 'dopzod', 'timluc']}}, () => console.log("Successful poke"), (err) => console.log(err));
window.urb.poke(window.ship, 'chanel', 'json', {'key1': 9}, () => console.log("JSON poke"), (err) => console.log(err));
// subscriptions
window.urb.subscribe(window.ship, 'chanel', '/example', (err) => console.log("Sub Error"), (data) => console.log(`got response: ${data}`), () => console.log("Sub Quit"));
};

View File

@ -1,13 +0,0 @@
:: Sends an action to btc-wallet-hook
::
:: Commands:
::
::
::
/- *btc-wallet-hook
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[act=action ~] ~]
==
[%btc-wallet-hook-action act]

View File

@ -1,13 +0,0 @@
:: Sends an action to btc-wallet-store
::
:: Commands:
::
::
::
/- *btc-wallet-store
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[act=action ~] ~]
==
[%btc-wallet-store-action act]

View File

@ -0,0 +1,9 @@
:: Sends an action to btc-wallet
::
/- *btc-wallet
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[act=action ~] ~]
==
[%btc-wallet-action act]

View File

@ -1,8 +1,4 @@
:: Sends a command to the BTC Provider
::
:: Commands:
::
::
:: Sends a command to btc-wallet
::
/- *btc-wallet-hook
::
@ -10,4 +6,4 @@
|= $: [now=@da eny=@uvJ =beak]
[[comm=command ~] ~]
==
[%btc-wallet-hook-command comm]
[%btc-wallet-command comm]

View File

@ -1,20 +0,0 @@
/- sur=btc-wallet-view
^?
=< [sur .]
=, sur
|%
++ dejs
=, dejs:format
|%
++ action
|= jon=json
^- ^action
=< (parse-json jon)
|%
++ parse-json
%- of
:~ [%blank so]
==
--
--
--

View File

@ -37,6 +37,24 @@
(fall max-gap max-gap:defaults)
(fall confs confs:defaults)
==
:: +address-meta: find wallet info for the address, if any
::
++ address-meta
|= [a=address ws=(list walt)]
^- (unit [w=walt =chyg =idx])
|^
|- ?~ ws ~
=/ res=(unit [=chyg =idx])
(lookup i.ws)
?^ res `[i.ws chyg.u.res idx.u.res]
$(ws t.ws)
::
++ lookup
|= w=walt
^- (unit [=chyg =idx])
=/ ad=(unit addi) (~(get by wach.w) a)
?~(ad ~ `[chyg.u.ad idx.u.ad])
--
::
++ new-txbu
|= $: w=walt
@ -225,7 +243,7 @@
?:(?=(%0 chyg) [idx q.nixt.w] [p.nixt.w idx])
--
--
:: sut: door to select utxos
:: sut: select utxos
::
++ sut
|_ [w=walt eny=@uvJ last-block=@ud payee=(unit ship) =feyb txos=(list txo)]

View File

@ -41,10 +41,7 @@
:: expect-payment: tell another ship that we're paying a previously requested address
:: - vout-n is the index of the output that has value
::
+$ action
$% local
peer
==
:: local and peer pokes are initiated by the agent itself
:: they exist to make the state machine explicit
:: they are not part of the API

View File

@ -75,10 +75,8 @@
==
+$ history (map hexb hest)
:: state/watch variables:
:: scanning addresses and monitoring generated addresses
:: batch: indexes to scan for a given chyg
:: scans: all scans underway (batches)
:: piym-watch: any address we've been told has an incoming payment promised
::
+$ batch [todo=(set idx) endpoint=idx has-used=?]
+$ scans (map [xpub chyg] batch)
@ -91,6 +89,7 @@
+$ action
$% [%add-wallet =xpub =fprint scan-to=(unit scon) max-gap=(unit @ud) confs=(unit @ud)]
[%delete-wallet =xpub]
:: TODO: can eliminate basically everything below here
[%address-info =xpub =chyg =idx utxos=(set utxo) used=? block=@ud]
[%tx-info =info:tx block=@ud]
[%generate-address =xpub =chyg =pmet]

111
sur/btc-wallet.hoon Normal file
View File

@ -0,0 +1,111 @@
/- *btc, bp=btc-provider
/+ bip32
|%
+$ params [batch-size=@ud fam-limit=@ud piym-limit=@ud]
+$ provider [host=ship connected=?]
+$ block @ud
+$ btc-state [=block fee=(unit sats) t=@da]
+$ payment [pend=(unit txid) =xpub =address payer=ship value=sats]
+$ piym [ps=(map ship payment) num-fam=(map ship @ud)]
+$ pend-piym (map txid payment)
+$ poym (unit txbu)
::
+$ command
$% [%set-provider provider=ship =network]
[%set-current-wallet =xpub]
[%add-wallet =xpub]
[%delete-wallet =xpub]
[%req-pay-address payee=ship value=sats feyb=sats]
[%broadcast-tx txhex=cord]
==
+$ action
:: local-only actions
::
$: [%close-pym ti=info:tx]
[%add-poym-raw-txi =txid rawtx=hexb]
[%fail-broadcast-tx =txid]
[%succeed-broadcast-tx =txid]
:: peer actions
::
[%gen-pay-address value=sats]
[%recv-pay-address =address value=sats]
[%expect-payment =txid value=sats]
==
::
::
:: Wallet Types
::
:: nixt: next indices to generate addresses from (non-change/change)
:: addi: HD path along with UTXOs
:: wach: map for watched addresses.
:: 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
:: - sitx: signed hex transaction
::
+$ insel [=utxo =chyg =idx]
+$ pmet (unit [payer=ship value=sats])
+$ feyb sats
+$ txi [=utxo ur=(unit hexb) =hdkey]
+$ txo [=address value=sats hk=(unit hdkey)]
+$ txbu
$: =xpub
payee=(unit ship)
=vbytes
txis=(list txi)
txos=(list txo)
sitx=(unit hexb)
==
:: 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)])
==
+$ history (map txid hest)
--

View File

@ -19,7 +19,8 @@
+$ hdkey [=fprint pubkey=hexb =network =bipt =chyg =idx]
+$ sats @ud
+$ vbytes @ud
+$ utxo [pos=@ txid=hexb height=@ value=sats recvd=(unit @da)]
+$ txid hexb
+$ utxo [pos=@ =txid height=@ value=sats recvd=(unit @da)]
++ address-info
$: =address
confirmed-value=sats
@ -36,7 +37,7 @@
segwit=(unit @ud)
==
+$ val
$: txid=hexb
$: =txid
pos=@ud
=address
value=sats
@ -45,14 +46,14 @@
::
+$ info
$: included=?
txid=hexb
=txid
confs=@ud
recvd=(unit @da)
inputs=(list val)
outputs=(list val)
==
+$ input
$: txid=hexb
$: =txid
pos=@ud
sequence=hexb
script-sig=(unit hexb)