Update keygen to match latest spec and have slightly better code style

This commit is contained in:
Fang 2018-08-10 20:36:26 +02:00
parent 6e33eea217
commit 3d984ff87f
2 changed files with 115 additions and 36 deletions

View File

@ -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)
--
--

15
sur/keygen.hoon Normal file
View File

@ -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]
--