From b810a2aef50a4bac48a475ca3fe715a756ac7b56 Mon Sep 17 00:00:00 2001 From: danielailie Date: Thu, 12 Dec 2024 12:43:11 +0200 Subject: [PATCH] Update Relayer not to be optional --- src/converters/transactionsConverter.ts | 2 +- src/interface.ts | 4 ++-- src/proto/serializer.ts | 8 ++++++-- src/transaction.spec.ts | 8 ++++---- src/transaction.ts | 7 ++++--- src/transactionComputer.ts | 10 +--------- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/converters/transactionsConverter.ts b/src/converters/transactionsConverter.ts index d97c2be0..c3798fbe 100644 --- a/src/converters/transactionsConverter.ts +++ b/src/converters/transactionsConverter.ts @@ -26,7 +26,7 @@ export class TransactionsConverter { chainID: transaction.chainID.valueOf(), version: transaction.version, options: transaction.options == 0 ? undefined : transaction.options, - relayer: !transaction.relayer || transaction.relayer.isEmpty() ? undefined : transaction.relayer.toBech32(), + relayer: transaction.relayer.isEmpty() ? undefined : transaction.relayer.toBech32(), guardian: transaction.guardian ? transaction.guardian : undefined, signature: this.toHexOrUndefined(transaction.signature), guardianSignature: this.toHexOrUndefined(transaction.guardianSignature), diff --git a/src/interface.ts b/src/interface.ts index dc7d3f23..f91d4b0a 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -105,8 +105,8 @@ export interface ITransaction { version: number; options: number; guardian: string; - relayer?: Address; + relayer: Address; signature: Uint8Array; guardianSignature: Uint8Array; - relayerSignature?: Uint8Array; + relayerSignature: Uint8Array; } diff --git a/src/proto/serializer.ts b/src/proto/serializer.ts index 8cb95a29..f1693c84 100644 --- a/src/proto/serializer.ts +++ b/src/proto/serializer.ts @@ -60,14 +60,18 @@ export class ProtoSerializer { protoTransaction.GuardianSignature = transaction.guardianSignature; } - if (transaction.relayer && !transaction.relayer.isEmpty()) { - protoTransaction.Relayer = transaction.relayer.getPublicKey(); + if (this.isRelayedTransaction(transaction)) { + protoTransaction.Relayer = transaction.relayer?.getPublicKey(); protoTransaction.RelayerSignature = transaction.relayerSignature; } return protoTransaction; } + private isRelayedTransaction(transaction: ITransaction) { + return !transaction.relayer.isEmpty(); + } + /** * Custom serialization, compatible with mx-chain-go. */ diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 7e4eefff..dfce314e 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -755,8 +755,8 @@ describe("test transaction", async () => { it("should serialize transaction with relayer", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.alice.address.toBech32(), + receiver: wallets.alice.address.toBech32(), relayer: wallets.bob.address, gasLimit: 50000n, value: 0n, @@ -776,8 +776,8 @@ describe("test transaction", async () => { it("should test relayed v3", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.alice.address.toBech32(), + receiver: wallets.alice.address.toBech32(), senderUsername: "alice", receiverUsername: "bob", gasLimit: 80000n, diff --git a/src/transaction.ts b/src/transaction.ts index 9bf6cdd2..5b0b7a96 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -92,9 +92,10 @@ export class Transaction { public guardian: string; /** - * The relayer, in address format, next version all the other addresses will not be string anymore. + * The relayer address. + * Note: in the next major version, `sender`, `receiver` and `guardian` will also have the type `Address`, instead of `string`. */ - public relayer?: Address; + public relayer: Address; /** * The signature. @@ -148,7 +149,7 @@ export class Transaction { this.version = Number(options.version?.valueOf() || TRANSACTION_VERSION_DEFAULT); this.options = Number(options.options?.valueOf() || TRANSACTION_OPTIONS_DEFAULT); this.guardian = options.guardian ? this.addressAsBech32(options.guardian) : ""; - this.relayer = options.relayer?.isEmpty() ? undefined : options.relayer; + this.relayer = options.relayer ? options.relayer : Address.empty(); this.signature = options.signature || Buffer.from([]); this.guardianSignature = options.guardianSignature || Buffer.from([]); diff --git a/src/transactionComputer.ts b/src/transactionComputer.ts index 7e437056..a9690b2f 100644 --- a/src/transactionComputer.ts +++ b/src/transactionComputer.ts @@ -1,5 +1,4 @@ import BigNumber from "bignumber.js"; -import { Address } from "./address"; import { MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS, TRANSACTION_OPTIONS_TX_GUARDED, @@ -95,15 +94,8 @@ export class TransactionComputer { transaction.guardian = guardian; } - applyRelayer(transaction: ITransaction, relayer: Address) { - transaction.relayer = relayer; - } - isRelayedV3Transaction(transaction: ITransaction) { - if (transaction.relayer && !transaction.relayer.isEmpty()) { - return true; - } - return false; + return !transaction.relayer.isEmpty(); } applyOptionsForHashSigning(transaction: ITransaction) {