Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the Relayed Transactions Factory #369

Merged
merged 9 commits into from
Jan 19, 2024
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "MIT",
"dependencies": {
"@multiversx/sdk-transaction-decoder": "1.0.2",
"json-bigint": "1.0.0",
"bech32": "1.1.4",
"bignumber.js": "9.0.1",
"blake2b": "2.1.3",
Expand Down
9 changes: 9 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,12 @@ export class ErrBadUsage extends Err {
super(message);
}
}

/**
* Signals an invalid inner transaction for relayed transactions
*/
export class ErrInvalidInnerTransaction extends Err{
public constructor(message: string){
super(message);
}
}
36 changes: 36 additions & 0 deletions src/relayedTransactionV1Builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,42 @@ describe("test relayed v1 transaction builder", function () {
assert.equal(relayedTxV1.getSignature().toString("hex"), "3787d640e5a579e7977a4a1bcdd435ad11855632fa4a414a06fbf8355692d1a58d76ef0adbdd6ccd6bd3c329f36bd53c180d4873ec1a6c558e659aeb9ab92d00");
});

it("should compute relayed v1 transaction with big value", async function () {
const networkConfig = {
MinGasLimit: 50_000,
GasPerDataByte: 1_500,
GasPriceModifier: 0.01,
ChainID: "T"
};

const innerTx = new Transaction({
nonce: 208,
value: TokenTransfer.egldFromAmount(1999999),
sender: carol.address,
receiver: alice.address,
senderUsername: "carol",
receiverUsername: "alice",
gasLimit: 50000,
chainID: networkConfig.ChainID
});

innerTx.applySignature(await carol.signer.sign(innerTx.serializeForSigning()));

const builder = new RelayedTransactionV1Builder();
const relayedTxV1 = builder
.setInnerTransaction(innerTx)
.setRelayerNonce(715)
.setNetworkConfig(networkConfig)
.setRelayerAddress(frank.address)
.build();

relayedTxV1.applySignature(await frank.signer.sign(relayedTxV1.serializeForSigning()));

assert.equal(relayedTxV1.getNonce().valueOf(), 715);
assert.equal(relayedTxV1.getData().toString(), "relayedTx@7b226e6f6e6365223a3230382c2273656e646572223a227371455656633553486b6c45344a717864556e59573068397a536249533141586f3534786f32634969626f3d222c227265636569766572223a2241546c484c76396f686e63616d433877673970645168386b77704742356a6949496f3349484b594e6165453d222c2276616c7565223a313939393939393030303030303030303030303030303030302c226761735072696365223a313030303030303030302c226761734c696d6974223a35303030302c2264617461223a22222c227369676e6174757265223a22594661677972512f726d614c7333766e7159307657553858415a7939354b4e31725738347a4f764b62376c7a3773576e2f566a546d68704378774d682b7261314e444832574d6f3965507648304f79427453776a44773d3d222c22636861696e4944223a2256413d3d222c2276657273696f6e223a322c22736e64557365724e616d65223a22593246796232773d222c22726376557365724e616d65223a22595778705932553d227d");
assert.equal(relayedTxV1.getSignature().toString("hex"), "c0fb5cf8c0a413d6988ba35dc279c63f8849572c5f23b1cab36dcc50952dc3ed9da01068d6ac0cbde7e14167bfc2eca5164d5c2154c89eb313c9c596e3f8b801");
});

it("should compute guarded inner Tx - relayed v1 transaction", async function () {
const networkConfig = {
MinGasLimit: 50_000,
Expand Down
9 changes: 7 additions & 2 deletions src/relayedTransactionV1Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { TransactionOptions, TransactionVersion } from "./networkParams";
import { Transaction } from "./transaction";
import { TransactionPayload } from "./transactionPayload";

const JSONbig = require("json-bigint");

/**
* @deprecated Use {@link RelayedTransactionsFactory} instead.
*/
export class RelayedTransactionV1Builder {
innerTransaction: Transaction | undefined;
relayerAddress: IAddress | undefined;
Expand Down Expand Up @@ -131,7 +136,7 @@ export class RelayedTransactionV1Builder {
"nonce": this.innerTransaction.getNonce().valueOf(),
"sender": new Address(this.innerTransaction.getSender().bech32()).pubkey().toString("base64"),
"receiver": new Address(this.innerTransaction.getReceiver().bech32()).pubkey().toString("base64"),
"value": new BigNumber(this.innerTransaction.getValue().toString(), 10).toNumber(),
"value": BigInt(this.innerTransaction.getValue().toString()),
"gasPrice": this.innerTransaction.getGasPrice().valueOf(),
"gasLimit": this.innerTransaction.getGasLimit().valueOf(),
"data": this.innerTransaction.getData().valueOf().toString("base64"),
Expand All @@ -145,6 +150,6 @@ export class RelayedTransactionV1Builder {
"rcvUserName": this.innerTransaction.getReceiverUsername() ? Buffer.from(this.innerTransaction.getReceiverUsername()).toString("base64") : undefined,
};

return JSON.stringify(txObject);
return JSONbig.stringify(txObject);
}
}
3 changes: 3 additions & 0 deletions src/relayedTransactionV2Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { AddressValue, ArgSerializer, BytesValue, U64Value } from "./smartcontra
import { Transaction } from "./transaction";
import { TransactionPayload } from "./transactionPayload";

/**
* @deprecated Use {@link RelayedTransactionsFactory} instead.
*/
export class RelayedTransactionV2Builder {
innerTransaction: Transaction | undefined;
innerTransactionGasLimit: IGasLimit | undefined;
Expand Down
3 changes: 3 additions & 0 deletions src/smartcontracts/transactionPayloadBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const WasmVirtualMachine = "0500";
/**
* A builder for {@link TransactionPayload} objects, to be used for Smart Contract deployment transactions.
*/
/**
* @deprecated Use {@link SmartContractTransactionsFactory} instead.
*/
export class ContractDeployPayloadBuilder {
private code: ICode | null = null;
private codeMetadata: ICodeMetadata = "";
Expand Down
Loading
Loading