|
| 1 | +import { coins, FlareNetwork } from '@bitgo/statics'; |
| 2 | +import * as assert from 'assert'; |
| 3 | +import { Utils } from '../../../src/lib/utils'; |
| 4 | + |
| 5 | +describe('Utils', function () { |
| 6 | + let utils: Utils; |
| 7 | + |
| 8 | + beforeEach(function () { |
| 9 | + utils = new Utils(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('recoverySignature', function () { |
| 13 | + it('should recover public key from valid signature', function () { |
| 14 | + const network = coins.get('flrp').network as FlareNetwork; |
| 15 | + const message = Buffer.from('hello world', 'utf8'); |
| 16 | + const privateKey = Buffer.from('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'hex'); |
| 17 | + |
| 18 | + // Create signature using the same private key |
| 19 | + const signature = utils.createSignature(network, message, privateKey); |
| 20 | + |
| 21 | + // Recover public key |
| 22 | + const recoveredPubKey = utils.recoverySignature(network, message, signature); |
| 23 | + |
| 24 | + assert.ok(recoveredPubKey instanceof Buffer); |
| 25 | + assert.strictEqual(recoveredPubKey.length, 33); // Should be compressed public key (33 bytes) |
| 26 | + }); |
| 27 | + |
| 28 | + it('should recover same public key for same message and signature', function () { |
| 29 | + const network = coins.get('flrp').network as FlareNetwork; |
| 30 | + const message = Buffer.from('hello world', 'utf8'); |
| 31 | + const privateKey = Buffer.from('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'hex'); |
| 32 | + const signature = utils.createSignature(network, message, privateKey); |
| 33 | + |
| 34 | + const pubKey1 = utils.recoverySignature(network, message, signature); |
| 35 | + const pubKey2 = utils.recoverySignature(network, message, signature); |
| 36 | + |
| 37 | + assert.deepStrictEqual(pubKey1, pubKey2); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should recover public key that matches original key', function () { |
| 41 | + const network = coins.get('flrp').network as FlareNetwork; |
| 42 | + const message = Buffer.from('hello world', 'utf8'); |
| 43 | + const privateKey = Buffer.from('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'hex'); |
| 44 | + |
| 45 | + // Get original public key |
| 46 | + const { ecc } = require('@bitgo/secp256k1'); |
| 47 | + const originalPubKey = Buffer.from(ecc.pointFromScalar(privateKey, true) as Uint8Array); |
| 48 | + |
| 49 | + // Create signature and recover public key |
| 50 | + const signature = utils.createSignature(network, message, privateKey); |
| 51 | + const recoveredPubKey = utils.recoverySignature(network, message, signature); |
| 52 | + |
| 53 | + // Convert both to hex strings for comparison |
| 54 | + assert.strictEqual(recoveredPubKey.toString('hex'), originalPubKey.toString('hex')); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should throw error for invalid signature', function () { |
| 58 | + const network = coins.get('flrp').network as FlareNetwork; |
| 59 | + const message = Buffer.from('hello world', 'utf8'); |
| 60 | + const invalidSignature = Buffer.from('invalid signature', 'utf8'); |
| 61 | + |
| 62 | + assert.throws(() => utils.recoverySignature(network, message, invalidSignature), /Failed to recover signature/); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should throw error for empty message', function () { |
| 66 | + const network = coins.get('flrp').network as FlareNetwork; |
| 67 | + const message = Buffer.alloc(0); |
| 68 | + const signature = Buffer.alloc(65); // Empty but valid length signature (65 bytes: 64 signature + 1 recovery param) |
| 69 | + |
| 70 | + assert.throws(() => utils.recoverySignature(network, message, signature), /Failed to recover signature/); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments