Skip to content

Commit 21964b2

Browse files
committed
Refactored TransactionMapping
- createTransactionFromPayload now use catbuffer
1 parent 8cfc9f8 commit 21964b2

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

src/model/transaction/AccountAddressRestrictionTransaction.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { SignSchema } from '../../core/crypto/SignSchema';
1718
import { Convert, RawAddress } from '../../core/format';
1819
import { AccountAddressRestrictionModificationBuilder } from '../../infrastructure/catbuffer/AccountAddressRestrictionModificationBuilder';
1920
import { AccountAddressRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/AccountAddressRestrictionTransactionBuilder';
2021
import { AmountDto } from '../../infrastructure/catbuffer/AmountDto';
21-
import { EmbeddedAccountAddressRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/EmbeddedAccountAddressRestrictionTransactionBuilder';
22+
import { EmbeddedAccountAddressRestrictionTransactionBuilder }from '../../infrastructure/catbuffer/EmbeddedAccountAddressRestrictionTransactionBuilder';
2223
import { KeyDto } from '../../infrastructure/catbuffer/KeyDto';
2324
import { SignatureDto } from '../../infrastructure/catbuffer/SignatureDto';
2425
import { TimestampDto } from '../../infrastructure/catbuffer/TimestampDto';
2526
import { UnresolvedAddressDto } from '../../infrastructure/catbuffer/UnresolvedAddressDto';
27+
import { Address } from '../account/Address';
2628
import { PublicAccount } from '../account/PublicAccount';
2729
import { RestrictionType } from '../account/RestrictionType';
2830
import { NetworkType } from '../blockchain/NetworkType';
2931
import { UInt64 } from '../UInt64';
3032
import { AccountRestrictionModification } from './AccountRestrictionModification';
3133
import { Deadline } from './Deadline';
34+
import { InnerTransaction } from './InnerTransaction';
3235
import { Transaction } from './Transaction';
3336
import { TransactionInfo } from './TransactionInfo';
3437
import { TransactionType } from './TransactionType';
@@ -82,6 +85,36 @@ export class AccountAddressRestrictionTransaction extends Transaction {
8285
networkType, version, deadline, maxFee, signature, signer, transactionInfo);
8386
}
8487

88+
/**
89+
* Create a transaction object from payload
90+
* @param {string} payload Binary payload
91+
* @param {Boolean} isEmbedded Is embedded transaction (Default: false)
92+
* @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
93+
* @returns {Transaction | InnerTransaction}
94+
*/
95+
public static createFromPayload(payload: string,
96+
isEmbedded: boolean = false,
97+
signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction {
98+
const builder = isEmbedded ? EmbeddedAccountAddressRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) :
99+
AccountAddressRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload));
100+
const signer = Convert.uint8ToHex(builder.getSigner().key);
101+
const networkType = Convert.hexToUint8(builder.getVersion().toString(16))[0];
102+
const transaction = AccountAddressRestrictionTransaction.create(
103+
isEmbedded ? Deadline.create() : Deadline.createFromDTO(
104+
(builder as AccountAddressRestrictionTransactionBuilder).getDeadline().timestamp),
105+
builder.getRestrictionType().valueOf(),
106+
builder.getModifications().map((modification) => {
107+
return AccountRestrictionModification.createForAddress(
108+
modification.modificationAction.valueOf(),
109+
Address.createFromEncoded(Convert.uint8ToHex(modification.value.unresolvedAddress)),
110+
);
111+
}),
112+
networkType,
113+
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountAddressRestrictionTransactionBuilder).fee.amount),
114+
);
115+
return isEmbedded ? transaction.toAggregate(PublicAccount.createFromPublicKey(signer, networkType, signSchema)) : transaction;
116+
}
117+
85118
/**
86119
* @override Transaction.size()
87120
* @description get the byte size of a AccountAddressRestrictionTransaction

src/model/transaction/AccountMosaicRestrictionTransaction.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { SignSchema } from '../../core/crypto/SignSchema';
1718
import { Convert } from '../../core/format';
1819
import { AccountMosaicRestrictionModificationBuilder } from '../../infrastructure/catbuffer/AccountMosaicRestrictionModificationBuilder';
1920
import { AccountMosaicRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/AccountMosaicRestrictionTransactionBuilder';
@@ -26,9 +27,11 @@ import { UnresolvedMosaicIdDto } from '../../infrastructure/catbuffer/Unresolved
2627
import { PublicAccount } from '../account/PublicAccount';
2728
import { RestrictionType } from '../account/RestrictionType';
2829
import { NetworkType } from '../blockchain/NetworkType';
30+
import { MosaicId } from '../mosaic/MosaicId';
2931
import { UInt64 } from '../UInt64';
3032
import { AccountRestrictionModification } from './AccountRestrictionModification';
3133
import { Deadline } from './Deadline';
34+
import { InnerTransaction } from './InnerTransaction';
3235
import { Transaction } from './Transaction';
3336
import { TransactionInfo } from './TransactionInfo';
3437
import { TransactionType } from './TransactionType';
@@ -82,6 +85,36 @@ export class AccountMosaicRestrictionTransaction extends Transaction {
8285
networkType, version, deadline, maxFee, signature, signer, transactionInfo);
8386
}
8487

88+
/**
89+
* Create a transaction object from payload
90+
* @param {string} payload Binary payload
91+
* @param {Boolean} isEmbedded Is embedded transaction (Default: false)
92+
* @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
93+
* @returns {Transaction | InnerTransaction}
94+
*/
95+
public static createFromPayload(payload: string,
96+
isEmbedded: boolean = false,
97+
signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction {
98+
const builder = isEmbedded ? EmbeddedAccountMosaicRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) :
99+
AccountMosaicRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload));
100+
const signer = Convert.uint8ToHex(builder.getSigner().key);
101+
const networkType = Convert.hexToUint8(builder.getVersion().toString(16))[0];
102+
const transaction = AccountMosaicRestrictionTransaction.create(
103+
isEmbedded ? Deadline.create() : Deadline.createFromDTO(
104+
(builder as AccountMosaicRestrictionTransactionBuilder).getDeadline().timestamp),
105+
builder.getRestrictionType().valueOf(),
106+
builder.getModifications().map((modification) => {
107+
return AccountRestrictionModification.createForMosaic(
108+
modification.modificationAction.valueOf(),
109+
new MosaicId(modification.value.unresolvedMosaicId),
110+
);
111+
}),
112+
networkType,
113+
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountMosaicRestrictionTransactionBuilder).fee.amount),
114+
);
115+
return isEmbedded ? transaction.toAggregate(PublicAccount.createFromPublicKey(signer, networkType, signSchema)) : transaction;
116+
}
117+
85118
/**
86119
* @override Transaction.size()
87120
* @description get the byte size of a AccountMosaicRestrictionTransaction

src/model/transaction/AccountOperationRestrictionTransaction.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { SignSchema } from '../../core/crypto/SignSchema';
1718
import { Convert } from '../../core/format';
1819
import { AccountOperationRestrictionModificationBuilder } from '../../infrastructure/catbuffer/AccountOperationRestrictionModificationBuilder';
1920
import { AccountOperationRestrictionTransactionBuilder } from '../../infrastructure/catbuffer/AccountOperationRestrictionTransactionBuilder';
@@ -28,6 +29,7 @@ import { NetworkType } from '../blockchain/NetworkType';
2829
import { UInt64 } from '../UInt64';
2930
import { AccountRestrictionModification } from './AccountRestrictionModification';
3031
import { Deadline } from './Deadline';
32+
import { InnerTransaction } from './InnerTransaction';
3133
import { Transaction } from './Transaction';
3234
import { TransactionInfo } from './TransactionInfo';
3335
import { TransactionType } from './TransactionType';
@@ -88,6 +90,36 @@ export class AccountOperationRestrictionTransaction extends Transaction {
8890
networkType, version, deadline, maxFee, signature, signer, transactionInfo);
8991
}
9092

93+
/**
94+
* Create a transaction object from payload
95+
* @param {string} payload Binary payload
96+
* @param {Boolean} isEmbedded Is embedded transaction (Default: false)
97+
* @param {SignSchema} signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
98+
* @returns {Transaction | InnerTransaction}
99+
*/
100+
public static createFromPayload(payload: string,
101+
isEmbedded: boolean = false,
102+
signSchema: SignSchema = SignSchema.SHA3): Transaction | InnerTransaction {
103+
const builder = isEmbedded ? EmbeddedAccountOperationRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload)) :
104+
AccountOperationRestrictionTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload));
105+
const signer = Convert.uint8ToHex(builder.getSigner().key);
106+
const networkType = Convert.hexToUint8(builder.getVersion().toString(16))[0];
107+
const transaction = AccountOperationRestrictionTransaction.create(
108+
isEmbedded ? Deadline.create() : Deadline.createFromDTO(
109+
(builder as AccountOperationRestrictionTransactionBuilder).getDeadline().timestamp),
110+
builder.getRestrictionType().valueOf(),
111+
builder.getModifications().map((modification) => {
112+
return AccountRestrictionModification.createForOperation(
113+
modification.modificationAction.valueOf(),
114+
modification.value.valueOf(),
115+
);
116+
}),
117+
networkType,
118+
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as AccountOperationRestrictionTransactionBuilder).fee.amount),
119+
);
120+
return isEmbedded ? transaction.toAggregate(PublicAccount.createFromPublicKey(signer, networkType, signSchema)) : transaction;
121+
}
122+
91123
/**
92124
* @override Transaction.size()
93125
* @description get the byte size of a AccountOperationRestrictionTransaction

0 commit comments

Comments
 (0)