-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.js
66 lines (54 loc) · 1.34 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const RSA = require('node-rsa');
const sha3256 = require('js-sha3').sha3_256;
const DEFAULT_KEY_SIZE = 512;
class Identity {
static generate ({ b = DEFAULT_KEY_SIZE } = {}) {
let rsa = new RSA({ b });
return new Identity(rsa.exportKey('private'));
}
constructor (key) {
Object.defineProperty(this, 'rsa', {
enumerable: false,
writable: false,
configurable: false,
value: new RSA(key),
});
if (this.rsa.isPrivate()) {
Object.defineProperty(this, 'privKey', {
enumerable: false,
writable: false,
configurable: false,
value: this.rsa.exportKey('private'),
});
}
let pubKey = this.rsa.exportKey('public');
Object.defineProperty(this, 'address', {
enumerable: true,
writable: false,
configurable: false,
value: sha3256(pubKey).slice(-20),
});
Object.defineProperty(this, 'pubKey', {
enumerable: true,
writable: false,
configurable: false,
value: pubKey,
});
}
isPrivate () {
return this.rsa.isPrivate();
}
sign (...args) {
return this.rsa.sign(...args);
}
verify (...args) {
return this.rsa.verify(...args);
}
encrypt (...args) {
return this.rsa.encrypt(...args);
}
decrypt (...args) {
return this.rsa.decrypt(...args);
}
}
module.exports = { Identity };