-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
245 lines (208 loc) · 6.1 KB
/
main.js
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
243
/*
import * as IPFS from 'ipfs-core'
const ipfs = await IPFS.create()
const { cid } = await ipfs.add('Hello world')
console.info(cid)
*/
import { createDID, registerDID, registerDIDSigner } from "fvm-did-registrar";
import {Resolver} from "did-resolver";
import * as didFVM from 'fvm-did-resolver';
/*import {promises} from "fs";
import {readFile} from promises;*/
import crypto from "crypto";
//import * as IPFS from 'ipfs'
//const ipfs = await IPFS.create()
import { create } from "ipfs-http-client";
const projectId = "2IQcCvMGSRJ0qKVdEBAEA38amJa";
const projectSecret = "842fad0b2300ddbace70d3b75b8492e1";
const auth =
"Basic " + Buffer.from(projectId + ":" + projectSecret).toString("base64");
const ipfs = create({
host: "ipfs.infura.io",
port: 5001,
protocol: "https",
headers: {
authorization: auth,
},
});
import Web3 from "web3";
const web3 = new Web3();
async function getLocationHash(file_locations) {
try {
return Promise.all(
file_locations.map((file) => {
return promises.readFile(file);
})
)
.then((fileBuffers) => {
return fileBuffers.map((fileBuffer) => {
const b64 = fileBuffer.toString("base64");
return hash(b64);
});
})
.catch((error) => {
console.error(error.message);
process.exit(1);
});
} catch (error) {
console.log(`Error occurred while creating DID ${error}`);
throw error;
}
}
async function getHash(base64List) {
try {;
let hashList = [];
base64List.forEach(element => {
hashList.push(hash(element))
});
return hashList;
} catch (error) {
console.log(`Error occurred while hashing data ${error}`);
throw error;
}
}
async function genCID(hashList, privateKey) {
try {
const concatenatedHashes = hashList.join();
hashList.push(
web3.eth.accounts.sign(concatenatedHashes, privateKey).signature
);
const { path } = await ipfs.add(JSON.stringify(hashList));
//setTimeout(() => console.log("Sleeeping"), 5000)
//ipfs.stop()
return path;
} catch (error) {
console.log(`Error occurred while creating DID ${error}`);
throw error;
}
}
async function genCIDFunc(hashList, sigFunc) {
try {
const concatenatedHashes = hashList.join();
hashList.push(sigFunc(concatenatedHashes));
const { path } = await ipfs.add(JSON.stringify(hashList));
//setTimeout(() => console.log("Sleeeping"), 5000)
//ipfs.stop()
return path;
} catch (error) {
console.log(`Error occurred while creating DID ${error}`);
throw error;
}
}
function hash(base64Data) {
try {
return crypto.createHash("sha256").update(base64Data).digest("hex");
} catch (error) {
console.log(`Error occurred while hashing ${error}`);
throw error;
}
}
function getAddress(hashList, signature) {
try {
const concatenatedHashes = hashList.join("");
return web3.eth.accounts.recover(concatenatedHashes, signature);
} catch (error) {
console.log(`Error occurred while creating DID ${error}`);
throw error;
}
}
async function create_DID(privateKey) {
try {
const network = "testnet";
const {
data: { address, publicKeyBase58, did },
} = await createDID(network, privateKey);
return { address, publicKeyBase58, did };
} catch (error) {
console.log(`Error occurred while creating DID ${error}`);
throw error;
}
}
function create_DID_Address(address) {
try {
if (!address.match(/^0x[0-9a-fA-F]{40}$/))
throw "Address not correct";
return {did : `did:fvm:testnet:${address}`};
} catch (error) {
console.log(`Error occurred while creating DID ${error}`);
throw error;
}
}
async function register_DID(did, cid, privateKey) {
try {
const tx = await registerDID(
did,
privateKey,
"https://api.hyperspace.node.glif.io/rpc/v1",
"0x74Cff4ee330854182D6FF5A2Bbe3449037e8b0Df",
cid
);
return tx.data.txnHash.hash;
} catch (error) {
console.log(`Error occurred while registering DID ${error}`);
throw error;
}
}
async function register_DIDSigner(did, cid, signer) {
try {
const tx = await registerDIDSigner(
did,
signer,
"https://api.hyperspace.node.glif.io/rpc/v1",
"0x74Cff4ee330854182D6FF5A2Bbe3449037e8b0Df",
cid
);
return tx.data.txnHash.hash;
} catch (error) {
console.log(`Error occurred while registering DID ${error}`);
throw error;
}
}
async function resolve_DID(did) {
try {
const myResolver = didFVM.getResolver()
const resolver = new Resolver({...myResolver})
return resolver.resolve(did)
} catch (error) {
console.log(`Error occurred while resolving DID ${error}`);
throw error;
}
}
async function verify(b64_file, did) {
try {
return resolve_DID(did).then( async ({didDocument, didDocumentMetadata , didResolutionMetadata}) => {
const serviceOBj = JSON.parse(didDocument[0]).service;
const encodedData = await ipfs.cat(serviceOBj[0].serviceEndpoint).next();
const data = new TextDecoder().decode(encodedData.value);
const hashList = JSON.parse(data);
//TODO: verify signature
const signature = hashList.pop();
const concatenatedHashes = hashList.join();
const addr = web3.eth.accounts.recover(concatenatedHashes, signature);
if (addr !== did.split(":")[3])
return {"verification": "invalid", "reason": "Credential Store Signature not done by DID Privatekey"}
const _hash = hash(b64_file);
if (hashList.indexOf(_hash) == -1)
return {"verification": "invalid", "reason": "File Hash not present on IPFS CID from DID document"}
return {"verification" : "valid"};
})
} catch (error) {
console.log(`Error occurred while verifying file ${error}`);
throw error;
}
}
async function getHashList(did) {
try {
return resolve_DID(did).then( async ({didDocument, didDocumentMetadata , didResolutionMetadata}) => {
const serviceOBj = JSON.parse(didDocument[0]).service;
const encodedData = await ipfs.cat(serviceOBj[0].serviceEndpoint).next();
const data = new TextDecoder().decode(encodedData.value);
const hashList = JSON.parse(data);
return hashList;
})
} catch (error) {
console.log(`Error occurred while verifying file ${error}`);
throw error;
}
}
export { getLocationHash, getHash, getHashList, genCID, genCIDFunc, create_DID, create_DID_Address, hash, register_DID, register_DIDSigner, resolve_DID, verify };