-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move conversion logic to converters package.
- Loading branch information
1 parent
d19f4b0
commit 6a6a810
Showing
3 changed files
with
79 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { TransactionsConverter } from "./transactionsConverter"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { IPlainTransactionObject, ITransaction } from "../interface"; | ||
import { Transaction } from "../transaction"; | ||
|
||
export class TransactionsConverter { | ||
transactionToPlainObject(transaction: ITransaction): IPlainTransactionObject { | ||
const plainObject = { | ||
nonce: Number(transaction.nonce), | ||
value: transaction.value.toString(), | ||
receiver: transaction.receiver, | ||
sender: transaction.sender, | ||
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 ? transaction.guardian : undefined, | ||
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: Number(object.nonce), | ||
value: BigInt(object.value || ""), | ||
receiver: object.receiver, | ||
receiverUsername: this.bufferFromBase64(object.receiverUsername).toString(), | ||
sender: object.sender, | ||
senderUsername: this.bufferFromBase64(object.senderUsername).toString(), | ||
guardian: object.guardian, | ||
gasPrice: Number(object.gasPrice), | ||
gasLimit: Number(object.gasLimit), | ||
data: this.bufferFromBase64(object.data), | ||
chainID: String(object.chainID), | ||
version: object.version, | ||
options: 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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters