-
Notifications
You must be signed in to change notification settings - Fork 37
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 Transaction class #366
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c54501c
implement new transaction class and transaction computer
popenta 2af440b
unit tests for TransctionNext
popenta 43cd1b3
fixes
popenta c82b0fa
fixes after review
popenta f3e66d3
Merge branch 'feat/next' into transaction-next
popenta 55703be
fixes after review
popenta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { BigNumber } from "bignumber.js"; | ||
import { Address } from "./address"; | ||
import { Compatibility } from "./compatibility"; | ||
import { TRANSACTION_MIN_GAS_PRICE } from "./constants"; | ||
import { TRANSACTION_MIN_GAS_PRICE, TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_VERSION_DEFAULT } from "./constants"; | ||
import * as errors from "./errors"; | ||
import { Hash } from "./hash"; | ||
import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, IPlainTransactionObject, ISignature, ITransactionOptions, ITransactionPayload, ITransactionValue, ITransactionVersion } from "./interface"; | ||
import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, IPlainTransactionObject, ISignature, ITransactionNext, ITransactionOptions, ITransactionPayload, ITransactionValue, ITransactionVersion } from "./interface"; | ||
import { INetworkConfig } from "./interfaceOfNetwork"; | ||
import { TransactionOptions, TransactionVersion } from "./networkParams"; | ||
import { ProtoSerializer } from "./proto"; | ||
|
@@ -153,20 +153,6 @@ export class Transaction { | |
this.hash = TransactionHash.empty(); | ||
} | ||
|
||
/** | ||
* Creates a new Transaction object from a DraftTransaction. | ||
*/ | ||
static fromDraft(draft: DraftTransaction): Transaction { | ||
return new Transaction({ | ||
sender: Address.fromBech32(draft.sender), | ||
receiver: Address.fromBech32(draft.receiver), | ||
gasLimit: new BigNumber(draft.gasLimit).toNumber(), | ||
chainID: "", | ||
value: draft.value, | ||
data: new TransactionPayload(Buffer.from(draft.data)) | ||
}) | ||
} | ||
|
||
getNonce(): INonce { | ||
return this.nonce; | ||
} | ||
|
@@ -435,6 +421,54 @@ export class Transaction { | |
|
||
return feeForMove.plus(processingFee); | ||
} | ||
|
||
/** | ||
* Creates a new Transaction object from a DraftTransaction. | ||
*/ | ||
static fromDraft(draft: DraftTransaction): Transaction { | ||
return new Transaction({ | ||
sender: Address.fromBech32(draft.sender), | ||
receiver: Address.fromBech32(draft.receiver), | ||
gasLimit: new BigNumber(draft.gasLimit).toNumber(), | ||
chainID: "", | ||
value: draft.value, | ||
data: new TransactionPayload(Buffer.from(draft.data)) | ||
}) | ||
} | ||
|
||
/** | ||
* Creates a new Transaction object from a TransactionNext object. | ||
*/ | ||
static fromTransactionNext(transaction: ITransactionNext): Transaction { | ||
const tx = new Transaction({ | ||
sender: Address.fromBech32(transaction.sender), | ||
receiver: Address.fromBech32(transaction.receiver), | ||
gasLimit: new BigNumber(transaction.gasLimit).toNumber(), | ||
chainID: transaction.chainID, | ||
value: new BigNumber(transaction.value).toFixed(0), | ||
data: new TransactionPayload(Buffer.from(transaction.data)), | ||
nonce: Number(transaction.nonce), | ||
gasPrice: Number(transaction.gasPrice), | ||
receiverUsername: transaction.receiverUsername, | ||
senderUsername: transaction.senderUsername, | ||
options: transaction.options, | ||
version: transaction.version | ||
}); | ||
|
||
if (transaction.guardian) { | ||
tx.guardian = Address.fromBech32(transaction.guardian) | ||
} | ||
|
||
if (transaction.signature.length) { | ||
tx.applySignature(transaction.signature); | ||
} | ||
|
||
if (transaction.guardianSignature.length) { | ||
tx.applyGuardianSignature(transaction.guardianSignature); | ||
} | ||
|
||
return tx; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -458,3 +492,215 @@ export class TransactionHash extends Hash { | |
} | ||
} | ||
|
||
/** | ||
* An abstraction for creating, signing and broadcasting transactions. | ||
* Will replace the {@link Transaction} class in the future. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, this is the one according to sdk-specs 👍 |
||
*/ | ||
export class TransactionNext{ | ||
/** | ||
* The nonce of the transaction (the account sequence number of the sender). | ||
*/ | ||
public nonce: BigNumber.Value; | ||
|
||
/** | ||
* The value to transfer. | ||
*/ | ||
public value: BigNumber.Value; | ||
|
||
/** | ||
* The address of the sender. | ||
*/ | ||
public sender: string; | ||
|
||
/** | ||
* The address of the receiver. | ||
*/ | ||
public receiver: string; | ||
|
||
/** | ||
* The username of the sender. | ||
*/ | ||
public senderUsername: string; | ||
|
||
/** | ||
* The username of the receiver. | ||
*/ | ||
public receiverUsername: string; | ||
|
||
/** | ||
* The gas price to be used. | ||
*/ | ||
public gasPrice: BigNumber.Value; | ||
|
||
/** | ||
* The maximum amount of gas to be consumed when processing the transaction. | ||
*/ | ||
public gasLimit: BigNumber.Value; | ||
|
||
/** | ||
* The payload of the transaction. | ||
*/ | ||
public data: Uint8Array; | ||
|
||
/** | ||
* The chain ID of the Network (e.g. "1" for Mainnet). | ||
*/ | ||
public chainID: string; | ||
|
||
/** | ||
* The version, required by the Network in order to correctly interpret the contents of the transaction. | ||
*/ | ||
public version: number; | ||
|
||
/** | ||
* The options field of the transactions. | ||
*/ | ||
public options: number; | ||
|
||
/** | ||
* The address of the guardian. | ||
*/ | ||
public guardian: string; | ||
|
||
/** | ||
* The signature. | ||
*/ | ||
public signature: Uint8Array; | ||
|
||
/** | ||
* The signature of the guardian. | ||
*/ | ||
public guardianSignature: Uint8Array; | ||
|
||
/** | ||
* Creates a new Transaction object. | ||
*/ | ||
public constructor({ | ||
sender, | ||
receiver, | ||
gasLimit, | ||
chainID, | ||
nonce, | ||
value, | ||
senderUsername, | ||
receiverUsername, | ||
gasPrice, | ||
data, | ||
version, | ||
options, | ||
guardian, | ||
}: { | ||
nonce?: BigNumber.Value; | ||
value?: BigNumber.Value; | ||
sender: string; | ||
receiver: string; | ||
senderUsername?: string; | ||
receiverUsername?: string; | ||
gasPrice?: BigNumber.Value; | ||
gasLimit: BigNumber.Value; | ||
data?: Uint8Array; | ||
chainID: string; | ||
version?: number; | ||
options?: number; | ||
guardian?: string; | ||
}) { | ||
this.nonce = nonce || 0; | ||
this.value = value || new BigNumber(0); | ||
this.sender = sender; | ||
this.receiver = receiver; | ||
this.senderUsername = senderUsername || ""; | ||
this.receiverUsername = receiverUsername || ""; | ||
this.gasPrice = gasPrice || new BigNumber(TRANSACTION_MIN_GAS_PRICE); | ||
this.gasLimit = gasLimit; | ||
this.data = data || new Uint8Array(); | ||
this.chainID = chainID; | ||
this.version = version || TRANSACTION_VERSION_DEFAULT; | ||
this.options = options || TRANSACTION_OPTIONS_DEFAULT; | ||
this.guardian = guardian || ""; | ||
|
||
this.signature = new Uint8Array(); | ||
this.guardianSignature = new Uint8Array(); | ||
} | ||
} | ||
|
||
/** | ||
* An utilitary class meant to work together with the {@link TransactionNext} class. | ||
*/ | ||
export class TransactionComputer { | ||
constructor() {} | ||
|
||
computeTransactionFee(transaction: ITransactionNext, networkConfig: INetworkConfig): BigNumber{ | ||
const moveBalanceGas = new BigNumber( | ||
networkConfig.MinGasLimit + transaction.data.length * networkConfig.GasPerDataByte); | ||
if (moveBalanceGas > transaction.gasLimit) { | ||
throw new errors.ErrNotEnoughGas(parseInt(transaction.gasLimit.toString(), 10)); | ||
} | ||
|
||
const gasPrice = new BigNumber(transaction.gasPrice); | ||
const feeForMove = moveBalanceGas.multipliedBy(gasPrice); | ||
if (moveBalanceGas === transaction.gasLimit) { | ||
return feeForMove; | ||
} | ||
|
||
const diff = new BigNumber(transaction.gasLimit).minus(moveBalanceGas); | ||
const modifiedGasPrice = gasPrice.multipliedBy( | ||
new BigNumber(networkConfig.GasPriceModifier) | ||
); | ||
const processingFee = diff.multipliedBy(modifiedGasPrice); | ||
|
||
return feeForMove.plus(processingFee); | ||
} | ||
|
||
computeBytesForSigning(transaction: ITransactionNext): Uint8Array{ | ||
const plainTransaction = this.toPlainObject(transaction); | ||
|
||
if (plainTransaction.signature) { | ||
delete plainTransaction.signature; | ||
} | ||
|
||
if (plainTransaction.guardianSignature) { | ||
delete plainTransaction.guardianSignature; | ||
} | ||
|
||
if (!plainTransaction.guardian) { | ||
delete plainTransaction.guardian | ||
CiprianDraghici marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const serialized = JSON.stringify(plainTransaction); | ||
|
||
return new Uint8Array(Buffer.from(serialized)); | ||
} | ||
|
||
computeTransactionHash(transaction: ITransactionNext): Uint8Array{ | ||
let serializer = new ProtoSerializer(); | ||
|
||
const tx = Transaction.fromTransactionNext(transaction); | ||
const buffer = serializer.serializeTransaction(tx); | ||
const hash = createTransactionHasher(TRANSACTION_HASH_LENGTH) | ||
.update(buffer) | ||
.digest("hex"); | ||
|
||
return Buffer.from(hash, "hex"); | ||
} | ||
|
||
private toPlainObject(transaction: ITransactionNext) { | ||
return { | ||
nonce: Number(transaction.nonce), | ||
value: new BigNumber(transaction.value).toFixed(0), | ||
receiver: transaction.receiver, | ||
sender: transaction.sender, | ||
senderUsername: transaction.senderUsername ? Buffer.from(transaction.senderUsername).toString("base64") : undefined, | ||
receiverUsername: transaction.receiverUsername ? Buffer.from(transaction.receiverUsername).toString("base64") : undefined, | ||
gasPrice: Number(transaction.gasPrice), | ||
gasLimit: Number(transaction.gasLimit), | ||
data: transaction.data && transaction.data.length === 0 ? undefined : Buffer.from(transaction.data).toString("base64"), | ||
chainID: transaction.chainID, | ||
version: transaction.version, | ||
options: transaction.options ? transaction.options : undefined, | ||
guardian: transaction.guardian ? transaction.guardian : undefined, | ||
signature: transaction.signature.length == 0 ? undefined : Buffer.from(transaction.signature).toString("hex"), | ||
guardianSignature: transaction.guardianSignature.length == 0 ? undefined : Buffer.from(transaction.guardianSignature).toString("hex") | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct 👍