|
| 1 | +import { BaseTransactionBuilder, BaseTransactionBuilderFactory } from '@bitgo/sdk-core'; |
| 2 | + |
| 3 | +export interface RecoverModeTestSuitArgs { |
| 4 | + transactionType: string; |
| 5 | + newTxFactory: () => BaseTransactionBuilderFactory; |
| 6 | + newTxBuilder: () => BaseTransactionBuilder; |
| 7 | + privateKey: { prv1: string; prv2: string; prv3: string }; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Test suite focusing on recovery mode signing. |
| 12 | + * In recovery mode, the backup key (prv3) is used instead of user key (prv1) along with BitGo key (prv2). |
| 13 | + * @param {RecoverModeTestSuitArgs} data with required info. |
| 14 | + */ |
| 15 | +export default function recoverModeTestSuit(data: RecoverModeTestSuitArgs): void { |
| 16 | + describe(`should test recovery mode for ${data.transactionType}`, () => { |
| 17 | + it('Should set recoverMode flag on builder', async () => { |
| 18 | + const txBuilder = data.newTxBuilder(); |
| 19 | + // @ts-expect-error - accessing protected property for testing |
| 20 | + txBuilder.recoverSigner.should.equal(false); |
| 21 | + |
| 22 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 23 | + txBuilder.recoverMode(true); |
| 24 | + // @ts-expect-error - accessing protected property for testing |
| 25 | + txBuilder.recoverSigner.should.equal(true); |
| 26 | + |
| 27 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 28 | + txBuilder.recoverMode(false); |
| 29 | + // @ts-expect-error - accessing protected property for testing |
| 30 | + txBuilder.recoverSigner.should.equal(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('Should default recoverMode to true when called without argument', async () => { |
| 34 | + const txBuilder = data.newTxBuilder(); |
| 35 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 36 | + txBuilder.recoverMode(); |
| 37 | + // @ts-expect-error - accessing protected property for testing |
| 38 | + txBuilder.recoverSigner.should.equal(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('Should build unsigned tx in recovery mode', async () => { |
| 42 | + const txBuilder = data.newTxBuilder(); |
| 43 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 44 | + txBuilder.recoverMode(true); |
| 45 | + const tx = await txBuilder.build(); |
| 46 | + tx.toBroadcastFormat().should.be.a.String(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('Should half sign tx in recovery mode using backup key (prv3)', async () => { |
| 50 | + const txBuilder = data.newTxBuilder(); |
| 51 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 52 | + txBuilder.recoverMode(true); |
| 53 | + |
| 54 | + // In recovery mode, sign with backup key (prv3) instead of user key (prv1) |
| 55 | + txBuilder.sign({ key: data.privateKey.prv3 }); |
| 56 | + const tx = await txBuilder.build(); |
| 57 | + const halfSignedHex = tx.toBroadcastFormat(); |
| 58 | + halfSignedHex.should.be.a.String(); |
| 59 | + halfSignedHex.length.should.be.greaterThan(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('Should full sign tx in recovery mode using backup key (prv3) and bitgo key (prv2)', async () => { |
| 63 | + const txBuilder = data.newTxBuilder(); |
| 64 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 65 | + txBuilder.recoverMode(true); |
| 66 | + |
| 67 | + // In recovery mode: backup key (prv3) + bitgo key (prv2) |
| 68 | + txBuilder.sign({ key: data.privateKey.prv3 }); |
| 69 | + txBuilder.sign({ key: data.privateKey.prv2 }); |
| 70 | + const tx = await txBuilder.build(); |
| 71 | + const fullSignedHex = tx.toBroadcastFormat(); |
| 72 | + fullSignedHex.should.be.a.String(); |
| 73 | + fullSignedHex.length.should.be.greaterThan(0); |
| 74 | + }); |
| 75 | + |
| 76 | + it('Should produce different signed tx in recovery mode vs regular mode', async () => { |
| 77 | + // Build and sign in regular mode (user key prv1 + bitgo key prv2) |
| 78 | + const regularTxBuilder = data.newTxBuilder(); |
| 79 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 80 | + regularTxBuilder.recoverMode(false); |
| 81 | + regularTxBuilder.sign({ key: data.privateKey.prv1 }); |
| 82 | + regularTxBuilder.sign({ key: data.privateKey.prv2 }); |
| 83 | + const regularTx = await regularTxBuilder.build(); |
| 84 | + const regularHex = regularTx.toBroadcastFormat(); |
| 85 | + |
| 86 | + // Build and sign in recovery mode (backup key prv3 + bitgo key prv2) |
| 87 | + const recoveryTxBuilder = data.newTxBuilder(); |
| 88 | + // @ts-expect-error - method exists on flrp TransactionBuilder |
| 89 | + recoveryTxBuilder.recoverMode(true); |
| 90 | + recoveryTxBuilder.sign({ key: data.privateKey.prv3 }); |
| 91 | + recoveryTxBuilder.sign({ key: data.privateKey.prv2 }); |
| 92 | + const recoveryTx = await recoveryTxBuilder.build(); |
| 93 | + const recoveryHex = recoveryTx.toBroadcastFormat(); |
| 94 | + |
| 95 | + // Both should be valid hex strings |
| 96 | + regularHex.should.be.a.String(); |
| 97 | + recoveryHex.should.be.a.String(); |
| 98 | + |
| 99 | + // The signed transactions should be different because different keys are used |
| 100 | + regularHex.should.not.equal(recoveryHex); |
| 101 | + |
| 102 | + // Signatures should also be different |
| 103 | + const regularSignatures = regularTx.signature; |
| 104 | + const recoverySignatures = recoveryTx.signature; |
| 105 | + regularSignatures.should.not.eql(recoverySignatures); |
| 106 | + }); |
| 107 | + |
| 108 | + it('Should set recoverMode via factory and pass to builder from raw tx', async () => { |
| 109 | + // First build an unsigned transaction |
| 110 | + const txBuilder = data.newTxBuilder(); |
| 111 | + const tx = await txBuilder.build(); |
| 112 | + const unsignedHex = tx.toBroadcastFormat(); |
| 113 | + |
| 114 | + // Parse from raw with recovery mode enabled on factory |
| 115 | + const factory = data.newTxFactory(); |
| 116 | + // @ts-expect-error - accessing the method which may not be on base type |
| 117 | + if (typeof factory.recoverMode === 'function') { |
| 118 | + // @ts-expect-error - calling recoverMode on factory |
| 119 | + factory.recoverMode(true); |
| 120 | + const recoveredBuilder = factory.from(unsignedHex); |
| 121 | + // Cast to any to access protected property for testing |
| 122 | + (recoveredBuilder as any).recoverSigner.should.equal(true); |
| 123 | + } |
| 124 | + }); |
| 125 | + }); |
| 126 | +} |
0 commit comments