Privkey.prototype.set
This commit is contained in:
parent
6b4bc4c49c
commit
109f31cfdb
|
@ -64,7 +64,7 @@ BIP32.prototype.fromSeed = function(bytes, network) {
|
|||
this.chainCode = hash.slice(32, 64);
|
||||
this.version = constants[network].bip32privkey;
|
||||
this.key = new Key();
|
||||
this.key.privkey = new Privkey(bn.fromBuffer(hash.slice(0, 32)));
|
||||
this.key.privkey = new Privkey({bn: bn.fromBuffer(hash.slice(0, 32))});
|
||||
this.key.privkey2pubkey();
|
||||
this.hasPrivateKey = true;
|
||||
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
||||
|
@ -98,7 +98,7 @@ BIP32.prototype.initFromBytes = function(bytes) {
|
|||
|
||||
if (isPrivate && keyBytes[0] == 0) {
|
||||
this.key = new Key();
|
||||
this.key.privkey = new Privkey(bn.fromBuffer(keyBytes.slice(1, 33)));
|
||||
this.key.privkey = new Privkey({bn: bn.fromBuffer(keyBytes.slice(1, 33))});
|
||||
this.key.privkey2pubkey();
|
||||
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
||||
this.hasPrivateKey = true;
|
||||
|
@ -273,7 +273,7 @@ BIP32.prototype.deriveChild = function(i) {
|
|||
ret.chainCode = ir;
|
||||
|
||||
ret.key = new Key();
|
||||
ret.key.privkey = new Privkey(k);
|
||||
ret.key.privkey = new Privkey({bn: k});
|
||||
ret.key.privkey2pubkey();
|
||||
ret.hasPrivateKey = true;
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ var BN = require('./bn');
|
|||
var Point = require('./point');
|
||||
var Signature = require('./signature');
|
||||
var Key = require('./key');
|
||||
var Privkey = require('./privkey');
|
||||
var Pubkey = require('./pubkey');
|
||||
var Random = require('./random');
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ Stealth.prototype.getReceivePubkeyAsSender = function(senderKey) {
|
|||
|
||||
Stealth.prototype.getReceiveKey = function(senderPubkey) {
|
||||
var sharedKey = this.getSharedKeyAsReceiver(senderPubkey);
|
||||
var privkey = Privkey(this.payloadKey.privkey.bn.add(sharedKey.privkey.bn).mod(Point.getN()));
|
||||
var privkey = Privkey({bn: this.payloadKey.privkey.bn.add(sharedKey.privkey.bn).mod(Point.getN())});
|
||||
var key = Key({privkey: privkey});
|
||||
key.privkey2pubkey();
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ KDF.sha256hmac2privkey = function(buf) {
|
|||
var bn = Bn.fromBuffer(hash);
|
||||
concat = Buffer.concat([concat, new Buffer(0)]);
|
||||
} while(!bn.lt(Point.getN()));
|
||||
return new Privkey(bn);
|
||||
return new Privkey({bn: bn});
|
||||
};
|
||||
|
||||
module.exports = KDF;
|
||||
|
|
|
@ -21,7 +21,7 @@ Key.prototype.set = function(obj) {
|
|||
Key.prototype.fromRandom = function() {
|
||||
do {
|
||||
var privbuf = Random.getRandomBuffer(32);
|
||||
this.privkey = new Privkey(Bn(privbuf));
|
||||
this.privkey = new Privkey({bn: Bn(privbuf)});
|
||||
var condition = this.privkey.bn.lt(point.getN());
|
||||
} while (!condition);
|
||||
this.privkey2pubkey();
|
||||
|
|
|
@ -3,12 +3,18 @@ var point = require('./point');
|
|||
var constants = require('./constants');
|
||||
var base58check = require('./base58check');
|
||||
|
||||
var Privkey = function Privkey(bn, network, compressed) {
|
||||
var Privkey = function Privkey(obj) {
|
||||
if (!(this instanceof Privkey))
|
||||
return new Privkey(bn, network, compressed);
|
||||
this.bn = bn;
|
||||
this.network = network;
|
||||
this.compressed = compressed;
|
||||
return new Privkey(obj);
|
||||
if (obj)
|
||||
this.set(obj);
|
||||
};
|
||||
|
||||
Privkey.prototype.set = function(obj) {
|
||||
this.bn = obj.bn || this.bn;
|
||||
this.network = obj.network || this.network;
|
||||
this.compressed = typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed;
|
||||
return this;
|
||||
};
|
||||
|
||||
Privkey.prototype.validate = function() {
|
||||
|
|
|
@ -17,7 +17,7 @@ describe("ECDSA", function() {
|
|||
var ecdsa = new ECDSA();
|
||||
ecdsa.hashbuf = Hash.sha256(new Buffer('test data'));
|
||||
ecdsa.key = new Key();
|
||||
ecdsa.key.privkey = new Privkey(BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex')));
|
||||
ecdsa.key.privkey = new Privkey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))});
|
||||
ecdsa.key.pubkey = new Pubkey(point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')),
|
||||
BN().fromBuffer(new Buffer('4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380', 'hex'))));
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ describe('Key', function() {
|
|||
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
|
||||
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
|
||||
var key = new Key();
|
||||
key.privkey = new Privkey(bn(new Buffer(privhex, 'hex')));
|
||||
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
|
||||
key.privkey2pubkey();
|
||||
key.getAddress().toString().should.equal((new Address()).fromPubkey(key.pubkey).toString());
|
||||
});
|
||||
|
@ -102,7 +102,7 @@ describe('Key', function() {
|
|||
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
|
||||
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
|
||||
var key = new Key();
|
||||
key.privkey = new Privkey(bn(new Buffer(privhex, 'hex')));
|
||||
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
|
||||
key.privkey2pubkey();
|
||||
key.pubkey.toString().should.equal(pubhex);
|
||||
});
|
||||
|
@ -110,7 +110,7 @@ describe('Key', function() {
|
|||
it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() {
|
||||
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
|
||||
var key = new Key();
|
||||
key.privkey = new Privkey(bn(new Buffer(privhex, 'hex')));
|
||||
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
|
||||
key.privkey.compressed = true;
|
||||
key.privkey2pubkey();
|
||||
key.pubkey.compressed.should.equal(true);
|
||||
|
@ -119,7 +119,7 @@ describe('Key', function() {
|
|||
it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() {
|
||||
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
|
||||
var key = new Key();
|
||||
key.privkey = new Privkey(bn(new Buffer(privhex, 'hex')));
|
||||
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
|
||||
key.privkey.compressed = false;
|
||||
key.privkey2pubkey();
|
||||
key.pubkey.compressed.should.equal(false);
|
||||
|
|
|
@ -17,20 +17,28 @@ describe('Privkey', function() {
|
|||
});
|
||||
|
||||
it('should create a mainnet private key', function() {
|
||||
var privkey = new Privkey(Bn.fromBuffer(buf), 'mainnet', true);
|
||||
var privkey = new Privkey({bn: Bn.fromBuffer(buf), network: 'mainnet', compressed: true});
|
||||
privkey.toString().should.equal(encmainnet);
|
||||
});
|
||||
|
||||
it('should create an uncompressed testnet private key', function() {
|
||||
var privkey = new Privkey(Bn.fromBuffer(buf), 'testnet', false);
|
||||
var privkey = new Privkey({bn: Bn.fromBuffer(buf), network: 'testnet', compressed: false});
|
||||
privkey.toString().should.equal(enctu);
|
||||
});
|
||||
|
||||
it('should create an uncompressed mainnet private key', function() {
|
||||
var privkey = new Privkey(Bn.fromBuffer(buf), 'mainnet', false);
|
||||
var privkey = new Privkey({bn: Bn.fromBuffer(buf), network: 'mainnet', compressed: false});
|
||||
privkey.toString().should.equal(encmu);
|
||||
});
|
||||
|
||||
describe('#set', function() {
|
||||
|
||||
it('should set bn', function() {
|
||||
should.exist(Privkey().set({bn: Bn.fromBuffer(buf)}).bn);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#fromWIF', function() {
|
||||
|
||||
it('should parse this compressed testnet address correctly', function() {
|
||||
|
|
Loading…
Reference in New Issue