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 convert = require('./convert');
var util = require('./util');
var mainnet = require('./network').mainnet.addressVersion;
var base58 = require('./base58')
var convert = require('./convert')
var error = require('./util').error
var mainnet = require('./network').mainnet.addressVersion
var Address = function (bytes, version) {
if (!(this instanceof Address)) { return new Address(bytes, version); }
if (arguments[0] instanceof Address) {
this.hash = arguments[0].hash;
this.version = arguments[0].version;
function Address(bytes, version) {
if (!(this instanceof Address))
return new Address(bytes, version)
if (bytes instanceof Address) {
this.hash = bytes.hash
this.version = bytes.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;
this.hash = stringToHash(bytes)
this.version = version || this.hash.version || mainnet
}
else {
this.hash = bytes;
this.version = version || mainnet;
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.
*
* Returns the address as a base58-encoded string in the standardized format.
*/
Address.prototype.toString = function () {
return base58.checkEncode(this.hash.slice(0), this.version);
};
Address.getVersion = function(string) {
return base58.decode(string)[0];
return base58.checkEncode(this.hash.slice(0), this.version)
}
Address.validate = function(string) {
/**
* Returns the version of an address, e.g. if the address belongs to the main
* net or the test net.
*/
Address.getVersion = function (address) {
return base58.decode(address)[0]
}
/**
* Returns true if a bitcoin address is a valid address, otherwise false.
*/
Address.validate = function (address) {
try {
base58.checkDecode(string);
return true;
base58.checkDecode(address)
return true
} catch (e) {
return false;
return false
}
}
};
module.exports = Address;
module.exports = Address

View file

@ -5,6 +5,7 @@ var format = require('util').format
var util = require('./util.js')
var Crypto = require('crypto-js');
var HmacSHA512 = Crypto.HmacSHA512
var HMAC= Crypto.algo.HMAC
var ECKey = require('./eckey.js').ECKey
var ECPubKey = require('./eckey.js').ECPubKey
var Address = require('./address.js')
@ -198,12 +199,12 @@ HDWallet.prototype.derive = function(i) {
// If 1, private derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:]
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 {
// If 0, public derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i)
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.
@ -243,3 +244,9 @@ HDWallet.prototype.getKeyVersion = function() {
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 Crypto = require('crypto-js');
var RIPEMD160 = Crypto.RIPEMD160;
var SHA256 = Crypto.SHA256;
var HMAC= Crypto.algo.HMAC;
var Crypto = require('crypto-js')
var RIPEMD160 = Crypto.RIPEMD160
var SHA256 = Crypto.SHA256
/**
* Calculate RIPEMD160(SHA256(data)).
@ -15,12 +14,10 @@ exports.sha256ripe160 = function (data) {
return convert.wordArrayToBytes(wordArray)
}
exports.HmacFromBytesToBytes = function (hasher, message, key) {
var hmac = HMAC.create(hasher, convert.bytesToWordArray(key))
hmac.update(convert.bytesToWordArray(message))
return convert.wordArrayToBytes(hmac.finalize())
}
/**
* Convenience method for throw new Error('some message'), e.g.
* error('something went wrong')
*/
exports.error = function (msg) {
throw new Error(msg);
throw new Error(msg)
}