|
9 | 9 | carolAddress,
|
10 | 10 | carolPriv,
|
11 | 11 | bobTokenAddress,
|
| 12 | + aliceAddress, |
| 13 | + alicePriv, |
12 | 14 | } from './fixture/vars.js';
|
13 | 15 | import { Network } from '../src/interfaces.js';
|
14 | 16 | import { utxoComparator, calculateDust, randomUtxo, randomToken, isNonTokenUtxo, isFungibleTokenUtxo } from '../src/utils.js';
|
@@ -293,4 +295,55 @@ describe('Transaction Builder', () => {
|
293 | 295 | expect(JSON.parse(stringify(wcTransactionObj))).toEqual(expectedResult);
|
294 | 296 | });
|
295 | 297 | });
|
| 298 | + |
| 299 | + it('should not fail when validly spending from only P2PKH inputs', async () => { |
| 300 | + const aliceUtxos = (await provider.getUtxos(aliceAddress)).filter(isNonTokenUtxo); |
| 301 | + const sigTemplate = new SignatureTemplate(alicePriv); |
| 302 | + |
| 303 | + expect(aliceUtxos.length).toBeGreaterThan(2); |
| 304 | + |
| 305 | + const change = aliceUtxos[0].satoshis + aliceUtxos[1].satoshis - 1000n; |
| 306 | + |
| 307 | + const transaction = new TransactionBuilder({ provider }) |
| 308 | + .addInput(aliceUtxos[0], sigTemplate.unlockP2PKH()) |
| 309 | + .addInput(aliceUtxos[1], sigTemplate.unlockP2PKH()) |
| 310 | + .addOutput({ to: aliceAddress, amount: change }); |
| 311 | + |
| 312 | + await expect(transaction.send()).resolves.not.toThrow(); |
| 313 | + }); |
| 314 | + |
| 315 | + // TODO: Currently, P2PKH inputs are not evaluated at all |
| 316 | + it.skip('should fail when invalidly spending from only P2PKH inputs', async () => { |
| 317 | + const aliceUtxos = (await provider.getUtxos(aliceAddress)).filter(isNonTokenUtxo); |
| 318 | + const incorrectSigTemplate = new SignatureTemplate(bobPriv); |
| 319 | + |
| 320 | + expect(aliceUtxos.length).toBeGreaterThan(2); |
| 321 | + |
| 322 | + const change = aliceUtxos[0].satoshis + aliceUtxos[1].satoshis - 1000n; |
| 323 | + |
| 324 | + const transaction = new TransactionBuilder({ provider }) |
| 325 | + .addInput(aliceUtxos[0], incorrectSigTemplate.unlockP2PKH()) |
| 326 | + .addInput(aliceUtxos[1], incorrectSigTemplate.unlockP2PKH()) |
| 327 | + .addOutput({ to: aliceAddress, amount: change }); |
| 328 | + |
| 329 | + await expect(transaction.send()).rejects.toThrow(); |
| 330 | + }); |
| 331 | + |
| 332 | + // TODO: Currently, P2PKH inputs are not evaluated at all |
| 333 | + it.skip('should fail when invalidly spending from P2PKH and correctly from contract inputs', async () => { |
| 334 | + const aliceUtxos = (await provider.getUtxos(aliceAddress)).filter(isNonTokenUtxo); |
| 335 | + const p2pkhUtxos = (await p2pkhInstance.getUtxos()).filter(isNonTokenUtxo).sort(utxoComparator).reverse(); |
| 336 | + const incorrectSigTemplate = new SignatureTemplate(bobPriv); |
| 337 | + |
| 338 | + expect(aliceUtxos.length).toBeGreaterThan(2); |
| 339 | + |
| 340 | + const change = aliceUtxos[0].satoshis + aliceUtxos[1].satoshis - 1000n; |
| 341 | + |
| 342 | + const transaction = new TransactionBuilder({ provider }) |
| 343 | + .addInput(aliceUtxos[0], incorrectSigTemplate.unlockP2PKH()) |
| 344 | + .addInput(p2pkhUtxos[0], p2pkhInstance.unlock.spend(carolPub, new SignatureTemplate(carolPriv))) |
| 345 | + .addOutput({ to: aliceAddress, amount: change }); |
| 346 | + |
| 347 | + await expect(transaction.send()).rejects.toThrow(); |
| 348 | + }); |
296 | 349 | });
|
0 commit comments