shrub/pkg/arvo/lib/keygen.hoon

107 lines
2.3 KiB
Plaintext
Raw Normal View History

2018-08-07 01:55:15 +03:00
:: urbit-style key generation and derivation functions
::
/- keygen
::
/+ bip32, bip39
2018-08-07 01:55:15 +03:00
::
2018-08-29 01:58:22 +03:00
=, keygen
::
|%
2018-08-07 01:55:15 +03:00
++ argon2u
|= [who=ship tic=byts]
2018-08-07 01:55:15 +03:00
^- @
~| [%who who (met 3 who)]
:: ?> (lte (met 3 who) 4)
2018-10-23 02:37:42 +03:00
%- (argon2-urbit:argon2:crypto 32)
:- tic
=- [(met 3 -) (swp 3 -)]
%- crip
(weld "urbitkeygen" (a-co:co who))
2018-08-07 01:55:15 +03:00
::
++ child-node-from-seed
|= [seed=@ typ=tape pass=(unit @t)]
^- node
=+ sed=(seed:ds 32^seed typ)
=+ nom=(from-entropy:bip39 32^sed)
:+ typ nom
2018-10-31 03:15:56 +03:00
%- wallet:ds
%+ to-seed:bip39 nom
(trip (fall pass ''))
::
++ derive-network-seed
|= [mngs=@ rev=@ud]
^- @ux
=+ (seed:ds 64^mngs (weld "network" (a-co:co rev)))
?: =(0 rev) -
:: hash again to prevent length extension attacks
(sha-256l:sha 32 -)
::
2018-08-14 02:36:21 +03:00
++ full-wallet-from-ticket
:: who: username
:: ticket: password
:: rev: network key revision
:: pass: optional passphrase
::
|= [who=ship ticket=byts rev=@ud pass=(unit @t)]
2018-11-06 04:58:47 +03:00
^- vault
=+ master-seed=(argon2u who ticket)
=/ cn :: child node
|= typ=nodetype
(child-node-from-seed master-seed typ pass)
::
:- ^= ownership ^- node
(cn "ownership")
::
:- ^= voting ^- node
(cn "voting")
::
=/ management=node
(cn "management")
2018-10-31 03:15:56 +03:00
:- management=management
::
:- ^= transfer ^- node
(cn "transfer")
::
:- ^= spawn ^- node
(cn "spawn")
::
^= network ^- uode
=/ mad :: management seed
%+ to-seed:bip39
seed:management
(trip (fall pass ''))
=+ sed=(derive-network-seed mad rev)
[rev sed (urbit:ds sed)]
::
++ ds :: derive from raw seed
2018-10-31 03:15:56 +03:00
|%
++ wallet
2018-10-31 03:15:56 +03:00
|= seed=@
^- ^wallet
2018-10-31 03:15:56 +03:00
=+ => (from-seed:bip32 64^seed)
(derive-path "m/44'/60'/0'/0/0")
:+ [public-key private-key]
(address-from-prv:key:ethereum private-key)
chain-code
::
++ urbit
2018-10-31 03:15:56 +03:00
|= seed=@
^- edkeys
=+ =< [pub=pub:ex sec=sec:ex]
2018-10-23 02:37:42 +03:00
(pit:nu:crub:crypto 256 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 salt=tape]
2018-10-23 02:37:42 +03:00
^- @ux
2018-10-31 03:15:56 +03:00
%- sha-256l:sha
:- (add wid.seed (lent salt))
(cat 3 (crip (flop salt)) dat.seed)
--
2018-08-07 01:55:15 +03:00
--