Skip to content

Commit

Permalink
Update Relayer not to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Dec 12, 2024
1 parent 642b00a commit b810a2a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/converters/transactionsConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions src/proto/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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([]);
Expand Down
10 changes: 1 addition & 9 deletions src/transactionComputer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import BigNumber from "bignumber.js";
import { Address } from "./address";
import {
MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS,
TRANSACTION_OPTIONS_TX_GUARDED,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b810a2a

Please sign in to comment.