-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
195 lines (168 loc) · 5.93 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
195
import { isAddress } from '../../../sdk/utils';
import { determineRequestMethod, determineRequestSigner, SupportedProvider } from '../../../sdk/types';
import { EIP712, EIP712Domain, EIP712Message, EIP712Types } from '../EIP712';
import { GenerateSealingKey, SealingKey } from '../../../sdk/sealing';
const PERMIT_PREFIX = "Fhenix_saved_permit_";
export type Permission = {
signature: string;
publicKey: string;
}
/**
* Represents a permit with cryptographic properties.
*/
export type Permit = {
/**
* The Ethereum contract address associated with the permit.
*/
contractAddress: string;
/**
* The sealing key information required to seal or unseal data related to the permit.
*/
sealingKey: SealingKey;
/**
* A cryptographic signature proving the authenticity and integrity of the permit.
*/
signature: string;
/**
* The public key corresponding to the private key used to generate the signature.
*/
publicKey: string;
};
type SerializedPermit = {
contractAddress: string;
sealingKey: {
privateKey: string;
publicKey: string;
}
signature: string;
}
const parsePermit = (savedPermit: string): Permit => {
const o = JSON.parse(savedPermit) as SerializedPermit;
if (o) {
return {
contractAddress: o.contractAddress,
sealingKey: new SealingKey(o.sealingKey.privateKey, o.sealingKey.publicKey),
signature: o.signature,
publicKey: `0x${o.sealingKey.publicKey}`
};
}
throw new Error(`Cannot parse permit`);
}
export const getPermit = async (contract: string, provider: SupportedProvider): Promise<Permit> => {
isAddress(contract);
if (!provider) {
throw new Error(`Missing provider`);
}
let savedPermit = null;
if (typeof window !== 'undefined' && window.localStorage) {
savedPermit = window.localStorage.getItem(`${PERMIT_PREFIX}${contract}`);
}
if (savedPermit) {
try {
return parsePermit(savedPermit);
} catch (err) {
console.warn(err);
}
}
return generatePermit(contract, provider);
}
export const getAllPermits = (): Map<string, Permit> => {
const permits: Map<string, Permit> = new Map();
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
if (key && key.includes(PERMIT_PREFIX)) {
const contract = key.replace(PERMIT_PREFIX,"");
try {
const permit = parsePermit(window.localStorage.getItem(key)!);
permits.set(contract, permit);
} catch(err) {
console.warn(err);
}
}
}
return permits;
}
interface SignerPublicSignedTypedData {
signTypedData(domain: object, types: object, value: object): Promise<string>;
}
interface SignerPrivateSignedTypedData {
_signTypedData(domain: object, types: object, value: object): Promise<string>;
}
type PermitSigner = SignerPrivateSignedTypedData | SignerPublicSignedTypedData;
const sign = async (signer: PermitSigner, domain: EIP712Domain, types: EIP712Types, value: EIP712Message): Promise<string> => {
if ('_signTypedData' in signer && typeof signer._signTypedData == 'function') {
return await signer._signTypedData(domain, types, value);
} else if ('signTypedData' in signer && typeof signer.signTypedData == 'function') {
return await signer.signTypedData(domain, types, value);
}
throw new Error('Unsupported signer');
}
export const generatePermit = async (contract: string, provider: SupportedProvider): Promise<Permit> => {
if (!provider) {
throw new Error('Provider is undefined');
}
const requestMethod = determineRequestMethod(provider);
const getSigner = determineRequestSigner(provider);
const signer = await getSigner(provider);
const chainId = await requestMethod(provider, "eth_chainId", [ ]);
const keypair = await GenerateSealingKey();
const msgParams: EIP712 = {
types: {
// This refers to the domain the contract is hosted on.
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
// Refer to primaryType.
Permissioned: [{ name: 'publicKey', type: 'bytes32' }],
},
// This defines the message you're proposing the user to sign, is dapp-specific, and contains
// anything you want. There are no required fields. Be as explicit as possible when building out
// the message schema.
// This refers to the keys of the following types object.
primaryType: 'Permissioned',
domain: {
// Give a user-friendly name to the specific contract you're signing for.
name: 'Fhenix Permission', // params.name
// This identifies the latest version.
version: '1.0', //params.version ||
// This defines the network, in this case, Mainnet.
chainId: chainId,
// // Add a verifying contract to make sure you're establishing contracts with the proper entity.
verifyingContract: contract //params.verifyingContract,
},
message: {
publicKey: `0x${keypair.publicKey}`,
},
};
const msgSig = await sign(signer, msgParams.domain,
{ Permissioned: msgParams.types.Permissioned },
msgParams.message
);
const permit : Permit = {
contractAddress: contract,
sealingKey: keypair,
signature: msgSig,
publicKey: `0x${keypair.publicKey}`
//permit: msgParams,
//msgSig
};
if (typeof window !== 'undefined' && window.localStorage) {
// Sealing key is a class, and will include methods in the JSON
const serialized: SerializedPermit = {
contractAddress: permit.contractAddress,
sealingKey: {
publicKey: permit.sealingKey.publicKey,
privateKey: permit.sealingKey.privateKey
},
signature: permit.signature
};
window.localStorage.setItem(`${PERMIT_PREFIX}${contract}`, JSON.stringify(serialized));
}
return permit;
};
export const removePermit = (contract: string): void => {
window.localStorage.removeItem(`${PERMIT_PREFIX}${contract}`);
}