diff --git a/src/converters/index.ts b/src/converters/index.ts deleted file mode 100644 index f43e67fc..00000000 --- a/src/converters/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./transactionsConverter"; diff --git a/src/converters/transactionsConverter.ts b/src/converters/transactionsConverter.ts deleted file mode 100644 index ba6d7649..00000000 --- a/src/converters/transactionsConverter.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { Address } from "../address"; -import { IPlainTransactionObject } from "../interface"; -import { Transaction } from "../transaction"; - -export class TransactionsConverter { - public transactionToPlainObject(transaction: Transaction): IPlainTransactionObject { - const plainObject = { - nonce: Number(transaction.nonce), - value: transaction.value.toString(), - receiver: transaction.receiver.toBech32(), - sender: transaction.sender.toBech32(), - senderUsername: this.toBase64OrUndefined(transaction.senderUsername), - receiverUsername: this.toBase64OrUndefined(transaction.receiverUsername), - gasPrice: Number(transaction.gasPrice), - gasLimit: Number(transaction.gasLimit), - data: this.toBase64OrUndefined(transaction.data), - chainID: transaction.chainID.valueOf(), - version: transaction.version, - options: transaction.options == 0 ? undefined : transaction.options, - guardian: transaction.guardian.isEmpty() ? undefined : transaction.guardian.toBech32(), - signature: this.toHexOrUndefined(transaction.signature), - guardianSignature: this.toHexOrUndefined(transaction.guardianSignature), - }; - - return plainObject; - } - - 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; - } - - public plainObjectToTransaction(object: IPlainTransactionObject): Transaction { - const transaction = new Transaction({ - nonce: BigInt(object.nonce), - value: BigInt(object.value || ""), - receiver: Address.newFromBech32(object.receiver), - receiverUsername: this.bufferFromBase64(object.receiverUsername).toString(), - sender: Address.newFromBech32(object.sender), - senderUsername: this.bufferFromBase64(object.senderUsername).toString(), - guardian: object.guardian ? Address.newFromBech32(object.guardian) : Address.empty(), - gasPrice: BigInt(object.gasPrice), - gasLimit: BigInt(object.gasLimit), - data: this.bufferFromBase64(object.data), - chainID: String(object.chainID), - version: Number(object.version), - options: Number(object.options), - signature: this.bufferFromHex(object.signature), - guardianSignature: this.bufferFromHex(object.guardianSignature), - }); - - return transaction; - } - - private bufferFromBase64(value?: string) { - return Buffer.from(value || "", "base64"); - } - - private bufferFromHex(value?: string) { - return Buffer.from(value || "", "hex"); - } -} diff --git a/src/converters/transactionsConverters.spec.ts b/src/converters/transactionsConverters.spec.ts deleted file mode 100644 index fe1fe20b..00000000 --- a/src/converters/transactionsConverters.spec.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { assert } from "chai"; -import { Address } from "../address"; -import { Transaction } from "../transaction"; -import { TransactionsConverter } from "./transactionsConverter"; - -describe("test transactions converter", async () => { - it("converts transaction to plain object and back", () => { - const converter = new TransactionsConverter(); - - 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 = converter.transactionToPlainObject(transaction); - const restoredTransaction = converter.plainObjectToTransaction(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, - }); - }); -}); diff --git a/src/index.ts b/src/index.ts index 9917a197..fd82a4eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index b68d12c5..4d1042f1 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -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, + }); + }); }); diff --git a/src/transaction.ts b/src/transaction.ts index 60ba73e0..0328afea 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -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"; @@ -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; } /** @@ -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; + } } /**