From e03d084fb0ca841006c6c2c0fe3b3df6f3b52202 Mon Sep 17 00:00:00 2001 From: timlucmiptev Date: Thu, 29 Oct 2020 14:10:54 +0200 Subject: [PATCH] skeleton for btc-wallet-store --- app/btc-provider.hoon | 2 +- app/btc-wallet-store.hoon | 42 ++++++++++++++++++++++++++------ gen/btc-wallet-store/action.hoon | 13 ++++++++++ lib/btc.hoon | 8 ++++++ sur/btc-wallet-store.hoon | 25 +++++++++++++------ sur/btc.hoon | 3 ++- 6 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 gen/btc-wallet-store/action.hoon diff --git a/app/btc-provider.hoon b/app/btc-provider.hoon index ddbbd09df..c694b2be2 100644 --- a/app/btc-provider.hoon +++ b/app/btc-provider.hoon @@ -47,7 +47,7 @@ ?> ?|((team:title our.bowl src.bowl) (is-client:hc src.bowl)) =^ cards state ?: ?=(%btc-provider-command mark) - ?> (team:title our.bowl src.bowl) + ?> (team:title our.bowl src.bowl) (handle-command:hc !<(command vase)) ?+ mark (on-poke:def mark vase) %btc-provider-action diff --git a/app/btc-wallet-store.hoon b/app/btc-wallet-store.hoon index 6f2ad67c4..423aa600e 100644 --- a/app/btc-wallet-store.hoon +++ b/app/btc-wallet-store.hoon @@ -5,28 +5,27 @@ :: To Subscribers: :: watched address updates :: -/+ dbug, default-agent +/- *btc-wallet-store +/+ dbug, default-agent, bip32, btc |% +$ versioned-state $% state-0 == :: -+$ state-0 [%0 connected=?] ++$ state-0 [%0 wallets=(map tape walt)] :: +$ card card:agent:gall -+$ command - $? %add-wallet - == :: -- =| state-0 =* state - %- agent:dbug ^- agent:gall +=< |_ =bowl:gall +* this . - des ~(. (default:shoe this command) bowl) def ~(. (default-agent this %|) bowl) + hc ~(. +> bowl) :: ++ on-init ^- (quip card _this) @@ -43,7 +42,13 @@ ++ on-poke |= [=mark =vase] ^- (quip card _this) - `this + ?> (team:title our.bowl src.bowl) + =^ cards state + ?+ mark (on-poke:def mark vase) + %btc-wallet-store-action + (handle-action:hc !<(action vase)) + == + [cards this] :: ++ on-watch on-watch:def ++ on-leave on-leave:def @@ -52,3 +57,26 @@ ++ on-arvo on-arvo:def ++ on-fail on-fail:def -- +:: +|_ =bowl:gall +++ handle-action + |= act=action + ^- (quip card _state) + ?- -.act + %add-wallet + (add-wallet xpub.act) + == +++ add-wallet + |= xpub=tape + ^- (quip card _state) + ?: (~(has by wallets.state) xpub) + ~& >>> "xpub already imported" + `state + =/ wallet=walt + :* (from-extended:bip32 xpub) + (xpub-type:btc xpub) + [%.n 0 *wach] + [%.n 0 *wach] + == + `state(wallets (~(put by wallets.state) xpub wallet)) +-- diff --git a/gen/btc-wallet-store/action.hoon b/gen/btc-wallet-store/action.hoon new file mode 100644 index 000000000..896a22aab --- /dev/null +++ b/gen/btc-wallet-store/action.hoon @@ -0,0 +1,13 @@ +:: Sends an action to btc-wallet-store +:: +:: Commands: +:: +:: +:: +/- *btc-wallet-store +:: +:- %say +|= $: [now=@da eny=@uvJ =beak] + [[act=action ~] ~] +== +[%btc-wallet-store-action act] diff --git a/lib/btc.hoon b/lib/btc.hoon index 78b52fb82..ee41ff4ab 100644 --- a/lib/btc.hoon +++ b/lib/btc.hoon @@ -1,5 +1,13 @@ /- *btc |% +++ xpub-type + |= xpub=tape + ^- bipt + =/ prefix=tape (scag 4 xpub) + ?: =("xpub" prefix) %bip44 + ?: =("ypub" prefix) %bip49 + ?: =("zpub" prefix) %bip84 + ~|("invalid xpub: {}" !!) :: big endian sha256: input and output are both MSB first (big endian) :: ++ sha256 diff --git a/sur/btc-wallet-store.hoon b/sur/btc-wallet-store.hoon index a9ee2773d..addd0ca95 100644 --- a/sur/btc-wallet-store.hoon +++ b/sur/btc-wallet-store.hoon @@ -1,22 +1,31 @@ :: wallets are compatible with BIPs 44, 49, and 84 :: m / purpose' / coin_type' / account' / change / address_index +:: change can be 0 or 1 :: -/- *btc, bip32 +/- *btc +/+ bip32 |% -:: wilt: copulates with thousands of indices to form addresses +:: addi: address with metadata inside a change path +:: wach: map for watched addresses +:: chyg: stores the state of a change/non-change path +:: wilt: stores xpub; copulates with thousands of indices to form addresses :: walt: wallet metadata :: ++$ addi [idx=@ used=? utxos=(set utxo)] ++$ wach (map address addi) ++$ chyg [scanned=? next=@ =wach] +$ wilt _bip32 +$ walt - $: :: scanned: whether we've checked all addresses for prior activity + $: :: bipt: BIP44/49/84 :: ching: non-change addresses :: chang: change addresses - :: watch: address -> [change address_index] :: =wilt - scanned=? - ching=((mop @ address-info) gth) - chang=((mop @ address-info) gth) - watch=(map address [change=@ index=@]) + =bipt + ching=chyg + chang=chyg + == ++$ action + $% [%add-wallet xpub=tape] == -- diff --git a/sur/btc.hoon b/sur/btc.hoon index 8094ebba0..4c2bbaf3b 100644 --- a/sur/btc.hoon +++ b/sur/btc.hoon @@ -3,7 +3,8 @@ +$ legacy-address $%([%legacy @uc]) +$ bech32-address $%([%bech32 cord]) +$ address ?(legacy-address bech32-address) -+$ sats @ud ++$ bipt $?(%bip44 %bip49 %bip84) ++$ sats @ud :: +$ hash256 [wid=%32 dat=@ux] +$ hash160 [wid=%20 dat=@ux] +$ hash ?(hash256 hash160)