-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwalletSetup.ts
242 lines (214 loc) · 6.84 KB
/
walletSetup.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
236
237
238
239
240
241
242
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { proving } from '@iden3/js-jwz';
import {
BjjProvider,
CredentialStorage,
CredentialWallet,
defaultEthConnectionConfig,
EthStateStorage,
ICredentialWallet,
IDataStorage,
Identity,
IdentityStorage,
IdentityWallet,
IIdentityWallet,
InMemoryDataSource,
InMemoryMerkleTreeStorage,
InMemoryPrivateKeyStore,
KMS,
KmsKeyType,
Profile,
W3CCredential,
EthConnectionConfig,
CircuitData,
IStateStorage,
ProofService,
ICircuitStorage,
CredentialStatusType,
CredentialStatusResolverRegistry,
IssuerResolver,
RHSResolver,
OnChainResolver,
AuthDataPrepareFunc,
StateVerificationFunc,
DataPrepareHandlerFunc,
VerificationHandlerFunc,
IPackageManager,
VerificationParams,
ProvingParams,
ZKPPacker,
PlainPacker,
PackageManager,
AgentResolver,
FSCircuitStorage,
AbstractPrivateKeyStore,
CredentialStatusPublisherRegistry,
Iden3SmtRhsCredentialStatusPublisher
} from '@0xpolygonid/js-sdk';
import path from 'path';
import dotenv from 'dotenv';
dotenv.config();
import { MongoDataSourceFactory, MerkleTreeMongodDBStorage } from '@0xpolygonid/mongo-storage';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { MongoClient, Db } from 'mongodb';
export type NetworkConfig = {
contractAddress: string;
rpcUrl: string;
chainId: number;
};
const circuitsFolder = process.env.CIRCUITS_PATH as string;
const mongoDbConnection = process.env.MONGO_DB_CONNECTION as string;
export function initInMemoryDataStorage({
contractAddress,
rpcUrl,
chainId
}: NetworkConfig): IDataStorage {
const conf: EthConnectionConfig = {
...defaultEthConnectionConfig,
contractAddress,
url: rpcUrl,
chainId
};
// change here priority fees in case transaction is stuck or processing too long
// conf.maxPriorityFeePerGas = '250000000000' - 250 gwei
// conf.maxFeePerGas = '250000000000' - 250 gwei
const dataStorage = {
credential: new CredentialStorage(new InMemoryDataSource<W3CCredential>()),
identity: new IdentityStorage(
new InMemoryDataSource<Identity>(),
new InMemoryDataSource<Profile>()
),
mt: new InMemoryMerkleTreeStorage(40),
states: new EthStateStorage(conf)
};
return dataStorage;
}
export async function initMongoDataStorage({
rpcUrl,
contractAddress,
chainId
}: NetworkConfig): Promise<IDataStorage> {
let url = mongoDbConnection;
if (!url) {
const mongodb = await MongoMemoryServer.create();
url = mongodb.getUri();
}
const client = new MongoClient(url);
await client.connect();
const db: Db = client.db('mongodb-sdk-example');
const conf: EthConnectionConfig = {
...defaultEthConnectionConfig,
chainId,
contractAddress,
url: rpcUrl
};
const dataStorage = {
credential: new CredentialStorage(
await MongoDataSourceFactory<W3CCredential>(db, 'credentials')
),
identity: new IdentityStorage(
await MongoDataSourceFactory<Identity>(db, 'identity'),
await MongoDataSourceFactory<Profile>(db, 'profile')
),
mt: await MerkleTreeMongodDBStorage.setup(db, 40),
states: new EthStateStorage(conf)
};
return dataStorage;
}
export async function initIdentityWallet(
dataStorage: IDataStorage,
credentialWallet: ICredentialWallet,
keyStore: AbstractPrivateKeyStore
): Promise<IIdentityWallet> {
const bjjProvider = new BjjProvider(KmsKeyType.BabyJubJub, keyStore);
const kms = new KMS();
kms.registerKeyProvider(KmsKeyType.BabyJubJub, bjjProvider);
const credentialStatusPublisherRegistry = new CredentialStatusPublisherRegistry();
credentialStatusPublisherRegistry.register(
CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
new Iden3SmtRhsCredentialStatusPublisher()
);
return new IdentityWallet(kms, dataStorage, credentialWallet, {
credentialStatusPublisherRegistry
});
}
export async function initInMemoryDataStorageAndWallets(config: NetworkConfig) {
const dataStorage = initInMemoryDataStorage(config);
const credentialWallet = await initCredentialWallet(dataStorage);
const memoryKeyStore = new InMemoryPrivateKeyStore();
const identityWallet = await initIdentityWallet(dataStorage, credentialWallet, memoryKeyStore);
return {
dataStorage,
credentialWallet,
identityWallet
};
}
export async function initMongoDataStorageAndWallets(config: NetworkConfig) {
const dataStorage = await initMongoDataStorage(config);
const credentialWallet = await initCredentialWallet(dataStorage);
const memoryKeyStore = new InMemoryPrivateKeyStore();
const identityWallet = await initIdentityWallet(dataStorage, credentialWallet, memoryKeyStore);
return {
dataStorage,
credentialWallet,
identityWallet
};
}
export async function initCredentialWallet(dataStorage: IDataStorage): Promise<CredentialWallet> {
const resolvers = new CredentialStatusResolverRegistry();
resolvers.register(CredentialStatusType.SparseMerkleTreeProof, new IssuerResolver());
resolvers.register(
CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
new RHSResolver(dataStorage.states)
);
resolvers.register(
CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023,
new OnChainResolver([defaultEthConnectionConfig])
);
resolvers.register(CredentialStatusType.Iden3commRevocationStatusV1, new AgentResolver());
return new CredentialWallet(dataStorage, resolvers);
}
export async function initCircuitStorage(): Promise<ICircuitStorage> {
return new FSCircuitStorage({
dirname: path.join(__dirname, circuitsFolder)
});
}
export async function initProofService(
identityWallet: IIdentityWallet,
credentialWallet: ICredentialWallet,
stateStorage: IStateStorage,
circuitStorage: ICircuitStorage
): Promise<ProofService> {
return new ProofService(identityWallet, credentialWallet, circuitStorage, stateStorage, {
ipfsGatewayURL: 'https://ipfs.io'
});
}
export async function initPackageManager(
circuitData: CircuitData,
prepareFn: AuthDataPrepareFunc,
stateVerificationFn: StateVerificationFunc
): Promise<IPackageManager> {
const authInputsHandler = new DataPrepareHandlerFunc(prepareFn);
const verificationFn = new VerificationHandlerFunc(stateVerificationFn);
const mapKey = proving.provingMethodGroth16AuthV2Instance.methodAlg.toString();
const verificationParamMap: Map<string, VerificationParams> = new Map([
[
mapKey,
{
key: circuitData.verificationKey!,
verificationFn
}
]
]);
const provingParamMap: Map<string, ProvingParams> = new Map();
provingParamMap.set(mapKey, {
dataPreparer: authInputsHandler,
provingKey: circuitData.provingKey!,
wasm: circuitData.wasm!
});
const mgr: IPackageManager = new PackageManager();
const packer = new ZKPPacker(provingParamMap, verificationParamMap);
const plainPacker = new PlainPacker();
mgr.registerPackers([packer, plainPacker]);
return mgr;
}