Browse Source

removed unused js files

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
29e959f809
  1. 379
      libethrpc/BigInteger.js
  2. 262
      libethrpc/eth.js
  3. 62
      libethrpc/ethString.js

379
libethrpc/BigInteger.js

@ -1,379 +0,0 @@
var bigInt = (function () {
var base = 10000000, logBase = 7;
var sign = {
positive: false,
negative: true
};
var normalize = function (first, second) {
var a = first.value, b = second.value;
var length = a.length > b.length ? a.length : b.length;
for (var i = 0; i < length; i++) {
a[i] = a[i] || 0;
b[i] = b[i] || 0;
}
for (var i = length - 1; i >= 0; i--) {
if (a[i] === 0 && b[i] === 0) {
a.pop();
b.pop();
} else break;
}
if (!a.length) a = [0], b = [0];
first.value = a;
second.value = b;
};
var parse = function (text, first) {
if (typeof text === "object") return text;
text += "";
var s = sign.positive, value = [];
if (text[0] === "-") {
s = sign.negative;
text = text.slice(1);
}
var base = 10;
if (text.slice(0, 2) == "0x") {
base = 16;
text = text.slice(2);
}
else {
var texts = text.split("e");
if (texts.length > 2) throw new Error("Invalid integer");
if (texts[1]) {
var exp = texts[1];
if (exp[0] === "+") exp = exp.slice(1);
exp = parse(exp);
if (exp.lesser(0)) throw new Error("Cannot include negative exponent part for integers");
while (exp.notEquals(0)) {
texts[0] += "0";
exp = exp.prev();
}
}
text = texts[0];
}
if (text === "-0") text = "0";
text = text.toUpperCase();
var isValid = (base == 16 ? /^[0-9A-F]*$/ : /^[0-9]+$/).test(text);
if (!isValid) throw new Error("Invalid integer");
if (base == 16) {
var val = bigInt(0);
while (text.length) {
v = text.charCodeAt(0) - 48;
if (v > 9)
v -= 7;
text = text.slice(1);
val = val.times(16).plus(v);
}
return val;
}
else {
while (text.length) {
var divider = text.length > logBase ? text.length - logBase : 0;
value.push(+text.slice(divider));
text = text.slice(0, divider);
}
var val = bigInt(value, s);
if (first) normalize(first, val);
return val;
}
};
var goesInto = function (a, b) {
var a = bigInt(a, sign.positive), b = bigInt(b, sign.positive);
if (a.equals(0)) throw new Error("Cannot divide by 0");
var n = 0;
do {
var inc = 1;
var c = bigInt(a.value, sign.positive), t = c.times(10);
while (t.lesser(b)) {
c = t;
inc *= 10;
t = t.times(10);
}
while (c.lesserOrEquals(b)) {
b = b.minus(c);
n += inc;
}
} while (a.lesserOrEquals(b));
return {
remainder: b.value,
result: n
};
};
var bigInt = function (value, s) {
var self = {
value: value,
sign: s
};
var o = {
value: value,
sign: s,
negate: function (m) {
var first = m || self;
return bigInt(first.value, !first.sign);
},
abs: function (m) {
var first = m || self;
return bigInt(first.value, sign.positive);
},
add: function (n, m) {
var s, first = self, second;
if (m) (first = parse(n)) && (second = parse(m));
else second = parse(n, first);
s = first.sign;
if (first.sign !== second.sign) {
first = bigInt(first.value, sign.positive);
second = bigInt(second.value, sign.positive);
return s === sign.positive ?
o.subtract(first, second) :
o.subtract(second, first);
}
normalize(first, second);
var a = first.value, b = second.value;
var result = [],
carry = 0;
for (var i = 0; i < a.length || carry > 0; i++) {
var sum = (a[i] || 0) + (b[i] || 0) + carry;
carry = sum >= base ? 1 : 0;
sum -= carry * base;
result.push(sum);
}
return bigInt(result, s);
},
plus: function (n, m) {
return o.add(n, m);
},
subtract: function (n, m) {
var first = self, second;
if (m) (first = parse(n)) && (second = parse(m));
else second = parse(n, first);
if (first.sign !== second.sign) return o.add(first, o.negate(second));
if (first.sign === sign.negative) return o.subtract(o.negate(second), o.negate(first));
if (o.compare(first, second) === -1) return o.negate(o.subtract(second, first));
var a = first.value, b = second.value;
var result = [],
borrow = 0;
for (var i = 0; i < a.length; i++) {
var tmp = a[i] - borrow;
borrow = tmp < b[i] ? 1 : 0;
var minuend = (borrow * base) + tmp - b[i];
result.push(minuend);
}
return bigInt(result, sign.positive);
},
minus: function (n, m) {
return o.subtract(n, m);
},
multiply: function (n, m) {
var s, first = self, second;
if (m) (first = parse(n)) && (second = parse(m));
else second = parse(n, first);
s = first.sign !== second.sign;
var a = first.value, b = second.value;
var resultSum = [];
for (var i = 0; i < a.length; i++) {
resultSum[i] = [];
var j = i;
while (j--) {
resultSum[i].push(0);
}
}
var carry = 0;
for (var i = 0; i < a.length; i++) {
var x = a[i];
for (var j = 0; j < b.length || carry > 0; j++) {
var y = b[j];
var product = y ? (x * y) + carry : carry;
carry = product > base ? Math.floor(product / base) : 0;
product -= carry * base;
resultSum[i].push(product);
}
}
var max = -1;
for (var i = 0; i < resultSum.length; i++) {
var len = resultSum[i].length;
if (len > max) max = len;
}
var result = [], carry = 0;
for (var i = 0; i < max || carry > 0; i++) {
var sum = carry;
for (var j = 0; j < resultSum.length; j++) {
sum += resultSum[j][i] || 0;
}
carry = sum > base ? Math.floor(sum / base) : 0;
sum -= carry * base;
result.push(sum);
}
return bigInt(result, s);
},
times: function (n, m) {
return o.multiply(n, m);
},
divmod: function (n, m) {
var s, first = self, second;
if (m) (first = parse(n)) && (second = parse(m));
else second = parse(n, first);
s = first.sign !== second.sign;
if (bigInt(first.value, first.sign).equals(0)) return {
quotient: bigInt([0], sign.positive),
remainder: bigInt([0], sign.positive)
};
if (second.equals(0)) throw new Error("Cannot divide by zero");
var a = first.value, b = second.value;
var result = [], remainder = [];
for (var i = a.length - 1; i >= 0; i--) {
var n = [a[i]].concat(remainder);
var quotient = goesInto(b, n);
result.push(quotient.result);
remainder = quotient.remainder;
}
result.reverse();
return {
quotient: bigInt(result, s),
remainder: bigInt(remainder, first.sign)
};
},
divide: function (n, m) {
return o.divmod(n, m).quotient;
},
over: function (n, m) {
return o.divide(n, m);
},
mod: function (n, m) {
return o.divmod(n, m).remainder;
},
pow: function (n, m) {
var first = self, second;
if (m) (first = parse(n)) && (second = parse(m));
else second = parse(n, first);
var a = first, b = second;
if (b.lesser(0)) return ZERO;
if (b.equals(0)) return ONE;
var result = bigInt(a.value, a.sign);
if (b.mod(2).equals(0)) {
var c = result.pow(b.over(2));
return c.times(c);
} else {
return result.times(result.pow(b.minus(1)));
}
},
next: function (m) {
var first = m || self;
return o.add(first, 1);
},
prev: function (m) {
var first = m || self;
return o.subtract(first, 1);
},
compare: function (n, m) {
var first = self, second;
if (m) (first = parse(n)) && (second = parse(m, first));
else second = parse(n, first);
normalize(first, second);
if (first.value.length === 1 && second.value.length === 1 && first.value[0] === 0 && second.value[0] === 0) return 0;
if (second.sign !== first.sign) return first.sign === sign.positive ? 1 : -1;
var multiplier = first.sign === sign.positive ? 1 : -1;
var a = first.value, b = second.value;
for (var i = a.length - 1; i >= 0; i--) {
if (a[i] > b[i]) return 1 * multiplier;
if (b[i] > a[i]) return -1 * multiplier;
}
return 0;
},
compareAbs: function (n, m) {
var first = self, second;
if (m) (first = parse(n)) && (second = parse(m, first));
else second = parse(n, first);
first.sign = second.sign = sign.positive;
return o.compare(first, second);
},
equals: function (n, m) {
return o.compare(n, m) === 0;
},
notEquals: function (n, m) {
return !o.equals(n, m);
},
lesser: function (n, m) {
return o.compare(n, m) < 0;
},
greater: function (n, m) {
return o.compare(n, m) > 0;
},
greaterOrEquals: function (n, m) {
return o.compare(n, m) >= 0;
},
lesserOrEquals: function (n, m) {
return o.compare(n, m) <= 0;
},
isPositive: function (m) {
var first = m || self;
return first.sign === sign.positive;
},
isNegative: function (m) {
var first = m || self;
return first.sign === sign.negative;
},
isEven: function (m) {
var first = m || self;
return first.value[0] % 2 === 0;
},
isOdd: function (m) {
var first = m || self;
return first.value[0] % 2 === 1;
},
toString: function (m) {
var first = m || self;
var str = "", len = first.value.length;
while (len--) {
if (first.value[len].toString().length === 8) str += first.value[len];
else str += (base.toString() + first.value[len]).slice(-logBase);
}
while (str[0] === "0") {
str = str.slice(1);
}
if (!str.length) str = "0";
var s = (first.sign === sign.positive || str == "0") ? "" : "-";
return s + str;
},
toHex: function (m) {
var first = m || self;
var str = "";
var l = this.abs();
while (l > 0) {
var qr = l.divmod(256);
var b = qr.remainder.toJSNumber();
str = (b >> 4).toString(16) + (b & 15).toString(16) + str;
l = qr.quotient;
}
return (this.isNegative() ? "-" : "") + "0x" + str;
},
toJSNumber: function (m) {
return +o.toString(m);
},
valueOf: function (m) {
return o.toJSNumber(m);
}
};
return o;
};
var ZERO = bigInt([0], sign.positive);
var ONE = bigInt([1], sign.positive);
var MINUS_ONE = bigInt([1], sign.negative);
var fnReturn = function (a) {
if (typeof a === "undefined") return ZERO;
return parse(a);
};
fnReturn.zero = ZERO;
fnReturn.one = ONE;
fnReturn.minusOne = MINUS_ONE;
return fnReturn;
})();
if (typeof module !== "undefined") {
module.exports = bigInt;
}

262
libethrpc/eth.js

@ -1,262 +0,0 @@
// https://github.com/ethereum/cpp-ethereum/wiki/PoC-6-JS-API
if (typeof(window.eth) === "undefined")
{
if (typeof(require) !== "undefined")
require( ['ethString'], function() {} )
else if (typeof(String.prototype.pad) === "undefined")
{
var scriptTag = document.getElementsByTagName('script');
scriptTag = scriptTag[scriptTag.length - 1];
var scriptPath = scriptTag.src;
var path = scriptPath.substr(0, scriptPath.lastIndexOf( '/' ));
var start = '<script src="' + path + '/';
var slash = '"><'+'/script>';
document.write(start + 'BigInteger.js' + slash);
document.write(start + 'ethString.js' + slash);
}
var spec = [
{ "method": "coinbase", "params": null, "order": [], "returns" : "" },
{ "method": "setCoinbase", "params": { "address": "" }, "order": ["address"], "returns" : true },
{ "method": "listening", "params": null, "order": [], "returns" : false },
{ "method": "setListening", "params": { "listening": false }, "order" : ["listening"], "returns" : true },
{ "method": "mining", "params": null, "order": [], "returns" : false },
{ "method": "setMining", "params": { "mining": false }, "order" : ["mining"], "returns" : true },
{ "method": "gasPrice", "params": null, "order": [], "returns" : "" },
{ "method": "accounts", "params": null, "order": [], "returns" : [] },
{ "method": "peerCount", "params": null, "order": [], "returns" : 0 },
{ "method": "defaultBlock", "params": null, "order": [], "returns" : 0},
{ "method": "number", "params": null, "order": [], "returns" : 0},
{ "method": "balanceAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns" : ""},
{ "method": "stateAt", "params": { "address": "", "storage": "", "block": 0}, "order": ["address", "storage", "block"], "returns": ""},
{ "method": "countAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns" : 0.0},
{ "method": "codeAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns": ""},
{ "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "block", "params": { "params": {}}, "order": ["params"], "returns": {}},
{ "method": "transaction", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "uncle", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "messages", "params": { "params": {}}, "order": ["params"], "returns": []},
{ "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": 0},
{ "method": "check", "params": { "id": 0}, "order": [], "returns": true},
{ "method": "killWatch", "params": { "id": 0}, "order": ["params"], "returns": true},
{ "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "sha3", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "toAscii", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "fromAscii", "params": { "s": "", "padding": 0}, "order": ["s", "padding"], "returns": ""},
{ "method": "toDecimal", "params": {"s": ""}, "order": ["s"], "returns" : ""},
{ "method": "toFixed", "params": {"s": 0.0}, "order": ["s"], "returns" : ""},
{ "method": "fromFixed", "params": {"s": ""}, "order": ["s"], "returns" : 0.0},
{ "method": "offset", "params": {"s": "", "o": 0}, "order": ["s", "o"], "returns" : ""}
];
window.eth = (function ethScope() {
var m_reqId = 0
var ret = {}
function reformat(m, d) { return m == "lll" ? d.bin() : d; };
function reqSync(m, p) {
var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId }
m_reqId++
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080", false)
request.send(JSON.stringify(req))
return reformat(m, JSON.parse(request.responseText).result)
};
function reqAsync(m, p, f) {
var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId }
m_reqId++
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080", true)
request.send(JSON.stringify(req))
request.onreadystatechange = function() {
if (request.readyState === 4)
if (f)
f(reformat(m, JSON.parse(request.responseText).result));
};
};
var getParams = function (spec, name, args) {
var setup = spec.filter(function (s) {
return s.method === name;
});
if (setup.length === 0) {
return {};
}
var paramSetup = setup[0];
var p = paramSetup.params ? {} : null;
for (j in paramSetup.order)
p[paramSetup.order[j]] = args[j];
return p;
};
var addPrefix = function (s, prefix) {
if (!s) {
return s;
}
return prefix + s.slice(0, 1).toUpperCase() + s.slice(1);
};
var toGetter = function (s) {
return addPrefix(s, "get");
};
var toSetter = function (s) {
return addPrefix(s, "set");
};
var defaults = function (def, obj) {
if (!def) {
return obj;
}
var rewriteProperties = function (dst, p) {
Object.keys(p).forEach(function (key) {
if (p[key] !== undefined) {
dst[key] = p[key];
}
});
};
var res = {};
rewriteProperties(res, def);
rewriteProperties(res, obj);
return res;
};
var setupProperties = function (root, spec) {
var properties = [
{ name: "coinbase", getter: "coinbase", setter: "setCoinbase" },
{ name: "listening", getter: "listening", setter: "setListening" },
{ name: "mining", getter: "mining", setter: "setMining" },
{ name: "gasPrice", getter: "gasPrice"},
{ name: "accounts", getter: "accounts" },
{ name: "peerCount", getter: "peerCount" },
{ name: "defaultBlock", getter: "defaultBlock" },
{ name: "number", getter: "number" }
];
properties.forEach(function (property) {
var p = {};
if (property.getter) {
p.get = function () {
return reqSync(property.getter, {});
};
root[toGetter(property.name)] = function (f) {
return reqAsync(property.getter, null, f);
};
}
if (property.setter) {
p.set = function (newVal) {
return reqSync(property.setter, getParams(spec, property.setter, arguments));
};
root[toSetter(property.name)] = function (newVal, f) {
return reqAsync(property.setter, getParams(spec, property.setter, arguments), f);
};
}
Object.defineProperty(root, property.name, p);
});
};
var setupMethods = function (root, spec) {
var methods = [
{ name: "balanceAt", async: "getBalanceAt", default: {block: 0} },
{ name: "stateAt", async: "getStateAt", default: {block: 0} },
{ name: "countAt", async: "getCountAt", default: {block: 0} },
{ name: "codeAt", async: "getCodeAt", default: {block: 0} },
{ name: "transact", async: "doTransact"},
{ name: "call", async: "doCall" },
{ name: "messages", async: "getMessages" },
{ name: "block", async: "getBlock" },
{ name: "transaction", async: "getTransaction" },
{ name: "uncle", async: "getUncle" },
{ name: "secretToAddress" },
{ name: "lll" },
{ name: "sha3" },
{ name: "toAscii" },
{ name: "fromAscii" },
{ name: "toDecimal" },
{ name: "toFixed" },
{ name: "fromFixed" },
{ name: "offset" }
];
methods.forEach(function (method) {
root[method.name] = function () {
return reqSync(method.name, defaults(method.default, getParams(spec, method.name, arguments)));
};
if (method.async) {
root[method.async] = function () {
return reqAsync(method.name, defaults(method.default, getParams(spec, method.name, arguments)), arguments[arguments.length - 1]);
};
};
});
};
var setupWatch = function (root) {
root.watch = function (val) {
if (typeof val !== 'string') {
val = JSON.stringify(val);
}
var id;
reqAsync('watch', {params: val}, function (result) {
id = result;
}); // async send watch
var callbacks = [];
var exist = true;
var w = {
changed: function (f) {
callbacks.push(f);
},
uninstall: function (f) {
reqAsync('killWatch', {id: id});
exist = false;
},
messages: function () {
// TODO!
},
getMessages: function (f) {
// TODO!
}
};
var check = function () {
if (!exist) {
return;
}
if (callbacks.length) {
reqAsync('check', {id: id}, function (res) {
if (!res) {
return;
}
callbacks.forEach(function (f) {
f();
});
});
}
window.setTimeout(check, 12000);
};
check();
return w;
}
};
setupProperties(ret, window.spec);
setupMethods(ret, window.spec);
setupWatch(ret);
return ret;
}());
}

62
libethrpc/ethString.js

@ -1,62 +0,0 @@
if (typeof(require) !== "undefined")
require( ['BigInteger'], function() {} )
else if (typeof(bigInt) === "undefined")
alert("You need to have included BigInteger.js for eth to work.")
String.prototype.pad = function(l, r) {
if (r === null) {
r = l
if (!(this.substr(0, 2) == "0x" || /^\d+$/.test(this)))
l = 0
}
var ret = this.bin();
while (ret.length < l)
ret = "\0" + ret
while (ret.length < r)
ret = ret + "\0"
return ret;
}
String.prototype.unpad = function() {
var i = this.length;
while (i && this[i - 1] == "\0")
--i
return this.substr(0, i)
}
String.prototype.bin = function() {
if (this.substr(0, 2) == "0x") {
bytes = []
var i = 2;
// Check if it's odd - pad with a zero if so.
if (this.length % 2)
bytes.push(parseInt(this.substr(i++, 1), 16))
for (; i < this.length - 1; i += 2)
bytes.push(parseInt(this.substr(i, 2), 16));
return String.fromCharCode.apply(String, bytes);
} else if (/^\d+$/.test(this))
return bigInt(this.substr(0)).toHex().bin()
// Otherwise we'll return the "String" object instead of an actual string
return this.substr(0, this.length)
}
String.prototype.unbin = function() {
var i, l, o = '';
for(i = 0, l = this.length; i < l; i++) {
var n = this.charCodeAt(i).toString(16);
o += n.length < 2 ? '0' + n : n;
}
return "0x" + o;
}
String.prototype.dec = function() {
return bigInt(this.substr(0)).toString()
}
String.prototype.hex = function() {
return bigInt(this.substr(0)).toHex()
}
Loading…
Cancel
Save