-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentity.js
46 lines (35 loc) · 813 Bytes
/
identity.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
const RSA = require('node-rsa');
const sha3256 = require('js-sha3').sha3_256;
const KEY_SIZE = 512;
class Identity {
constructor (key) {
if (key && typeof key === 'string') {
this.key = new RSA(key);
} else {
this.generate(key);
}
this.address = sha3256(this.publicKey).slice(-20);
}
get privateKey () {
return this.key.exportKey('private');
}
get publicKey () {
return this.key.exportKey('public');
}
generate (key) {
this.key = new RSA(key || { b: KEY_SIZE });
}
encrypt (...args) {
return this.key.encrypt(...args);
}
decrypt (...args) {
return this.key.decrypt(...args);
}
sign (...args) {
return this.key.sign(...args);
}
verify (...args) {
return this.key.verify(...args);
}
}
module.exports = { Identity };