From f7421477cb1be9b2edbd5e682d6c89ab12e27765 Mon Sep 17 00:00:00 2001 From: Evan Zheng Date: Fri, 23 Jun 2023 18:17:39 -0700 Subject: [PATCH] fix(sdk-coin-ada): cosmetic signature array might duplicate The signature array of native txn wrapper might duplicate in certain case EA-621 --- .../src/lib/transactionBuilder.ts | 3 +++ .../test/unit/stakingPledgeBuilder.ts | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/sdk-coin-ada/src/lib/transactionBuilder.ts b/modules/sdk-coin-ada/src/lib/transactionBuilder.ts index dd4713d294..bf942aad5c 100644 --- a/modules/sdk-coin-ada/src/lib/transactionBuilder.ts +++ b/modules/sdk-coin-ada/src/lib/transactionBuilder.ts @@ -291,6 +291,9 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder { ); vkeyWitnesses.add(vkeyWitness); }); + + // Clear the cosmetic signature array in native txn wrapper to prevent duplicate when builder is inited from a partially witnessed txn + this._transaction.signature.length = 0; this.getAllSignatures().forEach((signature) => { const vkey = CardanoWasm.Vkey.new(CardanoWasm.PublicKey.from_bytes(Buffer.from(signature.publicKey.pub, 'hex'))); const ed255Sig = CardanoWasm.Ed25519Signature.from_bytes(signature.signature); diff --git a/modules/sdk-coin-ada/test/unit/stakingPledgeBuilder.ts b/modules/sdk-coin-ada/test/unit/stakingPledgeBuilder.ts index d9d81e1822..2e67852729 100644 --- a/modules/sdk-coin-ada/test/unit/stakingPledgeBuilder.ts +++ b/modules/sdk-coin-ada/test/unit/stakingPledgeBuilder.ts @@ -78,11 +78,12 @@ describe('ADA Staking Pledge Transaction Builder', async () => { rebuiltTx.toBroadcastFormat().should.not.equal(rawTx.unsignedUpdatePledgeTx); }); - it('should init partially signed txn hex and preserver the signature', async () => { + it('should init from partially signed txn hex and preserve the signature', async () => { const txnBuilderFactory = new TransactionBuilderFactory(coins.get('tada')); const txnBuilder = txnBuilderFactory.from(rawTx.partiallySignedPledgeTx); let tx = (await txnBuilder.build()) as Transaction; tx.type.should.equal(TransactionType.StakingPledge); + tx.signature.length.should.equal(1); let txData = tx.toJson(); txData.witnesses.length.should.equal(1); txData.witnesses[0].publicKey.should.equal(rawTx.pledgeNodeKeyPubkey); @@ -98,4 +99,21 @@ describe('ADA Staking Pledge Transaction Builder', async () => { txData.witnesses[1].publicKey.should.equal(rawTx.pledgeNodeKeyPubkey); txData.witnesses[1].signature.should.equal(rawTx.pledgeNodeWitnessSignature); }); + + it('should init from partially signed txn object and preserve the signature', async () => { + const prebuiltTx = new Transaction(coins.get('tada')); + prebuiltTx.fromRawTransaction(rawTx.partiallySignedPledgeTx); + prebuiltTx.toBroadcastFormat().should.equal(rawTx.partiallySignedPledgeTx); + prebuiltTx.signature.length.should.equal(1); + const txBuilder = factory.getStakingPledgeBuilder(); + txBuilder.initBuilder(prebuiltTx); + const tx = (await txBuilder.build()) as Transaction; + tx.type.should.equal(TransactionType.StakingPledge); + tx.toBroadcastFormat().should.equal(rawTx.partiallySignedPledgeTx); + tx.signature.length.should.equal(1); + const txData = tx.toJson(); + txData.witnesses.length.should.equal(1); + txData.witnesses[0].publicKey.should.equal(rawTx.pledgeNodeKeyPubkey); + txData.witnesses[0].signature.should.equal(rawTx.pledgeNodeWitnessSignature); + }); });