Skip to content

Commit

Permalink
Big commit - to split
Browse files Browse the repository at this point in the history
  • Loading branch information
devrandom committed Dec 30, 2013
1 parent eec4521 commit d76182e
Show file tree
Hide file tree
Showing 33 changed files with 3,293 additions and 3,694 deletions.
68 changes: 12 additions & 56 deletions src/address.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
var base58 = require('./base58');
var Crypto = require('./crypto-js/crypto');
var conv = require('./convert');

var address_types = {
prod: 0,
testnet: 111
};

var p2sh_types = {
prod: 5,
testnet: 196
};

var Address = function (bytes) {
if (typeof bytes === 'string') {
bytes = Address.decodeString(bytes);
Bitcoin.Address = function (bytes) {
if ("string" == typeof bytes) {
bytes = Bitcoin.Address.decodeString(bytes);
}
this.hash = bytes;

Expand All @@ -26,7 +12,7 @@ var Address = function (bytes) {
*
* Returns the address as a base58-encoded string in the standardized format.
*/
Address.prototype.toString = function () {
Bitcoin.Address.prototype.toString = function () {
// Get a copy of the hash
var hash = this.hash.slice(0);

Expand All @@ -37,46 +23,18 @@ Address.prototype.toString = function () {

var bytes = hash.concat(checksum.slice(0,4));

return base58.encode(bytes);
};

Address.prototype.getHashBase64 = function () {
return conv.bytesToBase64(this.hash);
return Bitcoin.Base58.encode(bytes);
};

// TODO(shtylman) isValid?
Address.validate = function(string, type) {
try {
var bytes = base58.decode(string);
} catch (e) {
return false;
}

var hash = bytes.slice(0, 21);

var checksum = Crypto.SHA256(Crypto.SHA256(hash, {asBytes: true}), {asBytes: true});

if (checksum[0] != bytes[21] ||
checksum[1] != bytes[22] ||
checksum[2] != bytes[23] ||
checksum[3] != bytes[24]) {
return false;
}

var version = hash[0];

if (type && version !== address_types[type] && version !== p2sh_types[type]) {
return false;
}

return true;
Bitcoin.Address.prototype.getHashBase64 = function () {
return Crypto.util.bytesToBase64(this.hash);
};

/**
* Parse a Bitcoin address contained in a string.
*/
Address.decodeString = function (string) {
var bytes = base58.decode(string);
Bitcoin.Address.decodeString = function (string) {
var bytes = Bitcoin.Base58.decode(string);

var hash = bytes.slice(0, 21);

Expand All @@ -86,16 +44,14 @@ Address.decodeString = function (string) {
checksum[1] != bytes[22] ||
checksum[2] != bytes[23] ||
checksum[3] != bytes[24]) {
throw new Error('Address Checksum validation failed: ' + string);
throw "Checksum validation failed!";
}

var version = hash.shift();
// TODO(shtylman) allow for specific version decoding same as validate above

if (version != 0) {
throw new Error('Address version not supported: ' + string);
throw "Version "+version+" not supported!";
}

return hash;
};

module.exports = Address;
131 changes: 62 additions & 69 deletions src/base58.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,71 @@
(function (Bitcoin) {
Bitcoin.Base58 = {
alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
validRegex: /^[1-9A-HJ-NP-Za-km-z]+$/,
base: BigInteger.valueOf(58),

/**
* Convert a byte array to a base58-encoded string.
*
* Written by Mike Hearn for BitcoinJ.
* Copyright (c) 2011 Google Inc.
*
* Ported to JavaScript by Stefan Thomas.
*/
encode: function (input) {
var bi = BigInteger.fromByteArrayUnsigned(input);
var chars = [];

while (bi.compareTo(B58.base) >= 0) {
var mod = bi.mod(B58.base);
chars.unshift(B58.alphabet[mod.intValue()]);
bi = bi.subtract(mod).divide(B58.base);
}
chars.unshift(B58.alphabet[bi.intValue()]);

// https://en.bitcoin.it/wiki/Base58Check_encoding

var BigInteger = require('./jsbn/jsbn');

var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var base = BigInteger.valueOf(58);

var positions = {};
for (var i=0 ; i < alphabet.length ; ++i) {
positions[alphabet[i]] = i;
}

// Convert a byte array to a base58-encoded string.
// Written by Mike Hearn for BitcoinJ.
// Copyright (c) 2011 Google Inc.
// Ported to JavaScript by Stefan Thomas.
module.exports.encode = function (input) {
var bi = BigInteger.fromByteArrayUnsigned(input);
var chars = [];

while (bi.compareTo(base) >= 0) {
var mod = bi.mod(base);
chars.push(alphabet[mod.intValue()]);
bi = bi.subtract(mod).divide(base);
}
chars.push(alphabet[bi.intValue()]);

// Convert leading zeros too.
for (var i = 0; i < input.length; i++) {
// Convert leading zeros too.
for (var i = 0; i < input.length; i++) {
if (input[i] == 0x00) {
chars.push(alphabet[0]);
chars.unshift(B58.alphabet[0]);
} else break;
}

return chars.reverse().join('');
},

// decode a base58 string into a byte array
// input should be a base58 encoded string
// @return Array
module.exports.decode = function (input) {

var base = BigInteger.valueOf(58);

var length = input.length;
var num = BigInteger.valueOf(0);
var leading_zero = 0;
var seen_other = false;
for (var i=0; i<length ; ++i) {
var char = input[i];
var p = positions[char];

// if we encounter an invalid character, decoding fails
if (p === undefined) {
throw new Error('invalid base58 string: ' + input);
}

num = num.multiply(base).add(BigInteger.valueOf(p));

if (char == '1' && !seen_other) {
++leading_zero;
}
else {
seen_other = true;
return chars.join('');
},

/**
* Convert a base58-encoded string to a byte array.
*
* Written by Mike Hearn for BitcoinJ.
* Copyright (c) 2011 Google Inc.
*
* Ported to JavaScript by Stefan Thomas.
*/
decode: function (input) {
var bi = BigInteger.valueOf(0);
var leadingZerosNum = 0;
for (var i = input.length - 1; i >= 0; i--) {
var alphaIndex = B58.alphabet.indexOf(input[i]);
if (alphaIndex < 0) {
throw "Invalid character";
}
bi = bi.add(BigInteger.valueOf(alphaIndex)
.multiply(B58.base.pow(input.length - 1 -i)));

// This counts leading zero bytes
if (input[i] == "1") leadingZerosNum++;
else leadingZerosNum = 0;
}
}
var bytes = bi.toByteArrayUnsigned();

var bytes = num.toByteArrayUnsigned();
// Add leading zeros
while (leadingZerosNum-- > 0) bytes.unshift(0);

// remove leading zeros
while (leading_zero-- > 0) {
bytes.unshift(0);
}

return bytes;
}
return bytes;
}
};

var B58 = Bitcoin.Base58;
})(
'undefined' != typeof Bitcoin ? Bitcoin : module.exports
);
10 changes: 10 additions & 0 deletions src/bitcoin.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
(function (exports) {
var Bitcoin = exports;

if ('object' !== typeof module) {
Bitcoin.EventEmitter = EventEmitter;
}
})(
'object' === typeof module ? module.exports : (window.Bitcoin = {})
);

/*
function makeKeypair()
{
Expand Down
59 changes: 0 additions & 59 deletions src/convert.js

This file was deleted.

Loading

0 comments on commit d76182e

Please sign in to comment.