@@ -8,15 +8,14 @@ var Address = require('./address')
88var crypto = require ( './crypto' )
99
1010var sec = require ( './jsbn/sec' )
11- var ecparams = sec ( " secp256k1" )
11+ var ecparams = sec ( ' secp256k1' )
1212
1313var BigInteger = require ( './jsbn/jsbn' )
1414var ECPointFp = require ( './jsbn/ec' ) . ECPointFp
1515
1616function ECKey ( D , compressed ) {
17- assert ( D instanceof BigInteger )
18- assert ( D . compareTo ( BigInteger . ZERO ) > 0 )
19- assert ( D . compareTo ( ecparams . getN ( ) ) < 0 )
17+ assert ( D . compareTo ( BigInteger . ZERO ) > 0 , 'Private key must be greater than 0' )
18+ assert ( D . compareTo ( ecparams . getN ( ) ) < 0 , 'Private key must be less than the curve order' )
2019
2120 var Q = ecparams . getG ( ) . multiply ( D )
2221
@@ -26,9 +25,10 @@ function ECKey(D, compressed) {
2625
2726// Static constructors
2827ECKey . fromBuffer = function ( buffer , compressed ) {
29- assert ( Buffer . isBuffer ( buffer ) )
30- var D = BigInteger . fromByteArrayUnsigned ( buffer )
28+ assert ( Buffer . isBuffer ( buffer ) , 'First argument must be a Buffer' )
29+ assert . strictEqual ( buffer . length , 32 , 'Invalid buffer length' )
3130
31+ var D = BigInteger . fromByteArrayUnsigned ( buffer )
3232 return new ECKey ( D , compressed )
3333}
3434ECKey . fromHex = function ( hex , compressed ) {
@@ -40,12 +40,11 @@ ECKey.fromWIF = function(string) {
4040
4141 var payload = decode . payload
4242 if ( payload . length === 33 ) {
43- assert ( payload [ 32 ] === 0x01 )
43+ assert . strictEqual ( payload [ 32 ] , 0x01 , 'Invalid WIF string' )
4444
4545 return ECKey . fromBuffer ( payload . slice ( 0 , 32 ) , true )
4646 }
4747
48- assert ( payload . length === 32 )
4948 return ECKey . fromBuffer ( payload , false )
5049}
5150
@@ -68,19 +67,11 @@ ECKey.prototype.sign = function(hash) {
6867ECKey . prototype . toBuffer = function ( ) {
6968 var buffer = new Buffer ( this . D . toByteArrayUnsigned ( ) )
7069
71- // pad out the zero bytes
72- if ( buffer . length < 32 ) {
73- var padded = new Buffer ( 32 )
70+ // pad out to atleast 32 bytes
71+ var padded = new Buffer ( 32 - buffer . length )
72+ padded . fill ( 0 )
7473
75- padded . fill ( 0 )
76- buffer . copy ( padded , 32 - buffer . length )
77-
78- return padded
79- }
80-
81- assert ( buffer . length === 32 )
82-
83- return buffer
74+ return Buffer . concat ( [ padded , buffer ] )
8475}
8576ECKey . prototype . toHex = function ( ) {
8677 return this . toBuffer ( ) . toString ( 'hex' )
@@ -89,11 +80,9 @@ ECKey.prototype.toHex = function() {
8980ECKey . prototype . toWIF = function ( version ) {
9081 version = version || network . bitcoin . wif
9182
92- var buffer
83+ var buffer = this . toBuffer ( )
9384 if ( this . pub . compressed ) {
94- buffer = Buffer . concat ( [ this . toBuffer ( ) , new Buffer ( [ 0x01 ] ) ] )
95- } else {
96- buffer = this . toBuffer ( )
85+ buffer = Buffer . concat ( [ buffer , new Buffer ( [ 0x01 ] ) ] )
9786 }
9887
9988 return base58check . encode ( buffer , version )
@@ -102,24 +91,24 @@ ECKey.prototype.toWIF = function(version) {
10291//////////////////////////////////////////////////////
10392
10493function ECPubKey ( Q , compressed ) {
94+ assert ( Q instanceof ECPointFp , 'Q must be an ECPointFP' )
95+
10596 if ( compressed == undefined ) compressed = true
106- assert ( typeof compressed === 'boolean' )
107- assert ( Q instanceof ECPointFp )
97+ assert . strictEqual ( typeof compressed , 'boolean' , 'Invalid compression flag' )
10898
10999 this . compressed = compressed
110100 this . Q = Q
111101}
112102
113103// Static constructors
114104ECPubKey . fromBuffer = function ( buffer ) {
115- assert ( Buffer . isBuffer ( buffer ) )
116-
117- var Q = ECPointFp . decodeFrom ( ecparams . getCurve ( ) , buffer )
118-
119105 var type = buffer . readUInt8 ( 0 )
120- assert ( type >= 0x02 || type <= 0x04 )
106+ assert ( type >= 0x02 || type <= 0x04 , 'Invalid public key' )
121107
122108 var compressed = ( type !== 0x04 )
109+ assert . strictEqual ( buffer . length , compressed ? 33 : 65 , 'Invalid public key' )
110+
111+ var Q = ECPointFp . decodeFrom ( ecparams . getCurve ( ) , buffer )
123112 return new ECPubKey ( Q , compressed )
124113}
125114ECPubKey . fromHex = function ( hex ) {
@@ -137,10 +126,7 @@ ECPubKey.prototype.getAddress = function(version) {
137126
138127// Export functions
139128ECPubKey . prototype . toBuffer = function ( ) {
140- var buffer = new Buffer ( this . Q . getEncoded ( this . compressed ) )
141- assert ( buffer . length === ( this . compressed ? 33 : 65 ) )
142-
143- return buffer
129+ return new Buffer ( this . Q . getEncoded ( this . compressed ) )
144130}
145131ECPubKey . prototype . toHex = function ( ) {
146132 return this . toBuffer ( ) . toString ( 'hex' )
0 commit comments