From 3d984ff87f9980c31da86614eb7580d7ee7e1fe8 Mon Sep 17 00:00:00 2001 From: Fang Date: Fri, 10 Aug 2018 20:36:26 +0200 Subject: [PATCH] Update keygen to match latest spec and have slightly better code style --- lib/keygen.hoon | 136 +++++++++++++++++++++++++++++++++++------------- sur/keygen.hoon | 15 ++++++ 2 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 sur/keygen.hoon diff --git a/lib/keygen.hoon b/lib/keygen.hoon index 50dae0e8a..9d2bbabb5 100644 --- a/lib/keygen.hoon +++ b/lib/keygen.hoon @@ -1,8 +1,13 @@ :: urbit-style key generation and derivation functions :: +/- keygen +:: /+ bip32 :: -|% +:: +=, sha +=, ^keygen +:: ++ to-byts |= a=@t =+ (met 3 a) @@ -15,41 +20,100 @@ %- argon2:argon2:crypto [out %u 0x13 1 1.024 10 *byts *byts] :: -++ child-seed - |= [seed=byts type=tape series=@ud ship=@p] - ^- byts - :- 64 - %- sha-512l:sha - =+ :(weld type "-" (a-co:co series) "-" (a-co:co ship)) - :- (add wid.seed (lent -)) - (cat 3 (crip (flop -)) dat.seed) +++ full-wallet-from-entropy + |= [entropy=byts seed-size=@ud sis=(set ship) pass=(unit @t)] + =+ owner-seed=seed-size^(argon2u entropy seed-size) + (full-wallet-from-seed owner-seed sis pass) :: -++ wallet-from-seed - |= seed=byts - => (from-seed:bip32 seed) - :* public=`@ux`public-key - private=`@ux`private-key - chain=`@ux`chain-code - == +++ full-wallet-from-seed + |= [owner-seed=byts sis=(set ship) pass=(unit @t)] + =+ dr=~(. sd pass) + :: + :- ^= owner ^- node + :+ *meta dat.owner-seed + (wallet:dr owner-seed) + :: + =/ delegate-meta=meta ["delegate" 0 ~] + =+ delegate-seed=(seed:dr owner-seed delegate-meta) + :- ^= delegate ^- node + :- meta=delegate-meta + :- seed=`@ux`dat.delegate-seed + keys=(wallet:dr delegate-seed) + :: + =/ manage-meta=meta ["manage" 0 ~] + =+ manage-seed=(seed:dr owner-seed manage-meta) + :- ^= manage ^- node + :- meta=manage-meta + :- seed=`@ux`dat.manage-seed + keys=(wallet:dr manage-seed) + :: + =/ transfer=(map ship (pair byts node)) + %- ~(rep in sis) + |= [w=ship n=(map ship (pair byts node))] + %+ ~(put by n) w + =+ m=["transfer" 0 `w] + =+ s=(seed:dr owner-seed -) + [s [m dat.s (wallet:dr s)]] + :- ^= transfer ^- nodes + (~(run by transfer) tail) + :: + :- ^= spawn ^- nodes + %- ~(rep by transfer) + |= [[w=ship s=byts *] n=(map ship node)] + %+ ~(put by n) w + =+ m=["spawn" 0 `w] + =+ s=(seed:dr s m) + [m dat.s (wallet:dr s)] + :: + ^= network ^- uodes + %- ~(rep in sis) + |= [w=ship u=(map ship uode)] + %+ ~(put by u) w + =+ m=["network" 0 `w] + =+ s=(seed:dr manage-seed m) + [m dat.s (urbit:dr s)] :: -++ generate-full-wallet - |= [entropy=byts shh=ship] ::TODO set of ships - =+ owner-seed=16^(argon2u entropy 16) - :- owner=(wallet-from-seed owner-seed) - =+ transfer-seed=(child-seed owner-seed "transfer" 0 shh) - :- transfer=(wallet-from-seed transfer-seed) - =+ spawn-seed=(child-seed transfer-seed "spawn" 0 shh) - :- spawn=(wallet-from-seed spawn-seed) - =+ manage-seed=(child-seed transfer-seed "manage" 0 shh) - :- manage=(wallet-from-seed manage-seed) - =+ urbit-seed=(child-seed manage-seed "urbit" 0 shh) - =+ urbit=(pit:nu:crub:crypto (mul 8 wid.urbit-seed) dat.urbit-seed) - =+ pub=pub:ex:urbit - =+ sec=sec:ex:urbit - :- ^= auth - :- public=`@ux`(rsh 3 1 (end 3 33 pub)) - secret=`@ux`(rsh 3 1 (end 3 33 sec)) - ^= crypt - :- public=`@ux`(rsh 3 33 pub) - secret=`@ux`(rsh 3 33 sec) +++ sd :: seed derivation + |_ pass=(unit @t) + ++ append-pass + |= b=byts + ^- byts + =+ (fall pass '') + :- (add wid.b (met 3 -)) + (cat 3 (swp 3 -) dat.b) + :: + ++ wallet + %+ cork append-pass + |= seed=byts + ^- ^wallet + => (from-seed:bip32 64^(sha-512l seed)) + [public-key private-key chain-code] + :: + ++ urbit + %+ cork append-pass + |= seed=byts + ^- edkeys + =+ =< [pub=pub:ex sec=sec:ex] + (pit:nu:crub:crypto (mul 8 wid.seed) dat.seed) + :- ^= auth + :- (rsh 3 1 (end 3 33 pub)) + (rsh 3 1 (end 3 33 sec)) + ^= crypt + :- (rsh 3 33 pub) + (rsh 3 33 sec) + :: + ++ seed + |= [seed=byts meta] + ^- byts + :- wid.seed + %^ rsh 3 (sub 64 wid.seed) + %- sha-512l + %- append-pass + =+ ;: weld + typ "-" (a-co:co rev) + ?~(who ~ ['-' (a-co:co u.who)]) + == + :- (add wid.seed (lent -)) + (cat 3 (crip (flop -)) dat.seed) + -- -- diff --git a/sur/keygen.hoon b/sur/keygen.hoon new file mode 100644 index 000000000..c77b6f296 --- /dev/null +++ b/sur/keygen.hoon @@ -0,0 +1,15 @@ +|% ++= nodes (map ship node) ++= uodes (map ship uode) +:: ++= node [meta=meta seed=@ux keys=wallet] ++= uode [meta=meta seed=@ux keys=edkeys] +:: ++= meta [typ=tape rev=@ud who=(unit ship)] +:: ++= wallet [public=@ux private=@ux chain=@ux] +:: ++= edkeys [auth=keypair crypt=keypair] +:: ++= keypair [public=@ux secret=@ux] +--