From d4b25cc5f3621cc69e9dfaa98b0acde77242c4fa Mon Sep 17 00:00:00 2001 From: timlucmiptev Date: Sat, 28 Nov 2020 09:25:32 +0200 Subject: [PATCH] BTC signing removed; type changes --- MOON_INSTALL.md | 49 +++++++++++++++ app/btc-wallet-hook.hoon | 21 +++---- app/btc-wallet-store.hoon | 2 + lib/btc.hoon | 128 -------------------------------------- sur/btc-wallet-hook.hoon | 9 ++- sur/btc-wallet-store.hoon | 10 ++- 6 files changed, 75 insertions(+), 144 deletions(-) create mode 100644 MOON_INSTALL.md diff --git a/MOON_INSTALL.md b/MOON_INSTALL.md new file mode 100644 index 0000000000..81c26578e6 --- /dev/null +++ b/MOON_INSTALL.md @@ -0,0 +1,49 @@ +# Installing on a Moon +Requires base hash at least: `rd3oe` + +## Create Moon +In your Urbit: +``` +|moon +``` +Copy the key and note the moon name. + +## Install New `zuse.hoon` +``` +./urbit -w $MOON_NAME -G $COPIED_KEY +``` +The moon will compile and apply OTAs. After that is done, run: +``` +|mount % +``` + +Back outside: +``` +cd $BTC_AGENTS_DIR +./install-zuse.sh $MOON_PIER +``` + +In moon: +``` +|commit %home +|reset +``` + +Install the rest of the files: +``` +./install.sh $MOON_PIER +``` + +The kernel will recompile. Then test that the new `decompress-point` is included. +The below should yield: `0x3.30d5.4fd0.dd42.0a6e.5f8d.3624.f5f3.482c.ae35.0f79.d5f0.753b.f5be.ef9c.2d91.af3c` +``` +=bip32 -build-file %/lib/bip32/hoon +=ecc secp256k1:secp:crypto +=xpub "zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs" +`@ux`(compress-point:ecc pub:(derive-public:(derive-public:(from-extended:bip32 xpub) 0) 0)) +``` + +## Start `btc-provider` +``` +:btc-provider|command [%set-credentials api-url='http://localhost:50002'] +``` diff --git a/app/btc-wallet-hook.hoon b/app/btc-wallet-hook.hoon index 7f757546f9..2bf94c796d 100644 --- a/app/btc-wallet-hook.hoon +++ b/app/btc-wallet-hook.hoon @@ -24,7 +24,7 @@ == :: provider: maybe ship if provider is set :: moon-limit: how many addresses a ship and its moons can request in piym -:: feybs: fee/byte in sats for a given ship payee +:: feybs: fee/byte in sats used for a given ship payee :: piym/poym-watch: listen to btc-wallet-store for address updates; update payment info :: +$ state-0 @@ -34,13 +34,12 @@ def-wallet=(unit xpub) moon-limit=@ud feybs=(map ship sats) - =pend-addr + =reqs =piym poym=(unit txbu:bws) - =piym-watch - =poym-watch == :: +:: TODO: find all instances of scan-addr -- make them typed correctly +$ card card:agent:gall -- =| state-0 @@ -178,7 +177,7 @@ `state(poym ~) :: %force-retry - [retry-pend-addr state] + [retry-scan-addr state] == :: +handle-provider-status: handle connectivity updates from provider :: if status is %connected, retry all pending address lookups @@ -192,7 +191,7 @@ ?- -.s %connected :- ?: connected.u.provider ~ - (weld retry-pend-addr retry-txbu) + (weld retry-scan-addr retry-txbu) %= state provider `[host.u.provider %.y] btc-state [blockcount.s fee.s now.bowl] @@ -207,9 +206,9 @@ ?. ?=(%.y -.upd) `state ?- -.body.p.upd %address-info - =+ req=(~(get by pend-addr) req-id.p.upd) + =+ req=(~(get by scan-addr) req-id.p.upd) ?~ req `state - :_ state(pend-addr (~(del by pend-addr) req-id.p.upd)) + :_ state(scan-addr (~(del by scan-addr) req-id.p.upd)) :~ %- poke-wallet-store :* %address-info xpub.u.req chyg.u.req idx.u.req utxos.body.p.upd used.body.p.upd blockcount.body.p.upd @@ -229,7 +228,7 @@ ?- -.req %scan-address =+ ri=(gen-req-id:bp eny.bowl) - :_ state(pend-addr (~(put by pend-addr) ri req)) + :_ state(scan-addr (~(put by scan-addr) ri req)) ?~ provider ~ ?: provider-connected ~[(get-address-info ri host.u.provider a.req)] @@ -305,10 +304,10 @@ %+ levy txis.u.poym |=(t=txi:bws ?=(^ ur.t)) :: -++ retry-pend-addr +++ retry-scan-addr ^- (list card) ?~ provider ~|("provider not set" !!) - %+ turn ~(tap by pend-addr) + %+ turn ~(tap by scan-addr) |= [ri=req-id:bp req=request:bws] (get-address-info ri host.u.provider a.req) :: diff --git a/app/btc-wallet-store.hoon b/app/btc-wallet-store.hoon index 9729f44fcd..f3526453c4 100644 --- a/app/btc-wallet-store.hoon +++ b/app/btc-wallet-store.hoon @@ -19,6 +19,7 @@ == :: walts: all wallets, keyed by their xpubs :: scans: batch info for wallets being scanned +:: gena: generated addresses that haven't had activity yet :: batch-size: how many addresses to send out at once for checking :: last-block: most recent block seen by the store :: @@ -26,6 +27,7 @@ $: %0 walts=(map xpub:btc walt) =scans + =gena batch-size=@ud last-block=@ud == diff --git a/lib/btc.hoon b/lib/btc.hoon index ee33dfb3d4..5b9425d115 100644 --- a/lib/btc.hoon +++ b/lib/btc.hoon @@ -85,134 +85,6 @@ |= bs=(list ^buffer) ^- byts %- to-byts (zing bs) -- -:: -:: TODO: current status -:: - creates sighash for witness correctly -:: - creates sighash for 1-input legacy correctly -:: - don't know yet whether the sighash for multiple-input legacy is correct -- test w JS -:: -++ unsigned-tx - =, buffer - |_ ut=unsigned:tx - ++ sequence-buffer - |= =input:tx ^- ^buffer - (from-byts sequence.input) - :: - ++ outputs-buffer - |= =output:tx ^- ^buffer - %+ weld - (from-atom-le 8 value.output) - (address-to-script-pubkey address.output) - :: - ++ sighash - |= input-index=@ ^- hash256 - ?: (gte input-index (lent inputs.ut)) - ~|("Input index out of range" !!) - =/ =input:tx (snag input-index inputs.ut) - ?: =(1 witness-ver.input) - (sighash-witness input) - (sighash-legacy input-index) - :: - ++ sighash-witness - |= =input:tx - |^ ^- hash256 - =/ prevouts=byts - %- concat-as-byts (turn inputs.ut prevouts-buffer) - =/ sequences=byts - %- concat-as-byts (turn inputs.ut sequence-buffer) - =/ outputs=byts - %- concat-as-byts (turn outputs.ut outputs-buffer) - :: Hash inputs in order, as per BIP143 examples - :: - =/ n-version=^buffer (from-atom-le 4 version.ut) - =/ hash-prevouts=^buffer - %- from-byts (dsha256 prevouts) - =/ hash-sequence=^buffer - %- from-byts (dsha256 sequences) - =/ outpoint=^buffer - %+ weld (from-byts txid.input) - (from-atom-le 4 witness-ver.input) - =/ script-code=^buffer - %- to-script-pubkey - (slag 2 (from-byts script-pubkey.input)) - =/ amount=^buffer - (from-atom-le 8 value.input) - =/ n-sequence=^buffer (sequence-buffer input) - =/ hash-outputs=^buffer - %- from-byts (dsha256 outputs) - =/ n-locktime=^buffer (from-atom-le 4 locktime.ut) - =/ n-hashtype=^buffer (from-atom-le 4 1) - %- dsha256 - %- concat-as-byts - :~ n-version - hash-prevouts - hash-sequence - outpoint - script-code - amount - n-sequence - hash-outputs - n-locktime - n-hashtype - == - :: - ++ prevouts-buffer - |= =input:tx ^- ^buffer - %+ weld - (from-byts txid.input) - (from-atom-le 4 witness-ver.input) - -- - :: - ++ sighash-legacy - :: TODO: Not working--wrong sighash for multiple inputs (works for 1) - |= index-to-sign=@ - |^ ^- hash256 - =/ n-version=^buffer (from-atom-le 4 version.ut) - =/ num-inputs=^buffer ~[(@ux (lent inputs.ut))] - =/ prevouts=^buffer - %- zing - (turn inputs-with-index (cury prevouts-buffer index-to-sign)) - =/ num-outputs=^buffer ~[(@ux (lent outputs.ut))] - =/ outputs=^buffer - %- zing (turn outputs.ut outputs-buffer) - =/ n-locktime=^buffer (from-atom-le 4 locktime.ut) - =/ n-hashtype=^buffer (from-atom-le 4 1) :: we only support SIGHASH_ALL - =/ struct=(list ^buffer) - :~ v=n-version - ni=num-inputs - prev=prevouts - no=num-outputs - os=outputs - lock=n-locktime - hash-type=n-hashtype - == -:: ~& >>> struct - (dsha256 (concat-as-byts struct)) - :: - ++ inputs-with-index - ^- (list [@ input:tx]) - %+ turn (gulf 0 (dec (lent inputs.ut))) - |= i=@ [i (snag i inputs.ut)] - ++ prevouts-buffer - |= [index-to-sign=@ index=@ =input:tx] - ^- ^buffer - %- zing - :~ (from-byts txid.input) - (from-atom-le 4 tx-index.input) - :: only insert script-pubkey if we're on the input index being signed - ?: =(index-to-sign index) - (format-script-pubkey (from-byts script-pubkey.input)) - ~[0x0] - (from-byts sequence.input) - == - ++ format-script-pubkey - |= spk=^buffer ^- ^buffer - ?: =((^buffer ~[0x76 0xa9]) (scag 2 spk)) - [0x19 spk] - spk - -- - -- -:: :: Converts a list of bits to a list of n-bit numbers :: input-bits should be big-endian :: diff --git a/sur/btc-wallet-hook.hoon b/sur/btc-wallet-hook.hoon index e4ad987e93..f749a58ea6 100644 --- a/sur/btc-wallet-hook.hoon +++ b/sur/btc-wallet-hook.hoon @@ -1,17 +1,20 @@ /- *btc, bws=btc-wallet-store, bp=btc-provider |% +:: btc-state: state from the provider; t is last update time :: req-id: hash of [xpub chyg idx] -:: pend: lookup of req-id -> requests from wallet-store +:: reqs: lookup of req-id -> requests from wallet-store+blockcount +:: blockcount included so that we only request address info when +:: there's a newer block, in the case of addresses we are cooking :: :: payment: a payment expected from another ship :: - address: address generated for this payment :: piym: incoming payments. Stores all ship moons under their planet. :: piym-watch/poym-watch: :: let us link an address back to its incoming/outgoing payment -:: checked when address updates come from btc-wallet-store +:: checked when address updates come from btc-wallet-store :: +$ btc-state [blockcount=@ud fee=sats t=@da] -+$ pend-addr (map req-id:bp request:bws) ++$ reqs (map req-id:bp [blockcount=@ud req=request:bws]) :: +$ payment [=address payer=ship value=sats] :: diff --git a/sur/btc-wallet-store.hoon b/sur/btc-wallet-store.hoon index 46672530c6..a7712cd95e 100644 --- a/sur/btc-wallet-store.hoon +++ b/sur/btc-wallet-store.hoon @@ -51,10 +51,14 @@ +$ txi [=utxo ur=(unit rawtx) =key] +$ txo [=address value=sats] +$ txbu [payee=(unit ship) =vbytes txis=(list txi) txos=(list txo)] -:: TODO: document +:: scanning addresses and monitoring generated addresses +:: batch: indexes to scan for a given chyg +:: scans: all scans underway (batches) +:: gena: any generated address that hasn't had activity yet :: +$ batch [todo=(set idx) endpoint=idx has-used=?] +$ scans (map [xpub chyg] batch) ++$ gena (set address) :: :: %add-wallet: add wallet to state and initiate a scan :: %scan: start a scan of the next address batch in a wallet @@ -74,8 +78,10 @@ [%generate-txbu =xpub =txbu] [%scan-done =xpub] == +:: %scan-address: address we want [used? balance] for +:: %cook-address: monitor address until it gets N confs :: +$ request - $% [%scan-address a=address =xpub =chyg =idx] + $% [%address-info blockcount=@ud a=address =xpub =chyg =idx] == --