Merge pull request #202 from dcousens/ecneg

ECKey -D test and light cleanup
This commit is contained in:
Wei Lu 2014-06-03 13:38:25 +08:00
commit c76d3cef37
11 changed files with 34 additions and 24 deletions

View file

@ -18,7 +18,7 @@ function findScriptTypeByVersion(queryVersion) {
}
function Address(hash, version) {
assert(Buffer.isBuffer(hash), 'First argument must be a Buffer')
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
assert.strictEqual(hash.length, 20, 'Invalid hash length')
assert.strictEqual(version & 0xFF, version, 'Invalid version byte')

View file

@ -12,7 +12,7 @@ var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii')
var ALPHABET_MAP = {}
for(var i = 0; i < ALPHABET.length; i++) {
ALPHABET_MAP[ALPHABET[i]] = BigInteger.valueOf(i)
ALPHABET_MAP[ALPHABET.charAt(i)] = BigInteger.valueOf(i)
}
var BASE = BigInteger.valueOf(58)
@ -21,7 +21,7 @@ function encode(buffer) {
var result = new Buffer(buffer.length << 1)
var i = result.length - 1
while (bi.compareTo(BigInteger.ZERO) > 0) {
while (bi.signum() > 0) {
var remainder = bi.mod(BASE)
bi = bi.divide(BASE)
@ -43,7 +43,7 @@ function encode(buffer) {
function decode(string) {
if (string.length === 0) return new Buffer(0)
var num = BigInteger.ZERO.clone()
var num = BigInteger.ZERO
for (var i = 0; i < string.length; i++) {
num = num.multiply(BASE)
@ -55,16 +55,16 @@ function decode(string) {
}
// deal with leading zeros
var i = 0
while ((i < string.length) && (string[i] === ALPHABET[0])) {
i++
var j = 0
while ((j < string.length) && (string[j] === ALPHABET[0])) {
j++
}
var buffer = num.toBuffer()
var leadz = new Buffer(i)
leadz.fill(0)
var leadingZeros = new Buffer(j)
leadingZeros.fill(0)
return Buffer.concat([leadz, buffer])
return Buffer.concat([leadingZeros, buffer])
}
module.exports = {

View file

@ -4,13 +4,8 @@ var base58 = require('./base58')
var crypto = require('./crypto')
// Encode a buffer as a base58-check-encoded string
function encode(buffer, version) {
version = version || 0
// FIXME: `new Buffer(buffer)` is unnecessary if input is a Buffer
function encode(payload, version) {
var version = new Buffer([version])
var payload = new Buffer(buffer)
var message = Buffer.concat([version, payload])
var checksum = crypto.hash256(message).slice(0, 4)

View file

@ -45,6 +45,7 @@ function readPushDataInt(buffer, offset) {
}
return {
opcode: opcode,
number: number,
size: size
}

View file

@ -91,18 +91,18 @@ function serializeSig(signature) {
var sBa = signature.s.toDERInteger()
var sequence = []
sequence.push(0x02); // INTEGER
sequence.push(0x02) // INTEGER
sequence.push(rBa.length)
sequence = sequence.concat(rBa)
sequence.push(0x02); // INTEGER
sequence.push(0x02) // INTEGER
sequence.push(sBa.length)
sequence = sequence.concat(sBa)
sequence.unshift(sequence.length)
sequence.unshift(0x30); // SEQUENCE
sequence.unshift(0x30) // SEQUENCE
return sequence
return new Buffer(sequence)
}
/**

View file

@ -11,7 +11,7 @@ var sec = require('./sec')
var ecparams = sec('secp256k1')
function ECKey(D, compressed) {
assert(D.compareTo(BigInteger.ZERO) > 0, 'Private key must be greater than 0')
assert(D.signum() > 0, 'Private key must be greater than 0')
assert(D.compareTo(ecparams.getN()) < 0, 'Private key must be less than the curve order')
var Q = ecparams.getG().multiply(D)

View file

@ -11,7 +11,7 @@ describe('base58check', function() {
it('can decode ' + f.string, function() {
var actual = base58check.decode(f.string)
var expected = {
version: f.decode.version,
version: f.decode.version || 0,
payload: h2b(f.decode.payload),
checksum: h2b(f.decode.checksum)
}
@ -40,4 +40,3 @@ describe('base58check', function() {
})
})
})

View file

@ -23,7 +23,9 @@ describe('bufferutils', function() {
it('decodes ' + f.hexPD + ' correctly', function() {
var buffer = new Buffer(f.hexPD, 'hex')
var d = bufferutils.readPushDataInt(buffer, 0)
var fopcode = parseInt(f.hexPD.substr(0, 2), 16)
assert.equal(d.opcode, fopcode)
assert.equal(d.number, f.dec)
assert.equal(d.size, buffer.length)
})

View file

@ -31,8 +31,10 @@ describe('ECKey', function() {
fixtures.invalid.constructor.forEach(function(f) {
it('throws on ' + f.D, function() {
var D = new BigInteger(f.D)
assert.throws(function() {
new ECKey(new BigInteger(f.D))
new ECKey(D)
}, new RegExp(f.exception))
})
})

View file

@ -1,5 +1,12 @@
{
"valid": [
{
"string": "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"decode": {
"payload": "65a16059864a2fdbc7c99a4723a8395bc6f188eb",
"checksum": "c046b2ff"
}
},
{
"string": "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"decode": {

View file

@ -67,6 +67,10 @@
],
"invalid": {
"constructor": [
{
"exception": "Private key must be greater than 0",
"D": "-1"
},
{
"exception": "Private key must be greater than 0",
"D": "0"