Skip to content

Commit a8dc80c

Browse files
committed
Added serializeTransactionToJSON
Fixed bug Address alias using wrong type and version
1 parent 6dec1e5 commit a8dc80c

File tree

13 files changed

+635
-4
lines changed

13 files changed

+635
-4
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2018 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { AddressAliasTransaction } from '../../model/transaction/AddressAliasTransaction';
18+
import { AggregateTransaction } from '../../model/transaction/AggregateTransaction';
19+
import { LockFundsTransaction } from '../../model/transaction/LockFundsTransaction';
20+
import { ModifyAccountPropertyAddressTransaction } from '../../model/transaction/ModifyAccountPropertyAddressTransaction';
21+
import { ModifyAccountPropertyEntityTypeTransaction } from '../../model/transaction/ModifyAccountPropertyEntityTypeTransaction';
22+
import { ModifyAccountPropertyMosaicTransaction } from '../../model/transaction/ModifyAccountPropertyMosaicTransaction';
23+
import { ModifyMultisigAccountTransaction } from '../../model/transaction/ModifyMultisigAccountTransaction';
24+
import { MosaicAliasTransaction } from '../../model/transaction/MosaicAliasTransaction';
25+
import { MosaicDefinitionTransaction } from '../../model/transaction/MosaicDefinitionTransaction';
26+
import { MosaicSupplyChangeTransaction } from '../../model/transaction/MosaicSupplyChangeTransaction';
27+
import { RegisterNamespaceTransaction } from '../../model/transaction/RegisterNamespaceTransaction';
28+
import { SecretLockTransaction } from '../../model/transaction/SecretLockTransaction';
29+
import { SecretProofTransaction } from '../../model/transaction/SecretProofTransaction';
30+
import { Transaction } from '../../model/transaction/Transaction';
31+
import { TransactionType } from '../../model/transaction/TransactionType';
32+
import { TransferTransaction } from '../../model/transaction/TransferTransaction';
33+
34+
/**
35+
* @internal
36+
* @param transaction - The transaction class object
37+
* @returns JSON object
38+
* @constructor
39+
*/
40+
export const SerializeTransactionToJSON = (transaction: Transaction): any => {
41+
switch (transaction.type) {
42+
case TransactionType.ADDRESS_ALIAS:
43+
return {
44+
actionType: (transaction as AddressAliasTransaction).actionType,
45+
namespaceId: (transaction as AddressAliasTransaction).namespaceId.toDTO(),
46+
address: (transaction as AddressAliasTransaction).address.toDTO(),
47+
};
48+
case TransactionType.AGGREGATE_BONDED:
49+
case TransactionType.AGGREGATE_COMPLETE:
50+
return {
51+
innerTransactions: (transaction as AggregateTransaction).innerTransactions.map((innerTransaction) => {
52+
return SerializeTransactionToJSON(innerTransaction);
53+
}),
54+
cosignatures: (transaction as AggregateTransaction).cosignatures.map((cosignature) => {
55+
return cosignature.toDTO();
56+
}),
57+
};
58+
case TransactionType.LOCK:
59+
return {
60+
mosaic: (transaction as LockFundsTransaction).mosaic.toDTO(),
61+
duration: (transaction as LockFundsTransaction).duration.toDTO(),
62+
signedTransaction: (transaction as LockFundsTransaction).hash,
63+
};
64+
case TransactionType.MODIFY_ACCOUNT_PROPERTY_ADDRESS:
65+
return {
66+
propertyType: (transaction as ModifyAccountPropertyAddressTransaction).propertyType,
67+
modifications: (transaction as ModifyAccountPropertyAddressTransaction).
68+
modifications.map((modification) => {
69+
return modification.toDTO();
70+
}),
71+
};
72+
case TransactionType.MODIFY_ACCOUNT_PROPERTY_ENTITY_TYPE:
73+
return {
74+
propertyType: (transaction as ModifyAccountPropertyEntityTypeTransaction).propertyType,
75+
modifications: (transaction as ModifyAccountPropertyEntityTypeTransaction).
76+
modifications.map((modification) => {
77+
return modification.toDTO();
78+
}),
79+
};
80+
case TransactionType.MODIFY_ACCOUNT_PROPERTY_MOSAIC:
81+
return {
82+
propertyType: (transaction as ModifyAccountPropertyMosaicTransaction).propertyType,
83+
modifications: (transaction as ModifyAccountPropertyMosaicTransaction).modifications.map((modification) => {
84+
return modification.toDTO();
85+
}),
86+
};
87+
case TransactionType.MODIFY_MULTISIG_ACCOUNT:
88+
return {
89+
minApprovalDelta: (transaction as ModifyMultisigAccountTransaction).minApprovalDelta,
90+
minRemovalDelta: (transaction as ModifyMultisigAccountTransaction).minRemovalDelta,
91+
modifications: (transaction as ModifyMultisigAccountTransaction).modifications.map((modification) => {
92+
return modification.toDTO();
93+
}),
94+
};
95+
case TransactionType.MOSAIC_ALIAS:
96+
return {
97+
actionType: (transaction as MosaicAliasTransaction).actionType,
98+
namespaceId: (transaction as MosaicAliasTransaction).namespaceId.toDTO(),
99+
mosaicId: (transaction as MosaicAliasTransaction).mosaicId.toDTO(),
100+
};
101+
case TransactionType.MOSAIC_DEFINITION:
102+
return {
103+
nonce: (transaction as MosaicDefinitionTransaction).nonce.toDTO(),
104+
mosaicId: (transaction as MosaicDefinitionTransaction).mosaicId.toDTO(),
105+
mosaicProperties: (transaction as MosaicDefinitionTransaction).mosaicProperties.toDTO(),
106+
};
107+
case TransactionType.MOSAIC_SUPPLY_CHANGE:
108+
return {
109+
mosaicId: (transaction as MosaicSupplyChangeTransaction).mosaicId.toDTO(),
110+
direction: (transaction as MosaicSupplyChangeTransaction).direction,
111+
delta: (transaction as MosaicSupplyChangeTransaction).delta.toDTO(),
112+
};
113+
case TransactionType.REGISTER_NAMESPACE:
114+
const registerNamespaceDuration = (transaction as RegisterNamespaceTransaction).duration;
115+
const registerNamespaceParentId = (transaction as RegisterNamespaceTransaction).parentId;
116+
117+
const jsonObject = {
118+
namespaceType: (transaction as RegisterNamespaceTransaction).namespaceType,
119+
namespaceName: (transaction as RegisterNamespaceTransaction).namespaceName,
120+
namespaceId: (transaction as RegisterNamespaceTransaction).namespaceId.toDTO(),
121+
};
122+
123+
if (registerNamespaceDuration) {
124+
Object.assign(jsonObject, {duration: registerNamespaceDuration.toDTO()});
125+
}
126+
if (registerNamespaceParentId) {
127+
Object.assign(jsonObject, {parentId: registerNamespaceParentId.toDTO()});
128+
}
129+
return jsonObject;
130+
case TransactionType.SECRET_LOCK:
131+
return {
132+
mosaic: (transaction as SecretLockTransaction).mosaic.toDTO(),
133+
duration: (transaction as SecretLockTransaction).duration.toDTO(),
134+
hashType: (transaction as SecretLockTransaction).hashType,
135+
secret: (transaction as SecretLockTransaction).secret,
136+
recipient: (transaction as SecretLockTransaction).recipient.toDTO(),
137+
};
138+
case TransactionType.SECRET_PROOF:
139+
return {
140+
hashType: (transaction as SecretProofTransaction).hashType,
141+
secret: (transaction as SecretProofTransaction).secret,
142+
proof: (transaction as SecretProofTransaction).proof,
143+
};
144+
case TransactionType.TRANSFER:
145+
return {
146+
recipient: (transaction as TransferTransaction).recipient.toDTO(),
147+
mosaics: (transaction as TransferTransaction).mosaics.map((mosaic) => {
148+
return mosaic.toDTO();
149+
}),
150+
message: (transaction as TransferTransaction).message.toDTO(),
151+
};
152+
}
153+
154+
};

src/model/account/Address.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,14 @@ export class Address {
113113
public equals(address: Address): boolean {
114114
return this.plain() === address.plain() && this.networkType === address.networkType;
115115
}
116+
117+
/**
118+
* Create DTO object
119+
*/
120+
public toDTO() {
121+
return {
122+
address: this.address,
123+
networkType: this.networkType,
124+
};
125+
}
116126
}

src/model/account/PublicAccount.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,13 @@ export class PublicAccount {
9595
return this.publicKey === publicAccount.publicKey && this.address.plain() === publicAccount.address.plain();
9696
}
9797

98+
/**
99+
* Create DTO object
100+
*/
101+
toDTO() {
102+
return {
103+
publicKey: this.publicKey,
104+
address: this.address.toDTO(),
105+
};
106+
}
98107
}

src/model/mosaic/MosaicId.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,13 @@ export class MosaicId {
8686
}
8787
return false;
8888
}
89+
90+
/**
91+
* Create DTO object.
92+
*/
93+
toDTO() {
94+
return {
95+
id: this.id.toDTO(),
96+
};
97+
}
8998
}

src/model/mosaic/MosaicProperties.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,17 @@ export class MosaicProperties {
8383
const flags = (params.supplyMutable ? 1 : 0) + (params.transferable ? 2 : 0) + (params.levyMutable ? 4 : 0);
8484
return new MosaicProperties(UInt64.fromUint(flags), params.divisibility, params.duration);
8585
}
86+
87+
/**
88+
* Create DTO object
89+
*/
90+
toDTO() {
91+
return {
92+
supplyMutable: this.supplyMutable,
93+
transferable: this.transferable,
94+
levyMutable: this.levyMutable,
95+
divisibility: this.divisibility,
96+
duration: this.duration.toDTO(),
97+
};
98+
}
8699
}

src/model/namespace/NamespaceId.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ export class NamespaceId {
6767
}
6868
return false;
6969
}
70+
71+
/**
72+
* Create DTO object
73+
*/
74+
public toDTO() {
75+
return {
76+
id: this.id.toDTO(),
77+
fullName: this.fullName ? this.fullName : '',
78+
};
79+
}
7080
}

src/model/transaction/AddressAliasTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class AddressAliasTransaction extends Transaction {
4848
address: Address,
4949
networkType: NetworkType): AddressAliasTransaction {
5050
return new AddressAliasTransaction(networkType,
51-
TransactionVersion.MOSAIC_ALIAS,
51+
TransactionVersion.ADDRESS_ALIAS,
5252
deadline,
5353
new UInt64([0, 0]),
5454
actionType,
@@ -88,7 +88,7 @@ export class AddressAliasTransaction extends Transaction {
8888
signature?: string,
8989
signer?: PublicAccount,
9090
transactionInfo?: TransactionInfo) {
91-
super(TransactionType.MOSAIC_ALIAS, networkType, version, deadline, fee, signature, signer, transactionInfo);
91+
super(TransactionType.ADDRESS_ALIAS, networkType, version, deadline, fee, signature, signer, transactionInfo);
9292
}
9393

9494
/**

src/model/transaction/AggregateTransactionCosignature.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import {PublicAccount} from '../account/PublicAccount';
18-
18+
import { NetworkType } from '../blockchain/NetworkType';
1919
/**
2020
* Model representing cosignature of an aggregate transaction.
2121
*/
@@ -35,4 +35,14 @@ export class AggregateTransactionCosignature {
3535
public readonly signer: PublicAccount) {
3636

3737
}
38+
39+
/**
40+
* Create DTO object
41+
*/
42+
public toDTO() {
43+
return {
44+
signature: this.signature,
45+
signer: this.signer.toDTO(),
46+
};
47+
}
3848
}

src/model/transaction/Message.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ export abstract class Message {
5151
*/
5252
public readonly payload: string) {
5353
}
54+
55+
/**
56+
* Create DTO object
57+
*/
58+
toDTO() {
59+
return {
60+
type: this.type,
61+
payload: this.payload,
62+
};
63+
}
5464
}

src/model/transaction/SignedTransaction.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,17 @@ export class SignedTransaction {
5353
throw new Error('hash must be 64 characters long');
5454
}
5555
}
56+
57+
/**
58+
* Create DTO object
59+
*/
60+
toDTO() {
61+
return {
62+
payload: this.payload,
63+
hash: this.hash,
64+
signer: this.signer,
65+
type: this.type,
66+
networkType: this.networkType,
67+
};
68+
}
5669
}

0 commit comments

Comments
 (0)