Skip to content

Commit bfbbe11

Browse files
authored
Remove message type from payload for empty message + block meta change (#679)
* Fixed #678 Remove message type from payload for empty message * Updaged method name * - Updated openAPI (removed message.type & payload) * Fixed #680 - Updated message .toJSON to use hex string input * fixed message toJson
1 parent 9696f4a commit bfbbe11

File tree

16 files changed

+130
-65
lines changed

16 files changed

+130
-65
lines changed

e2e/infrastructure/BlockHttp.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('BlockHttp', () => {
9191
expect(blockInfo.timestamp.lower).to.be.equal(0);
9292
expect(blockInfo.timestamp.higher).to.be.equal(0);
9393
expect(blockInfo.beneficiaryAddress).not.to.be.undefined;
94-
expect(blockInfo.numStatements).not.to.be.undefined;
94+
expect(blockInfo.statementsCount).not.to.be.undefined;
9595
});
9696
});
9797

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
@@ -109,7 +109,7 @@
109109
"ripemd160": "^2.0.2",
110110
"rxjs": "^6.5.3",
111111
"rxjs-compat": "^6.5.3",
112-
"symbol-openapi-typescript-fetch-client": "0.9.7-SNAPSHOT.202009171123",
112+
"symbol-openapi-typescript-fetch-client": "0.10.0-SNAPSHOT.202009251042",
113113
"tweetnacl": "^1.0.3",
114114
"utf8": "^3.0.0",
115115
"ws": "^7.2.3"

src/infrastructure/BlockHttp.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class BlockHttp extends Http implements BlockRepository {
9595
dto.meta.generationHash,
9696
UInt64.fromNumericString(dto.meta.totalFee),
9797
dto.meta.stateHashSubCacheMerkleRoots,
98-
dto.meta.numTransactions,
98+
dto.meta.totalTransactionsCount,
9999
dto.block.signature,
100100
PublicAccount.createFromPublicKey(dto.block.signerPublicKey, networkType),
101101
networkType,
@@ -112,8 +112,9 @@ export class BlockHttp extends Http implements BlockRepository {
112112
dto.block.proofGamma,
113113
dto.block.proofScalar,
114114
dto.block.proofVerificationHash,
115-
dto.block.beneficiaryAddress ? Address.createFromEncoded(dto.block.beneficiaryAddress) : undefined,
116-
dto.meta.numStatements,
115+
Address.createFromEncoded(dto.block.beneficiaryAddress),
116+
dto.meta.transactionsCount,
117+
dto.meta.statementsCount,
117118
);
118119
}
119120

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
1818
import { Address } from '../../model/account/Address';
1919
import { PublicAccount } from '../../model/account/PublicAccount';
2020
import { EncryptedMessage } from '../../model/message/EncryptedMessage';
21+
import { Message } from '../../model/message/Message';
2122
import { MessageType } from '../../model/message/MessageType';
2223
import { PersistentHarvestingDelegationMessage } from '../../model/message/PersistentHarvestingDelegationMessage';
2324
import { EmptyMessage, PlainMessage } from '../../model/message/PlainMessage';
@@ -104,19 +105,20 @@ export const extractMosaics = (mosaics: any): Mosaic[] => {
104105
* Extract message from either JSON payload (unencoded) or DTO (encoded)
105106
*
106107
* @param message - message payload
107-
* @return {PlainMessage}
108+
* @return {Message}
108109
*/
109-
const extractMessage = (message: any): PlainMessage | EncryptedMessage => {
110+
const extractMessage = (message: any): Message => {
110111
let msgObj = EmptyMessage;
111112
if (message) {
112-
if (message.type === MessageType.PlainMessage) {
113-
msgObj = convert.isHexString(message.payload)
114-
? PlainMessage.createFromPayload(message.payload)
115-
: PlainMessage.create(message.payload);
116-
} else if (message.type === MessageType.EncryptedMessage) {
117-
msgObj = EncryptedMessage.createFromPayload(message.payload);
118-
} else if (message.type === MessageType.PersistentHarvestingDelegationMessage) {
119-
msgObj = PersistentHarvestingDelegationMessage.createFromPayload(message.payload);
113+
const messagePayload = message.payload ? message.payload : message.substring(2);
114+
const messageType = message.type !== undefined ? message.type : convert.hexToUint8(message.substring(0, 2))[0];
115+
116+
if (messageType === MessageType.PlainMessage) {
117+
msgObj = PlainMessage.createFromPayload(messagePayload);
118+
} else if (messageType === MessageType.EncryptedMessage) {
119+
msgObj = EncryptedMessage.createFromPayload(messagePayload);
120+
} else if (messageType === MessageType.PersistentHarvestingDelegationMessage) {
121+
msgObj = PersistentHarvestingDelegationMessage.createFromPayload(messagePayload);
120122
}
121123
}
122124
return msgObj;

src/infrastructure/transaction/SerializeTransactionToJSON.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,18 @@ export const SerializeTransactionToJSON = (transaction: Transaction): any => {
197197
};
198198
case TransactionType.TRANSFER:
199199
const transferTx = transaction as TransferTransaction;
200-
return {
200+
const messageObject = {
201201
recipientAddress: transferTx.recipientAddress.toDTO(),
202202
mosaics: transferTx.mosaics.map((mosaic) => {
203203
return mosaic.toDTO();
204204
}),
205-
message: transferTx.message.toDTO(),
206205
};
206+
if (transferTx.message.toDTO().length) {
207+
Object.assign(messageObject, {
208+
message: transferTx.message.toDTO(),
209+
});
210+
}
211+
return messageObject;
207212
case TransactionType.MOSAIC_GLOBAL_RESTRICTION:
208213
const mosaicGlobalRestrictionTx = transaction as MosaicGlobalRestrictionTransaction;
209214
return {

src/model/blockchain/BlockInfo.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ export class BlockInfo {
7676
*/
7777
public readonly stateHashSubCacheMerkleRoots: string[],
7878
/**
79-
* The number of transactions included.
79+
* The total number of transactions confirmed (including embedded transaction) included.
8080
*/
81-
public readonly numTransactions: number,
81+
public readonly totalTransactionsCount: number,
8282
/**
8383
* The block signature.
8484
* The signature was generated by the signer and can be used to validate that the blockchain
@@ -149,10 +149,14 @@ export class BlockInfo {
149149
/**
150150
* The beneficiary address.
151151
*/
152-
public readonly beneficiaryAddress?: Address | undefined,
152+
public readonly beneficiaryAddress: Address,
153+
/**
154+
* The number of statements confiemd (excluding embedded transaction) included.
155+
*/
156+
public readonly transactionsCount: number,
153157
/**
154158
* The number of statements included.
155159
*/
156-
public readonly numStatements?: number,
160+
public readonly statementsCount: number,
157161
) {}
158162
}

src/model/message/Message.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
import { Convert } from '../../core/format/Convert';
23
* Copyright 2018 NEM
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,6 +15,9 @@
1415
* limitations under the License.
1516
*/
1617

18+
import { MessageType } from './MessageType';
19+
import { Convert } from '../../core/format/Convert';
20+
1721
/**
1822
* An abstract message class that serves as the base class of all message types.
1923
*/
@@ -46,10 +50,12 @@ export abstract class Message {
4650
/**
4751
* Create DTO object
4852
*/
49-
toDTO(): any {
50-
return {
51-
type: this.type,
52-
payload: this.payload,
53-
};
53+
toDTO(): string {
54+
if (!this.payload) {
55+
return '';
56+
}
57+
return this.type === MessageType.PersistentHarvestingDelegationMessage
58+
? this.payload
59+
: this.type.toString(16).padStart(2, '0').toUpperCase() + Convert.utf8ToHex(this.payload);
5460
}
5561
}

src/model/transaction/TransferTransaction.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ import { DtoMapping } from '../../core/utils/DtoMapping';
3434
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
3535
import { Address } from '../account/Address';
3636
import { PublicAccount } from '../account/PublicAccount';
37-
import { EncryptedMessage } from '../message/EncryptedMessage';
3837
import { Message } from '../message/Message';
3938
import { MessageType } from '../message/MessageType';
40-
import { PlainMessage } from '../message/PlainMessage';
4139
import { Mosaic } from '../mosaic/Mosaic';
4240
import { NamespaceId } from '../namespace/NamespaceId';
4341
import { NetworkType } from '../network/NetworkType';
@@ -50,6 +48,9 @@ import { TransactionInfo } from './TransactionInfo';
5048
import { TransactionType } from './TransactionType';
5149
import { TransactionVersion } from './TransactionVersion';
5250
import { UnresolvedAddress } from '../account/UnresolvedAddress';
51+
import { EmptyMessage, PlainMessage } from '../message/PlainMessage';
52+
import { EncryptedMessage } from '../message/EncryptedMessage';
53+
import { PersistentHarvestingDelegationMessage } from '../message/PersistentHarvestingDelegationMessage';
5354

5455
/**
5556
* Transfer transactions contain data about transfers of mosaics and message to another account.
@@ -121,7 +122,7 @@ export class TransferTransaction extends Transaction {
121122
/**
122123
* The transaction message of 2048 characters.
123124
*/
124-
public readonly message: Message,
125+
public readonly message: Message = EmptyMessage,
125126
signature?: string,
126127
signer?: PublicAccount,
127128
transactionInfo?: TransactionInfo,
@@ -140,8 +141,6 @@ export class TransferTransaction extends Transaction {
140141
const builder = isEmbedded
141142
? EmbeddedTransferTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload))
142143
: TransferTransactionBuilder.loadFromBinary(Convert.hexToUint8(payload));
143-
const messageType = builder.getMessage()[0];
144-
const messageHex = Convert.uint8ToHex(builder.getMessage()).substring(2);
145144
const signerPublicKey = Convert.uint8ToHex(builder.getSignerPublicKey().key);
146145
const networkType = builder.getNetwork().valueOf();
147146
const signature = payload.substring(16, 144);
@@ -152,9 +151,7 @@ export class TransferTransaction extends Transaction {
152151
const id = new UInt64(mosaic.mosaicId.unresolvedMosaicId).toHex();
153152
return new Mosaic(UnresolvedMapping.toUnresolvedMosaic(id), new UInt64(mosaic.amount.amount));
154153
}),
155-
messageType === MessageType.PlainMessage
156-
? PlainMessage.createFromPayload(messageHex)
157-
: EncryptedMessage.createFromPayload(messageHex),
154+
TransferTransaction.createMessageFromBuffer(builder.getMessage()),
158155
networkType,
159156
isEmbedded ? new UInt64([0, 0]) : new UInt64((builder as TransferTransactionBuilder).fee.amount),
160157
isEmbedded || signature.match(`^[0]+$`) ? undefined : signature,
@@ -168,7 +165,7 @@ export class TransferTransaction extends Transaction {
168165
* @internal
169166
*/
170167
protected validate(): void {
171-
if (this.message.type === MessageType.PersistentHarvestingDelegationMessage) {
168+
if (this.message?.type === MessageType.PersistentHarvestingDelegationMessage) {
172169
if (this.mosaics.length > 0) {
173170
throw new Error('PersistentDelegationRequestTransaction should be created without Mosaic');
174171
} else if (!/^[0-9a-fA-F]{264}$/.test(this.message.payload)) {
@@ -211,13 +208,16 @@ export class TransferTransaction extends Transaction {
211208
* @returns {Uint8Array}
212209
*/
213210
public getMessageBuffer(): Uint8Array {
211+
if (!this.message || !this.message.payload) {
212+
return Uint8Array.of();
213+
}
214214
const messgeHex =
215215
this.message.type === MessageType.PersistentHarvestingDelegationMessage
216216
? this.message.payload
217217
: Convert.utf8ToHex(this.message.payload);
218218
const payloadBuffer = Convert.hexToUint8(messgeHex);
219219
const typeBuffer = GeneratorUtils.uintToBuffer(this.message.type, 1);
220-
return this.message.type === MessageType.PersistentHarvestingDelegationMessage
220+
return this.message.type === MessageType.PersistentHarvestingDelegationMessage || !this.message.payload
221221
? payloadBuffer
222222
: GeneratorUtils.concatTypedArrays(typeBuffer, payloadBuffer);
223223
}
@@ -298,4 +298,25 @@ export class TransferTransaction extends Transaction {
298298
alias.find((name) => this.recipientAddress.equals(name)) !== undefined
299299
);
300300
}
301+
302+
/**
303+
* @internal
304+
*/
305+
private static createMessageFromBuffer(messageBuffer: Uint8Array): Message {
306+
if (!messageBuffer.length) {
307+
return EmptyMessage;
308+
}
309+
const messageType = messageBuffer[0];
310+
const messageHex = Convert.uint8ToHex(messageBuffer).substring(2);
311+
switch (messageType) {
312+
case MessageType.PlainMessage:
313+
return PlainMessage.createFromPayload(messageHex);
314+
case MessageType.EncryptedMessage:
315+
return EncryptedMessage.createFromPayload(messageHex);
316+
case MessageType.PersistentHarvestingDelegationMessage:
317+
return PersistentHarvestingDelegationMessage.createFromPayload(messageHex);
318+
default:
319+
throw new Error('Message Type is not valid');
320+
}
321+
}
301322
}

test/infrastructure/BlockHttp.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ describe('BlockHttp', () => {
5757
const blockMetaDTO = {} as BlockMetaDTO;
5858
blockMetaDTO.generationHash = 'abc';
5959
blockMetaDTO.hash = 'aHash';
60-
blockMetaDTO.numStatements = 10;
61-
blockMetaDTO.numTransactions = 20;
60+
blockMetaDTO.statementsCount = 10;
61+
blockMetaDTO.transactionsCount = 20;
62+
blockMetaDTO.totalTransactionsCount = 30;
6263
blockMetaDTO.totalFee = '30';
6364
blockMetaDTO.stateHashSubCacheMerkleRoots = ['a', 'b', 'c'];
6465

@@ -94,8 +95,9 @@ describe('BlockHttp', () => {
9495

9596
expect(blockInfo.generationHash).to.be.equals(blockInfoDto.meta.generationHash);
9697
expect(blockInfo.hash).to.be.equals(blockInfoDto.meta.hash);
97-
expect(blockInfo.numStatements).to.be.equals(blockInfoDto.meta.numStatements);
98-
expect(blockInfo.numTransactions).to.be.equals(blockInfoDto.meta.numTransactions);
98+
expect(blockInfo.statementsCount).to.be.equals(blockInfoDto.meta.statementsCount);
99+
expect(blockInfo.transactionsCount).to.be.equals(blockInfoDto.meta.transactionsCount);
100+
expect(blockInfo.totalTransactionsCount).to.be.equals(blockInfoDto.meta.totalTransactionsCount);
99101
expect(blockInfo.totalFee.toString()).to.be.equals(blockInfoDto.meta.totalFee);
100102
}
101103

0 commit comments

Comments
 (0)