Merge pull request #630 from braydonf/feature/immutable

Immutable Address, PublicKey and PrivateKey
This commit is contained in:
Esteban Ordano 2014-12-02 12:17:03 -03:00
commit 2b866a8460
4 changed files with 47 additions and 21 deletions

View File

@ -22,7 +22,7 @@ var Hash = require('./crypto/hash');
* *
* // get an address from a public key * // get an address from a public key
* var address = Address(publicKey, 'testnet').toString(); * var address = Address(publicKey, 'testnet').toString();
* *
* *
* @param {String} data - The encoded data in various formats * @param {String} data - The encoded data in various formats
* @param {String} [network] - The network: 'livenet' or 'testnet' * @param {String} [network] - The network: 'livenet' or 'testnet'
@ -71,10 +71,20 @@ function Address(data, network, type) {
info.network = info.network || network || networks.defaultNetwork.name; info.network = info.network || network || networks.defaultNetwork.name;
info.type = info.type || type || 'pubkeyhash'; info.type = info.type || type || 'pubkeyhash';
// set the validated values Object.defineProperty(this, 'hashBuffer', {
this.hashBuffer = info.hashBuffer; configurable: false,
this.network = info.network; value: info.hashBuffer
this.type = info.type; });
Object.defineProperty(this, 'network', {
configurable: false,
value: info.network
});
Object.defineProperty(this, 'type', {
configurable: false,
value: info.type
});
return this; return this;
@ -132,7 +142,7 @@ Address._transformBuffer = function(buffer, network, type){
bufNetwork = 'livenet'; bufNetwork = 'livenet';
bufType = 'scripthash'; bufType = 'scripthash';
break; break;
case networks.testnet.pubkeyhash: case networks.testnet.pubkeyhash:
bufNetwork = 'testnet'; bufNetwork = 'testnet';
bufType = 'pubkeyhash'; bufType = 'pubkeyhash';
@ -143,7 +153,7 @@ Address._transformBuffer = function(buffer, network, type){
bufType = 'scripthash'; bufType = 'scripthash';
break; break;
} }
if (!bufNetwork || (network && network !== bufNetwork)) { if (!bufNetwork || (network && network !== bufNetwork)) {
throw new TypeError('Address has mismatched network type.'); throw new TypeError('Address has mismatched network type.');
} }

View File

@ -67,9 +67,20 @@ var PrivateKey = function PrivateKey(data, network, compressed) {
throw new TypeError('Must specify whether the corresponding public key is compressed or not (true or false)'); throw new TypeError('Must specify whether the corresponding public key is compressed or not (true or false)');
} }
this.bn = info.bn; Object.defineProperty(this, 'bn', {
this.compressed = info.compressed; configurable: false,
this.network = info.network; value: info.bn
});
Object.defineProperty(this, 'compressed', {
configurable: false,
value: info.compressed
});
Object.defineProperty(this, 'network', {
configurable: false,
value: info.network
});
return this; return this;

View File

@ -12,10 +12,10 @@ var Address = require('./address');
* *
* // instantiate from a private key * // instantiate from a private key
* var key = PublicKey(privateKey, true); * var key = PublicKey(privateKey, true);
* *
* // export to as a DER hex encoded string * // export to as a DER hex encoded string
* var exported = key.toString(); * var exported = key.toString();
* *
* // import the public key * // import the public key
* var imported = PublicKey.fromString(exported); * var imported = PublicKey.fromString(exported);
* *
@ -45,7 +45,7 @@ var PublicKey = function PublicKey(data, compressed) {
info = PublicKey._transformDER(new Buffer(data, 'hex' )); info = PublicKey._transformDER(new Buffer(data, 'hex' ));
} else if (data instanceof Buffer || data instanceof Uint8Array){ } else if (data instanceof Buffer || data instanceof Uint8Array){
info = PublicKey._transformDER(data); info = PublicKey._transformDER(data);
} else if (data.constructor && (data.constructor.name && } else if (data.constructor && (data.constructor.name &&
data.constructor.name === 'PrivateKey')) { data.constructor.name === 'PrivateKey')) {
info = PublicKey._transformPrivateKey(data); info = PublicKey._transformPrivateKey(data);
} else { } else {
@ -55,8 +55,15 @@ var PublicKey = function PublicKey(data, compressed) {
// validation // validation
info.point.validate(); info.point.validate();
this.point = info.point; Object.defineProperty(this, 'point', {
this.compressed = info.compressed; configurable: false,
value: info.point
});
Object.defineProperty(this, 'compressed', {
configurable: false,
value: info.compressed
});
return this; return this;
@ -71,7 +78,7 @@ var PublicKey = function PublicKey(data, compressed) {
*/ */
PublicKey._transformPrivateKey = function(privkey) { PublicKey._transformPrivateKey = function(privkey) {
var info = {}; var info = {};
if (!privkey.constructor || if (!privkey.constructor ||
(privkey.constructor.name && privkey.constructor.name !== 'PrivateKey')) { (privkey.constructor.name && privkey.constructor.name !== 'PrivateKey')) {
throw new TypeError('Must be an instance of PrivateKey'); throw new TypeError('Must be an instance of PrivateKey');
} }

View File

@ -263,16 +263,14 @@ describe('PrivateKey', function() {
it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() { it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex'))); var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex')), 'livenet', true);
privkey.compressed = true;
var pubkey = privkey.toPublicKey(); var pubkey = privkey.toPublicKey();
pubkey.compressed.should.equal(true); pubkey.compressed.should.equal(true);
}); });
it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() { it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex'))); var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex')), 'livenet', false);
privkey.compressed = false;
var pubkey = privkey.toPublicKey(); var pubkey = privkey.toPublicKey();
pubkey.compressed.should.equal(false); pubkey.compressed.should.equal(false);
}); });