@@ -143,8 +143,7 @@ export const generatePrivate = function (): Buffer {
143143
144144export const getPublic = function ( privateKey : Buffer ) : Buffer {
145145 // This function has sync API so we throw an error immediately.
146- assert ( privateKey . length === 32 , "Bad private key" ) ;
147- assert ( isValidPrivateKey ( privateKey ) , "Bad private key" ) ;
146+ assertPrivateKey ( privateKey ) ;
148147 // XXX(Kagami): `elliptic.utils.encode` returns array for every
149148 // encoding except `hex`.
150149 return Buffer . from ( ec . keyFromPrivate ( privateKey ) . getPublic ( "array" ) ) ;
@@ -154,9 +153,7 @@ export const getPublic = function (privateKey: Buffer): Buffer {
154153 * Get compressed version of public key.
155154 */
156155export const getPublicCompressed = function ( privateKey : Buffer ) : Buffer {
157- // jshint ignore:line
158- assert ( privateKey . length === 32 , "Bad private key" ) ;
159- assert ( isValidPrivateKey ( privateKey ) , "Bad private key" ) ;
156+ assertPrivateKey ( privateKey ) ;
160157 // See https://github.com/wanderer/secp256k1-node/issues/46
161158 const compressed = true ;
162159 return Buffer . from ( ec . keyFromPrivate ( privateKey ) . getPublic ( compressed , "array" ) ) ;
@@ -168,8 +165,7 @@ export const getPublicCompressed = function (privateKey: Buffer): Buffer {
168165// because of the WebCryptoAPI (see
169166// <http://caniuse.com/#feat=cryptography>).
170167export const sign = async function ( privateKey : Buffer , msg : Buffer ) : Promise < Buffer > {
171- assert ( privateKey . length === 32 , "Bad private key" ) ;
172- assert ( isValidPrivateKey ( privateKey ) , "Bad private key" ) ;
168+ assertPrivateKey ( privateKey ) ;
173169 assert ( msg . length > 0 , "Message should not be empty" ) ;
174170 assert ( msg . length <= 32 , "Message is too long" ) ;
175171 return Buffer . from (
@@ -181,14 +177,24 @@ export const sign = async function (privateKey: Buffer, msg: Buffer): Promise<Bu
181177 ) ;
182178} ;
183179
184- export const verify = async function ( publicKey : Buffer , msg : Buffer , sig : Buffer ) : Promise < null > {
185- assert ( publicKey . length === 65 || publicKey . length === 33 , "Bad public key" ) ;
180+ const assertPublicKey = function ( publicKey : Buffer ) {
181+ assert ( publicKey . length === 65 || publicKey . length === 33 , "Bad public key: expected 65 or 33 bytes, got " + publicKey . length ) ;
186182 if ( publicKey . length === 65 ) {
187- assert ( publicKey [ 0 ] === 4 , "Bad public key" ) ;
183+ assert ( publicKey [ 0 ] === 4 , "Bad public key: expected first byte 4, got " + publicKey [ 0 ] ) ;
188184 }
189185 if ( publicKey . length === 33 ) {
190- assert ( publicKey [ 0 ] === 2 || publicKey [ 0 ] === 3 , "Bad public key" ) ;
186+ assert ( publicKey [ 0 ] === 2 || publicKey [ 0 ] === 3 , "Bad public key: expected first byte 2 or 3, got " + publicKey [ 0 ] ) ;
191187 }
188+ } ;
189+
190+ const assertPrivateKey = function ( privateKey : Buffer ) {
191+ assert ( Buffer . isBuffer ( privateKey ) , "Bad private key: expected Buffer" ) ;
192+ assert ( privateKey . length === 32 , "Bad private key: expected 32 bytes, got " + privateKey . length ) ;
193+ assert ( isValidPrivateKey ( privateKey ) , "Bad private key: out of range" ) ;
194+ } ;
195+
196+ export const verify = async function ( publicKey : Buffer , msg : Buffer , sig : Buffer ) : Promise < null > {
197+ assertPublicKey ( publicKey ) ;
192198 assert ( msg . length > 0 , "Message should not be empty" ) ;
193199 assert ( msg . length <= 32 , "Message is too long" ) ;
194200 if ( ec . verify ( msg , sig , publicKey ) ) {
@@ -198,17 +204,8 @@ export const verify = async function (publicKey: Buffer, msg: Buffer, sig: Buffe
198204} ;
199205
200206export const derive = async function ( privateKeyA : Buffer , publicKeyB : Buffer , padding ?: boolean ) : Promise < Buffer > {
201- assert ( Buffer . isBuffer ( privateKeyA ) , "Bad private key: expected Buffer" ) ;
202- assert ( Buffer . isBuffer ( publicKeyB ) , "Bad public key: expected Buffer" ) ;
203- assert ( privateKeyA . length === 32 , "Bad private key: expected 32 bytes, got " + privateKeyA . length ) ;
204- assert ( isValidPrivateKey ( privateKeyA ) , "Bad private key" ) ;
205- assert ( publicKeyB . length === 65 || publicKeyB . length === 33 , "Bad public key: expected 65 or 33 bytes, got " + publicKeyB . length ) ;
206- if ( publicKeyB . length === 65 ) {
207- assert ( publicKeyB [ 0 ] === 4 , "Bad public key: expected first byte 4, got " + publicKeyB [ 0 ] ) ;
208- }
209- if ( publicKeyB . length === 33 ) {
210- assert ( publicKeyB [ 0 ] === 2 || publicKeyB [ 0 ] === 3 , "Bad public key: expected first byte 2 or 3, got " + publicKeyB [ 0 ] ) ;
211- }
207+ assertPrivateKey ( privateKeyA ) ;
208+ assertPublicKey ( publicKeyB ) ;
212209 const keyA = ec . keyFromPrivate ( privateKeyA ) ;
213210 const keyB = ec . keyFromPublic ( publicKeyB ) ;
214211 const Px = keyA . derive ( keyB . getPublic ( ) ) ; // BN instance
0 commit comments