Merge pull request #96 from ralphtheninja/master

cleaned up address.js and util.js
This commit is contained in:
Wei Lu 2014-03-28 10:01:26 +08:00
commit 0ce14a7c84
3 changed files with 71 additions and 53 deletions

View file

@ -1,48 +1,62 @@
var base58 = require('./base58'); var base58 = require('./base58')
var convert = require('./convert'); var convert = require('./convert')
var util = require('./util'); var error = require('./util').error
var mainnet = require('./network').mainnet.addressVersion; var mainnet = require('./network').mainnet.addressVersion
var Address = function (bytes, version) { function Address(bytes, version) {
if (!(this instanceof Address)) { return new Address(bytes, version); } if (!(this instanceof Address))
if (arguments[0] instanceof Address) { return new Address(bytes, version)
this.hash = arguments[0].hash;
this.version = arguments[0].version;
}
else if (typeof bytes === 'string') {
this.hash =
bytes.length <= 35 ? base58.checkDecode(bytes)
: bytes.length <= 40 ? convert.hexToBytes(bytes)
: util.error('invalid or unrecognized input');
this.version = version || this.hash.version || mainnet; if (bytes instanceof Address) {
} this.hash = bytes.hash
else { this.version = bytes.version
this.hash = bytes; }
this.version = version || mainnet; else if (typeof bytes === 'string') {
} this.hash = stringToHash(bytes)
}; this.version = version || this.hash.version || mainnet
}
else {
this.hash = bytes
this.version = version || mainnet
}
}
function stringToHash(str) {
if (str.length <= 35) {
return base58.checkDecode(str)
}
if (str.length <= 40) {
return convert.hexToBytes(str)
}
error('invalid or unrecognized input')
}
/** /**
* Serialize this object as a standard Bitcoin address. * Serialize this object as a standard Bitcoin address.
*
* Returns the address as a base58-encoded string in the standardized format. * Returns the address as a base58-encoded string in the standardized format.
*/ */
Address.prototype.toString = function () { Address.prototype.toString = function () {
return base58.checkEncode(this.hash.slice(0), this.version); return base58.checkEncode(this.hash.slice(0), this.version)
};
Address.getVersion = function(string) {
return base58.decode(string)[0];
} }
Address.validate = function(string) { /**
try { * Returns the version of an address, e.g. if the address belongs to the main
base58.checkDecode(string); * net or the test net.
return true; */
} catch (e) { Address.getVersion = function (address) {
return false; return base58.decode(address)[0]
} }
};
module.exports = Address; /**
* Returns true if a bitcoin address is a valid address, otherwise false.
*/
Address.validate = function (address) {
try {
base58.checkDecode(address)
return true
} catch (e) {
return false
}
}
module.exports = Address

View file

@ -5,6 +5,7 @@ var format = require('util').format
var util = require('./util.js') var util = require('./util.js')
var Crypto = require('crypto-js'); var Crypto = require('crypto-js');
var HmacSHA512 = Crypto.HmacSHA512 var HmacSHA512 = Crypto.HmacSHA512
var HMAC= Crypto.algo.HMAC
var ECKey = require('./eckey.js').ECKey var ECKey = require('./eckey.js').ECKey
var ECPubKey = require('./eckey.js').ECPubKey var ECPubKey = require('./eckey.js').ECPubKey
var Address = require('./address.js') var Address = require('./address.js')
@ -198,12 +199,12 @@ HDWallet.prototype.derive = function(i) {
// If 1, private derivation is used: // If 1, private derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:] // let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:]
var kPar = this.priv.toBytes().slice(0, 32) var kPar = this.priv.toBytes().slice(0, 32)
I = util.HmacFromBytesToBytes(SHA512, [0].concat(kPar, iBytes), cPar) I = HmacFromBytesToBytes(SHA512, [0].concat(kPar, iBytes), cPar)
} else { } else {
// If 0, public derivation is used: // If 0, public derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i) // let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i)
var KPar = this.pub.toBytes(true) var KPar = this.pub.toBytes(true)
I = util.HmacFromBytesToBytes(SHA512, KPar.concat(iBytes), cPar) I = HmacFromBytesToBytes(SHA512, KPar.concat(iBytes), cPar)
} }
// Split I = IL || IR into two 32-byte sequences, IL and IR. // Split I = IL || IR into two 32-byte sequences, IL and IR.
@ -243,3 +244,9 @@ HDWallet.prototype.getKeyVersion = function() {
HDWallet.prototype.toString = HDWallet.prototype.toBase58 HDWallet.prototype.toString = HDWallet.prototype.toBase58
function HmacFromBytesToBytes(hasher, message, key) {
var hmac = HMAC.create(hasher, convert.bytesToWordArray(key))
hmac.update(convert.bytesToWordArray(message))
return convert.wordArrayToBytes(hmac.finalize())
}

View file

@ -1,8 +1,7 @@
var convert = require('./convert.js') var convert = require('./convert.js')
var Crypto = require('crypto-js'); var Crypto = require('crypto-js')
var RIPEMD160 = Crypto.RIPEMD160; var RIPEMD160 = Crypto.RIPEMD160
var SHA256 = Crypto.SHA256; var SHA256 = Crypto.SHA256
var HMAC= Crypto.algo.HMAC;
/** /**
* Calculate RIPEMD160(SHA256(data)). * Calculate RIPEMD160(SHA256(data)).
@ -11,16 +10,14 @@ var HMAC= Crypto.algo.HMAC;
* array. * array.
*/ */
exports.sha256ripe160 = function (data) { exports.sha256ripe160 = function (data) {
var wordArray = RIPEMD160(SHA256(convert.bytesToWordArray(data))) var wordArray = RIPEMD160(SHA256(convert.bytesToWordArray(data)))
return convert.wordArrayToBytes(wordArray) return convert.wordArrayToBytes(wordArray)
} }
exports.HmacFromBytesToBytes = function (hasher, message, key) { /**
var hmac = HMAC.create(hasher, convert.bytesToWordArray(key)) * Convenience method for throw new Error('some message'), e.g.
hmac.update(convert.bytesToWordArray(message)) * error('something went wrong')
return convert.wordArrayToBytes(hmac.finalize()) */
} exports.error = function (msg) {
throw new Error(msg)
exports.error = function(msg) {
throw new Error(msg);
} }