Skip to content

Commit b59194f

Browse files
author
Grégory Saive
authored
Merge branch 'master' into task/g112_sign_with_cosignatories
2 parents 8880b88 + 3d8a889 commit b59194f

12 files changed

+183
-115
lines changed

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import {AggregateTransaction} from '../../model/transaction/AggregateTransaction
3030
import {AggregateTransactionCosignature} from '../../model/transaction/AggregateTransactionCosignature';
3131
import {AggregateTransactionInfo} from '../../model/transaction/AggregateTransactionInfo';
3232
import {Deadline} from '../../model/transaction/Deadline';
33+
import { EncryptedMessage } from '../../model/transaction/EncryptedMessage';
3334
import {LockFundsTransaction} from '../../model/transaction/LockFundsTransaction';
35+
import { MessageType } from '../../model/transaction/MessageType';
3436
import {ModifyAccountPropertyAddressTransaction} from '../../model/transaction/ModifyAccountPropertyAddressTransaction';
3537
import {ModifyAccountPropertyEntityTypeTransaction} from '../../model/transaction/ModifyAccountPropertyEntityTypeTransaction';
3638
import {ModifyAccountPropertyMosaicTransaction} from '../../model/transaction/ModifyAccountPropertyMosaicTransaction';
@@ -126,7 +128,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
126128
new UInt64(transactionDTO.maxFee || [0, 0]),
127129
extractRecipient(transactionDTO.recipient),
128130
extractMosaics(transactionDTO.mosaics),
129-
extractMessage(transactionDTO.message !== undefined ? transactionDTO.message.payload : undefined),
131+
extractMessage(transactionDTO.message !== undefined ? transactionDTO.message : undefined),
130132
transactionDTO.signature,
131133
transactionDTO.signer ? PublicAccount.createFromPublicKey(transactionDTO.signer,
132134
extractNetworkType(transactionDTO.version)) : undefined,
@@ -426,15 +428,15 @@ export const extractMosaics = (mosaics: any): Mosaic[] => {
426428
* @param message - message payload
427429
* @return {PlainMessage}
428430
*/
429-
const extractMessage = (message: any): PlainMessage => {
430-
let plainMessage = EmptyMessage;
431-
if (message !== undefined && convert.isHexString(message)) {
432-
plainMessage = PlainMessage.createFromPayload(message);
433-
} else {
434-
plainMessage = PlainMessage.create(message);
431+
const extractMessage = (message: any): PlainMessage | EncryptedMessage => {
432+
let msgObj = EmptyMessage;
433+
if (message.type === MessageType.PlainMessage) {
434+
msgObj = convert.isHexString(message) ? PlainMessage.createFromPayload(message.payload) :
435+
PlainMessage.create(message.payload);
436+
} else if (message.type === MessageType.EncryptedMessage) {
437+
msgObj = EncryptedMessage.createFromPayload(message.payload);
435438
}
436-
437-
return plainMessage;
439+
return msgObj;
438440
};
439441

440442
/**
@@ -454,7 +456,7 @@ const extractMessage = (message: any): PlainMessage => {
454456
*/
455457
export const extractBeneficiary = (
456458
blockDTO: any,
457-
networkType: NetworkType
459+
networkType: NetworkType,
458460
): PublicAccount | undefined => {
459461

460462
let dtoPublicAccount: PublicAccount | undefined;

src/infrastructure/transaction/CreateTransactionFromPayload.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import { AddressAliasTransaction } from '../../model/transaction/AddressAliasTra
3131
import { AggregateTransaction } from '../../model/transaction/AggregateTransaction';
3232
import { AggregateTransactionCosignature } from '../../model/transaction/AggregateTransactionCosignature';
3333
import { Deadline } from '../../model/transaction/Deadline';
34+
import { EncryptedMessage } from '../../model/transaction/EncryptedMessage';
3435
import { HashType } from '../../model/transaction/HashType';
3536
import { LockFundsTransaction } from '../../model/transaction/LockFundsTransaction';
3637
import { Message } from '../../model/transaction/Message';
38+
import { MessageType } from '../../model/transaction/MessageType';
3739
import { ModifyAccountPropertyAddressTransaction } from '../../model/transaction/ModifyAccountPropertyAddressTransaction';
3840
import { ModifyAccountPropertyEntityTypeTransaction } from '../../model/transaction/ModifyAccountPropertyEntityTypeTransaction';
3941
import { ModifyAccountPropertyMosaicTransaction } from '../../model/transaction/ModifyAccountPropertyMosaicTransaction';
@@ -267,7 +269,8 @@ const CreateTransaction = (type: number, transactionData: string, networkType: N
267269
const transferMessageSize = parseInt(convert.uint8ToHex(convert.hexToUint8(transactionData.substring(50, 54)).reverse()), 16);
268270

269271
const transferMessageAndMosaicSubString = transactionData.substring(56);
270-
const transferMessageType = transferMessageAndMosaicSubString.substring(0, 2);
272+
const transferMessageType = parseInt(convert.uint8ToHex(convert.hexToUint8(
273+
transferMessageAndMosaicSubString.substring(0, 2)).reverse()), 16);
271274
const transferMessage = transferMessageAndMosaicSubString.substring(2, (transferMessageSize - 1) * 2 + 2);
272275
const transferMosaic = transferMessageAndMosaicSubString.substring(transferMessageSize * 2);
273276
const transferMosaicArray = transferMosaic.match(/.{1,32}/g);
@@ -279,7 +282,7 @@ const CreateTransaction = (type: number, transactionData: string, networkType: N
279282
new MosaicId(UInt64.fromHex(reverse(mosaic.substring(0, 16))).toDTO()),
280283
UInt64.fromHex(reverse(mosaic.substring(16))),
281284
)) : [],
282-
PlainMessage.createFromPayload(transferMessage),
285+
extractMessage(transferMessageType, transferMessage),
283286
networkType,
284287
);
285288
case TransactionType.SECRET_LOCK:
@@ -480,3 +483,20 @@ const decodeHex = (hex: string): string => {
480483
return str;
481484
}
482485
};
486+
487+
488+
/**
489+
* @internal
490+
* @param messageType - Message Type
491+
* @param payload - Message Payload
492+
* @returns {Message}
493+
*/
494+
const extractMessage = (messageType: MessageType, payload: string): Message => {
495+
if (messageType === MessageType.PlainMessage) {
496+
return PlainMessage.createFromPayload(payload);
497+
} else if (messageType === MessageType.EncryptedMessage) {
498+
return EncryptedMessage.createFromPayload(payload);
499+
} else {
500+
throw new Error('Invalid message type');
501+
}
502+
};

src/model/transaction/AccountPropertyModification.ts

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

17+
import { Address } from '../account/Address';
1718
import { PropertyModificationType } from '../account/PropertyModificationType';
19+
import { MosaicId } from '../mosaic/MosaicId';
20+
import { TransactionType } from './TransactionType';
1821

1922
export class AccountPropertyModification<T> {
2023

@@ -35,6 +38,38 @@ export class AccountPropertyModification<T> {
3538

3639
}
3740

41+
/**
42+
* Create an address filter for account property modification
43+
* @param modificationType - modification type. 0: Add, 1: Remove
44+
* @param address - modification value (Address)
45+
* @returns {AccountPropertyModification}
46+
*/
47+
public static createForAddress(modificationType: PropertyModificationType,
48+
address: Address): AccountPropertyModification<string> {
49+
return new AccountPropertyModification<string>(modificationType, address.plain());
50+
}
51+
/**
52+
* Create an mosaic filter for account property modification
53+
* @param modificationType - modification type. 0: Add, 1: Remove
54+
* @param mosaicId - modification value (Mosaic)
55+
* @returns {AccountPropertyModification}
56+
*/
57+
public static createForMosaic(modificationType: PropertyModificationType,
58+
mosaicId: MosaicId): AccountPropertyModification<number[]> {
59+
return new AccountPropertyModification<number[]>(modificationType, mosaicId.id.toDTO());
60+
}
61+
62+
/**
63+
* Create an entity type filter for account property modification
64+
* @param modificationType - modification type. 0: Add, 1: Remove
65+
* @param entityType - modification value (Transaction Type)
66+
* @returns {AccountPropertyModification}
67+
*/
68+
public static createForEntityType(modificationType: PropertyModificationType,
69+
entityType: number): AccountPropertyModification<TransactionType> {
70+
return new AccountPropertyModification<TransactionType>(modificationType, entityType);
71+
}
72+
3873
/**
3974
* @internal
4075
*/

src/model/transaction/AccountPropertyTransaction.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Address } from '../account/Address';
18-
import { PropertyModificationType } from '../account/PropertyModificationType';
1917
import { PropertyType } from '../account/PropertyType';
2018
import { NetworkType } from '../blockchain/NetworkType';
21-
import { MosaicId } from '../mosaic/MosaicId';
2219
import { UInt64 } from '../UInt64';
2320
import { AccountPropertyModification } from './AccountPropertyModification';
2421
import { Deadline } from './Deadline';
2522
import { ModifyAccountPropertyAddressTransaction } from './ModifyAccountPropertyAddressTransaction';
2623
import { ModifyAccountPropertyEntityTypeTransaction } from './ModifyAccountPropertyEntityTypeTransaction';
2724
import { ModifyAccountPropertyMosaicTransaction } from './ModifyAccountPropertyMosaicTransaction';
25+
import { TransactionType } from './TransactionType';
2826

2927
export class AccountPropertyTransaction {
3028
/**
@@ -95,7 +93,7 @@ export class AccountPropertyTransaction {
9593
public static createEntityTypePropertyModificationTransaction(
9694
deadline: Deadline,
9795
propertyType: PropertyType,
98-
modifications: Array<AccountPropertyModification<number>>,
96+
modifications: Array<AccountPropertyModification<TransactionType>>,
9997
networkType: NetworkType,
10098
maxFee: UInt64 = new UInt64([0, 0])
10199
): ModifyAccountPropertyEntityTypeTransaction {
@@ -110,37 +108,4 @@ export class AccountPropertyTransaction {
110108
maxFee,
111109
);
112110
}
113-
114-
/**
115-
* Create an address filter for account property modification
116-
* @param modificationType - modification type. 0: Add, 1: Remove
117-
* @param address - modification value (Address)
118-
* @returns {AccountPropertyModification}
119-
*/
120-
public static createAddressFilter(modificationType: PropertyModificationType,
121-
address: Address): AccountPropertyModification<string> {
122-
return new AccountPropertyModification<string>(modificationType, address.plain());
123-
}
124-
125-
/**
126-
* Create an mosaic filter for account property modification
127-
* @param modificationType - modification type. 0: Add, 1: Remove
128-
* @param mosaicId - modification value (Mosaic)
129-
* @returns {AccountPropertyModification}
130-
*/
131-
public static createMosaicFilter(modificationType: PropertyModificationType,
132-
mosaicId: MosaicId): AccountPropertyModification<number[]> {
133-
return new AccountPropertyModification<number[]>(modificationType, mosaicId.id.toDTO());
134-
}
135-
136-
/**
137-
* Create an entity type filter for account property modification
138-
* @param modificationType - modification type. 0: Add, 1: Remove
139-
* @param entityType - modification value (Transaction Type)
140-
* @returns {AccountPropertyModification}
141-
*/
142-
public static createEntityTypeFilter(modificationType: PropertyModificationType,
143-
entityType: number): AccountPropertyModification<number> {
144-
return new AccountPropertyModification<number>(modificationType, entityType);
145-
}
146111
}

src/model/transaction/EncryptedMessage.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export class EncryptedMessage extends Message {
3535

3636
/**
3737
*
38-
* @param message
39-
* @param recipientPublicAccount
40-
* @param privateKey
38+
* @param message - Plain message to be encrypted
39+
* @param recipientPublicAccount - Recipient public account
40+
* @param privateKey - Sender private key
4141
*/
4242
public static create(message: string, recipientPublicAccount: PublicAccount, privateKey) {
4343
return new EncryptedMessage(
@@ -49,17 +49,17 @@ export class EncryptedMessage extends Message {
4949
*
5050
* @param payload
5151
*/
52-
public static createFromDTO(payload: string): EncryptedMessage {
53-
return new EncryptedMessage(payload);
52+
public static createFromPayload(payload: string): EncryptedMessage {
53+
return new EncryptedMessage(this.decodeHex(payload));
5454
}
5555

5656
/**
5757
*
58-
* @param encryptMessage
59-
* @param privateKey
60-
* @param recipientPublicAccount
58+
* @param encryptMessage - Encrypted message to be decrypted
59+
* @param privateKey - Recipient private key
60+
* @param recipientPublicAccount - Sender public account
6161
*/
6262
public static decrypt(encryptMessage: EncryptedMessage, privateKey, recipientPublicAccount: PublicAccount): PlainMessage {
63-
return new PlainMessage(Message.decodeHex(crypto.decode(privateKey, recipientPublicAccount.publicKey, encryptMessage.payload)));
63+
return new PlainMessage(this.decodeHex(crypto.decode(privateKey, recipientPublicAccount.publicKey, encryptMessage.payload)));
6464
}
6565
}

src/model/transaction/ModifyAccountPropertyEntityTypeTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ModifyAccountPropertyEntityTypeTransaction extends Transaction {
4040
*/
4141
public static create(deadline: Deadline,
4242
propertyType: PropertyType,
43-
modifications: Array<AccountPropertyModification<number>>,
43+
modifications: Array<AccountPropertyModification<TransactionType>>,
4444
networkType: NetworkType,
4545
maxFee: UInt64 = new UInt64([0, 0])): ModifyAccountPropertyEntityTypeTransaction {
4646
return new ModifyAccountPropertyEntityTypeTransaction(networkType,
@@ -68,7 +68,7 @@ export class ModifyAccountPropertyEntityTypeTransaction extends Transaction {
6868
deadline: Deadline,
6969
maxFee: UInt64,
7070
public readonly propertyType: PropertyType,
71-
public readonly modifications: Array<AccountPropertyModification<number>>,
71+
public readonly modifications: Array<AccountPropertyModification<TransactionType>>,
7272
signature?: string,
7373
signer?: PublicAccount,
7474
transactionInfo?: TransactionInfo) {

test/core/utils/TransactionMapping.spec.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { PropertyModificationType } from '../../../src/model/account/PropertyMod
2525
import { PropertyType } from '../../../src/model/account/PropertyType';
2626
import { PublicAccount } from '../../../src/model/account/PublicAccount';
2727
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
28+
import { EncryptedMessage } from '../../../src/model/model';
2829
import { MosaicId } from '../../../src/model/mosaic/MosaicId';
2930
import { MosaicNonce } from '../../../src/model/mosaic/MosaicNonce';
3031
import { MosaicProperties } from '../../../src/model/mosaic/MosaicProperties';
@@ -34,13 +35,15 @@ import { AliasActionType } from '../../../src/model/namespace/AliasActionType';
3435
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
3536
import { NamespaceType } from '../../../src/model/namespace/NamespaceType';
3637
import { AccountLinkTransaction } from '../../../src/model/transaction/AccountLinkTransaction';
38+
import { AccountPropertyModification } from '../../../src/model/transaction/AccountPropertyModification';
3739
import { AccountPropertyTransaction } from '../../../src/model/transaction/AccountPropertyTransaction';
3840
import { AddressAliasTransaction } from '../../../src/model/transaction/AddressAliasTransaction';
3941
import { AggregateTransaction } from '../../../src/model/transaction/AggregateTransaction';
4042
import { Deadline } from '../../../src/model/transaction/Deadline';
4143
import { HashType } from '../../../src/model/transaction/HashType';
4244
import { LinkAction } from '../../../src/model/transaction/LinkAction';
4345
import { LockFundsTransaction } from '../../../src/model/transaction/LockFundsTransaction';
46+
import { MessageType } from '../../../src/model/transaction/MessageType';
4447
import { ModifyAccountPropertyAddressTransaction } from '../../../src/model/transaction/ModifyAccountPropertyAddressTransaction';
4548
import { ModifyAccountPropertyMosaicTransaction } from '../../../src/model/transaction/ModifyAccountPropertyMosaicTransaction';
4649
import { ModifyMultisigAccountTransaction } from '../../../src/model/transaction/ModifyMultisigAccountTransaction';
@@ -67,7 +70,7 @@ describe('TransactionMapping - createFromPayload', () => {
6770

6871
it('should create AccountPropertyAddressTransaction', () => {
6972
const address = Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
70-
const addressPropertyFilter = AccountPropertyTransaction.createAddressFilter(
73+
const addressPropertyFilter = AccountPropertyModification.createForAddress(
7174
PropertyModificationType.Add,
7275
address,
7376
);
@@ -89,7 +92,7 @@ describe('TransactionMapping - createFromPayload', () => {
8992

9093
it('should create AccountPropertyMosaicTransaction', () => {
9194
const mosaicId = new MosaicId([2262289484, 3405110546]);
92-
const mosaicPropertyFilter = AccountPropertyTransaction.createMosaicFilter(
95+
const mosaicPropertyFilter = AccountPropertyModification.createForMosaic(
9396
PropertyModificationType.Add,
9497
mosaicId,
9598
);
@@ -111,7 +114,7 @@ describe('TransactionMapping - createFromPayload', () => {
111114

112115
it('should create AccountPropertyMosaicTransaction', () => {
113116
const entityType = TransactionType.ADDRESS_ALIAS;
114-
const entityTypePropertyFilter = AccountPropertyTransaction.createEntityTypeFilter(
117+
const entityTypePropertyFilter = AccountPropertyModification.createForEntityType(
115118
PropertyModificationType.Add,
116119
entityType,
117120
);
@@ -572,6 +575,23 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () =>
572575
expect(transaction.message.payload).to.be.equal('test-message');
573576
});
574577

578+
it('should create TransferTransaction - Encrypted Message', () => {
579+
const transferTransaction = TransferTransaction.create(
580+
Deadline.create(),
581+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
582+
[
583+
NetworkCurrencyMosaic.createRelative(100),
584+
],
585+
new EncryptedMessage('12324556'),
586+
NetworkType.MIJIN_TEST,
587+
);
588+
589+
const transaction = TransactionMapping.createFromDTO(transferTransaction.toJSON()) as TransferTransaction;
590+
591+
expect((transaction.recipient as Address).plain()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
592+
expect(transaction.message.type).to.be.equal(MessageType.EncryptedMessage);
593+
});
594+
575595
it('should create AccountLinkTransaction', () => {
576596
const accountLinkTransaction = AccountLinkTransaction.create(
577597
Deadline.create(),
@@ -588,7 +608,7 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () =>
588608

589609
it('should create AccountPropertyAddressTransaction', () => {
590610
const address = Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
591-
const addressPropertyFilter = AccountPropertyTransaction.createAddressFilter(
611+
const addressPropertyFilter = AccountPropertyModification.createForAddress(
592612
PropertyModificationType.Add,
593613
address,
594614
);
@@ -609,7 +629,7 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () =>
609629

610630
it('should create AccountPropertyMosaicTransaction', () => {
611631
const mosaicId = new MosaicId([2262289484, 3405110546]);
612-
const mosaicPropertyFilter = AccountPropertyTransaction.createMosaicFilter(
632+
const mosaicPropertyFilter = AccountPropertyModification.createForMosaic(
613633
PropertyModificationType.Add,
614634
mosaicId,
615635
);
@@ -630,7 +650,7 @@ describe('TransactionMapping - createFromDTO (Transaction.toJSON() feed)', () =>
630650

631651
it('should create AccountPropertyMosaicTransaction', () => {
632652
const entityType = TransactionType.ADDRESS_ALIAS;
633-
const entityTypePropertyFilter = AccountPropertyTransaction.createEntityTypeFilter(
653+
const entityTypePropertyFilter = AccountPropertyModification.createForEntityType(
634654
PropertyModificationType.Add,
635655
entityType,
636656
);

0 commit comments

Comments
 (0)