Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
18 changes: 9 additions & 9 deletions src/base58.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our use of BigInteger is non-mutating in this instance, so there is no reason in taking a clone initially. (see num = num.multiply(BASE).


for (var i = 0; i < string.length; i++) {
num = num.multiply(BASE)
Expand All @@ -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 = {
Expand Down
7 changes: 1 addition & 6 deletions src/base58check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions src/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function readPushDataInt(buffer, offset) {
}

return {
opcode: opcode,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be necessary for parsing Script from binary data. Note it isn't used yet because all that code is currently in-lined inside Script.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not adding it only when it's started being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in this case, it was more about completing the API.

number: number,
size: size
}
Expand Down
8 changes: 4 additions & 4 deletions src/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/eckey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions test/base58check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If version is undefined, we expect 0, this behaviour is implied here.

payload: h2b(f.decode.payload),
checksum: h2b(f.decode.checksum)
}
Expand Down Expand Up @@ -40,4 +40,3 @@ describe('base58check', function() {
})
})
})

2 changes: 2 additions & 0 deletions test/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
4 changes: 3 additions & 1 deletion test/eckey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/base58check.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"valid": [
{
"string": "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"decode": {
"payload": "65a16059864a2fdbc7c99a4723a8395bc6f188eb",
"checksum": "c046b2ff"
}
},
{
"string": "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
"decode": {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/eckey.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down