Skip to content

Commit

Permalink
Merge pull request #566 from multiversx/TOOL-448-remove-transaction-h…
Browse files Browse the repository at this point in the history
…ash-and-hash

Remove TransactionHash and hash and update to use TransactionComputer.computeTransactionHash
  • Loading branch information
danielailie authored Jan 29, 2025
2 parents 0c69ddc + 2af7f14 commit 4555bb0
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 142 deletions.
16 changes: 5 additions & 11 deletions src/abi/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,30 +248,24 @@ describe("test smart contract interactor", function () {
let transaction = interaction.withSender(alice.address).withNonce(0n).buildTransaction();
transaction.setSender(alice.address);
transaction.applySignature(await alice.signer.sign(transaction.serializeForSigning()));
await provider.sendTransaction(transaction);
let hash = await provider.sendTransaction(transaction);
assert.equal(transaction.getNonce().valueOf(), 0n);
assert.equal(transaction.getData().toString(), "getUltimateAnswer");
assert.equal(
transaction.getHash().toString(),
"3579ad09099feb9755c860ddd225251170806d833342e912fccdfe2ed5c3a364",
);
assert.equal(hash, "3579ad09099feb9755c860ddd225251170806d833342e912fccdfe2ed5c3a364");

transaction = interaction.withNonce(1n).buildTransaction();
transaction.setSender(alice.address);
transaction.applySignature(await alice.signer.sign(transaction.serializeForSigning()));
await provider.sendTransaction(transaction);
hash = await provider.sendTransaction(transaction);
assert.equal(transaction.getNonce(), 1n);
assert.equal(
transaction.getHash().toString(),
"ad513ce7c5d371d30e48f073326899766736eac1ac231d847d45bc3facbcb496",
);
assert.equal(hash, "ad513ce7c5d371d30e48f073326899766736eac1ac231d847d45bc3facbcb496");

// Execute, and wait for execution
transaction = interaction.withNonce(2n).buildTransaction();
transaction.setSender(alice.address);
transaction.applySignature(await alice.signer.sign(transaction.serializeForSigning()));
provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@2bs", "getUltimateAnswer");
let hash = await provider.sendTransaction(transaction);
hash = await provider.sendTransaction(transaction);
let responseExecute = await controller.awaitCompletedExecute(hash);

assert.isTrue(responseExecute.values.length == 1);
Expand Down
11 changes: 7 additions & 4 deletions src/abi/smartContract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TestWallet,
Wait,
} from "../testutils";
import { TransactionComputer } from "../transactionComputer";
import { TransactionStatus } from "../transactionStatus";
import { TransactionWatcher } from "../transactionWatcher";
import { Code } from "./code";
Expand Down Expand Up @@ -73,6 +74,8 @@ describe("test contract", () => {

// Now let's broadcast the deploy transaction, and wait for its execution.
let hash = await provider.sendTransaction(deployTransaction);
const computer = new TransactionComputer();
const hashString = computer.computeTransactionHash(deployTransaction);

await Promise.all([
provider.mockTransactionTimeline(deployTransaction, [
Expand All @@ -82,7 +85,7 @@ describe("test contract", () => {
new TransactionStatus("executed"),
new MarkCompleted(),
]),
watcher.awaitCompleted(deployTransaction.getHash().hex()),
watcher.awaitCompleted(hashString),
]);

assert.isTrue((await provider.getTransaction(hash)).status.isCompleted());
Expand Down Expand Up @@ -150,8 +153,8 @@ describe("test contract", () => {
new TransactionStatus("executed"),
new MarkCompleted(),
]),
watcher.awaitCompleted(callTransactionOne.getHash().hex()),
watcher.awaitCompleted(callTransactionTwo.getHash().hex()),
watcher.awaitCompleted(hashOne),
watcher.awaitCompleted(hashTwo),
]);

assert.isTrue((await provider.getTransaction(hashOne)).status.isCompleted());
Expand Down Expand Up @@ -197,7 +200,7 @@ describe("test contract", () => {
new TransactionStatus("executed"),
new MarkCompleted(),
]),
watcher.awaitCompleted(deployTransaction.getHash().hex()),
watcher.awaitCompleted(hash),
]);

assert.isTrue((await provider.getTransaction(hash)).status.isCompleted());
Expand Down
48 changes: 0 additions & 48 deletions src/hash.ts

This file was deleted.

21 changes: 12 additions & 9 deletions src/testutils/mockNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
} from "../networkProviders/resources";
import { SmartContractQuery, SmartContractQueryResponse } from "../smartContractQuery";
import { Token } from "../tokens";
import { Transaction, TransactionHash } from "../transaction";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionOnNetwork } from "../transactionOnNetwork";
import { SmartContractResult } from "../transactionsOutcomeParsers";
import { TransactionStatus } from "../transactionStatus";
Expand Down Expand Up @@ -129,16 +130,16 @@ export class MockNetworkProvider implements INetworkProvider {
}
}

mockUpdateTransaction(hash: TransactionHash, mutate: (item: TransactionOnNetwork) => void) {
let transaction = this.transactions.get(hash.toString());
mockUpdateTransaction(hash: string, mutate: (item: TransactionOnNetwork) => void) {
let transaction = this.transactions.get(hash);
if (transaction) {
mutate(transaction);
}
}

mockPutTransaction(hash: TransactionHash, item: TransactionOnNetwork) {
mockPutTransaction(hash: string, item: TransactionOnNetwork) {
item.status = TransactionStatus.createUnknown();
this.transactions.set(hash.toString(), item);
this.transactions.set(hash, item);
}

mockQueryContractOnFunction(functionName: string, response: SmartContractQueryResponse) {
Expand Down Expand Up @@ -181,14 +182,15 @@ export class MockNetworkProvider implements INetworkProvider {
}

async mockTransactionTimeline(transaction: Transaction, timelinePoints: any[]): Promise<void> {
return this.mockTransactionTimelineByHash(transaction.getHash(), timelinePoints);
const computer = new TransactionComputer();
return this.mockTransactionTimelineByHash(computer.computeTransactionHash(transaction), timelinePoints);
}

async mockNextTransactionTimeline(timelinePoints: any[]): Promise<void> {
this.nextTransactionTimelinePoints = timelinePoints;
}

async mockTransactionTimelineByHash(hash: TransactionHash, timelinePoints: any[]): Promise<void> {
async mockTransactionTimelineByHash(hash: string, timelinePoints: any[]): Promise<void> {
let timeline = new AsyncTimer(`mock timeline of ${hash}`);

await timeline.start(0);
Expand Down Expand Up @@ -222,8 +224,9 @@ export class MockNetworkProvider implements INetworkProvider {
}

async sendTransaction(transaction: Transaction): Promise<string> {
const computer = new TransactionComputer();
this.mockPutTransaction(
transaction.getHash(),
computer.computeTransactionHash(transaction),
new TransactionOnNetwork({
sender: transaction.getSender(),
receiver: transaction.getReceiver(),
Expand All @@ -233,7 +236,7 @@ export class MockNetworkProvider implements INetworkProvider {
);

this.mockTransactionTimeline(transaction, this.nextTransactionTimelinePoints);
return transaction.getHash().hex();
return computer.computeTransactionHash(transaction);
}

async simulateTransaction(_transaction: Transaction): Promise<any> {
Expand Down
12 changes: 6 additions & 6 deletions src/transaction.local.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ describe("test transaction", function () {
await signTransaction({ transaction: transactionOne, wallet: alice });
await signTransaction({ transaction: transactionTwo, wallet: alice });

await provider.sendTransaction(transactionOne);
await provider.sendTransaction(transactionTwo);
const hashOne = await provider.sendTransaction(transactionOne);
const hashTwo = await provider.sendTransaction(transactionTwo);

await watcher.awaitCompleted(transactionOne.getHash().hex());
await watcher.awaitCompleted(transactionTwo.getHash().hex());
await watcher.awaitCompleted(hashOne);
await watcher.awaitCompleted(hashTwo);

await bob.sync(provider);
let newBalanceOfBob = new BigNumber((await bob.getBalance(provider)).toString());
Expand Down Expand Up @@ -100,8 +100,8 @@ describe("test transaction", function () {

transactionOne.setNonce(alice.account.nonce);
await signTransaction({ transaction: transactionOne, wallet: alice });
await provider.sendTransaction(transactionOne);
await watcher.awaitCompleted(transactionOne.getHash().hex());
const hashOne = await provider.sendTransaction(transactionOne);
await watcher.awaitCompleted(hashOne);

await bob.sync(provider);
let newBalanceOfBob = new BigNumber((await bob.getBalance(provider)).toString());
Expand Down
33 changes: 12 additions & 21 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("test transaction", async () => {
"3f08a1dd64fbb627d10b048e0b45b1390f29bb0e457762a2ccb710b029f299022a67a4b8e45cf62f4314afec2e56b5574c71e38df96cc41fae757b7ee5062503",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction),
"1359fb9d5b0b47ca9f3b4adce6e4a524fa74099dd4732743b9226774a4cb0ad8",
);
});
Expand All @@ -109,7 +109,7 @@ describe("test transaction", async () => {
"f9e8c1caf7f36b99e7e76ee1118bf71b55cde11a2356e2b3adf15f4ad711d2e1982469cbba7eb0afbf74e8a8f78e549b9410cd86eeaa88fcba62611ac9f6e30e",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction),
"10a2bd6f9c358d2c9645368081999efd2a4cc7f24bdfdd75e8f57485fd702001",
);
});
Expand Down Expand Up @@ -156,10 +156,7 @@ describe("test transaction", async () => {

const hash = transactionComputer.computeTransactionHash(transaction);

assert.equal(
Buffer.from(hash).toString("hex"),
"169b76b752b220a76a93aeebc462a1192db1dc2ec9d17e6b4d7b0dcc91792f03",
);
assert.equal(hash, "169b76b752b220a76a93aeebc462a1192db1dc2ec9d17e6b4d7b0dcc91792f03");
});

it("should compute hash (with usernames)", async () => {
Expand All @@ -183,10 +180,7 @@ describe("test transaction", async () => {

const hash = transactionComputer.computeTransactionHash(transaction);

assert.equal(
Buffer.from(hash).toString("hex"),
"41b5acf7ebaf4a9165a64206b6ebc02021b3adda55ffb2a2698aac2e7004dc29",
);
assert.equal(hash, "41b5acf7ebaf4a9165a64206b6ebc02021b3adda55ffb2a2698aac2e7004dc29");
});

it("should sign & compute hash (with data, with opaque, unused options) (legacy)", async () => {
Expand Down Expand Up @@ -227,7 +221,7 @@ describe("test transaction", async () => {
"b45f22e9f57a6df22670fcc3566723a0711a05ac2547456de59fd222a54940e4a1d99bd414897ccbf5c02a842ad86e638989b7f4d30edd26c99a8cd1eb092304",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction).toString(),
"84125d7154d81a723642100bdf74e6df99f7c069c016d1e6bbeb408fd4e961bf",
);
});
Expand All @@ -251,7 +245,7 @@ describe("test transaction", async () => {
"01f05aa8cb0614e12a94ab9dcbde5e78370a4e05d23ef25a1fb9d5fcf1cb3b1f33b919cd8dafb1704efb18fa233a8aa0d3344fb6ee9b613a7d7a403786ffbd0a",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction),
"321e1f1a0e3d06edade34fd0fdf3b4859e4328a73706a442c2439968a074113c",
);
});
Expand All @@ -276,7 +270,7 @@ describe("test transaction", async () => {
"dfa3e9f2fdec60dcb353bac3b3435b4a2ff251e7e98eaf8620f46c731fc70c8ba5615fd4e208b05e75fe0f7dc44b7a99567e29f94fcd91efac7e67b182cd2a04",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction),
"6ffa1a75f98aaf336bfb87ef13b9b5a477a017158285d34ee2a503668767e69e",
);
});
Expand All @@ -299,7 +293,7 @@ describe("test transaction", async () => {
"3f08a1dd64fbb627d10b048e0b45b1390f29bb0e457762a2ccb710b029f299022a67a4b8e45cf62f4314afec2e56b5574c71e38df96cc41fae757b7ee5062503",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction),
"1359fb9d5b0b47ca9f3b4adce6e4a524fa74099dd4732743b9226774a4cb0ad8",
);

Expand All @@ -325,7 +319,7 @@ describe("test transaction", async () => {
"3f08a1dd64fbb627d10b048e0b45b1390f29bb0e457762a2ccb710b029f299022a67a4b8e45cf62f4314afec2e56b5574c71e38df96cc41fae757b7ee5062503",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction),
"1359fb9d5b0b47ca9f3b4adce6e4a524fa74099dd4732743b9226774a4cb0ad8",
);

Expand All @@ -352,7 +346,7 @@ describe("test transaction", async () => {
"51e6cd78fb3ab4b53ff7ad6864df27cb4a56d70603332869d47a5cf6ea977c30e696103e41e8dddf2582996ad335229fdf4acb726564dbc1a0bc9e705b511f06",
);
assert.equal(
transaction.getHash().toString(),
transactionComputer.computeTransactionHash(transaction),
"edc84d776bfd655ddbd6fce24a83e379496ac47890d00be9c8bb2c6666fa3fd8",
);
});
Expand All @@ -371,7 +365,7 @@ describe("test transaction", async () => {
options: 2,
nonce: 92n,
value: 123456789000000000000000000000n,
guardian: Address.fromBech32("erd1x23lzn8483xs2su4fak0r0dqx6w38enpmmqf2yrkylwq7mfnvyhsxqw57y"),
guardian: Address.newFromBech32("erd1x23lzn8483xs2su4fak0r0dqx6w38enpmmqf2yrkylwq7mfnvyhsxqw57y"),
});
transaction.guardianSignature = new Uint8Array(64);
transaction.signature = await alice.signer.sign(transactionComputer.computeBytesForSigning(transaction));
Expand All @@ -385,10 +379,7 @@ describe("test transaction", async () => {
);

const txHash = transactionComputer.computeTransactionHash(transaction);
assert.equal(
Buffer.from(txHash).toString("hex"),
"242022e9dcfa0ee1d8199b0043314dbda8601619f70069ebc441b9f03349a35c",
);
assert.equal(txHash, "242022e9dcfa0ee1d8199b0043314dbda8601619f70069ebc441b9f03349a35c");
});

it("computes fee (legacy)", () => {
Expand Down
28 changes: 0 additions & 28 deletions src/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BigNumber } from "bignumber.js";
import { Address } from "./address";
import { TRANSACTION_MIN_GAS_PRICE, TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_VERSION_DEFAULT } from "./constants";
import { Hash } from "./hash";
import { IPlainTransactionObject } from "./interface";
import { INetworkConfig } from "./interfaceOfNetwork";
import { interpretSignatureAsBuffer } from "./signature";
Expand Down Expand Up @@ -324,13 +323,6 @@ export class Transaction {
this.guardian = guardian;
}

/**
* Legacy method, use "TransactionComputer.computeTransactionHash()" instead.
*/
getHash(): TransactionHash {
return TransactionHash.compute(this);
}

/**
* Legacy method, use "TransactionComputer.computeBytesForSigning()" instead.
* Serializes a transaction to a sequence of bytes, ready to be signed.
Expand Down Expand Up @@ -467,23 +459,3 @@ export class Transaction {
return value && value.length ? Buffer.from(value).toString("hex") : undefined;
}
}

/**
* Legacy class, use "TransactionComputer.computeTransactionHash()" instead.
* An abstraction for handling and computing transaction hashes.
*/
export class TransactionHash extends Hash {
constructor(hash: string) {
super(hash);
}

/**
* Legacy method, use "TransactionComputer.computeTransactionHash()" instead.
* Computes the hash of a transaction.
*/
static compute(transaction: Transaction): TransactionHash {
const computer = new TransactionComputer();
const hash = computer.computeTransactionHash(transaction);
return new TransactionHash(Buffer.from(hash).toString("hex"));
}
}
Loading

0 comments on commit 4555bb0

Please sign in to comment.