This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Claimer.ts
235 lines (225 loc) · 7.81 KB
/
Claimer.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { u8aToHex } from '@polkadot/util'
import validate from '@polkadot/util-crypto/mnemonic/validate'
import { mnemonicToMiniSecret } from '@polkadot/util-crypto'
import IClaimer, {
AttestationRequest,
ClaimerAttestationSession,
Presentation,
CombinedPresentation,
ClaimError,
} from '../types/Claim'
import WasmHooks from '../wasm/WasmHooks'
import {
IGabiMsgSession,
InitiateAttestationRequest,
Attestation,
AttesterPublicKey,
KeyLength,
DEFAULT_KEY_LENGTH,
} from '../types/Attestation'
import {
CombinedPresentationRequest,
PresentationRequest,
} from '../types/Verification'
import goWasmExec, { wasmStringify } from '../wasm/wasm_exec_wrapper'
import Credential from './Credential'
/**
* Checks that the provided claim is a valid object.
*
* @param claim The object which should be a valid claim.
* @throws [[ClaimError.claimMissing]] If the claim is missing inside the [[AttestationRequest]].
* @throws [[ClaimError.notAnObject]] If the [[Attestation]] object includes a non-object type claim.
* @throws [[ClaimError.duringParsing]] If an error occurs during JSON deserialization.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function checkValidClaimStructure(claim: Record<string, any>): void | Error {
if (!Object.keys(claim).length) {
throw ClaimError.claimMissing
}
if (typeof claim !== 'object') {
throw ClaimError.notAnObject(typeof claim)
}
if (Array.isArray(claim)) {
throw ClaimError.duringParsing
}
}
export default class Claimer implements IClaimer {
private readonly secret: string
/**
* Generates a claimer using the provided mnemonic.
*
* @param mnemonic The mnemonic which is used to generate the key.
* @param options An optional object containing options for the key generation.
* @param options.password The password which is used to generate the key.
* @param options.keyLength The key length of the new secret. Note that this secret will only support credentials and attester with the same key length.
* @returns A new [[Claimer]].
*/
public static async buildFromMnemonic(
mnemonic: string,
{
password,
keyLength,
}: {
password?: string
keyLength?: KeyLength
} = {}
): Promise<Claimer> {
if (!validate(mnemonic)) {
throw new Error('Invalid mnemonic')
}
const seed = mnemonicToMiniSecret(mnemonic, password)
return this.buildFromSeed(seed, keyLength)
}
/**
* Generates a claimer using the provided seed.
*
* @param seed The seed which is used to generate the key.
* @param keyLength The key length of the new secret. Note that this secret will only support credentials and attester with the same key length.
* @returns A new [[Claimer]].
*/
public static async buildFromSeed(
seed: Uint8Array,
keyLength?: KeyLength
): Promise<Claimer> {
// secret's structure unmarshalled is { MasterSecret: string }
const secret = await goWasmExec<string>(WasmHooks.keyFromSeed, [
u8aToHex(seed),
keyLength || DEFAULT_KEY_LENGTH,
])
return new this(secret)
}
/**
* Generates a new master secret and returns a new [[Claimer]] object.
*
* @returns A new [[Claimer]].
*/
public static async create(): Promise<Claimer> {
const secret = await goWasmExec<string>(WasmHooks.genKey)
return new this(secret)
}
/**
* Constructs a new [[Claimer]] using the given master secret.
*
* @param secret The master secret of the [[Claimer]].
*/
constructor(secret: string) {
this.secret = secret
}
/**
* Creates an [[AttestationRequest]] using the provided [[InitiateAttestationRequest]].
*
* @param p The parameter object.
* @param p.claim The claim which should get attested.
* @param p.startAttestationMsg The [[InitiateAttestationRequest]] provided by the attester.
* @param p.attesterPubKey The [[AttesterPublicKey]].
* @returns An [[AttestationRequest]] and a [[ClaimerAttestationSession]] which together with an [[Attestation]]
* can be used to create a [[Credential]].
*/
public async requestAttestation({
claim,
startAttestationMsg,
attesterPubKey,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
claim: Record<string, any>
startAttestationMsg: InitiateAttestationRequest
attesterPubKey: AttesterPublicKey
}): ReturnType<IClaimer['requestAttestation']> {
// check for invalid claim structure
checkValidClaimStructure(claim)
const { message, session } = await goWasmExec<IGabiMsgSession>(
WasmHooks.requestAttestation,
[
this.secret,
JSON.stringify(claim),
wasmStringify(startAttestationMsg),
wasmStringify(attesterPubKey),
]
)
return {
message: new AttestationRequest(message),
session: new ClaimerAttestationSession(session),
}
}
/**
* Builds a [[Credential]] using the [[ClaimerAttestationSession]] and the [[Attestation]].
*
* @param p The parameter object.
* @param p.claimerSession The session object corresponding to the [[AttestationRequest]].
* @param p.attestation The [[Attestation]] provided by the [[Attester]].
* @returns A signed and valid [[Credential]].
*/
public async buildCredential({
claimerSession,
attestation,
}: {
claimerSession: ClaimerAttestationSession
attestation: Attestation
}): ReturnType<IClaimer['buildCredential']> {
return new Credential(
await goWasmExec<string>(WasmHooks.buildCredential, [
this.secret,
wasmStringify(claimerSession),
wasmStringify(attestation),
])
)
}
/**
* Uses the [[PresentationRequest]] and a [[Credential]] to build an anonymous presentation.
*
* @param p The parameter object.
* @param p.credential The [[Credential]] which contains all the requested attributes.
* @param p.presentationReq The [[PresentationRequest]] received from the Verifier.
* @param p.attesterPubKey The public key of the [[Attester]] who signed the [[Credential]].
* @returns A [[Presentation]] that can be used to disclose attributes with a Verifier.
* Must only be used once!
*/
public async buildPresentation({
credential,
presentationReq,
attesterPubKey,
}: {
credential: Credential
presentationReq: PresentationRequest
attesterPubKey: AttesterPublicKey
}): ReturnType<IClaimer['buildPresentation']> {
return new Presentation(
await goWasmExec<string>(WasmHooks.buildPresentation, [
this.secret,
wasmStringify(credential),
wasmStringify(presentationReq),
wasmStringify(attesterPubKey),
])
)
}
/**
* Uses the [[PresentationRequest]] and a [[Credential]] to build an anonymous presentation.
*
* @param p The parameter object.
* @param p.credentials An array of [[Credential]]s which is used to provide the requested attributes.
* @param p.combinedPresentationReq The array of [[PresentationRequest]]s received from the Verifier.
* @param p.attesterPubKeys An array of [[AttesterPublicKey]]s which corresponds to the array of [[Credential]]s.
* @returns A [[CombinedPresentation]] that can be used to disclose attributes with a Verifier.
* Must only be used once!
*/
public async buildCombinedPresentation({
credentials,
combinedPresentationReq,
attesterPubKeys,
}: {
credentials: Credential[]
combinedPresentationReq: CombinedPresentationRequest
attesterPubKeys: AttesterPublicKey[]
}): ReturnType<IClaimer['buildCombinedPresentation']> {
// make a json array out of already json serialised values
// we don't want a json array of strings
return new CombinedPresentation(
await goWasmExec<string>(WasmHooks.buildCombinedPresentation, [
this.secret,
`[${credentials.join(',')}]`,
wasmStringify(combinedPresentationReq),
`[${attesterPubKeys.join(',')}]`,
])
)
}
}