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
/
Claim.ts
187 lines (176 loc) · 4.79 KB
/
Claim.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
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable camelcase */
/* eslint-disable max-classes-per-file */
import Credential from '../claim/Credential'
import { wasmStringify } from '../wasm/wasm_exec_wrapper'
import {
Attestation,
AttesterPublicKey,
IIssueAttestation,
InitiateAttestationRequest,
} from './Attestation'
import {
CombinedPresentationRequest,
PresentationRequest,
} from './Verification'
import WasmData from './Wasm'
export default interface IClaimer {
requestAttestation: ({
claim,
startAttestationMsg,
attesterPubKey,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
claim: Record<string, any>
startAttestationMsg: InitiateAttestationRequest
attesterPubKey: AttesterPublicKey
}) => Promise<{
message: AttestationRequest
session: ClaimerAttestationSession
}>
buildCredential: ({
claimerSession,
attestation,
}: {
claimerSession: ClaimerAttestationSession
attestation: Attestation
}) => Promise<Credential>
buildPresentation: ({
credential,
presentationReq,
attesterPubKey,
}: {
credential: Credential
presentationReq: PresentationRequest
attesterPubKey: AttesterPublicKey
}) => Promise<Presentation>
buildCombinedPresentation: ({
credentials,
combinedPresentationReq,
attesterPubKeys,
}: {
credentials: Credential[]
combinedPresentationReq: CombinedPresentationRequest
attesterPubKeys: AttesterPublicKey[]
}) => Promise<CombinedPresentation>
}
export interface IProof {
proof: {
A: 'string'
a_disclosed: {
[key: number]: string
}
a_responses: {
[key: number]: string
}
c: string
e_response: string
nonrev_proof: {
C_r: string
C_u: string
responses: {
beta: string
delta: string
epsilon: string
zeta: string
}
sacc: {
data: string
pk: number
}
}
nonrev_response: string
v_response: string
}
}
/**
* An error which can occur during the [[Attestation]] process.
*/
export class ClaimError extends Error {
/**
* An error which is thrown when the [[Attestation]] object cannot be deserialized to JSON.
*/
public static duringParsing = new ClaimError(
'invalid request: could not parse json'
)
/**
* An error which is thrown when the [[Attestation]] object does not include the original claim object.
*/
public static claimMissing = new ClaimError(
'invalid request: claim is missing'
)
/**
* An error which is thrown when the [[Attestation]] object includes a non-object type claim.
*
* @param type The type of the claim found in the [[Attestation]] object.
* @returns A new claim error.
*/
public static notAnObject = (
type:
| 'string'
| 'number'
| 'bigint'
| 'boolean'
| 'symbol'
| 'undefined'
| 'object'
| 'function'
): ClaimError =>
new ClaimError(`invalid request: expected object, received ${type}`)
}
/**
* The message result of [[requestAttestation]] which is sent to the [[Attester]] and used in [[Attester.issueAttestation]].
*/
export class AttestationRequest extends WasmData {
/**
* Extracts the original claim object from the [[AttestationRequest]].
*
* @throws [[ClaimError.duringParsing]] If an error occurs during JSON deserialization.
* @throws [[ClaimError.claimMissing]] If the claim is missing inside the [[AttestationRequest]].
* @returns The original claim object which has been attested.
*/
public getClaim(): Record<string, unknown> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let claim: Record<string, any>
try {
claim = this.parse()?.claim
} catch (e) {
throw ClaimError.duringParsing
}
if (claim === undefined) {
throw ClaimError.claimMissing
}
return claim
}
}
/**
* The session result of [[requestAttestation]] which should be kept private by the [[Claimer]] and used in [[buildCredential]].
*/
export class ClaimerAttestationSession extends WasmData {
// @ts-ignore
private thisIsOnlyHereToPreventClassMixes: undefined
}
/**
* The result of [[buildPresentation]] which can be used to disclose attributes with a Verifier.
*/
export class Presentation extends WasmData {
public parse(): IProof {
return JSON.parse(wasmStringify(this))
}
}
/**
* The result of [[buildCombinedPresentation]] which can be used to verify of multiple [[Credential]]s at once.
*/
export class CombinedPresentation extends WasmData {
// @ts-ignore
private thisIsOnlyHereToPreventClassMixes: undefined
}
export interface ICredential<Claim> {
claim: Claim
credential: {
attributes: string[]
nonrevWitness: IIssueAttestation['nonrev']
signature: IIssueAttestation['signature']
}
updateCounter: number
}