@@ -2,10 +2,12 @@ import { coins, FlareNetwork } from '@bitgo/statics';
22import { NotImplementedError } from '@bitgo/sdk-core' ;
33import * as assert from 'assert' ;
44import { Utils } from '../../../src/lib/utils' ;
5+ import { KeyPair } from '../../../src/lib' ;
56import * as testData from '../../resources/account' ;
67
78describe ( 'Utils' , function ( ) {
89 let utils : Utils ;
10+ const network = coins . get ( 'tflrp' ) . network as FlareNetwork ;
911
1012 beforeEach ( function ( ) {
1113 utils = new Utils ( ) ;
@@ -190,7 +192,6 @@ describe('Utils', function () {
190192
191193 describe ( 'createSignature' , function ( ) {
192194 it ( 'should create signature using secp256k1' , function ( ) {
193- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
194195 const message = Buffer . from ( testData . SEED_ACCOUNT . message , 'utf8' ) ;
195196 const privateKey = Buffer . from ( testData . SEED_ACCOUNT . privateKey , 'hex' ) ;
196197
@@ -201,7 +202,6 @@ describe('Utils', function () {
201202 } ) ;
202203
203204 it ( 'should create different signatures for different messages' , function ( ) {
204- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
205205 const message1 = Buffer . from ( 'message 1' , 'utf8' ) ;
206206 const message2 = Buffer . from ( 'message 2' , 'utf8' ) ;
207207 const privateKey = Buffer . from ( testData . SEED_ACCOUNT . privateKey , 'hex' ) ;
@@ -213,7 +213,6 @@ describe('Utils', function () {
213213 } ) ;
214214
215215 it ( 'should throw error for invalid private key' , function ( ) {
216- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
217216 const message = Buffer . from ( 'hello world' , 'utf8' ) ;
218217 const invalidPrivateKey = Buffer . from ( 'invalid' , 'utf8' ) ;
219218
@@ -223,59 +222,38 @@ describe('Utils', function () {
223222
224223 describe ( 'verifySignature' , function ( ) {
225224 it ( 'should verify valid signature' , function ( ) {
226- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
227225 const message = Buffer . from ( testData . SEED_ACCOUNT . message , 'utf8' ) ;
228226 const privateKey = Buffer . from ( testData . SEED_ACCOUNT . privateKey , 'hex' ) ;
229-
230- // Create signature
231227 const signature = utils . createSignature ( network , message , privateKey ) ;
232-
233- // Get public key (this would normally come from the private key)
234- // For testing, we'll use a mock public key approach
235228 const publicKey = Buffer . from ( testData . SEED_ACCOUNT . publicKey , 'hex' ) ; // Compressed public key format
236-
237- // Note: This test may fail if the public key doesn't match the private key
238- // In a real implementation, you'd derive the public key from the private key
239- // The method returns false when verification fails instead of throwing
240229 const isValid = utils . verifySignature ( network , message , signature , publicKey ) ;
241230 assert . strictEqual ( typeof isValid , 'boolean' ) ;
242- // With mock public key, this should return false
243231 assert . strictEqual ( isValid , true ) ;
244232 } ) ;
245233
246234 it ( 'should return false for invalid signature' , function ( ) {
247- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
248235 const message = Buffer . from ( 'hello world' , 'utf8' ) ;
249236 const invalidSignature = Buffer . from ( 'invalid signature' , 'utf8' ) ;
250237 const publicKey = Buffer . from ( '02' + '0' . repeat ( 62 ) , 'hex' ) ;
251-
252- // This should return false due to invalid signature format
253- // The method catches errors internally and returns false
254238 const result = utils . verifySignature ( network , message , invalidSignature , publicKey ) ;
255239 assert . strictEqual ( result , false ) ;
256240 } ) ;
257241 } ) ;
258242
259243 describe ( 'recoverySignature' , function ( ) {
260- it ( 'should recover public key from valid signature' , function ( ) {
261- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
262- const message = Buffer . from ( 'hello world' , 'utf8' ) ;
263- const privateKey = Buffer . from ( '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' , 'hex' ) ;
264-
265- // Create signature using the same private key
266- const signature = utils . createSignature ( network , message , privateKey ) ;
267-
268- // Recover public key
269- const recoveredPubKey = utils . recoverySignature ( network , message , signature ) ;
270-
271- assert . ok ( recoveredPubKey instanceof Buffer ) ;
272- assert . strictEqual ( recoveredPubKey . length , 33 ) ; // Should be compressed public key (33 bytes)
244+ it ( 'should recover signature' , ( ) => {
245+ const compressed = true ;
246+ const keyPair = new KeyPair ( { prv : testData . SEED_ACCOUNT . flrpPrivateKey } ) ;
247+ const prv = keyPair . getPrivateKey ( ) ;
248+ const pub = keyPair . getPublicKey ( { compressed } ) ;
249+ const message = Buffer . from ( testData . SEED_ACCOUNT . message , 'hex' ) ;
250+ const signature = utils . createSignature ( network , message , prv ! ) ;
251+ utils . recoverySignature ( network , message , signature ) . should . deepEqual ( pub ) ;
273252 } ) ;
274253
275254 it ( 'should recover same public key for same message and signature' , function ( ) {
276- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
277- const message = Buffer . from ( 'hello world' , 'utf8' ) ;
278- const privateKey = Buffer . from ( '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' , 'hex' ) ;
255+ const message = Buffer . from ( testData . SEED_ACCOUNT . message , 'utf8' ) ;
256+ const privateKey = Buffer . from ( testData . SEED_ACCOUNT . privateKey , 'hex' ) ;
279257 const signature = utils . createSignature ( network , message , privateKey ) ;
280258
281259 const pubKey1 = utils . recoverySignature ( network , message , signature ) ;
@@ -284,33 +262,14 @@ describe('Utils', function () {
284262 assert . deepStrictEqual ( pubKey1 , pubKey2 ) ;
285263 } ) ;
286264
287- it ( 'should recover public key that matches original key' , function ( ) {
288- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
289- const message = Buffer . from ( 'hello world' , 'utf8' ) ;
290- const privateKey = Buffer . from ( '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' , 'hex' ) ;
291-
292- // Get original public key
293- const { ecc } = require ( '@bitgo/secp256k1' ) ;
294- const originalPubKey = Buffer . from ( ecc . pointFromScalar ( privateKey , true ) as Uint8Array ) ;
295-
296- // Create signature and recover public key
297- const signature = utils . createSignature ( network , message , privateKey ) ;
298- const recoveredPubKey = utils . recoverySignature ( network , message , signature ) ;
299-
300- // Convert both to hex strings for comparison
301- assert . strictEqual ( recoveredPubKey . toString ( 'hex' ) , originalPubKey . toString ( 'hex' ) ) ;
302- } ) ;
303-
304265 it ( 'should throw error for invalid signature' , function ( ) {
305- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
306- const message = Buffer . from ( 'hello world' , 'utf8' ) ;
266+ const message = Buffer . from ( testData . SEED_ACCOUNT . message , 'utf8' ) ;
307267 const invalidSignature = Buffer . from ( 'invalid signature' , 'utf8' ) ;
308268
309269 assert . throws ( ( ) => utils . recoverySignature ( network , message , invalidSignature ) , / F a i l e d t o r e c o v e r s i g n a t u r e / ) ;
310270 } ) ;
311271
312272 it ( 'should throw error for empty message' , function ( ) {
313- const network = coins . get ( 'flrp' ) . network as FlareNetwork ;
314273 const message = Buffer . alloc ( 0 ) ;
315274 const signature = Buffer . alloc ( 65 ) ; // Empty but valid length signature (65 bytes: 64 signature + 1 recovery param)
316275
0 commit comments