-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.js
42 lines (38 loc) · 977 Bytes
/
Message.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
const eccrypto = require('eccrypto')
class Message {
/**
* creates a Message object
* @param {string} content - a message to be sent
*/
constructor (content) {
this.content = content
this.encryptionKey = ''
}
/**
* encrypts the message content
* @param {Buffer} publicKey - the public key of who can decrypt the message
* @return {Promise}
*/
encrypt (publicKey) {
if (!this.encryptionKey) {
this.encryptionKey = publicKey
}
return eccrypto.encrypt(publicKey, Buffer.from(this.content))
.then((encrypted) => {
this.content = encrypted
return this
})
}
/**
* encrypts the message content
* @param {Buffer} privateKey - the key that decrypts the content of the msg
*/
decrypt (privateKey) {
return eccrypto.decrypt(privateKey, this.content)
.then((plaintext) => {
this.content = plaintext
return this
})
}
}
module.exports.Message = Message