commit
6596ca1ec8
7 changed files with 7 additions and 120 deletions
|
@ -73,6 +73,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"bigi": "1.1.0",
|
||||
"bs58": "1.1.0",
|
||||
"crypto-js": "3.1.2-3",
|
||||
"crypto-browserify": "2.1.8",
|
||||
"ecurve": "0.10.0",
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
// Base58 encoding/decoding
|
||||
// Originally written by Mike Hearn for BitcoinJ
|
||||
// Copyright (c) 2011 Google Inc
|
||||
// Ported to JavaScript by Stefan Thomas
|
||||
// Merged Buffer refactorings from base58-native by Stephen Pair
|
||||
// Copyright (c) 2013 BitPay Inc
|
||||
|
||||
var assert = require('assert')
|
||||
var BigInteger = require('bigi')
|
||||
|
||||
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
||||
var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii')
|
||||
var ALPHABET_MAP = {}
|
||||
for(var i = 0; i < ALPHABET.length; i++) {
|
||||
ALPHABET_MAP[ALPHABET.charAt(i)] = BigInteger.valueOf(i)
|
||||
}
|
||||
var BASE = BigInteger.valueOf(58)
|
||||
|
||||
function encode(buffer) {
|
||||
var bi = BigInteger.fromBuffer(buffer)
|
||||
var result = new Buffer(buffer.length << 1)
|
||||
|
||||
var i = result.length - 1
|
||||
while (bi.signum() > 0) {
|
||||
var remainder = bi.mod(BASE)
|
||||
bi = bi.divide(BASE)
|
||||
|
||||
result[i] = ALPHABET_BUF[remainder.intValue()]
|
||||
i--
|
||||
}
|
||||
|
||||
// deal with leading zeros
|
||||
var j = 0
|
||||
while (buffer[j] === 0) {
|
||||
result[i] = ALPHABET_BUF[0]
|
||||
j++
|
||||
i--
|
||||
}
|
||||
|
||||
return result.slice(i + 1, result.length).toString('ascii')
|
||||
}
|
||||
|
||||
function decode(string) {
|
||||
if (string.length === 0) return new Buffer(0)
|
||||
|
||||
var num = BigInteger.ZERO
|
||||
|
||||
for (var i = 0; i < string.length; i++) {
|
||||
num = num.multiply(BASE)
|
||||
|
||||
var figure = ALPHABET_MAP[string.charAt(i)]
|
||||
assert.notEqual(figure, undefined, 'Non-base58 character')
|
||||
|
||||
num = num.add(figure)
|
||||
}
|
||||
|
||||
// deal with leading zeros
|
||||
var j = 0
|
||||
while ((j < string.length) && (string[j] === ALPHABET[0])) {
|
||||
j++
|
||||
}
|
||||
|
||||
var buffer = num.toBuffer()
|
||||
var leadingZeros = new Buffer(j)
|
||||
leadingZeros.fill(0)
|
||||
|
||||
return Buffer.concat([leadingZeros, buffer])
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
encode: encode,
|
||||
decode: decode
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
// https://en.bitcoin.it/wiki/Base58Check_encoding
|
||||
var assert = require('assert')
|
||||
var base58 = require('./base58')
|
||||
var base58 = require('bs58')
|
||||
var crypto = require('./crypto')
|
||||
|
||||
// Encode a buffer as a base58-check-encoded string
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
module.exports = {
|
||||
Address: require('./address'),
|
||||
base58: require('./base58'),
|
||||
base58check: require('./base58check'),
|
||||
bufferutils: require('./bufferutils'),
|
||||
convert: require('./convert'),
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
var assert = require('assert')
|
||||
var base58 = require('../src/base58')
|
||||
|
||||
var fixtures = require('./fixtures/base58.json')
|
||||
|
||||
describe('base58', function() {
|
||||
describe('decode', function() {
|
||||
it('can decode Bitcoin core test data', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
var actual = base58.decode(f.string)
|
||||
var expected = f.hex
|
||||
|
||||
assert.strictEqual(actual.toString('hex'), expected)
|
||||
})
|
||||
})
|
||||
|
||||
fixtures.invalid.forEach(function(f) {
|
||||
it('throws on ' + f.description, function() {
|
||||
assert.throws(function() {
|
||||
base58.decode(f.string)
|
||||
}, /Non-base58 character/)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('encode', function() {
|
||||
it('can encode Bitcoin core test data', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
var actual = base58.encode(new Buffer(f.hex, 'hex'))
|
||||
var expected = f.string.trim()
|
||||
|
||||
assert.strictEqual(actual, expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
|
@ -3,16 +3,13 @@ var base58check = require('../src/base58check')
|
|||
|
||||
var fixtures = require('./fixtures/base58check.json')
|
||||
|
||||
function h2b(h) { return new Buffer(h, 'hex') }
|
||||
|
||||
describe('base58check', function() {
|
||||
describe('decode', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
it('can decode ' + f.string, function() {
|
||||
var actual = base58check.decode(f.string)
|
||||
var expected = h2b(f.payload)
|
||||
var actual = base58check.decode(f.string).toString('hex')
|
||||
|
||||
assert.deepEqual(actual, expected)
|
||||
assert.equal(actual, f.payload)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -28,10 +25,9 @@ describe('base58check', function() {
|
|||
describe('encode', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
it('can encode ' + f.string, function() {
|
||||
var actual = base58check.encode(h2b(f.payload))
|
||||
var expected = f.string
|
||||
var actual = base58check.encode(new Buffer(f.payload, 'hex'))
|
||||
|
||||
assert.strictEqual(actual, expected)
|
||||
assert.strictEqual(actual, f.string)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
var assert = require('assert')
|
||||
var base58 = require('../src/base58')
|
||||
var base58 = require('bs58')
|
||||
var base58check = require('../src/base58check')
|
||||
var networks = require('../src/networks')
|
||||
|
||||
|
|
Loading…
Reference in a new issue