Skip to content

Commit

Permalink
Remove Transaction converter
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Dec 12, 2024
1 parent 372cd24 commit 2553c87
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 126 deletions.
1 change: 0 additions & 1 deletion src/converters/index.ts

This file was deleted.

65 changes: 0 additions & 65 deletions src/converters/transactionsConverter.ts

This file was deleted.

48 changes: 0 additions & 48 deletions src/converters/transactionsConverters.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export * from "./accounts";
export * from "./address";
export * from "./asyncTimer";
export * from "./config";
export * from "./converters";
export * from "./delegation";
export * from "./entrypoints";
export * from "./errors";
Expand Down
40 changes: 40 additions & 0 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,4 +748,44 @@ describe("test transaction", async () => {
assert.equal(isSignedByAlice, true);
assert.equal(isSignedByBob, false);
});

it("converts transaction to plain object and back", () => {
const transaction = new Transaction({
nonce: 90n,
value: BigInt("123456789000000000000000000000"),
sender: Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
receiver: Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"),
senderUsername: "alice",
receiverUsername: "bob",
gasPrice: 1000000000n,
gasLimit: 80000n,
data: Buffer.from("hello"),
chainID: "localnet",
version: 2,
});

const plainObject = transaction.toPlainObject();
const restoredTransaction = Transaction.newFromPlainObject(plainObject);

assert.deepEqual(plainObject, transaction.toPlainObject());
assert.deepEqual(restoredTransaction, Transaction.fromPlainObject(plainObject));
assert.deepEqual(restoredTransaction, transaction);
assert.deepEqual(plainObject, {
nonce: 90,
value: "123456789000000000000000000000",
sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
receiver: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx",
senderUsername: "YWxpY2U=",
receiverUsername: "Ym9i",
gasPrice: 1000000000,
gasLimit: 80000,
data: "aGVsbG8=",
chainID: "localnet",
version: 2,
options: undefined,
guardian: undefined,
signature: undefined,
guardianSignature: undefined,
});
});
});
68 changes: 57 additions & 11 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 { TransactionsConverter } from "./converters/transactionsConverter";
import { Hash } from "./hash";
import { IGasLimit, IGasPrice, INonce, IPlainTransactionObject, ISignature, ITransactionValue } from "./interface";
import { INetworkConfig } from "./interfaceOfNetwork";
Expand Down Expand Up @@ -341,27 +340,66 @@ export class Transaction {
}

/**
* Legacy method, use "TransactionsConverter.transactionToPlainObject()" instead.
*
* Converts the transaction object into a ready-to-serialize, plain JavaScript object.
* This function is called internally within the signing procedure.
*/
toPlainObject(): IPlainTransactionObject {
// Ideally, "converters" package should be outside of "core", and not referenced here.
const converter = new TransactionsConverter();
return converter.transactionToPlainObject(this);
const plainObject = {
nonce: Number(this.nonce),
value: this.value.toString(),
receiver: this.receiver.toBech32(),
sender: this.sender.toBech32(),
senderUsername: this.toBase64OrUndefined(this.senderUsername),
receiverUsername: this.toBase64OrUndefined(this.receiverUsername),
gasPrice: Number(this.gasPrice),
gasLimit: Number(this.gasLimit),
data: this.toBase64OrUndefined(this.data),
chainID: this.chainID.valueOf(),
version: this.version,
options: this.options == 0 ? undefined : this.options,
guardian: this.guardian.isEmpty() ? undefined : this.guardian.toBech32(),
signature: this.toHexOrUndefined(this.signature),
guardianSignature: this.toHexOrUndefined(this.guardianSignature),
};

return plainObject;
}

/**
* Legacy method, use "Transaction.newFromPlainObject()" instead.
* Converts a plain object transaction into a Transaction Object.
*
* @param plainObjectTransaction Raw data of a transaction, usually obtained by calling toPlainObject()
*/
static fromPlainObject(plainObjectTransaction: IPlainTransactionObject): Transaction {
return Transaction.newFromPlainObject(plainObjectTransaction);
}

/**
* Legacy method, use "TransactionsConverter.plainObjectToTransaction()" instead.
* Converts a plain object transaction into a Transaction Object.
*
* @param plainObjectTransaction Raw data of a transaction, usually obtained by calling toPlainObject()
*/
static fromPlainObject(plainObjectTransaction: IPlainTransactionObject): Transaction {
// Ideally, "converters" package should be outside of "core", and not referenced here.
const converter = new TransactionsConverter();
return converter.plainObjectToTransaction(plainObjectTransaction);
static newFromPlainObject(object: IPlainTransactionObject): Transaction {
const transaction = new Transaction({
nonce: BigInt(object.nonce),
value: BigInt(object.value || ""),
receiver: Address.newFromBech32(object.receiver),
receiverUsername: Buffer.from(object.receiverUsername || "", "base64").toString(),
sender: Address.newFromBech32(object.sender),
senderUsername: Buffer.from(object.senderUsername || "", "base64").toString(),
guardian: object.guardian ? Address.newFromBech32(object.guardian) : Address.empty(),
gasPrice: BigInt(object.gasPrice),
gasLimit: BigInt(object.gasLimit),
data: Buffer.from(object.data || "", "base64"),
chainID: String(object.chainID),
version: Number(object.version),
options: Number(object.options),
signature: Buffer.from(object.signature || "", "hex"),
guardianSignature: Buffer.from(object.guardianSignature || "", "hex"),
});

return transaction;
}

/**
Expand Down Expand Up @@ -403,6 +441,14 @@ export class Transaction {
const fee = computer.computeTransactionFee(this, networkConfig);
return new BigNumber(fee.toString());
}

private toBase64OrUndefined(value?: string | Uint8Array) {
return value && value.length ? Buffer.from(value).toString("base64") : undefined;
}

private toHexOrUndefined(value?: Uint8Array) {
return value && value.length ? Buffer.from(value).toString("hex") : undefined;
}
}

/**
Expand Down

0 comments on commit 2553c87

Please sign in to comment.