Skip to content

Commit 972f55d

Browse files
author
Grégory Saive
authored
Merge pull request #86 from rg911/task/g56_expose_serialization_methods
Task/g56 expose serialization methods
2 parents cfb1773 + a119552 commit 972f55d

22 files changed

+1685
-8
lines changed

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
export * from './src/infrastructure/infrastructure';
1818
export * from './src/model/model';
1919
export * from './src/service/service';
20+
export * from './src/core/utils/utility';

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@types/crypto-js": "^3.1.43",
5454
"crypto-js": "^3.1.9-1",
5555
"js-joda": "^1.6.2",
56-
"nem2-library": "^0.9.12",
56+
"nem2-library": "^0.9.13",
5757
"request": "^2.83.0",
5858
"request-promise-native": "^1.0.5",
5959
"rxjs": "^6.2.1",

src/core/utils/TransactionMapping.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2019 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 { CreateTransactionFromDTO } from '../../infrastructure/transaction/CreateTransactionFromDTO';
18+
import { CreateTransactionFromPayload } from '../../infrastructure/transaction/CreateTransactionFromPayload';
19+
import { Transaction } from '../../model/transaction/Transaction';
20+
21+
export class TransactionMapping {
22+
23+
/**
24+
* Create transaction class from Json.
25+
* @param {object} dataJson The transaction json object.
26+
* @returns {module: model/transaction/transaction} The transaction class.
27+
*/
28+
public static createFromDTO(dataJson: object): Transaction {
29+
return CreateTransactionFromDTO(dataJson);
30+
}
31+
32+
/**
33+
* Create transaction class from payload binary.
34+
* @param {string} dataBytes The transaction json object.
35+
* @returns {module: model/transaction/transaction} The transaction class.
36+
*/
37+
public static createFromPayload(dataBytes: string): Transaction {
38+
return CreateTransactionFromPayload(dataBytes);
39+
}
40+
}

src/core/utils/utility.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2019 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+
export * from './TransactionMapping';

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 NEM
2+
* Copyright 2019 NEM
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/infrastructure/transaction/CreateTransactionFromPayload.ts

Lines changed: 482 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 2019 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 { AccountLinkTransaction } from '../../model/transaction/AccountLinkTransaction';
18+
import { AddressAliasTransaction } from '../../model/transaction/AddressAliasTransaction';
19+
import { AggregateTransaction } from '../../model/transaction/AggregateTransaction';
20+
import { LockFundsTransaction } from '../../model/transaction/LockFundsTransaction';
21+
import { ModifyAccountPropertyAddressTransaction } from '../../model/transaction/ModifyAccountPropertyAddressTransaction';
22+
import { ModifyAccountPropertyEntityTypeTransaction } from '../../model/transaction/ModifyAccountPropertyEntityTypeTransaction';
23+
import { ModifyAccountPropertyMosaicTransaction } from '../../model/transaction/ModifyAccountPropertyMosaicTransaction';
24+
import { ModifyMultisigAccountTransaction } from '../../model/transaction/ModifyMultisigAccountTransaction';
25+
import { MosaicAliasTransaction } from '../../model/transaction/MosaicAliasTransaction';
26+
import { MosaicDefinitionTransaction } from '../../model/transaction/MosaicDefinitionTransaction';
27+
import { MosaicSupplyChangeTransaction } from '../../model/transaction/MosaicSupplyChangeTransaction';
28+
import { RegisterNamespaceTransaction } from '../../model/transaction/RegisterNamespaceTransaction';
29+
import { SecretLockTransaction } from '../../model/transaction/SecretLockTransaction';
30+
import { SecretProofTransaction } from '../../model/transaction/SecretProofTransaction';
31+
import { Transaction } from '../../model/transaction/Transaction';
32+
import { TransactionType } from '../../model/transaction/TransactionType';
33+
import { TransferTransaction } from '../../model/transaction/TransferTransaction';
34+
35+
/**
36+
* @internal
37+
* @param transaction - The transaction class object
38+
* @returns JSON object
39+
* @constructor
40+
*/
41+
export const SerializeTransactionToJSON = (transaction: Transaction): any => {
42+
switch (transaction.type) {
43+
case TransactionType.LINK_ACCOUNT:
44+
return {
45+
remoteAccountKey: (transaction as AccountLinkTransaction).remoteAccountKey,
46+
linkAction: (transaction as AccountLinkTransaction).linkAction,
47+
};
48+
case TransactionType.ADDRESS_ALIAS:
49+
return {
50+
actionType: (transaction as AddressAliasTransaction).actionType,
51+
namespaceId: (transaction as AddressAliasTransaction).namespaceId.toDTO(),
52+
address: (transaction as AddressAliasTransaction).address.toDTO(),
53+
};
54+
case TransactionType.AGGREGATE_BONDED:
55+
case TransactionType.AGGREGATE_COMPLETE:
56+
return {
57+
innerTransactions: (transaction as AggregateTransaction).innerTransactions.map((innerTransaction) => {
58+
return SerializeTransactionToJSON(innerTransaction);
59+
}),
60+
cosignatures: (transaction as AggregateTransaction).cosignatures.map((cosignature) => {
61+
return cosignature.toDTO();
62+
}),
63+
};
64+
case TransactionType.LOCK:
65+
return {
66+
mosaic: (transaction as LockFundsTransaction).mosaic.toDTO(),
67+
duration: (transaction as LockFundsTransaction).duration.toDTO(),
68+
signedTransaction: (transaction as LockFundsTransaction).hash,
69+
};
70+
case TransactionType.MODIFY_ACCOUNT_PROPERTY_ADDRESS:
71+
return {
72+
propertyType: (transaction as ModifyAccountPropertyAddressTransaction).propertyType,
73+
modifications: (transaction as ModifyAccountPropertyAddressTransaction).
74+
modifications.map((modification) => {
75+
return modification.toDTO();
76+
}),
77+
};
78+
case TransactionType.MODIFY_ACCOUNT_PROPERTY_ENTITY_TYPE:
79+
return {
80+
propertyType: (transaction as ModifyAccountPropertyEntityTypeTransaction).propertyType,
81+
modifications: (transaction as ModifyAccountPropertyEntityTypeTransaction).
82+
modifications.map((modification) => {
83+
return modification.toDTO();
84+
}),
85+
};
86+
case TransactionType.MODIFY_ACCOUNT_PROPERTY_MOSAIC:
87+
return {
88+
propertyType: (transaction as ModifyAccountPropertyMosaicTransaction).propertyType,
89+
modifications: (transaction as ModifyAccountPropertyMosaicTransaction).modifications.map((modification) => {
90+
return modification.toDTO();
91+
}),
92+
};
93+
case TransactionType.MODIFY_MULTISIG_ACCOUNT:
94+
return {
95+
minApprovalDelta: (transaction as ModifyMultisigAccountTransaction).minApprovalDelta,
96+
minRemovalDelta: (transaction as ModifyMultisigAccountTransaction).minRemovalDelta,
97+
modifications: (transaction as ModifyMultisigAccountTransaction).modifications.map((modification) => {
98+
return modification.toDTO();
99+
}),
100+
};
101+
case TransactionType.MOSAIC_ALIAS:
102+
return {
103+
actionType: (transaction as MosaicAliasTransaction).actionType,
104+
namespaceId: (transaction as MosaicAliasTransaction).namespaceId.toDTO(),
105+
mosaicId: (transaction as MosaicAliasTransaction).mosaicId.toDTO(),
106+
};
107+
case TransactionType.MOSAIC_DEFINITION:
108+
return {
109+
nonce: (transaction as MosaicDefinitionTransaction).nonce.toDTO(),
110+
mosaicId: (transaction as MosaicDefinitionTransaction).mosaicId.toDTO(),
111+
mosaicProperties: (transaction as MosaicDefinitionTransaction).mosaicProperties.toDTO(),
112+
};
113+
case TransactionType.MOSAIC_SUPPLY_CHANGE:
114+
return {
115+
mosaicId: (transaction as MosaicSupplyChangeTransaction).mosaicId.toDTO(),
116+
direction: (transaction as MosaicSupplyChangeTransaction).direction,
117+
delta: (transaction as MosaicSupplyChangeTransaction).delta.toDTO(),
118+
};
119+
case TransactionType.REGISTER_NAMESPACE:
120+
const registerNamespaceDuration = (transaction as RegisterNamespaceTransaction).duration;
121+
const registerNamespaceParentId = (transaction as RegisterNamespaceTransaction).parentId;
122+
123+
const jsonObject = {
124+
namespaceType: (transaction as RegisterNamespaceTransaction).namespaceType,
125+
namespaceName: (transaction as RegisterNamespaceTransaction).namespaceName,
126+
namespaceId: (transaction as RegisterNamespaceTransaction).namespaceId.toDTO(),
127+
};
128+
129+
if (registerNamespaceDuration) {
130+
Object.assign(jsonObject, {duration: registerNamespaceDuration.toDTO()});
131+
}
132+
if (registerNamespaceParentId) {
133+
Object.assign(jsonObject, {parentId: registerNamespaceParentId.toDTO()});
134+
}
135+
return jsonObject;
136+
case TransactionType.SECRET_LOCK:
137+
return {
138+
mosaic: (transaction as SecretLockTransaction).mosaic.toDTO(),
139+
duration: (transaction as SecretLockTransaction).duration.toDTO(),
140+
hashType: (transaction as SecretLockTransaction).hashType,
141+
secret: (transaction as SecretLockTransaction).secret,
142+
recipient: (transaction as SecretLockTransaction).recipient.toDTO(),
143+
};
144+
case TransactionType.SECRET_PROOF:
145+
return {
146+
hashType: (transaction as SecretProofTransaction).hashType,
147+
secret: (transaction as SecretProofTransaction).secret,
148+
proof: (transaction as SecretProofTransaction).proof,
149+
};
150+
case TransactionType.TRANSFER:
151+
return {
152+
recipient: (transaction as TransferTransaction).recipient.toDTO(),
153+
mosaics: (transaction as TransferTransaction).mosaics.map((mosaic) => {
154+
return mosaic.toDTO();
155+
}),
156+
message: (transaction as TransferTransaction).message.toDTO(),
157+
};
158+
default:
159+
throw new Error ('Transaction type not implemented yet.');
160+
}
161+
162+
};

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
}

0 commit comments

Comments
 (0)