-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
86 lines (82 loc) · 2.87 KB
/
index.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
import { decodeToken } from 'jsontokens';
import axios from 'axios';
const { createECDH } = require('crypto');
const { decryptECIES } = require('blockstack/lib/encryption');
export function handleOAuthFlow(object) {
const ecdh = createECDH('secp256k1');
const separator = '?';
ecdh.generateKeys('hex');
const pubKey = ecdh.getPublicKey('hex');
const encodedPubKey = encodeURI(pubKey);
const privKey = ecdh.getPrivateKey('hex');
localStorage.setItem('graphite-transit-key', JSON.stringify(privKey));
window.location.replace(object.targetURI + separator + object.appName + separator + object.redirectURI + separator + 'token=' + encodedPubKey + '=?');
};
export function decryptPayload(token) {
const privateKey = JSON.parse(localStorage.getItem('graphite-transit-key'));
const tokenData = decodeToken(token);
const payload = decryptECIES(privateKey, tokenData.payload);
return payload;
};
export function getCollection(object) {
if(object.docType === "documents") {
return axios.get(object.storagePath + '/documentscollection.json')
.then((res) => {
return JSON.parse(decryptECIES(object.privateKey, res.data)).value;
})
.catch(error => {
console.log(error);
})
} else if(object.docType === "sheets") {
return axios.get(object.storagePath + '/sheetscollection.json')
.then((res) => {
return JSON.parse(decryptECIES(object.privateKey, res.data)).sheets;
})
.catch(error => {
console.log(error);
})
} else if(object.docType === "vault") {
return axios.get(object.storagePath + '/uploads.json')
.then((res) => {
return JSON.parse(decryptECIES(object.privateKey, res.data));
})
.catch(error => {
console.log(error);
})
} else if(object.docType === "contacts") {
return axios.get(object.storagePath + '/contact.json')
.then((res) => {
return JSON.parse(decryptECIES(object.privateKey, res.data)).contacts;
})
.catch(error => {
console.log(error);
})
}
}
export function getFile(object) {
if(object.docType === "documents") {
return axios.get(object.storagePath + '//documents/' + object.id + '.json')
.then((res) => {
return JSON.parse(decryptECIES(object.privateKey, res.data));
})
.catch(error => {
console.log(error);
})
} else if(object.docType === "sheets") {
return axios.get(object.storagePath + '//sheets/' + object.id + '.json')
.then((res) => {
return JSON.parse(decryptECIES(object.privateKey, res.data));
})
.catch(error => {
console.log(error);
})
} else if(object.docType === 'vault') {
return axios.get(object.storagePath + '/' + object.id + '.json')
.then((res) => {
return JSON.parse(decryptECIES(object.privateKey, res.data));
})
.catch(error => {
console.log(error);
})
}
}