/
registrar.ts
69 lines (59 loc) · 2.32 KB
/
registrar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Identity } from '../../identity/identity'
import { getRegistrar } from '@jolocom/local-resolver-registrar'
import { DidDocument } from '../../identity/didDocument/didDocument'
import { SignedCredential } from '../../credentials/signedCredential/signedCredential'
import { IRegistrar } from '../types'
import { createDb } from '@jolocom/local-resolver-registrar/js/db'
import { getIdFromEvent, validateEvents, getIcp } from '@jolocom/native-core'
import { SoftwareKeyProvider } from '@jolocom/vaulted-key-provider'
interface CreationReturn {
id: string
encryptedWallet: string
inceptionEvent: string
}
export class LocalRegistrar implements IRegistrar {
public prefix = 'jun'
private registrar: ReturnType<typeof getRegistrar>
public constructor(db = createDb()) {
this.registrar = getRegistrar({
dbInstance: db,
create: getIcp,
getIdFromEvent,
validateEvents,
})
}
public async create(keyProvider: SoftwareKeyProvider, password: string) {
const ret = (await this.registrar.create({
encryptedWallet: keyProvider.encryptedWallet,
id: keyProvider.id,
pass: password,
})) as CreationReturn
// @ts-ignore Assigning directly to a private property
keyProvider._encryptedWallet = Buffer.from(ret.encryptedWallet, 'base64')
// @ts-ignore Assigning directly to a private property
keyProvider._id = ret.id
const didDoc = JSON.parse(await validateEvents(ret.inceptionEvent))
const identity = Identity.fromDidDocument({
didDocument: DidDocument.fromJSON(didDoc),
})
await this.encounter(ret.inceptionEvent)
return identity
}
// TODO Verify signature on the public profile? Or just assume it's correct
// TODO Public profile should perhaps be JSON / any, so that the registrars can be used without having to typecheck / guard / use generics
public async updatePublicProfile(
keyProvider: SoftwareKeyProvider,
password: string,
identity: Identity,
publicProfile: SignedCredential,
) {
console.error(`"updatePublicProfile not implemented for did:${this.prefix}`) // TODO Better error
return false
}
public async encounter(deltas: string) {
const didDocJson = JSON.parse(await this.registrar.update(deltas))
return Identity.fromDidDocument({
didDocument: DidDocument.fromJSON(didDocJson),
})
}
}