Using symkey as a public channel

This commit is contained in:
Richard Ramos 2018-04-17 11:47:12 -04:00
parent 4a59f582a9
commit cae1b967ee
4 changed files with 38 additions and 27 deletions

View File

@ -6,7 +6,7 @@
}, },
"whisper": { "whisper": {
"privateKey": "0x1d8ef80c9933e20fa9720bf82f3ad481c6be9e44920932c008bb76655a211add", "symKey": "0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b",
"protocol": "ws", "protocol": "ws",
"host": "localhost", "host": "localhost",
"port": 8546, "port": 8546,

View File

@ -7,9 +7,10 @@ const erc20ABI = require('../abi/ERC20.json');
console.info("Starting...") console.info("Starting...")
// TODO A node should call an API (probably from a status node) to register itself as a
// token gas relayer.
async function start(){ async function start(){
for(token in config.tokens){ for(token in config.tokens){
if(config.tokens[token].pricePlugin !== undefined){ if(config.tokens[token].pricePlugin !== undefined){
let PricePlugin = require(config.tokens[token].pricePlugin); let PricePlugin = require(config.tokens[token].pricePlugin);
@ -71,25 +72,31 @@ const shhOptions = {
}; };
let kId; let kId;
let symKId;
// Listening to whisper // Listening to whisper
web3.shh.addPrivateKey(config.whisper.privateKey)
.then((keyId) => { web3.shh.addSymKey(config.whisper.symKey)
shhOptions.privateKeyID = keyId; .then(symKeyId => {
symKId = symKeyId;
return web3.shh.newKeyPair();
})
.then(keyId => {
shhOptions.symKeyId = symKId;
kId = keyId; kId = keyId;
web3.shh.getPublicKey(keyId).then(pk => { console.info(`Sym Key: ${config.whisper.symKey}`);
console.info(`Public Key: ${pk}`);
console.info("Topics Available:"); console.info("Topics Available:");
config.topics = []; config.topics = [];
for(let contractName in config.contracts) { for(let contractName in config.contracts) {
console.info("- %s: %s [%s]", config.contracts[contractName].name, contractName, Object.keys(config.contracts[contractName].allowedFunctions).join(', ')); console.info("- %s: %s [%s]", config.contracts[contractName].name, contractName, Object.keys(config.contracts[contractName].allowedFunctions).join(', '));
shhOptions.topics = [contractName];
web3.shh.subscribe('messages', shhOptions, processMessages);
} }
});
console.info("Started."); console.info("Started.");
console.info("Listening for messages...") console.info("Listening for messages...")
web3.shh.subscribe('messages', shhOptions, processMessages);
}); });

View File

@ -4,7 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Send whisper message</title> <title>Send whisper message to node</title>
<style type="text/css"> <style type="text/css">
h4 small { h4 small {
color: #c3c3c3; color: #c3c3c3;
@ -25,15 +25,15 @@
</ul> </ul>
<form novalidate> <form novalidate>
<div class="mb-3"> <div class="mb-3">
<label for="publicKey">Public Key</label> <label for="sKey">Symkey</label>
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"> <div class="input-group-prepend">
<span class="input-group-text">0x</span> <span class="input-group-text">0x</span>
</div> </div>
<input type="text" class="form-control" id="publicKey" placeholder="Public Key" required> <input type="text" class="form-control" id="sKey" placeholder="Public Key" required value="0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b">
</div> </div>
<div class="invalid-feedback publicKey" style="width: 100%;"> <div class="invalid-feedback sKey" style="width: 100%;">
Invalid Public Key Invalid Sym Key
</div> </div>
</div> </div>
<div class="row"> <div class="row">

View File

@ -42,7 +42,7 @@ $(function(){
$('p.result').text(''); $('p.result').text('');
let publicKey = add0x($("#publicKey")); let sKey = add0x($("#sKey"));
let msgTopic = add0x($('#topic')); let msgTopic = add0x($('#topic'));
let msgPayload = add0x($('#payload')); let msgPayload = add0x($('#payload'));
let timeToLive = $('#ttl').val(); let timeToLive = $('#ttl').val();
@ -52,9 +52,9 @@ $(function(){
$('.invalid-feedback').hide(); $('.invalid-feedback').hide();
$('.is-invalid').removeClass('is-invalid'); $('.is-invalid').removeClass('is-invalid');
if(!/^0x[0-9a-f]{130}$/i.test(publicKey)){ if(!/^0x[0-9a-f]{64}$/i.test(sKey)){
$('#publicKey').addClass('is-invalid'); $('#sKey').addClass('is-invalid');
$('.invalid-feedback.publicKey').show(); $('.invalid-feedback.sKey').show();
} }
if(!/^0x[0-9a-f]{8}$/i.test(msgTopic)){ if(!/^0x[0-9a-f]{8}$/i.test(msgTopic)){
@ -85,12 +85,16 @@ $(function(){
if($('.is-invalid').length > 0) return; if($('.is-invalid').length > 0) return;
console.log(`%c await web3.shh.post({pubKey: "${publicKey}", sig: signature, ttl: ${timeToLive}, powTarget: ${powTarget}, powTime: ${powTime}, topic: "${msgTopic}", payload: "${msgPayload}"})`, 'font-weight: bold'); console.log(`%c await web3.shh.post({symKeyID: "${sKey}", sig: signature, ttl: ${timeToLive}, powTarget: ${powTarget}, powTime: ${powTime}, topic: "${msgTopic}", payload: "${msgPayload}"})`, 'font-weight: bold');
let identity; let identity;
web3.shh.post({ pubKey: publicKey, let _symKeyId = await web3.shh.addSymKey(sKey);
web3.shh.post({
symKeyID: _symKeyId,
sig: keyPair, sig: keyPair,
ttl: parseInt(timeToLive), ttl: parseInt(timeToLive),
powTarget: parseFloat(powTarget), powTarget: parseFloat(powTarget),