Merge pull request #678 from eordano/feature/privkeyhex
Add functionality to create private key from a hex string
This commit is contained in:
commit
dcbd0738b0
|
@ -7,6 +7,7 @@ var networks = require('./networks');
|
|||
var base58check = require('./encoding/base58check');
|
||||
var Address = require('./address');
|
||||
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) {
|
||||
info = PrivateKey._transformBuffer(data, network, compressed);
|
||||
} else if (typeof(data) === 'string'){
|
||||
if (jsUtil.isHexa(data)) {
|
||||
info.bn = BN(new Buffer(data, 'hex'));
|
||||
} else {
|
||||
info = PrivateKey._transformWIF(data, network, compressed);
|
||||
}
|
||||
} else {
|
||||
throw new TypeError('First argument is an unrecognized data type.');
|
||||
}
|
||||
|
|
|
@ -72,6 +72,13 @@ describe('PrivateKey', function() {
|
|||
}).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() {
|
||||
(function() {
|
||||
var a = new PrivateKey(null, 'testnet', 'compressed');
|
||||
|
|
Loading…
Reference in New Issue