Merge pull request #630 from braydonf/feature/immutable
Immutable Address, PublicKey and PrivateKey
This commit is contained in:
commit
2b866a8460
@ -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;
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
|
|||||||
@ -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);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user