-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathemailVault.ts
More file actions
144 lines (128 loc) · 3.7 KB
/
Copy pathemailVault.ts
File metadata and controls
144 lines (128 loc) · 3.7 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
EmailVaultData,
EmailVaultOperation,
EmailVaultSecret,
RecoveryKey
} from '../../interfaces/emailVault'
import { Fetch } from '../../interfaces/fetch'
import { relayerCall } from '../relayerCall/relayerCall'
export interface Secret {
key: String
type: String
}
// NOTE: its a quick fix. Will be updated in other branch
export interface EmailVaultInfo {
email: String
recoveryKey: String
availableSecrets: Secret[]
availableAccounts: any
}
export class EmailVault {
private callRelayer: Function
constructor(fetch: Fetch, relayerUrl: string) {
this.callRelayer = relayerCall.bind({ url: relayerUrl, fetch })
}
async getRecoveryKeyAddress(email: String, authKey: String): Promise<RecoveryKey> {
return (await this.callRelayer(`/email-vault/get-recovery-key/${email}/${authKey}`)).data
}
async getSessionKey(email: String, authKey: String): Promise<string> {
return (await this.callRelayer(`/email-vault/get-session-key/${email}/${authKey}`))?.data
?.sessionKey
}
async getEmailVaultInfo(email: String, authKey: String): Promise<EmailVaultData | null> {
const result = await this.callRelayer(`/email-vault/email-vault-info/${email}/${authKey}`).then(
(res: any) => res.data
)
return {
...result,
availableAccounts: Object.fromEntries(
result.availableAccounts.map((acc: any) => [acc.addr, acc])
),
availableSecrets: Object.fromEntries(
result.availableSecrets.map((secret: any) => [secret.key, secret])
)
}
}
async operations(
email: String,
authKey: String,
operations: EmailVaultOperation[]
): Promise<EmailVaultOperation[] | null> {
return (
await this.callRelayer(`/email-vault/post-operations/${email}/${authKey}`, 'POST', {
operations
})
).data
}
async getOperations(
email: String,
authKey: String,
operations: EmailVaultOperation[]
): Promise<EmailVaultOperation[] | null> {
return (
await this.callRelayer(`/email-vault/get-operations/${email}/${authKey}`, 'POST', {
operations
})
).data
}
async addKeyStoreSecret(
email: String,
authKey: String,
keyStoreUid: String,
secret: String
): Promise<Boolean> {
return (
await this.callRelayer(`/email-vault/add-key-store-secret/${email}/${authKey}`, 'POST', {
secret,
uid: keyStoreUid
})
).success
}
async removeKeyStoreSecretFromRelayer(
email: String,
authKey: String,
keyStoreUid: String
): Promise<Boolean> {
return (
await this.callRelayer(`/email-vault/remove-key-store-secret/${email}/${authKey}`, 'POST', {
uid: keyStoreUid
})
).success
}
async retrieveKeyStoreSecret(
email: String,
authKey: String,
keyStoreUid: String
): Promise<EmailVaultSecret> {
return (
await this.callRelayer(
`/email-vault/retrieve-key-store-secret/${email}/${keyStoreUid}/${authKey}`
)
).data
}
async addKeyBackup(
email: String,
authKey: String,
keyAddress: String,
privateKeyEncryptedJSON: String
): Promise<Boolean> {
return (
await this.callRelayer(`/email-vault/add-key-backup/${email}/${authKey}`, 'POST', {
keyAddress,
encryptedBackup: privateKeyEncryptedJSON
})
).success
}
async retrieveKeyBackup(
email: String,
authKey: String,
keyAddress: String
): Promise<EmailVaultSecret> {
return (
await this.callRelayer(`/email-vault/retrieve-key-backup/${email}/${keyAddress}/${authKey}`)
).data
}
async getInfo(email: String, authKey: String): Promise<EmailVaultInfo> {
return (await this.callRelayer(`/email-vault/email-vault-info/${email}/${authKey}`)).data
}
}