forked from neti-software/chalcedony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
194 lines (176 loc) · 5.03 KB
/
index.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
import { TypedDataSigner } from "@ethersproject/abstract-signer";
import { DIDWithKeys } from "@jpmorganchase/onyx-ssi-sdk";
import { CredentialPayload } from "did-jwt-vc";
import { Wallet, Signer } from "ethers";
// TYPES
export interface EIP712Config {
name: string;
version: string;
chainId: number;
verifyingContract: string;
}
export interface TypedData {
name: string;
type:
| "bool"
| "uint8"
| "uint16"
| "uint32"
| "uint64"
| "uint128"
| "uint256"
| "address"
| "string"
| "string[]"
| "bytes"
| "bytes32"
| "Issuer"
| "CredentialSubject"
| "CredentialSchema"
| "Proof";
}
export interface EIP712MessageTypes {
EIP712Domain: TypedData[];
[additionalProperties: string]: TypedData[];
}
export interface EIP712DomainTypedData {
chainId: number;
name: string;
verifyingContract: string;
version: string;
}
export interface EIP712TypedData<T extends EIP712MessageTypes> {
types: T;
primaryType: keyof T;
domain: EIP712DomainTypedData;
message: any;
}
export interface EIP712CredentialMessageTypes extends EIP712MessageTypes {
VerifiableCredential: typeof VERIFIABLE_CREDENTIAL_EIP712_TYPE;
Issuer: any;
CredentialSubject: any;
CredentialSchema: typeof CREDENTIAL_SCHEMA_EIP712_TYPE;
Proof: typeof PROOF_EIP712_TYPE;
}
export type Extensible<T> = T & { [x: string]: any };
export type EIP712CredentialPayload = {
_context: string[];
_type: string[];
id: string;
issuer: Extensible<{ id: string }> | string;
credentialSubject: Extensible<{
id?: string;
}>;
};
export type EIP712Credential = Extensible<EIP712CredentialPayload>;
export interface EIP712CredentialTypedData
extends EIP712TypedData<EIP712CredentialMessageTypes> {
message: EIP712Credential;
}
// CONSTS
export const VERIFIABLE_CREDENTIAL_PRIMARY_TYPE = "VerifiableCredential";
export const VERIFIABLE_CREDENTIAL_EIP712_TYPE: TypedData[] = [
{ name: "_context", type: "string[]" },
{ name: "id", type: "string" },
{ name: "_type", type: "string[]" },
{ name: "issuer", type: "Issuer" },
{ name: "credentialSubject", type: "CredentialSubject" },
];
export const CREDENTIAL_SCHEMA_EIP712_TYPE: TypedData[] = [
{ name: "_context", type: "string[]" },
{ name: "id", type: "string" },
{ name: "_type", type: "string[]" },
{ name: "issuer", type: "Issuer" },
{
name: "credentialSubject",
type: "CredentialSubject",
},
];
export const PROOF_EIP712_TYPE: TypedData[] = [
{ name: "verificationMethod", type: "string" },
{ name: "ethereumAddress", type: "address" },
{ name: "created", type: "string" },
{ name: "proofPurpose", type: "string" },
{ name: "_type", type: "string" },
];
export class EIP712Service {
private eip712Config: EIP712Config;
public name = "EIP712";
public constructor(eip712Config: EIP712Config) {
this.eip712Config = eip712Config;
}
public async signVCWithEthers(
signer: TypedDataSigner & Signer,
token: CredentialPayload,
credentialSubjectTypes: any
): Promise<string> {
const credentialTypedData = this.getEIP712CredentialTypedData(
token,
credentialSubjectTypes
);
return await signer._signTypedData(
credentialTypedData.domain,
credentialTypedData.types,
credentialTypedData.message
);
}
public async signVC(
keys: DIDWithKeys,
token: CredentialPayload,
credentialSubjectTypes: any
): Promise<string> {
const credentialTypedData = this.getEIP712CredentialTypedData(
token,
credentialSubjectTypes
);
const signer = new Wallet(keys.keyPair.privateKey);
return await signer._signTypedData(
credentialTypedData.domain,
credentialTypedData.types,
credentialTypedData.message
);
}
private getDomainTypedData(): EIP712DomainTypedData {
return {
name: this.eip712Config.name,
version: this.eip712Config.version,
chainId: this.eip712Config.chainId,
verifyingContract: this.eip712Config.verifyingContract,
};
}
public static onyxCredentialToEIP712Credential(
credential: CredentialPayload
): EIP712CredentialPayload {
const _context = Array.isArray(credential["@context"])
? credential["@context"]
: [credential["@context"]];
const _type = Array.isArray(credential.type)
? credential.type
: [credential.type];
if (!credential.id) {
throw new Error("Id is required to create EIP712 credential");
}
return {
_context,
id: credential.id,
_type,
issuer: credential.issuer,
credentialSubject: credential.credentialSubject,
};
}
private getEIP712CredentialTypedData(
credential: CredentialPayload,
credentialSubjectTypes: any
): EIP712CredentialTypedData {
const message = EIP712Service.onyxCredentialToEIP712Credential(credential);
return {
domain: this.getDomainTypedData(),
primaryType: VERIFIABLE_CREDENTIAL_PRIMARY_TYPE,
message,
types: {
[VERIFIABLE_CREDENTIAL_PRIMARY_TYPE]: VERIFIABLE_CREDENTIAL_EIP712_TYPE,
...credentialSubjectTypes,
},
};
}
}