Merge pull request #96 from ralphtheninja/master
cleaned up address.js and util.js
This commit is contained in:
commit
0ce14a7c84
3 changed files with 71 additions and 53 deletions
|
@ -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;
|
||||
}
|
||||
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');
|
||||
function Address(bytes, version) {
|
||||
if (!(this instanceof Address))
|
||||
return new Address(bytes, version)
|
||||
|
||||
this.version = version || this.hash.version || mainnet;
|
||||
}
|
||||
else {
|
||||
this.hash = bytes;
|
||||
this.version = version || mainnet;
|
||||
}
|
||||
};
|
||||
if (bytes instanceof Address) {
|
||||
this.hash = bytes.hash
|
||||
this.version = bytes.version
|
||||
}
|
||||
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.
|
||||
*
|
||||
* 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) {
|
||||
try {
|
||||
base58.checkDecode(string);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 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]
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
25
src/util.js
25
src/util.js
|
@ -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)).
|
||||
|
@ -11,16 +10,14 @@ var HMAC= Crypto.algo.HMAC;
|
|||
* array.
|
||||
*/
|
||||
exports.sha256ripe160 = function (data) {
|
||||
var wordArray = RIPEMD160(SHA256(convert.bytesToWordArray(data)))
|
||||
return convert.wordArrayToBytes(wordArray)
|
||||
var wordArray = RIPEMD160(SHA256(convert.bytesToWordArray(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())
|
||||
}
|
||||
|
||||
exports.error = function(msg) {
|
||||
throw new Error(msg);
|
||||
/**
|
||||
* Convenience method for throw new Error('some message'), e.g.
|
||||
* error('something went wrong')
|
||||
*/
|
||||
exports.error = function (msg) {
|
||||
throw new Error(msg)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue