-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ts
170 lines (97 loc) · 4.23 KB
/
create.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
import { Account } from 'aleph-sdk-ts/dist/accounts/account';
import { Publish as publishAggregate } from 'aleph-sdk-ts/dist/messages/aggregate';
import { ItemType } from 'aleph-sdk-ts/dist/messages/types';
import { aleph_fetch } from './fetch';
const enc = new TextEncoder();
const dec = new TextDecoder("utf-8");
//recuperer pbkdf de la data
export async function cipherData(data : Object, pbkdf : CryptoKey) {
try {
const encoded = enc.encode(data.toString());
// iv will be needed for decryption
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const res = await window.crypto.subtle.encrypt(
{ name: "AES-GCM", iv: iv },
pbkdf,
encoded,
);
return { res, iv }; // associer res et iv dans le backend.
} catch(e) { // VOIR COMMENT GERER ERREUR POTENTIELLE
console.log("error while ciphering data");
throw new Error('couldnt cipher one of the parameters');
/*
const res = undefined;
return { res, iv };*/
}
}
// le content est de la forme { 'a' : ckeyiv, 'b' : mon_titre_chiffre, 'c' : iv_titre, 'd' : mon_login_chiffre, 'e' : iv_login, 'f' : mon_password_chiffre, 'g' : iv_password } et key correspond au website(url ?)
export async function aleph_create(account : Account, key : string, content : Object) {
try{
const res = await publishAggregate({
account: account,
key: key,
content: content,
// DEBUT QUESTIONS
channel: "TEST", // A HARDCODER CEST QUOI CHANNEL ?
storageEngine: ItemType.inline, // A HARDCODER CA DOIT ETRE ALEPH_STORAGE OU INLINE ?
//inlineRequested: true, // car deprecated
// FIN QUESTIONS
APIServer: "https://api2.aleph.im"
});
return res;
} catch(e) {
console.log("erreur pour fetch");
//res = e;
}
//console.log("requête create ou update : ", res);
}
// Appeler importAccount de accountgeneration.ts pour retrieve account
export async function create(account : Account, content : { a : number, b : string, c : string, d : string, e : string}, pbkdf : CryptoKey) {
const prevKey = await aleph_fetch(account.address, ['cpt']);
const str : string = JSON.stringify(prevKey);
const json = JSON.parse(str);
const newKey : number = json.cpt.cpt + 1;
const { res, iv } = await cipherData(content, pbkdf);
aleph_create(account, `${newKey}`, { a : res, b : iv});
aleph_create(account, 'cpt', {newKey});
}
export async function update_delete(account : Account, content : { a : number, b : string, c : string, d : string, e : string}, pbkdf : CryptoKey) {
const { res, iv } = await cipherData(content, pbkdf);
aleph_create(account, `${content.a}`, { a : res, b : iv});
}
//recuperer pbkdf de la data
/*
export async function cipherString(myString : string, pbkdf : CryptoKey) {
const encoded = enc.encode(myString);
// iv will be needed for decryption
const iv = window.crypto.getRandomValues(new Uint8Array(12));
try {
const res = await window.crypto.subtle.encrypt(
{ name: "AES-GCM", iv: iv },
pbkdf,
encoded,
);
return { res, iv }; // associer res et iv dans le backend.
} catch(e) { // VOIR COMMENT GERER ERREUR POTENTIELLE
console.log("error while ciphering data");
throw new Error('couldnt cipher one of the parameters');
const res = undefined;
return { res, iv };
}
}
*/
//key correspond au website (url ?)
/*
export async function cipherMessage(content : { a : string, b : string, c : string, d : string}, pbkdf : CryptoKey) {
if(typeof pbkdf === undefined){
throw new Error('couldnt generate a CryptoKey for ciphering data');
}
let cContent = [];
const cservice = await cipherString(content.a, pbkdf);
const ctitle = await cipherString(content.b, pbkdf);
const clogin = await cipherString(content.c, pbkdf);
const cpassword = await cipherString(content.d, pbkdf);
cContent.push({ a : cservice, b : ctitle, c : clogin, d : cpassword });
return { cContent };
}
*/