refactor int conversion, encoding, and utils
This commit is contained in:
parent
5505491e8d
commit
9ef8b78182
|
@ -509,7 +509,7 @@ Script.stringToBuffer = function(s) {
|
||||||
if (!isNaN(integer)) {
|
if (!isNaN(integer)) {
|
||||||
// integer
|
// integer
|
||||||
//console.log('integer');
|
//console.log('integer');
|
||||||
var data = util.intToBuffer(integer);
|
var data = util.intToBufferSM(integer);
|
||||||
buf.put(Script.chunksToBuffer([data]));
|
buf.put(Script.chunksToBuffer([data]));
|
||||||
} else if (word[0] === '\'' && word[word.length-1] === '\'') {
|
} else if (word[0] === '\'' && word[word.length-1] === '\'') {
|
||||||
// string
|
// string
|
||||||
|
|
|
@ -18,6 +18,9 @@ for (var i in Opcode.map) {
|
||||||
eval(i + " = " + Opcode.map[i] + ";");
|
eval(i + " = " + Opcode.map[i] + ";");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var intToBufferSM = Util.intToBufferSM
|
||||||
|
var bufferSMToInt = Util.bufferSMToInt;
|
||||||
|
|
||||||
function ScriptInterpreter() {
|
function ScriptInterpreter() {
|
||||||
this.stack = [];
|
this.stack = [];
|
||||||
this.disableUnsafeOpcodes = true;
|
this.disableUnsafeOpcodes = true;
|
||||||
|
@ -120,8 +123,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
case OP_15:
|
case OP_15:
|
||||||
case OP_16:
|
case OP_16:
|
||||||
var opint = opcode - OP_1 + 1;
|
var opint = opcode - OP_1 + 1;
|
||||||
var opbuf = bigintToBuffer(opint);
|
var opbuf = intToBufferSM(opint);
|
||||||
console.log('op'+opcode+' = '+opint+', '+buffertools.toHex(opbuf));
|
|
||||||
this.stack.push(opbuf);
|
this.stack.push(opbuf);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -246,7 +248,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
case OP_DEPTH:
|
case OP_DEPTH:
|
||||||
// -- stacksize
|
// -- stacksize
|
||||||
var value = bignum(this.stack.length);
|
var value = bignum(this.stack.length);
|
||||||
this.stack.push(bigintToBuffer(value));
|
this.stack.push(intToBufferSM(value));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OP_DROP:
|
case OP_DROP:
|
||||||
|
@ -355,7 +357,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
case OP_SIZE:
|
case OP_SIZE:
|
||||||
// (in -- in size)
|
// (in -- in size)
|
||||||
var value = bignum(this.stackTop().length);
|
var value = bignum(this.stackTop().length);
|
||||||
this.stack.push(bigintToBuffer(value));
|
this.stack.push(intToBufferSM(value));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OP_INVERT:
|
case OP_INVERT:
|
||||||
|
@ -397,9 +399,6 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
// (x1 x2 - bool)
|
// (x1 x2 - bool)
|
||||||
var v1 = this.stackTop(2);
|
var v1 = this.stackTop(2);
|
||||||
var v2 = this.stackTop(1);
|
var v2 = this.stackTop(1);
|
||||||
console.log('equal');
|
|
||||||
console.log(v1);
|
|
||||||
console.log(v2);
|
|
||||||
|
|
||||||
var value = buffertools.compare(v1, v2) === 0;
|
var value = buffertools.compare(v1, v2) === 0;
|
||||||
|
|
||||||
|
@ -430,7 +429,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
case OP_NOT:
|
case OP_NOT:
|
||||||
case OP_0NOTEQUAL:
|
case OP_0NOTEQUAL:
|
||||||
// (in -- out)
|
// (in -- out)
|
||||||
var num = castBigint(this.stackTop());
|
var num = bufferSMToInt(this.stackTop());
|
||||||
switch (opcode) {
|
switch (opcode) {
|
||||||
case OP_1ADD:
|
case OP_1ADD:
|
||||||
num = num.add(bignum(1));
|
num = num.add(bignum(1));
|
||||||
|
@ -457,7 +456,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
num = bignum(num.cmp(0) == 0 ? 0 : 1);
|
num = bignum(num.cmp(0) == 0 ? 0 : 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
this.stack[this.stack.length - 1] = bigintToBuffer(num);
|
this.stack[this.stack.length - 1] = intToBufferSM(num);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
|
@ -479,11 +478,8 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
case OP_MIN:
|
case OP_MIN:
|
||||||
case OP_MAX:
|
case OP_MAX:
|
||||||
// (x1 x2 -- out)
|
// (x1 x2 -- out)
|
||||||
var v1 = castBigint(this.stackTop(2));
|
var v1 = bufferSMToInt(this.stackTop(2));
|
||||||
var v2 = castBigint(this.stackTop(1));
|
var v2 = bufferSMToInt(this.stackTop(1));
|
||||||
console.log('add');
|
|
||||||
console.log(v1);
|
|
||||||
console.log(v2);
|
|
||||||
var num;
|
var num;
|
||||||
switch (opcode) {
|
switch (opcode) {
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
|
@ -559,7 +555,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
}
|
}
|
||||||
this.stackPop();
|
this.stackPop();
|
||||||
this.stackPop();
|
this.stackPop();
|
||||||
this.stack.push(bigintToBuffer(num));
|
this.stack.push(intToBufferSM(num));
|
||||||
|
|
||||||
if (opcode === OP_NUMEQUALVERIFY) {
|
if (opcode === OP_NUMEQUALVERIFY) {
|
||||||
if (castBool(this.stackTop())) {
|
if (castBool(this.stackTop())) {
|
||||||
|
@ -572,14 +568,14 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
|
|
||||||
case OP_WITHIN:
|
case OP_WITHIN:
|
||||||
// (x min max -- out)
|
// (x min max -- out)
|
||||||
var v1 = castBigint(this.stackTop(3));
|
var v1 = bufferSMToInt(this.stackTop(3));
|
||||||
var v2 = castBigint(this.stackTop(2));
|
var v2 = bufferSMToInt(this.stackTop(2));
|
||||||
var v3 = castBigint(this.stackTop(1));
|
var v3 = bufferSMToInt(this.stackTop(1));
|
||||||
this.stackPop();
|
this.stackPop();
|
||||||
this.stackPop();
|
this.stackPop();
|
||||||
this.stackPop();
|
this.stackPop();
|
||||||
var value = v1.cmp(v2) >= 0 && v1.cmp(v3) < 0;
|
var value = v1.cmp(v2) >= 0 && v1.cmp(v3) < 0;
|
||||||
this.stack.push(bigintToBuffer(value ? 1 : 0));
|
this.stack.push(intToBufferSM(value ? 1 : 0));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OP_RIPEMD160:
|
case OP_RIPEMD160:
|
||||||
|
@ -759,7 +755,6 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
console.log('opcode '+opcode);
|
|
||||||
throw new Error("Unknown opcode encountered");
|
throw new Error("Unknown opcode encountered");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -854,7 +849,7 @@ ScriptInterpreter.prototype.getPrimitiveStack = function getPrimitiveStack() {
|
||||||
if (entry.length > 2) {
|
if (entry.length > 2) {
|
||||||
return buffertools.toHex(entry.slice(0));
|
return buffertools.toHex(entry.slice(0));
|
||||||
}
|
}
|
||||||
var num = castBigint(entry);
|
var num = bufferSMToInt(entry);
|
||||||
if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) {
|
if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) {
|
||||||
return num.toNumber();
|
return num.toNumber();
|
||||||
} else {
|
} else {
|
||||||
|
@ -876,69 +871,7 @@ var castBool = ScriptInterpreter.castBool = function castBool(v) {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
var castInt = ScriptInterpreter.castInt = function castInt(v) {
|
var castInt = ScriptInterpreter.castInt = function castInt(v) {
|
||||||
return castBigint(v).toNumber();
|
return bufferSMToInt(v).toNumber();
|
||||||
};
|
|
||||||
var castBigint = ScriptInterpreter.castBigint = function castBigint(v) {
|
|
||||||
if (!v.length) {
|
|
||||||
return bignum(0);
|
|
||||||
}
|
|
||||||
// Arithmetic operands must be in range [-2^31...2^31]
|
|
||||||
if (v.length > 4) {
|
|
||||||
throw new Error("Bigint cast overflow (> 4 bytes)");
|
|
||||||
}
|
|
||||||
|
|
||||||
var w = new Buffer(v.length);
|
|
||||||
v.copy(w);
|
|
||||||
w = buffertools.reverse(w);
|
|
||||||
console.log('v ='+buffertools.toHex(w));
|
|
||||||
var isNeg = w[0] & 0x80;
|
|
||||||
if (isNeg) {
|
|
||||||
for (var i = 0; i<w.length; i++) {
|
|
||||||
console.log('before = '+w[i]);
|
|
||||||
w[i] = ~w[i];
|
|
||||||
console.log('after = '+w[i]);
|
|
||||||
}
|
|
||||||
console.log('w ='+buffertools.toHex(w));
|
|
||||||
return bignum.fromBuffer(w).add(bignum(1)).neg();
|
|
||||||
} else {
|
|
||||||
return bignum.fromBuffer(w);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var padSign = function(b) {
|
|
||||||
var c;
|
|
||||||
if (b[0] & 0x80) {
|
|
||||||
c = new Buffer(b.length + 1);
|
|
||||||
b.copy(c, 1);
|
|
||||||
c[0] = 0;
|
|
||||||
} else {
|
|
||||||
c = b;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
var bigintToBuffer = ScriptInterpreter.bigintToBuffer = function bigintToBuffer(v) {
|
|
||||||
if ("number" === typeof v) {
|
|
||||||
v = bignum(v);
|
|
||||||
}
|
|
||||||
var b, c;
|
|
||||||
var cmp = v.cmp(0);
|
|
||||||
if (cmp > 0) {
|
|
||||||
console.log('positive');
|
|
||||||
b = v.toBuffer();
|
|
||||||
c = padSign(b);
|
|
||||||
c = buffertools.reverse(c);
|
|
||||||
} else if (cmp == 0) {
|
|
||||||
c = new Buffer([]);
|
|
||||||
} else {
|
|
||||||
console.log('negative '+v);
|
|
||||||
b = v.neg().toBuffer();
|
|
||||||
console.log(b);
|
|
||||||
c = padSign(b);
|
|
||||||
c = Util.negativeBuffer(c);
|
|
||||||
c = buffertools.reverse(c);
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ScriptInterpreter.prototype.getResult = function getResult() {
|
ScriptInterpreter.prototype.getResult = function getResult() {
|
||||||
|
|
|
@ -23,28 +23,12 @@ describe('ScriptInterpreter', function() {
|
||||||
var si = new ScriptInterpreter();
|
var si = new ScriptInterpreter();
|
||||||
should.exist(si);
|
should.exist(si);
|
||||||
});
|
});
|
||||||
var data = [
|
|
||||||
[0, ''],
|
|
||||||
[1, '01'],
|
|
||||||
[-1, 'ff'],
|
|
||||||
];
|
|
||||||
data.forEach(function(datum) {
|
|
||||||
var i = datum[0];
|
|
||||||
var hex = datum[1];
|
|
||||||
it('bigintToBuffer should work for ' + i, function() {
|
|
||||||
var result = ScriptInterpreter.bigintToBuffer(i);
|
|
||||||
buffertools.toHex(result).should.equal(hex);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
var i = 0;
|
|
||||||
testdata.dataScriptValid.forEach(function(datum) {
|
testdata.dataScriptValid.forEach(function(datum) {
|
||||||
if (datum.length < 2) throw new Error('Invalid test data');
|
if (datum.length < 2) throw new Error('Invalid test data');
|
||||||
var scriptSig = datum[0]; // script inputs
|
var scriptSig = datum[0]; // script inputs
|
||||||
var scriptPubKey = datum[1]; // output script
|
var scriptPubKey = datum[1]; // output script
|
||||||
var human = scriptSig + ' ' + scriptPubKey;
|
var human = scriptSig + ' ' + scriptPubKey;
|
||||||
it('should validate script ' + human, function(done) {
|
it('should validate script ' + human, function(done) {
|
||||||
i++;
|
|
||||||
console.log(i + ' ' + human);
|
|
||||||
ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig),
|
ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig),
|
||||||
Script.fromHumanReadable(scriptPubKey),
|
Script.fromHumanReadable(scriptPubKey),
|
||||||
null, 0, 0, // tx, output index, and hashtype
|
null, 0, 0, // tx, output index, and hashtype
|
||||||
|
@ -56,8 +40,6 @@ describe('ScriptInterpreter', function() {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var i = 0;
|
|
||||||
testdata.dataSigCanonical.forEach(function(datum) {
|
testdata.dataSigCanonical.forEach(function(datum) {
|
||||||
it('should validate valid canonical signatures', function() {
|
it('should validate valid canonical signatures', function() {
|
||||||
ScriptInterpreter.isCanonicalSignature(new Buffer(datum,'hex')).should.equal(true);
|
ScriptInterpreter.isCanonicalSignature(new Buffer(datum,'hex')).should.equal(true);
|
||||||
|
@ -65,7 +47,6 @@ describe('ScriptInterpreter', function() {
|
||||||
});
|
});
|
||||||
testdata.dataSigNonCanonical.forEach(function(datum) {
|
testdata.dataSigNonCanonical.forEach(function(datum) {
|
||||||
it('should NOT validate invalid canonical signatures', function() {
|
it('should NOT validate invalid canonical signatures', function() {
|
||||||
|
|
||||||
var sig;
|
var sig;
|
||||||
var isHex;
|
var isHex;
|
||||||
//is Hex?
|
//is Hex?
|
||||||
|
|
|
@ -15,8 +15,8 @@ var examples = [
|
||||||
];
|
];
|
||||||
|
|
||||||
describe('Examples', function() {
|
describe('Examples', function() {
|
||||||
//before(mute);
|
before(mute);
|
||||||
//after(unmute);
|
after(unmute);
|
||||||
examples.forEach(function(example) {
|
examples.forEach(function(example) {
|
||||||
it('valid '+example, function() {
|
it('valid '+example, function() {
|
||||||
var ex = require('../examples/'+example);
|
var ex = require('../examples/'+example);
|
||||||
|
|
|
@ -39,12 +39,12 @@ describe('util', function() {
|
||||||
describe('#ripe160', function() {
|
describe('#ripe160', function() {
|
||||||
var pk = 'a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d';
|
var pk = 'a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d';
|
||||||
it('should work for ' + pk, function() {
|
it('should work for ' + pk, function() {
|
||||||
var pubKeyHash = coinUtil.ripe160(new Buffer(pk,'hex'));
|
var pubKeyHash = coinUtil.ripe160(new Buffer(pk, 'hex'));
|
||||||
var pkh = buffertools.toHex(pubKeyHash);
|
var pkh = buffertools.toHex(pubKeyHash);
|
||||||
pkh.should.equal('d166a41f27fd4b158f70314e5eee8998bf3d97d5');
|
pkh.should.equal('d166a41f27fd4b158f70314e5eee8998bf3d97d5');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('#sha256', function() {
|
describe('#sha256', function() {
|
||||||
var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071'
|
var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071'
|
||||||
|
@ -54,7 +54,7 @@ describe('util', function() {
|
||||||
pkh.should.equal('a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d');
|
pkh.should.equal('a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#sha256ripe160', function() {
|
describe('#sha256ripe160', function() {
|
||||||
var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071'
|
var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071'
|
||||||
it('should work for ' + pk, function() {
|
it('should work for ' + pk, function() {
|
||||||
|
@ -71,7 +71,7 @@ describe('util', function() {
|
||||||
];
|
];
|
||||||
ripemdData.forEach(function(datum) {
|
ripemdData.forEach(function(datum) {
|
||||||
it('should work for ' + datum[0], function() {
|
it('should work for ' + datum[0], function() {
|
||||||
var r = coinUtil.ripe160( new bitcore.Buffer(datum[0]));
|
var r = coinUtil.ripe160(new bitcore.Buffer(datum[0]));
|
||||||
buffertools.toHex(r).should.equal(datum[1]);
|
buffertools.toHex(r).should.equal(datum[1]);
|
||||||
});
|
});
|
||||||
it('should work for Buffer ' + datum[0], function() {
|
it('should work for Buffer ' + datum[0], function() {
|
||||||
|
@ -80,7 +80,7 @@ describe('util', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#intToBuffer', function() {
|
describe('#intToBuffer2C', function() {
|
||||||
var data = [
|
var data = [
|
||||||
[0, ''],
|
[0, ''],
|
||||||
[-0, ''],
|
[-0, ''],
|
||||||
|
@ -109,13 +109,13 @@ describe('util', function() {
|
||||||
var integer = datum[0];
|
var integer = datum[0];
|
||||||
var result = datum[1];
|
var result = datum[1];
|
||||||
it('should work for ' + integer, function() {
|
it('should work for ' + integer, function() {
|
||||||
buffertools.toHex(coinUtil.intToBuffer(integer)).should.equal(result);
|
buffertools.toHex(coinUtil.intToBuffer2C(integer)).should.equal(result);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#varIntBuf', function() {
|
describe('#varIntBuf', function() {
|
||||||
var data = [
|
var data = [
|
||||||
[0, '00' ],
|
[0, '00'],
|
||||||
[1, '01'],
|
[1, '01'],
|
||||||
[253, 'fdfd00'],
|
[253, 'fdfd00'],
|
||||||
[254, 'fdfe00'],
|
[254, 'fdfe00'],
|
||||||
|
@ -161,4 +161,22 @@ describe('util', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('#intToBufferSM', function() {
|
||||||
|
var data = [
|
||||||
|
[0, ''],
|
||||||
|
[1, '01'],
|
||||||
|
[-1, '81'],
|
||||||
|
[2, '02'],
|
||||||
|
[-2, '82'],
|
||||||
|
[-32768, '008080'],
|
||||||
|
];
|
||||||
|
data.forEach(function(datum) {
|
||||||
|
var i = datum[0];
|
||||||
|
var hex = datum[1];
|
||||||
|
it('should work for ' + i, function() {
|
||||||
|
var result = coinUtil.intToBufferSM(i);
|
||||||
|
buffertools.toHex(result).should.equal(hex);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
78
util/util.js
78
util/util.js
|
@ -133,11 +133,16 @@ exports.negativeBuffer = negativeBuffer = function(b) {
|
||||||
c[i] += 1;
|
c[i] += 1;
|
||||||
if (c[i] !== 0) break;
|
if (c[i] !== 0) break;
|
||||||
}
|
}
|
||||||
console.log('negative of '+buffertools.toHex(b)+' is '+buffertools.toHex(c));
|
|
||||||
return c;
|
return c;
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.intToBuffer = function(integer) {
|
/*
|
||||||
|
* Transforms an integer into a buffer using two-complement encoding
|
||||||
|
* For example, 1 is encoded as 01 and -1 is encoded as ff
|
||||||
|
* For more info see:
|
||||||
|
* http://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement
|
||||||
|
*/
|
||||||
|
exports.intToBuffer2C = function(integer) {
|
||||||
var size = bytesNeededToStore(integer);
|
var size = bytesNeededToStore(integer);
|
||||||
var buf = new Put();
|
var buf = new Put();
|
||||||
var s = integer.toString(16);
|
var s = integer.toString(16);
|
||||||
|
@ -159,6 +164,73 @@ exports.intToBuffer = function(integer) {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var padSign = function(b) {
|
||||||
|
var c;
|
||||||
|
if (b[0] & 0x80) {
|
||||||
|
c = new Buffer(b.length + 1);
|
||||||
|
b.copy(c, 1);
|
||||||
|
c[0] = 0;
|
||||||
|
} else {
|
||||||
|
c = b;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Transforms an integer into a buffer using sign+magnitude encoding
|
||||||
|
* For example, 1 is encoded as 01 and -1 is encoded as 81
|
||||||
|
* For more info see:
|
||||||
|
* http://en.wikipedia.org/wiki/Signed_number_representations#Signed_magnitude_representation
|
||||||
|
*/
|
||||||
|
exports.intToBufferSM = function(v) {
|
||||||
|
if ("number" === typeof v) {
|
||||||
|
v = bignum(v);
|
||||||
|
}
|
||||||
|
var b, c;
|
||||||
|
var cmp = v.cmp(0);
|
||||||
|
if (cmp > 0) {
|
||||||
|
b = v.toBuffer();
|
||||||
|
c = padSign(b);
|
||||||
|
c = buffertools.reverse(c);
|
||||||
|
} else if (cmp == 0) {
|
||||||
|
c = new Buffer([]);
|
||||||
|
} else {
|
||||||
|
b = v.neg().toBuffer();
|
||||||
|
c = padSign(b);
|
||||||
|
c[0] |= 0x80;
|
||||||
|
c = buffertools.reverse(c);
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reverse of intToBufferSM
|
||||||
|
*/
|
||||||
|
exports.bufferSMToInt = function(v) {
|
||||||
|
if (!v.length) {
|
||||||
|
return bignum(0);
|
||||||
|
}
|
||||||
|
// Arithmetic operands must be in range [-2^31...2^31]
|
||||||
|
if (v.length > 4) {
|
||||||
|
throw new Error('Bigint cast overflow (> 4 bytes)');
|
||||||
|
}
|
||||||
|
|
||||||
|
var w = new Buffer(v.length);
|
||||||
|
v.copy(w);
|
||||||
|
w = buffertools.reverse(w);
|
||||||
|
var isNeg = w[0] & 0x80;
|
||||||
|
if (isNeg) {
|
||||||
|
w[0] &= 0x7f;
|
||||||
|
return bignum.fromBuffer(w).neg();
|
||||||
|
} else {
|
||||||
|
return bignum.fromBuffer(w);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var formatValue = exports.formatValue = function (valueBuffer) {
|
var formatValue = exports.formatValue = function (valueBuffer) {
|
||||||
var value = valueToBigInt(valueBuffer).toString();
|
var value = valueToBigInt(valueBuffer).toString();
|
||||||
var integerPart = value.length > 8 ? value.substr(0, value.length-8) : '0';
|
var integerPart = value.length > 8 ? value.substr(0, value.length-8) : '0';
|
||||||
|
|
Loading…
Reference in New Issue