forked from bitfloor/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
3,293 additions
and
3,694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.