Skip to content

Commit

Permalink
fix: 1560 Transaction.ts allows making of incomplete signed instances (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucanicoladebiasi authored Dec 9, 2024
1 parent c90db4d commit 6039727
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
19 changes: 3 additions & 16 deletions packages/core/src/transaction/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as nc_utils from '@noble/curves/abstract/utils';
import {
InvalidDataType,
InvalidSecp256k1PrivateKey,
InvalidSecp256k1Signature,
InvalidTransactionField,
NotDelegatedTransaction,
UnavailableTransactionField
Expand Down Expand Up @@ -474,31 +473,19 @@ class Transaction {
}

/**
* Creates a new Transaction instance if the provided body and optional
* signature are valid.
* Creates a new Transaction instance if the provided body is valid.
*
* @param {TransactionBody} body - The transaction body to be validated.
* @param {Uint8Array} [signature] - Optional signature to be validated.
* @param {Uint8Array} [signature] - Optional signature.
* @return {Transaction} A new Transaction instance if validation is successful.
* @throws {InvalidSecp256k1Signature} If the provided signature is invalid.
* @throws {InvalidTransactionField} If the provided body is invalid.
*/
public static of(
body: TransactionBody,
signature?: Uint8Array
): Transaction {
if (Transaction.isValidBody(body)) {
if (
signature === undefined ||
Transaction.isSignatureValid(body, signature)
) {
return new Transaction(body, signature);
}
throw new InvalidSecp256k1Signature(
'Transaction.of',
'invalid signature',
{ signature }
);
return new Transaction(body, signature);
}
throw new InvalidTransactionField('Transaction.of', 'invalid body', {
fieldName: 'body',
Expand Down
34 changes: 24 additions & 10 deletions packages/core/tests/transaction/Transaction.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { describe, expect } from '@jest/globals';
import {
InvalidDataType,
InvalidSecp256k1PrivateKey,
InvalidSecp256k1Signature,
InvalidTransactionField,
NotDelegatedTransaction,
UnavailableTransactionField
Expand Down Expand Up @@ -217,6 +216,30 @@ describe('Transaction class tests', () => {
true
);
});

test('Transaction <- of delegated transactions simulating partial signed transmission between two different JS runtimes.', () => {
const signed = Transaction.of(
TransactionFixture.delegated.body
).signForDelegator(SignerFix.privateKey);

const reconstructed = Transaction.of(
signed.body,
signed.signature
);
expect(reconstructed.signature).toBeDefined();
expect(reconstructed.isDelegated).toBe(true);
expect(reconstructed.isSigned).toBe(false);

const signer = Address.ofPrivateKey(SignerFix.privateKey);
const actual = signed.signAsDelegator(
signer,
DelegatorFix.privateKey
);
expect(actual.isDelegated).toBe(true);
expect(actual.isSigned).toBe(true);
const expected = TransactionFixture.delegated.encodedSigned;
expect(actual.encoded).toEqual(expected);
});
});

describe('of signed transactions', () => {
Expand Down Expand Up @@ -305,15 +328,6 @@ describe('Transaction class tests', () => {
});

describe('Exceptions', () => {
test('Throw <- of invalid signature', () => {
expect(() =>
Transaction.of(
TransactionFixture.delegated.body,
HexUInt.of('0xBAAAAAAD').bytes
)
).toThrowError(InvalidSecp256k1Signature);
});

test('Throw <- of invalid body', () => {
expect(() =>
Transaction.of({
Expand Down

1 comment on commit 6039727

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 99%
99% (4366/4410) 97.55% (1397/1432) 99.01% (906/915)
Title Tests Skipped Failures Errors Time
core 836 0 💤 0 ❌ 0 🔥 2m 24s ⏱️
network 727 0 💤 0 ❌ 0 🔥 5m 7s ⏱️
errors 42 0 💤 0 ❌ 0 🔥 17.931s ⏱️
logging 3 0 💤 0 ❌ 0 🔥 19.182s ⏱️
hardhat-plugin 19 0 💤 0 ❌ 0 🔥 1m 5s ⏱️
aws-kms-adapter 23 0 💤 0 ❌ 0 🔥 1m 10s ⏱️
ethers-adapter 5 0 💤 0 ❌ 0 🔥 1m 15s ⏱️
rpc-proxy 37 0 💤 0 ❌ 0 🔥 1m 6s ⏱️

Please sign in to comment.