import { encrypt } from './encrypt' ; import { pair as createPair } from './pair' ; import { pseudoRandomText } from './pseudoRandomText' ; import { signGraph } from './sign' ; import { work } from './work' ; export async function createUser ( chaingun , alias , password ) { const aliasSoul = `~@${ alias }` ; const salt = pseudoRandomText ( 64 ) ; const proof = await work ( password , salt ) ; const pair = await createPair ( ) ; const { pub , priv , epub , epriv } = pair ; const pubSoul = `~${ pub }` ; const ek = await encrypt ( JSON . stringify ( { priv , epriv } ) , proof , { raw : true } ) ; const auth = JSON . stringify ( { ek , s : salt } ) ; const data = { alias , auth , epub , pub } ; const now = new Date ( ) . getTime ( ) ; const graph = await signGraph ( { [ pubSoul ] : { _ : { '#' : pubSoul , '>' : Object . keys ( data ) . reduce ( ( state , key ) => { state [ key ] = now ; return state ; } , { } ) } , ... data } } , { pub , priv } ) ; await new Promise ( ok => chaingun . get ( aliasSoul ) . put ( graph , ok ) ) ; return { ... data , epriv , priv , pub } ; }