Add option to create private key from hex
This commit is contained in:
parent
55525db73e
commit
f8397a617e
@ -7,6 +7,7 @@ var networks = require('./networks');
|
|||||||
var base58check = require('./encoding/base58check');
|
var base58check = require('./encoding/base58check');
|
||||||
var Address = require('./address');
|
var Address = require('./address');
|
||||||
var PublicKey = require('./publickey');
|
var PublicKey = require('./publickey');
|
||||||
|
var jsUtil = require('./util/js');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -51,7 +52,11 @@ var PrivateKey = function PrivateKey(data, network, compressed) {
|
|||||||
} else if (data instanceof Buffer || data instanceof Uint8Array) {
|
} else if (data instanceof Buffer || data instanceof Uint8Array) {
|
||||||
info = PrivateKey._transformBuffer(data, network, compressed);
|
info = PrivateKey._transformBuffer(data, network, compressed);
|
||||||
} else if (typeof(data) === 'string'){
|
} else if (typeof(data) === 'string'){
|
||||||
info = PrivateKey._transformWIF(data, network, compressed);
|
if (jsUtil.isHexa(data)) {
|
||||||
|
info.bn = BN(new Buffer(data, 'hex'));
|
||||||
|
} else {
|
||||||
|
info = PrivateKey._transformWIF(data, network, compressed);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new TypeError('First argument is an unrecognized data type.');
|
throw new TypeError('First argument is an unrecognized data type.');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -72,6 +72,13 @@ describe('PrivateKey', function() {
|
|||||||
}).should.throw('Invalid network');
|
}).should.throw('Invalid network');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can be instantiated from a hex string', function() {
|
||||||
|
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
|
||||||
|
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
|
||||||
|
var privkey = new PrivateKey(privhex);
|
||||||
|
privkey.publicKey.toString().should.equal(pubhex);
|
||||||
|
});
|
||||||
|
|
||||||
it('should not be able to instantiate because compressed is non-boolean', function() {
|
it('should not be able to instantiate because compressed is non-boolean', function() {
|
||||||
(function() {
|
(function() {
|
||||||
var a = new PrivateKey(null, 'testnet', 'compressed');
|
var a = new PrivateKey(null, 'testnet', 'compressed');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user