add shh topic auto transformation for filter options

This commit is contained in:
Fabian Vogelsteller 2015-02-27 14:05:00 +01:00
parent 37e6d1185b
commit aef0171927
11 changed files with 154 additions and 27 deletions

View File

@ -1,4 +1,4 @@
3stack:bignumber@2.0.0 3stack:bignumber@2.0.0
ethereum:js@0.0.15-rc4 ethereum:js@0.0.15-rc6
meteor@1.1.4 meteor@1.1.4
underscore@1.0.2 underscore@1.0.2

View File

@ -46,6 +46,8 @@
"lib", "lib",
"node_modules", "node_modules",
"package.json", "package.json",
"package.js",
".versions",
".bowerrc", ".bowerrc",
".editorconfig", ".editorconfig",
".gitignore", ".gitignore",

137
dist/ethereum.js vendored
View File

@ -821,6 +821,8 @@ module.exports = {
* @date 2014 * @date 2014
*/ */
var utils = require('./utils');
/// Should be called to check if filter implementation is valid /// Should be called to check if filter implementation is valid
/// @returns true if it is, otherwise false /// @returns true if it is, otherwise false
var implementationIsValid = function (i) { var implementationIsValid = function (i) {
@ -836,16 +838,25 @@ var implementationIsValid = function (i) {
/// @param should be string or object /// @param should be string or object
/// @returns options string or object /// @returns options string or object
var getOptions = function (options) { var getOptions = function (options) {
/*jshint maxcomplexity:5 */
if (typeof options === 'string') { if (typeof options === 'string') {
return options; return options;
} }
options = options || {}; options = options || {};
if (options.topics) { if (options.topics)
console.warn('"topics" is deprecated, is "topic" instead'); console.warn('"topics" is deprecated, is "topic" instead');
// make sure topics, get converted to hex
if(options.topic instanceof Array) {
options.topic = options.topic.map(function(topic){
return utils.toHex(topic);
});
} }
// evaluate lazy properties // evaluate lazy properties
return { return {
to: options.to, to: options.to,
@ -872,6 +883,8 @@ var filter = function(options, implementation, formatter) {
options = getOptions(options); options = getOptions(options);
var callbacks = []; var callbacks = [];
var filterId = implementation.newFilter(options); var filterId = implementation.newFilter(options);
// call the callbacks
var onMessages = function (messages) { var onMessages = function (messages) {
messages.forEach(function (message) { messages.forEach(function (message) {
message = formatter ? formatter(message) : message; message = formatter ? formatter(message) : message;
@ -933,7 +946,7 @@ var filter = function(options, implementation, formatter) {
module.exports = filter; module.exports = filter;
},{}],8:[function(require,module,exports){ },{"./utils":15}],8:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
@ -1173,6 +1186,70 @@ var outputBlockFormatter = function(block){
return block; return block;
}; };
/**
Formats the output of a log
@returns object
*/
var outputLogFormatter = function(log){
log.number = utils.toDecimal(log.number);
return log;
};
/**
Formats the input of a whisper post and converts all values to HEX
@returns object
*/
var inputPostFormatter = function(post){
post.payload = utils.toHex(post.payload);
post.ttl = utils.toHex(post.ttl);
post.workToProve = utils.toHex(post.workToProve);
if(!(post.topic instanceof Array))
post.topic = [post.topic];
// format the following options
post.topic = post.topic.map(function(topic){
return utils.toHex(topic);
});
return post;
};
/**
Formats the output of a received post message
@returns object
*/
var outputPostFormatter = function(post){
post.expiry = utils.toDecimal(post.expiry);
post.sent = utils.toDecimal(post.sent);
post.ttl = utils.toDecimal(post.ttl);
post.payloadRaw = post.payload;
post.payload = utils.toAscii(post.payload);
if(post.payload.indexOf('{') === 0 || post.payload.indexOf('[') === 0) {
try {
post.payload = JSON.parse(post.payload);
} catch (e) { }
}
// format the following options
post.topic = post.topic.map(function(topic){
return utils.toAscii(topic);
});
return post;
};
module.exports = { module.exports = {
formatInputInt: formatInputInt, formatInputInt: formatInputInt,
@ -1190,7 +1267,10 @@ module.exports = {
convertToBigNumber: convertToBigNumber, convertToBigNumber: convertToBigNumber,
inputTransactionFormatter: inputTransactionFormatter, inputTransactionFormatter: inputTransactionFormatter,
outputTransactionFormatter: outputTransactionFormatter, outputTransactionFormatter: outputTransactionFormatter,
outputBlockFormatter: outputBlockFormatter outputBlockFormatter: outputBlockFormatter,
outputLogFormatter: outputLogFormatter,
inputPostFormatter: inputPostFormatter,
outputPostFormatter: outputPostFormatter
}; };
@ -1488,10 +1568,12 @@ module.exports = requestManager;
* @date 2015 * @date 2015
*/ */
var formatters = require('./formatters');
/// @returns an array of objects describing web3.shh api methods /// @returns an array of objects describing web3.shh api methods
var methods = function () { var methods = function () {
return [ return [
{ name: 'post', call: 'shh_post' }, { name: 'post', call: 'shh_post', inputFormatter: formatters.inputPostFormatter },
{ name: 'newIdentity', call: 'shh_newIdentity' }, { name: 'newIdentity', call: 'shh_newIdentity' },
{ name: 'hasIdentity', call: 'shh_haveIdentity' }, { name: 'hasIdentity', call: 'shh_haveIdentity' },
{ name: 'newGroup', call: 'shh_newGroup' }, { name: 'newGroup', call: 'shh_newGroup' },
@ -1507,7 +1589,7 @@ module.exports = {
}; };
},{}],14:[function(require,module,exports){ },{"./formatters":8}],14:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
@ -1670,7 +1752,7 @@ var toAscii = function(hex) {
return str; return str;
}; };
var toHex = function(str) { var toHexNative = function(str) {
var hex = ""; var hex = "";
for(var i = 0; i < str.length; i++) { for(var i = 0; i < str.length; i++) {
var n = str.charCodeAt(i).toString(16); var n = str.charCodeAt(i).toString(16);
@ -1683,7 +1765,7 @@ var toHex = function(str) {
/// @returns hex representation (prefixed by 0x) of ascii string /// @returns hex representation (prefixed by 0x) of ascii string
var fromAscii = function(str, pad) { var fromAscii = function(str, pad) {
pad = pad === undefined ? 0 : pad; pad = pad === undefined ? 0 : pad;
var hex = toHex(str); var hex = toHexNative(str);
while (hex.length < pad*2) while (hex.length < pad*2)
hex += "00"; hex += "00";
return "0x" + hex; return "0x" + hex;
@ -1756,7 +1838,7 @@ var toDecimal = function (val) {
// remove 0x and place 0, if it's required // remove 0x and place 0, if it's required
val = val.length > 2 ? val.substring(2) : "0"; val = val.length > 2 ? val.substring(2) : "0";
return (new BigNumber(val, 16).toString(10)); return new BigNumber(val, 16).toNumber(); //.toString(10));
}; };
var fromDecimal = function (val) { var fromDecimal = function (val) {
@ -1764,6 +1846,27 @@ var fromDecimal = function (val) {
}; };
var toHex = function (val) {
/*jshint maxcomplexity:5 */
// pass it through is its already a number
if(typeof val === 'object')
return fromAscii(JSON.stringify(val));
// pass it through is its already a number
if(typeof val === 'string' && val.indexOf('0x') === 0)
return val;
if(typeof val === 'string' && !isFinite(val))
return fromAscii(val);
if(isFinite(val))
return fromDecimal(val);
return val;
};
/** /**
Takes a number of wei and converts it to any other ether unit. Takes a number of wei and converts it to any other ether unit.
@ -1907,6 +2010,7 @@ var toBigNumber = function(number) {
module.exports = { module.exports = {
findIndex: findIndex, findIndex: findIndex,
toHex: toHex,
toDecimal: toDecimal, toDecimal: toDecimal,
fromDecimal: fromDecimal, fromDecimal: fromDecimal,
toAscii: toAscii, toAscii: toAscii,
@ -2006,6 +2110,7 @@ var shh = require('./shh');
var watches = require('./watches'); var watches = require('./watches');
var filter = require('./filter'); var filter = require('./filter');
var utils = require('./utils'); var utils = require('./utils');
var formatters = require('./formatters');
var requestManager = require('./requestmanager'); var requestManager = require('./requestmanager');
/// @returns an array of objects describing web3 api methods /// @returns an array of objects describing web3 api methods
@ -2128,6 +2233,9 @@ var web3 = {
web3.manager.reset(); web3.manager.reset();
}, },
/// @returns hex string of the input
toHex: utils.toHex,
/// @returns ascii string representation of hex value prefixed with 0x /// @returns ascii string representation of hex value prefixed with 0x
toAscii: utils.toAscii, toAscii: utils.toAscii,
@ -2175,20 +2283,19 @@ var web3 = {
/// @param filter may be a string, object or event /// @param filter may be a string, object or event
/// @param eventParams is optional, this is an object with optional event eventParams params /// @param eventParams is optional, this is an object with optional event eventParams params
/// @param options is optional, this is an object with optional event options ('max'...) /// @param options is optional, this is an object with optional event options ('max'...)
/// TODO: fix it, 4 params? no way
/*jshint maxparams:4 */ /*jshint maxparams:4 */
filter: function (fil, eventParams, options, formatter) { filter: function (fil, eventParams, options) {
// if its event, treat it differently // if its event, treat it differently
if (fil._isEvent) if (fil._isEvent)
return fil(eventParams, options); return fil(eventParams, options);
return filter(fil, ethWatch, formatter); return filter(fil, ethWatch, formatters.outputLogFormatter);
}, },
// DEPRECATED // DEPRECATED
watch: function (fil, eventParams, options, formatter) { watch: function (fil, eventParams, options) {
console.warn('eth.watch() is deprecated please use eth.filter() instead.'); console.warn('eth.watch() is deprecated please use eth.filter() instead.');
return this.filter(fil, eventParams, options, formatter); return this.filter(fil, eventParams, options);
} }
/*jshint maxparams:3 */ /*jshint maxparams:3 */
}, },
@ -2200,7 +2307,7 @@ var web3 = {
shh: { shh: {
/// @param filter may be a string, object or event /// @param filter may be a string, object or event
filter: function (fil) { filter: function (fil) {
return filter(fil, shhWatch); return filter(fil, shhWatch, formatters.outputPostFormatter);
}, },
// DEPRECATED // DEPRECATED
watch: function (fil) { watch: function (fil) {
@ -2222,7 +2329,7 @@ setupMethods(shhWatch, watches.shh());
module.exports = web3; module.exports = web3;
},{"./db":4,"./eth":5,"./filter":7,"./requestmanager":12,"./shh":13,"./utils":15,"./watches":16}],"web3":[function(require,module,exports){ },{"./db":4,"./eth":5,"./filter":7,"./formatters":8,"./requestmanager":12,"./shh":13,"./utils":15,"./watches":16}],"web3":[function(require,module,exports){
var web3 = require('./lib/web3'); var web3 = require('./lib/web3');
web3.providers.HttpSyncProvider = require('./lib/httpsync'); web3.providers.HttpSyncProvider = require('./lib/httpsync');
web3.providers.QtSyncProvider = require('./lib/qtsync'); web3.providers.QtSyncProvider = require('./lib/qtsync');

12
dist/ethereum.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -23,6 +23,8 @@
* @date 2014 * @date 2014
*/ */
var utils = require('./utils');
/// Should be called to check if filter implementation is valid /// Should be called to check if filter implementation is valid
/// @returns true if it is, otherwise false /// @returns true if it is, otherwise false
var implementationIsValid = function (i) { var implementationIsValid = function (i) {
@ -38,16 +40,25 @@ var implementationIsValid = function (i) {
/// @param should be string or object /// @param should be string or object
/// @returns options string or object /// @returns options string or object
var getOptions = function (options) { var getOptions = function (options) {
/*jshint maxcomplexity:5 */
if (typeof options === 'string') { if (typeof options === 'string') {
return options; return options;
} }
options = options || {}; options = options || {};
if (options.topics) { if (options.topics)
console.warn('"topics" is deprecated, is "topic" instead'); console.warn('"topics" is deprecated, is "topic" instead');
// make sure topics, get converted to hex
if(options.topic instanceof Array) {
options.topic = options.topic.map(function(topic){
return utils.toHex(topic);
});
} }
// evaluate lazy properties // evaluate lazy properties
return { return {
to: options.to, to: options.to,

View File

@ -284,6 +284,7 @@ var outputPostFormatter = function(post){
post.expiry = utils.toDecimal(post.expiry); post.expiry = utils.toDecimal(post.expiry);
post.sent = utils.toDecimal(post.sent); post.sent = utils.toDecimal(post.sent);
post.ttl = utils.toDecimal(post.ttl); post.ttl = utils.toDecimal(post.ttl);
post.payloadRaw = post.payload;
post.payload = utils.toAscii(post.payload); post.payload = utils.toAscii(post.payload);
if(post.payload.indexOf('{') === 0 || post.payload.indexOf('[') === 0) { if(post.payload.indexOf('{') === 0 || post.payload.indexOf('[') === 0) {

View File

@ -174,6 +174,7 @@ var fromDecimal = function (val) {
var toHex = function (val) { var toHex = function (val) {
/*jshint maxcomplexity:5 */
// pass it through is its already a number // pass it through is its already a number
if(typeof val === 'object') if(typeof val === 'object')

View File

@ -1,3 +1,5 @@
if(typeof web3 === 'undefined') { /*jslint browser: true */
web3 = require('web3');
if(typeof window.web3 === 'undefined') {
window.web3 = require('web3');
} }

View File

@ -1,6 +1,7 @@
/* jshint ignore:start */
Package.describe({ Package.describe({
name: 'ethereum:js', name: 'ethereum:js',
version: '0.0.15-rc4', version: '0.0.15-rc6',
summary: 'Ethereum JavaScript API, middleware to talk to a ethreum node over RPC', summary: 'Ethereum JavaScript API, middleware to talk to a ethreum node over RPC',
git: 'https://github.com/ethereum/ethereum.js', git: 'https://github.com/ethereum/ethereum.js',
// By default, Meteor will default to using README.md for documentation. // By default, Meteor will default to using README.md for documentation.
@ -25,3 +26,4 @@ Package.onUse(function(api) {
// api.use('test'); // api.use('test');
// api.addFiles('test-tests.js'); // api.addFiles('test-tests.js');
// }); // });
/* jshint ignore:end */

View File

@ -16,6 +16,7 @@ describe('formatters', function () {
sent: 1000, sent: 1000,
ttl: 1000, ttl: 1000,
payload: {test: 'test'}, payload: {test: 'test'},
payloadRaw: '0x7b2274657374223a2274657374227d',
topic: ['hello','mytopics'] topic: ['hello','mytopics']
}); });
}); });