Skip to content

Commit cc66c64

Browse files
authored
Allowing signasgaspayer on unsigned tx (#1883)
* feat: allowing signing as gas payer on unsigned transactions * feat: fixed unit tests
1 parent e932ffb commit cc66c64

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

packages/core/src/transaction/Transaction.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,8 @@ class Transaction {
557557
): Transaction {
558558
if (Secp256k1.isValidPrivateKey(gasPayerPrivateKey)) {
559559
if (this.isDelegated) {
560+
const senderHash = this.getTransactionHash(sender).bytes;
560561
if (this.signature !== undefined) {
561-
const senderHash = this.getTransactionHash(sender).bytes;
562562
return new Transaction(
563563
this.body,
564564
nc_utils.concatBytes(
@@ -567,12 +567,12 @@ class Transaction {
567567
Secp256k1.sign(senderHash, gasPayerPrivateKey)
568568
)
569569
);
570+
} else {
571+
return new Transaction(
572+
this.body,
573+
Secp256k1.sign(senderHash, gasPayerPrivateKey)
574+
);
570575
}
571-
throw new InvalidTransactionField(
572-
'Transaction.signAsGasPayer',
573-
'unsigned transaction: use signAsSender method',
574-
{ fieldName: 'signature' }
575-
);
576576
}
577577
throw new NotDelegatedTransaction(
578578
'Transaction.signAsGasPayer',

packages/core/tests/transaction/Transaction.unit.test.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,17 @@ describe('Transaction class tests', () => {
701701
}).toThrowError(NotDelegatedTransaction);
702702
});
703703

704-
test('Throw <- unsigned tx', () => {
705-
expect(() => {
706-
Transaction.of(
707-
TransactionFixture.delegated.body
708-
).signAsGasPayer(
709-
Address.ofPrivateKey(SignerFix.privateKey),
710-
SignerFix.privateKey
711-
);
712-
}).toThrowError(InvalidTransactionField);
704+
test('signature (only of the gasPayer) <- unsigned tx', () => {
705+
expect(
706+
(
707+
Transaction.of(
708+
TransactionFixture.delegated.body
709+
).signAsGasPayer(
710+
Address.ofPrivateKey(SignerFix.privateKey),
711+
SignerFix.privateKey
712+
).signature as Uint8Array
713+
).length
714+
).toBe(65);
713715
});
714716

715717
test('Throw <- invalid private keys - delegated tx', () => {

0 commit comments

Comments
 (0)