-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcodeTokenTransfer.js
118 lines (106 loc) · 3.79 KB
/
shortcodeTokenTransfer.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
// Shortcode Token Transfer
// Dependencies
// web3
const Web3 = require('web3');
const events = require('events');
const eventEmitter = new events.EventEmitter();
// contractabi
// Flurkel's geth node
// const url = 'http://192.168.0.134:8545'
// const url = 'ws://192.168.0.172:8545'
// dapplion's digital ocean geth node
const url = 'http://my.ropsten.dnp.dappnode.eth:8545'
// Create the ropsten web3
const web3 = new Web3(url);
web3.eth.getBlockNumber()
.then(res => console.log(`block number: ${res}`));
web3.eth.isSyncing()
.then(res => console.log(res ? `syncing: ${res.currentBlock}/${res.highestBlock} (${res.currentBlock-res.highestBlock})` : `synced!`));
module.exports = {
getweb3: async function () {
return web3;
},
subscribe: async function (_dappSymKeyId) {
// Subscribe function
web3.shh.newMessageFilter({
symKeyID: _dappSymKeyId,
topics: ['0x00000001'],
ttl: 20,
minPow: 0.8,
}).then(_Id => {
//console.log(_id);
//return ({id: _id});
setInterval(() => web3.shh.getFilterMessages(_Id).then(msgs => {
//console.log('listening on ', _dappSymKeyId, ' - ', Date.now());
if (!msgs.length) return
msgs.forEach(m => {
const obj = JSON.parse(web3.utils.hexToUtf8(m.payload))
//console.log(obj, m.sig);
this.processMessage(obj, m.sig);
//return (_Id, obj, m.sig, m);
})
}), 1000)
});
},
unsubscribe: async function (_id) {
// whatever
web3.shh.deleteMessageFilter(_id)
.then((res) => {
return res;
});
},
_getpincode: function(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;
},
postMessage: async function (_dappSymKeyId, _message, _from) {
// whatever
//console.log('posting: ', JSON.stringify(_message));
web3.shh.post({
symKeyID: _dappSymKeyId,
ttl: 60,
sig: _from.keyPair,
powTarget: 2.01,
powTime: 2,
topic: '0x00000001',
payload: web3.utils.fromAscii(JSON.stringify(_message), + Date.now())
}).then(hash => {
console.log('Successfully posted message ', JSON.stringify(_message), ' --- ', + Date.now())
}).catch(err => {
console.error('Error posting msg: ',err)
});
},
createIdentity: async function () {
const keyPair = await web3.shh.newKeyPair();
const publicKey = await web3.shh.getPublicKey(keyPair);
const privateKey = await web3.shh.getPrivateKey(keyPair);
return ({keyPair, publicKey, privateKey});
},
createDappKey: async function () {
// Create a dappKey
const dappSymKey = await web3.shh.generateSymKeyFromPassword('swarmcity');
// Add it to the node
let Id = await web3.shh.addSymKey('0x'+dappSymKey);
// CHeck if it's there
let hasit = await web3.shh.hasSymKey('0x'+dappSymKey);
console.log('key stored on node? ', hasit);
return (dappSymKey);
},
processMessage: async function (msg, publickey) {
console.log('Received msg: ',msg, ' - from: ',publickey);
switch (msg.command) {
case 'shortcode':
//this.postMessage(publickey, 'message', receiver.publickey);
//eventEmitter.emit('receivedMessage', msg);
break;
}
}
};