|
@ -6,6 +6,7 @@ var BN = require('./crypto/bn'); |
|
|
var Point = require('./crypto/point'); |
|
|
var Point = require('./crypto/point'); |
|
|
var JSUtil = require('./util/js'); |
|
|
var JSUtil = require('./util/js'); |
|
|
var Network = require('./networks'); |
|
|
var Network = require('./networks'); |
|
|
|
|
|
var _ = require('lodash'); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'. |
|
|
* Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'. |
|
@ -137,21 +138,24 @@ PublicKey._transformPrivateKey = function(privkey) { |
|
|
* Internal function to transform DER into a public key point |
|
|
* Internal function to transform DER into a public key point |
|
|
* |
|
|
* |
|
|
* @param {Buffer} buf - An hex encoded buffer |
|
|
* @param {Buffer} buf - An hex encoded buffer |
|
|
|
|
|
* @param {bool} [strict] - if set to false, will loosen some conditions |
|
|
* @returns {Object} An object with keys: point and compressed |
|
|
* @returns {Object} An object with keys: point and compressed |
|
|
* @private |
|
|
* @private |
|
|
*/ |
|
|
*/ |
|
|
PublicKey._transformDER = function(buf) { |
|
|
PublicKey._transformDER = function(buf, strict) { |
|
|
var info = {}; |
|
|
var info = {}; |
|
|
if (!PublicKey._isBuffer(buf)) { |
|
|
if (!PublicKey._isBuffer(buf)) { |
|
|
throw new TypeError('Must be a hex buffer of DER encoded public key'); |
|
|
throw new TypeError('Must be a hex buffer of DER encoded public key'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
strict = _.isUndefined(strict) ? true : strict; |
|
|
|
|
|
|
|
|
var x; |
|
|
var x; |
|
|
var y; |
|
|
var y; |
|
|
var xbuf; |
|
|
var xbuf; |
|
|
var ybuf; |
|
|
var ybuf; |
|
|
|
|
|
|
|
|
if (buf[0] === 0x04) { |
|
|
if (buf[0] === 0x04 || (!strict && (buf[0] === 0x06 || buf[0] === 0x07))) { |
|
|
xbuf = buf.slice(1, 33); |
|
|
xbuf = buf.slice(1, 33); |
|
|
ybuf = buf.slice(33, 65); |
|
|
ybuf = buf.slice(33, 65); |
|
|
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) { |
|
|
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) { |
|
@ -166,7 +170,7 @@ PublicKey._transformDER = function(buf) { |
|
|
x = BN(xbuf); |
|
|
x = BN(xbuf); |
|
|
info = PublicKey._transformX(true, x); |
|
|
info = PublicKey._transformX(true, x); |
|
|
info.compressed = true; |
|
|
info.compressed = true; |
|
|
} else if (buf[0] == 0x02) { |
|
|
} else if (buf[0] === 0x02) { |
|
|
xbuf = buf.slice(1); |
|
|
xbuf = buf.slice(1); |
|
|
x = BN(xbuf); |
|
|
x = BN(xbuf); |
|
|
info = PublicKey._transformX(false, x); |
|
|
info = PublicKey._transformX(false, x); |
|
@ -276,6 +280,18 @@ PublicKey.fromPoint = function(point, compressed) { |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Instantiate a PublicKey from a DER Buffer |
|
|
|
|
|
* |
|
|
|
|
|
* @param {Buffer} buf - A DER Buffer |
|
|
|
|
|
* @param {bool} [strict] - if set to false, will loosen some conditions |
|
|
|
|
|
* @returns {PublicKey} A new valid instance of PublicKey |
|
|
|
|
|
*/ |
|
|
|
|
|
PublicKey.fromDER = function(buf, strict) { |
|
|
|
|
|
var info = PublicKey._transformDER(buf, strict); |
|
|
|
|
|
return new PublicKey(info.point, info.compressed); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Instantiate a PublicKey from a DER hex encoded string |
|
|
* Instantiate a PublicKey from a DER hex encoded string |
|
|
* |
|
|
* |
|
|