forked from 0xPolygonID/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
206 lines (187 loc) · 7.03 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="./dist/browser/umd/index.js"></script>
<script type="importmap">
{
"imports": {
"@iden3/js-crypto": "./node_modules/@iden3/js-crypto/dist/browser/esm/index.js",
"@iden3/js-iden3-core": "./node_modules/@iden3/js-iden3-core/dist/browser/esm/index.js",
"@iden3/js-merkletree": "./node_modules/@iden3/js-merkletree/dist/browser/esm/index.js",
"@iden3/js-jwz": "./node_modules/@iden3/js-jwz/dist/browser/esm/index.js",
"@iden3/js-jsonld-merklization": "./node_modules/@iden3/js-jsonld-merklization/dist/browser/esm/index.js",
"snarkjs": "./node_modules/snarkjs/build/browser.esm.js",
"ffjavascript": "./node_modules/ffjavascript/build/browser.esm.js"
}
}
</script>
<title>Test</title>
</head>
<body>
Test browser
</body>
<script type="module">
const umd = window.PolygonIdSdk;
const esm = await import('./dist/browser/esm/index.js');
test(umd);
test(esm);
function test(module) {
const {
LocalStoragePrivateKeyStore,
IdentityStorage,
MerkleTreeLocalStorage,
CredentialStorage,
W3CCredential,
BrowserDataSource,
BjjProvider,
KmsKeyType,
IdentityWallet,
CredentialWallet,
KMS,
core,
CredentialStatusType,
CredentialStatusResolverRegistry,
IssuerResolver,
RHSResolver,
defaultEthConnectionConfig,
OnChainResolver,
MerkleTreeIndexedDBStorage,
VerifiableConstants,
IndexedDBDataSource
} = module;
const run = async () => {
const keyStore = new LocalStoragePrivateKeyStore();
const bjjProvider = new BjjProvider(KmsKeyType.BabyJubJub, keyStore);
const kms = new KMS();
kms.registerKeyProvider(KmsKeyType.BabyJubJub, bjjProvider);
const dataStorage = {
credential: new CredentialStorage(new BrowserDataSource(CredentialStorage.storageKey)),
identity: new IdentityStorage(
new BrowserDataSource(IdentityStorage.identitiesStorageKey),
new BrowserDataSource(IdentityStorage.profilesStorageKey)
),
mt: new MerkleTreeLocalStorage(40),
states: {
getLatestStateById: async () => {
throw new Error(VerifiableConstants.ERRORS.IDENTITY_DOES_NOT_EXIST);
},
publishState: async () => {
return '0xc837f95c984892dbcc3ac41812ecb145fedc26d7003202c50e1b87e226a9b33c';
},
publishStateGeneric: async () => {
return '0xc837f95c984892dbcc3ac41812ecb145fedc26d7003202c50e1b87e226a9b33c';
},
getGISTProof: () => {
return Promise.resolve({
root: 0n,
existence: false,
siblings: [],
index: 0n,
value: 0n,
auxExistence: false,
auxIndex: 0n,
auxValue: 0n
});
},
getGISTRootInfo: () => {
return Promise.resolve({
root: 0n,
replacedByRoot: 0n,
createdAtTimestamp: 0n,
replacedAtTimestamp: 0n,
createdAtBlock: 0n,
replacedAtBlock: 0n
});
}
}
};
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])
);
const credWallet = new CredentialWallet(dataStorage, resolvers);
const wallet = new IdentityWallet(kms, dataStorage, credWallet);
console.log(wallet);
const { did, credential } = await wallet.createIdentity({
method: core.DidMethod.Iden3,
blockchain: core.Blockchain.Polygon,
networkId: core.NetworkId.Amoy,
revocationOpts: {
type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
id: 'https://rhs-staging.polygonid.me'
}
});
const { did: issuerDID, credential: issuerAuthCredential } = await wallet.createIdentity({
method: core.DidMethod.Iden3,
blockchain: core.Blockchain.Polygon,
networkId: core.NetworkId.Amoy,
revocationOpts: {
type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
id: 'https://rhs-staging.polygonid.me'
}
});
await credWallet.save(issuerAuthCredential);
const claimReq = {
credentialSchema:
'https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json/kyc-nonmerklized.json',
type: 'KYCAgeCredential',
credentialSubject: {
id: did.string(),
birthday: 19960424,
documentType: 99
},
expiration: 2793526400,
revocationOpts: {
type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
nonce: 1000,
id: 'https://rhs-staging.polygonid.me'
}
};
const issuerCred = await wallet.issueCredential(issuerDID, claimReq);
await credWallet.save(issuerCred);
const claimReqMerklized = {
credentialSchema:
'https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json/KYCAgeCredential-v4.json',
type: 'KYCAgeCredential',
credentialSubject: {
id: did.string(),
birthday: 19960423,
documentType: 77
},
expiration: 2793526400,
revocationOpts: {
type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
nonce: 1001,
id: 'https://rhs-staging.polygonid.me'
}
};
const issuerCredMerklized = await wallet.issueCredential(issuerDID, claimReqMerklized);
await credWallet.save(issuerCred);
const res = await wallet.addCredentialsToMerkleTree(
[issuerCred, issuerCredMerklized],
issuerDID
);
// publish to rhs
await wallet.publishStateToRHS(issuerDID, 'https://rhs-staging.polygonid.me');
// you must store stat info (e.g. state and it's roots)
const credsWithIden3MTPProof = await wallet.generateIden3SparseMerkleTreeProof(
issuerDID,
res.credentials,
'txId'
);
await credWallet.saveAll(credsWithIden3MTPProof);
console.log(did.string());
};
run().catch(console.error);
}
</script>
</html>