-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
175 lines (151 loc) · 6.39 KB
/
common.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
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
171
172
173
174
175
const EventEmitter = require('events');
const colors = require('colors');
const level = require('level');
const nacl = require('tweetnacl');
const naclUtil = require('tweetnacl-util');
nacl.util = naclUtil;
let web3;
let dappSym;
let iam;
class Gossip extends EventEmitter {
constructor() {
super();
}
// Initialize the library
async init(_web3, _dappSym, _sender, _secret) {
console.clear();
console.log("Gossip - Whisper".yellow);
web3 = _web3;
dappSym = _dappSym;
const db = level(_sender+'-localCache');
//dappSym = await web3.shh.generateSymKeyFromPassword('swarmcity1');
let Id = await web3.shh.addSymKey('0x'+dappSym);
let hasit = await web3.shh.hasSymKey(dappSym);
console.log(colors.blue('Initializing: \n', 'DappSym: ', dappSym, '\n Id: ', Id, '\n Exists on node: ', hasit, '\n Secret: ', _secret));
//console.log(nacl.util.encodeBase64(nacl.randomBytes(32)).red);
return dappSym;
};
async subscribe(_dappSymKeyId, _secret, _from, _to) {
// Subscribe function
web3.shh.newMessageFilter({
symKeyID: _dappSymKeyId,
topics: ['0x00000001'],
ttl: 20,
minPow: 0.8,
}).then(_Id => {
setInterval(() => web3.shh.getFilterMessages(_Id).then(msgs => {
if (!msgs.length) return
msgs.forEach(m => {
const decodePayload = web3.utils.hexToUtf8(m.payload);
this.unHash(decodePayload).then((res) => {
const hash = res[0];
//console.log('\n\Received:'.gray);
//console.log(hash.blue);
const hashOfInterest = web3.utils.sha3(web3.utils.sha3(_from.keyPair) + _to.keyPair);
//console.log(hash, hashOfInterest);
if (hash === hashOfInterest) {
//console.log('found hash of interest');
//console.log(hashOfInterest);
this.decrypt(_secret, res[1], res[2]).then((res) => {
console.log(colors.blue('\nI, ', _from.publicKey.substring(0,8),"...",' am interested in this message.'));
console.log(res.blue);
this.emit('message', res, m.sig);
});
} else {
console.log(colors.gray('\nI, ', _from.publicKey.substring(0,8),"...",' am not interested in this message ' + Date.now()));
//console.log(hash);
//console.log(hashOfInterest);
// store for future rebroadcasting
// let key = _from.publicKey+'-msg-' + m.hash;
// let val = m;
// db.put(key, JSON.stringify(val)).then(() => {
// console.log('Storing %j at %s', val, key);
// }).catch((err) => {
// console.log(err);
// //return reject(err);
// });
}
});
})
}), 1000)
});
};
getDappSym() {
return dappSym;
};
async unHash(_hash) {
return _hash.split(".");
};
/* Encrypt the payload */
async encrypt(_from, _to, _message, _secret) {
//console.log('about to ecnr: ', _secret);
const hash = web3.utils.sha3(web3.utils.sha3(_to.keyPair) + _from.keyPair);
const messageString = _message;
const nonce = await nacl.randomBytes(nacl.secretbox.nonceLength);
const key = await nacl.util.decodeBase64('A1v/xZFFMwUry3r5n6Uo6BPNNhr0YGSlqUxTuNAWrHk=');
const messageBytes = await nacl.util.decodeUTF8(messageString);
const box = await nacl.secretbox(messageBytes, nonce, key);
const nonceEncoded = await nacl.util.encodeBase64(nonce);
const cipherText = await nacl.util.encodeBase64(box);
//console.log(`${hash}.${nonceEncoded}.${cipherText}`);
return (`${hash}.${nonceEncoded}.${cipherText}`);
};
/* Decrypt the payload */
async decrypt(_secret, _nonceEncoded, _cipherText) {
const key = nacl.util.decodeBase64('A1v/xZFFMwUry3r5n6Uo6BPNNhr0YGSlqUxTuNAWrHk=');
const nonce = nacl.util.decodeBase64(_nonceEncoded);
const box = nacl.util.decodeBase64(_cipherText);
const messageBytes = nacl.secretbox.open(box, nonce, key);
const msg = nacl.util.encodeUTF8(messageBytes);
return msg;
};
/* Check hash vs. user */
async validateInterest(_hash) {
}
/* Create a pincode */
_getpincode(decimals) {
if (decimals < 2) {
decimals = 2;
}
var chars = "0123456789";
var randomstring = '';
for (var i = 0; i < decimals; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
};
async postMessage(_message, _from , _to, _secret) {
// Hash
let dappSymHere = this.getDappSym();
const encrypted = await this.encrypt(_from, _to, _message, _secret);
web3.shh.post({
symKeyID: dappSymHere,
ttl: 60,
sig: _from.keyPair,
powTarget: 2.01,
powTime: 2,
topic: '0x00000001',
payload: web3.utils.fromAscii(encrypted)
}).then(hash => {
console.log('\n',_from.publicKey.substring(0,8).grey,' posts:\n'.grey,encrypted.green);
console.log('To:\n'.grey,_to.publicKey.substring(0,8),"...");
}).catch(err => {
console.error('Error posting msg: ',err)
});
};
async createIdentity() {
const keyPair = await web3.shh.newKeyPair();
const publicKey = await web3.shh.getPublicKey(keyPair);
const privateKey = await web3.shh.getPrivateKey(keyPair);
return ({keyPair, publicKey, privateKey});
};
async unsubscribe(_id) {
// whatever
web3.shh.deleteMessageFilter(_id)
.then((res) => {
return res;
});
};
}
module.exports = Gossip