2016-08-26 11:01:22 +00:00
|
|
|
var EmbarkJS =
|
|
|
|
/******/ (function(modules) { // webpackBootstrap
|
|
|
|
/******/ // The module cache
|
|
|
|
/******/ var installedModules = {};
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // The require function
|
|
|
|
/******/ function __webpack_require__(moduleId) {
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // Check if module is in cache
|
|
|
|
/******/ if(installedModules[moduleId])
|
|
|
|
/******/ return installedModules[moduleId].exports;
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // Create a new module (and put it into the cache)
|
|
|
|
/******/ var module = installedModules[moduleId] = {
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/ i: moduleId,
|
|
|
|
/******/ l: false,
|
|
|
|
/******/ exports: {}
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ };
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // Execute the module function
|
|
|
|
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // Flag the module as loaded
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/ module.l = true;
|
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // Return the exports of the module
|
|
|
|
/******/ return module.exports;
|
|
|
|
/******/ }
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // expose the modules object (__webpack_modules__)
|
|
|
|
/******/ __webpack_require__.m = modules;
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // expose the module cache
|
|
|
|
/******/ __webpack_require__.c = installedModules;
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
|
|
|
/******/ // identity function for calling harmony imports with the correct context
|
|
|
|
/******/ __webpack_require__.i = function(value) { return value; };
|
|
|
|
/******/
|
|
|
|
/******/ // define getter function for harmony exports
|
|
|
|
/******/ __webpack_require__.d = function(exports, name, getter) {
|
|
|
|
/******/ if(!__webpack_require__.o(exports, name)) {
|
|
|
|
/******/ Object.defineProperty(exports, name, {
|
|
|
|
/******/ configurable: false,
|
|
|
|
/******/ enumerable: true,
|
|
|
|
/******/ get: getter
|
|
|
|
/******/ });
|
|
|
|
/******/ }
|
|
|
|
/******/ };
|
|
|
|
/******/
|
|
|
|
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
|
|
|
/******/ __webpack_require__.n = function(module) {
|
|
|
|
/******/ var getter = module && module.__esModule ?
|
|
|
|
/******/ function getDefault() { return module['default']; } :
|
|
|
|
/******/ function getModuleExports() { return module; };
|
|
|
|
/******/ __webpack_require__.d(getter, 'a', getter);
|
|
|
|
/******/ return getter;
|
|
|
|
/******/ };
|
|
|
|
/******/
|
|
|
|
/******/ // Object.prototype.hasOwnProperty.call
|
|
|
|
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // __webpack_public_path__
|
|
|
|
/******/ __webpack_require__.p = "";
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ // Load entry module and return exports
|
2017-03-12 21:59:21 +00:00
|
|
|
/******/ return __webpack_require__(__webpack_require__.s = 0);
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ })
|
|
|
|
/************************************************************************/
|
|
|
|
/******/ ([
|
|
|
|
/* 0 */
|
2017-03-12 21:59:21 +00:00
|
|
|
/***/ (function(module, exports) {
|
|
|
|
|
|
|
|
/*jshint esversion: 6 */
|
|
|
|
//var Ipfs = require('./ipfs.js');
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
//=========================================================
|
|
|
|
// Embark Smart Contracts
|
|
|
|
//=========================================================
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var EmbarkJS = {};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
|
|
|
EmbarkJS.Contract = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var self = this;
|
|
|
|
var i, abiElement;
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
this.abi = options.abi;
|
|
|
|
this.address = options.address;
|
|
|
|
this.code = '0x' + options.code;
|
|
|
|
this.web3 = options.web3 || web3;
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var ContractClass = this.web3.eth.contract(this.abi);
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
this.eventList = [];
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
if (this.abi) {
|
|
|
|
for (i = 0; i < this.abi.length; i++) {
|
|
|
|
abiElement = this.abi[i];
|
|
|
|
if (abiElement.type === 'event') {
|
|
|
|
this.eventList.push(abiElement.name);
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 21:59:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var messageEvents = function() {
|
|
|
|
this.cb = function() {};
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
messageEvents.prototype.then = function(cb) {
|
|
|
|
this.cb = cb;
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
messageEvents.prototype.error = function(err) {
|
|
|
|
return err;
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
this._originalContractObject = ContractClass.at(this.address);
|
|
|
|
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
|
|
|
|
// TODO: check for forbidden properties
|
|
|
|
if (self.eventList.indexOf(p) >= 0) {
|
|
|
|
|
|
|
|
self[p] = function() {
|
|
|
|
var promise = new messageEvents();
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
args.push(function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
promise.error(err);
|
|
|
|
} else {
|
|
|
|
promise.cb(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self._originalContractObject[p].apply(self._originalContractObject[p], args);
|
|
|
|
return promise;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
2017-03-17 14:02:26 +00:00
|
|
|
return true;
|
|
|
|
} else if (typeof self._originalContractObject[p] === 'function') {
|
|
|
|
self[p] = function(_args) {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var fn = self._originalContractObject[p];
|
|
|
|
var props = self.abi.find((x) => x.name == p);
|
|
|
|
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
args.push(function(err, transaction) {
|
|
|
|
promise.tx = transaction;
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var getConfirmation = function() {
|
|
|
|
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (receipt !== null) {
|
|
|
|
return resolve(receipt);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(getConfirmation, 1000);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof(transaction) !== "string" || props.constant) {
|
|
|
|
resolve(transaction);
|
|
|
|
} else {
|
|
|
|
getConfirmation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fn.apply(fn, args);
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var self = this;
|
|
|
|
var contractParams;
|
|
|
|
var options = _options || {};
|
|
|
|
|
|
|
|
contractParams = args || [];
|
|
|
|
|
|
|
|
contractParams.push({
|
|
|
|
from: this.web3.eth.accounts[0],
|
|
|
|
data: this.code,
|
|
|
|
gas: options.gas || 800000
|
2017-03-12 21:59:21 +00:00
|
|
|
});
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var contractObject = this.web3.eth.contract(this.abi);
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
contractParams.push(function(err, transaction) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else if (transaction.address !== undefined) {
|
|
|
|
resolve(new EmbarkJS.Contract({
|
|
|
|
abi: self.abi,
|
|
|
|
code: self.code,
|
|
|
|
address: transaction.address
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
// returns promise
|
|
|
|
// deploys contract
|
|
|
|
// wraps it around EmbarkJS.Contract
|
|
|
|
contractObject["new"].apply(contractObject, contractParams);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return promise;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
//=========================================================
|
|
|
|
// Embark Storage
|
|
|
|
//=========================================================
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Storage = {};
|
2017-03-13 13:38:30 +00:00
|
|
|
|
|
|
|
EmbarkJS.Storage.Providers = {
|
2017-03-17 14:02:26 +00:00
|
|
|
IPFS: 'ipfs',
|
|
|
|
SWARM: 'swarm'
|
2017-03-13 13:38:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Storage.IPFS = {};
|
2017-03-13 13:38:30 +00:00
|
|
|
|
|
|
|
EmbarkJS.Storage.saveText = function(text) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentStorage.saveText(text);
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
EmbarkJS.Storage.get = function(hash) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentStorage.get(hash);
|
2017-03-14 15:39:09 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
|
|
|
return this.currentStorage.uploadFile(inputSelector);
|
2017-03-14 15:39:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Storage.getUrl = function(hash) {
|
|
|
|
return this.currentStorage.getUrl(hash);
|
2017-03-14 15:39:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 21:59:21 +00:00
|
|
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var self = this;
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
|
|
|
//I don't think currentStorage is used anywhere, this might not be needed
|
|
|
|
//for now until additional storage providers are supported. But keeping it
|
|
|
|
//anyways
|
|
|
|
self.currentStorage = EmbarkJS.Storage.IPFS;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (options === undefined) {
|
|
|
|
self.ipfsConnection = IpfsApi('localhost', '5001');
|
|
|
|
} else {
|
|
|
|
self.ipfsConnection = IpfsApi(options.server, options.port);
|
|
|
|
}
|
|
|
|
resolve(self);
|
|
|
|
} catch (err) {
|
|
|
|
self.ipfsConnection = null;
|
|
|
|
reject(new Error('Failed to connect to IPFS'));
|
|
|
|
}
|
|
|
|
} else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM) {
|
|
|
|
reject('Swarm not implemented');
|
|
|
|
// TODO Implement Swarm
|
|
|
|
// this.currentStorage = EmbarkJS.Storage.SWARM;
|
|
|
|
// if (options === undefined) {
|
|
|
|
// //Connect to default Swarm node
|
|
|
|
// } else {
|
|
|
|
// //Connect using options
|
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
reject('Unknown storage provider');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return promise;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
if (!EmbarkJS.Storage.ipfsConnection) {
|
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
|
|
|
EmbarkJS.Storage.ipfsConnection.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result[0].path);
|
|
|
|
}
|
|
|
|
});
|
2017-03-12 21:59:21 +00:00
|
|
|
});
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
return promise;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
EmbarkJS.Storage.IPFS.get = function(hash) {
|
2017-03-17 14:02:26 +00:00
|
|
|
// TODO: detect type, then convert if needed
|
|
|
|
//var ipfsHash = web3.toAscii(hash);
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
if (!EmbarkJS.Storage.ipfsConnection) {
|
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
|
|
|
EmbarkJS.Storage.ipfsConnection.object.get([hash]).then(function(node) {
|
|
|
|
resolve(node.data);
|
|
|
|
}).catch(function(err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
2017-03-14 15:39:09 +00:00
|
|
|
});
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
return promise;
|
2017-03-14 15:39:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var file = inputSelector[0].files[0];
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
if (file === undefined) {
|
|
|
|
throw new Error('no file found');
|
2017-03-14 15:39:09 +00:00
|
|
|
}
|
2017-03-17 14:02:26 +00:00
|
|
|
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
if (!EmbarkJS.Storage.ipfsConnection) {
|
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
2017-03-12 21:59:21 +00:00
|
|
|
}
|
2017-03-17 14:02:26 +00:00
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onloadend = function() {
|
|
|
|
var fileContent = reader.result;
|
|
|
|
var buffer = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent);
|
|
|
|
EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result[0].path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
return promise;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
2017-03-17 14:02:26 +00:00
|
|
|
//var ipfsHash = web3.toAscii(hash);
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
return 'http://localhost:8080/ipfs/' + hash;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
//=========================================================
|
|
|
|
// Embark Messaging
|
|
|
|
//=========================================================
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Messages = {};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
|
|
|
EmbarkJS.Messages.setProvider = function(provider, options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var self = this;
|
|
|
|
var ipfs;
|
|
|
|
if (provider === 'whisper') {
|
|
|
|
this.currentMessages = EmbarkJS.Messages.Whisper;
|
|
|
|
if (typeof variable === 'undefined') {
|
|
|
|
if (options === undefined) {
|
|
|
|
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
|
|
|
} else {
|
|
|
|
web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
web3.version.getWhisper(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
console.log("whisper not available");
|
|
|
|
} else {
|
|
|
|
self.currentMessages.identity = web3.shh.newIdentity();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (provider === 'orbit') {
|
|
|
|
this.currentMessages = EmbarkJS.Messages.Orbit;
|
|
|
|
if (options === undefined) {
|
|
|
|
ipfs = HaadIpfsApi('localhost', '5001');
|
|
|
|
} else {
|
|
|
|
ipfs = HaadIpfsApi(options.server, options.port);
|
|
|
|
}
|
|
|
|
this.currentMessages.orbit = new Orbit(ipfs);
|
|
|
|
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
2017-03-12 21:59:21 +00:00
|
|
|
} else {
|
2017-03-17 14:02:26 +00:00
|
|
|
throw Error('Unknown message provider');
|
2017-03-12 21:59:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.sendMessage = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentMessages.sendMessage(options);
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.listenTo = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentMessages.listenTo(options);
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Messages.Whisper = {};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
|
|
|
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var topics = options.topic || options.topics;
|
|
|
|
var data = options.data || options.payload;
|
|
|
|
var identity = options.identity || this.identity || web3.shh.newIdentity();
|
|
|
|
var ttl = options.ttl || 100;
|
|
|
|
var priority = options.priority || 1000;
|
|
|
|
var _topics;
|
|
|
|
|
|
|
|
if (topics === undefined) {
|
|
|
|
throw new Error("missing option: topic");
|
2017-03-12 21:59:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
if (data === undefined) {
|
|
|
|
throw new Error("missing option: data");
|
|
|
|
}
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
// do fromAscii to each topics unless it's already a string
|
|
|
|
if (typeof topics === 'string') {
|
|
|
|
_topics = [web3.fromAscii(topics)];
|
|
|
|
} else {
|
|
|
|
// TODO: replace with es6 + babel;
|
|
|
|
for (var i = 0; i < topics.length; i++) {
|
|
|
|
_topics.push(web3.fromAscii(topics[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
topics = _topics;
|
|
|
|
|
|
|
|
var payload = JSON.stringify(data);
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
from: identity,
|
|
|
|
topics: topics,
|
|
|
|
payload: web3.fromAscii(payload),
|
|
|
|
ttl: ttl,
|
|
|
|
priority: priority
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
return web3.shh.post(message, function() {});
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var topics = options.topic || options.topics;
|
|
|
|
var _topics = [];
|
|
|
|
|
|
|
|
if (typeof topics === 'string') {
|
|
|
|
_topics = [topics];
|
|
|
|
} else {
|
|
|
|
// TODO: replace with es6 + babel;
|
|
|
|
for (var i = 0; i < topics.length; i++) {
|
|
|
|
_topics.push(topics[i]);
|
|
|
|
}
|
2017-03-12 21:59:21 +00:00
|
|
|
}
|
2017-03-17 14:02:26 +00:00
|
|
|
topics = _topics;
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var filterOptions = {
|
|
|
|
topics: topics
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var messageEvents = function() {
|
|
|
|
this.cb = function() {};
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
messageEvents.prototype.then = function(cb) {
|
|
|
|
this.cb = cb;
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
messageEvents.prototype.error = function(err) {
|
|
|
|
return err;
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
messageEvents.prototype.stop = function() {
|
|
|
|
this.filter.stopWatching();
|
|
|
|
};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var promise = new messageEvents();
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
|
|
|
var payload = JSON.parse(web3.toAscii(result.payload));
|
|
|
|
var data;
|
|
|
|
if (err) {
|
|
|
|
promise.error(err);
|
|
|
|
} else {
|
|
|
|
data = {
|
|
|
|
topic: topics,
|
|
|
|
data: payload,
|
|
|
|
from: result.from,
|
|
|
|
time: (new Date(result.sent * 1000))
|
|
|
|
};
|
|
|
|
promise.cb(payload, data, result);
|
|
|
|
}
|
|
|
|
});
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
promise.filter = filter;
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
return promise;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Messages.Orbit = {};
|
2017-03-12 21:59:21 +00:00
|
|
|
|
|
|
|
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var topics = options.topic || options.topics;
|
|
|
|
var data = options.data || options.payload;
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
if (topics === undefined) {
|
|
|
|
throw new Error("missing option: topic");
|
|
|
|
}
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
if (data === undefined) {
|
|
|
|
throw new Error("missing option: data");
|
|
|
|
}
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
if (typeof topics === 'string') {
|
|
|
|
topics = topics;
|
|
|
|
} else {
|
|
|
|
// TODO: better to just send to different channels instead
|
|
|
|
topics = topics.join(',');
|
|
|
|
}
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
this.orbit.join(topics);
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var payload = JSON.stringify(data);
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
this.orbit.send(topics, data);
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.Orbit.listenTo = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var self = this;
|
|
|
|
var topics = options.topic || options.topics;
|
|
|
|
|
|
|
|
if (typeof topics === 'string') {
|
|
|
|
topics = topics;
|
|
|
|
} else {
|
|
|
|
topics = topics.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.orbit.join(topics);
|
|
|
|
|
|
|
|
var messageEvents = function() {
|
|
|
|
this.cb = function() {};
|
|
|
|
};
|
|
|
|
|
|
|
|
messageEvents.prototype.then = function(cb) {
|
|
|
|
this.cb = cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
messageEvents.prototype.error = function(err) {
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
|
|
|
|
var promise = new messageEvents();
|
|
|
|
|
|
|
|
this.orbit.events.on('message', (channel, message) => {
|
|
|
|
// TODO: looks like sometimes it's receving messages from all topics
|
|
|
|
if (topics !== channel) return;
|
|
|
|
self.orbit.getPost(message.payload.value, true).then((post) => {
|
|
|
|
var data = {
|
|
|
|
topic: channel,
|
|
|
|
data: post.content,
|
|
|
|
from: post.meta.from.name,
|
|
|
|
time: (new Date(post.meta.ts))
|
|
|
|
};
|
|
|
|
promise.cb(post.content, data, post);
|
|
|
|
});
|
2017-03-12 21:59:21 +00:00
|
|
|
});
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
return promise;
|
2017-03-12 21:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = EmbarkJS;
|
|
|
|
|
|
|
|
|
|
|
|
/***/ })
|
2016-08-26 11:01:22 +00:00
|
|
|
/******/ ]);
|