From c513cc5369cfae7981e31c45326971df23aa9c01 Mon Sep 17 00:00:00 2001 From: dnice Date: Mon, 8 Apr 2019 13:32:20 +0800 Subject: [PATCH 1/3] add EncryptedMessage class --- src/model/account/Account.ts | 20 ++++++ src/model/transaction/EncryptedMessage.ts | 62 +++++++++++++++++++ .../transaction/EncryptedMessage.spec.ts | 58 +++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 src/model/transaction/EncryptedMessage.ts create mode 100644 test/model/transaction/EncryptedMessage.spec.ts diff --git a/src/model/account/Account.ts b/src/model/account/Account.ts index 7cacc19d04..b5a1dce8c4 100644 --- a/src/model/account/Account.ts +++ b/src/model/account/Account.ts @@ -19,6 +19,8 @@ import {NetworkType} from '../blockchain/NetworkType'; import {AggregateTransaction} from '../transaction/AggregateTransaction'; import {CosignatureSignedTransaction} from '../transaction/CosignatureSignedTransaction'; import {CosignatureTransaction} from '../transaction/CosignatureTransaction'; +import {EncryptedMessage} from '../transaction/EncryptedMessage'; +import {PlainMessage} from '../transaction/PlainMessage'; import {SignedTransaction} from '../transaction/SignedTransaction'; import {Transaction} from '../transaction/Transaction'; import {Address} from './Address'; @@ -79,7 +81,25 @@ export class Account { const address = Address.createFromPublicKey(convert.uint8ToHex(keyPair.publicKey), networkType); return new Account(address, keyPair); } + /** + * Create a new encrypted Message + * @param message + * @param recipientPublicAccount + * @returns {EncryptedMessage} + */ + public encryptMessage(message: string, recipientPublicAccount: PublicAccount): EncryptedMessage { + return EncryptedMessage.create(message, recipientPublicAccount, this.privateKey); + } + /** + * Decrypts an encrypted message + * @param encryptedMessage + * @param recipientPublicAccount + * @returns {PlainMessage} + */ + public decryptMessage(encryptedMessage: EncryptedMessage, recipientPublicAccount: PublicAccount): PlainMessage { + return EncryptedMessage.decrypt(encryptedMessage, this.privateKey, recipientPublicAccount); + } /** * Account public key. * @return {string} diff --git a/src/model/transaction/EncryptedMessage.ts b/src/model/transaction/EncryptedMessage.ts new file mode 100644 index 0000000000..847aefd170 --- /dev/null +++ b/src/model/transaction/EncryptedMessage.ts @@ -0,0 +1,62 @@ +/* + * Copyright 2019 NEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {crypto} from 'nem2-library'; +import {PublicAccount} from '../account/PublicAccount'; +import {Message} from './Message'; +import {PlainMessage} from './PlainMessage'; + +/** + * Encrypted Message model + */ +export class EncryptedMessage extends Message { + + public readonly recipientPublicAccount?: PublicAccount; + + constructor(payload: string, + recipientPublicAccount?: PublicAccount){ + super(1,payload); + this.recipientPublicAccount = recipientPublicAccount; + } + + /** + * + * @param message + * @param recipientPublicAccount + * @param privateKey + */ + public static create(message: string, recipientPublicAccount: PublicAccount, privateKey) { + return new EncryptedMessage(crypto.encode(privateKey, recipientPublicAccount.publicKey, message), recipientPublicAccount); + } + + /** + * + * @param payload + */ + public static createFromDTO(payload: string): EncryptedMessage { + return new EncryptedMessage(payload); + } + + /** + * + * @param encryptMessage + * @param privateKey + * @param recipientPublicAccount + */ + public static decrypt(encryptMessage: EncryptedMessage, privateKey, recipientPublicAccount: PublicAccount): PlainMessage { + return new PlainMessage(Message.decodeHex(crypto.decode(privateKey, recipientPublicAccount.publicKey, encryptMessage.payload))); + } +} diff --git a/test/model/transaction/EncryptedMessage.spec.ts b/test/model/transaction/EncryptedMessage.spec.ts new file mode 100644 index 0000000000..1eee4e9c9a --- /dev/null +++ b/test/model/transaction/EncryptedMessage.spec.ts @@ -0,0 +1,58 @@ +/* + * Copyright 2019 NEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {expect} from 'chai'; +import {Account} from '../../../src/model/account/Account'; + +import {PublicAccount} from '../../../src/model/account/PublicAccount'; +import {NetworkType} from '../../../src/model/blockchain/NetworkType'; +import {EncryptedMessage} from '../../../src/model/transaction/EncryptedMessage'; + + +describe('EncryptedMessage', () => { + + const accountInformation = { + address: 'SCTVW23D2MN5VE4AQ4TZIDZENGNOZXPRPRLIKCF2', + privateKey: '26b64cb10f005e5988a36744ca19e20d835ccc7c105aaa5f3b212da593180930'.toUpperCase(), + publicKey: 'c2f93346e27ce6ad1a9f8f5e3066f8326593a406bdf357acb041e2f9ab402efe'.toUpperCase(), + }; + let recipientPublicAccount:PublicAccount; + + before(() => { + recipientPublicAccount = PublicAccount.createFromPublicKey(accountInformation.publicKey,NetworkType.MIJIN_TEST); + }); + + it("should create a encrypted message from a DTO", () => { + const encryptedMessage = EncryptedMessage.createFromDTO("test transaction"); + expect(encryptedMessage.payload).to.be.equal("test transaction"); + }); + + it("should return encrypted message dto", () => { + const account = Account.createFromPrivateKey(accountInformation.privateKey,NetworkType.MIJIN_TEST); + const publicAccount = PublicAccount.createFromPublicKey(account.publicKey,NetworkType.MIJIN_TEST); + const encryptedMessage = account.encryptMessage("test transaction", publicAccount); + const plainMessage = account.decryptMessage(encryptedMessage, publicAccount); + expect(plainMessage.payload).to.be.equal("test transaction"); + }); + + it("should create an encrypted message from a DTO and decrypt it", () => { + const account = Account.createFromPrivateKey(accountInformation.privateKey,NetworkType.MIJIN_TEST); + const publicAccount = PublicAccount.createFromPublicKey("0414fe7647ec008e533aac98a4bf1c5fbf1d236c75b81fdadf1f5d1042fdd2ff",NetworkType.MIJIN_TEST); + const encryptMessage = EncryptedMessage.createFromDTO("02bb332c0fdd445455117882b2bec5e49f5713860d6b34650d0f769159d021a27518ea03539af8913231b9f80f600daae9291bb100a6d32e36b52a6c457fea287ca9942a32368618fe1fd0c185dbf834"); + const plainMessage = account.decryptMessage(encryptMessage, publicAccount); + expect(plainMessage.payload).to.be.equal("test transaction"); + }); +}); From 90b3d8c9fe985fa41410063a18a8a5cea9ecf742 Mon Sep 17 00:00:00 2001 From: dnice Date: Mon, 8 Apr 2019 15:33:04 +0800 Subject: [PATCH 2/3] add MessageType --- src/model/transaction/EncryptedMessage.ts | 3 ++- src/model/transaction/MessageType.ts | 25 +++++++++++++++++++++++ src/model/transaction/PlainMessage.ts | 3 ++- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/model/transaction/MessageType.ts diff --git a/src/model/transaction/EncryptedMessage.ts b/src/model/transaction/EncryptedMessage.ts index 847aefd170..443af6c125 100644 --- a/src/model/transaction/EncryptedMessage.ts +++ b/src/model/transaction/EncryptedMessage.ts @@ -17,6 +17,7 @@ import {crypto} from 'nem2-library'; import {PublicAccount} from '../account/PublicAccount'; import {Message} from './Message'; +import {MessageType} from './MessageType'; import {PlainMessage} from './PlainMessage'; /** @@ -28,7 +29,7 @@ export class EncryptedMessage extends Message { constructor(payload: string, recipientPublicAccount?: PublicAccount){ - super(1,payload); + super(MessageType.EncryptedMessage,payload); this.recipientPublicAccount = recipientPublicAccount; } diff --git a/src/model/transaction/MessageType.ts b/src/model/transaction/MessageType.ts new file mode 100644 index 0000000000..b37220a9cf --- /dev/null +++ b/src/model/transaction/MessageType.ts @@ -0,0 +1,25 @@ +/* + * Copyright 2018 NEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * The supply type. Supported supply types are: + * 0: Increase in supply. + * 1: Decrease in supply. + */ +export enum MessageType { + PlainMessage = 0, + EncryptedMessage = 1, +} diff --git a/src/model/transaction/PlainMessage.ts b/src/model/transaction/PlainMessage.ts index 2f611ca473..ee7f2f44de 100644 --- a/src/model/transaction/PlainMessage.ts +++ b/src/model/transaction/PlainMessage.ts @@ -15,6 +15,7 @@ */ import {Message} from './Message'; +import {MessageType} from './MessageType'; /** * The plain message model defines a plain string. When sending it to the network we transform the payload to hex-string. @@ -40,7 +41,7 @@ export class PlainMessage extends Message { * @param payload */ constructor(payload: string) { - super(0, payload); + super(MessageType.PlainMessage, payload); } } From 6da136eee63cda1010c4fded81f768f1093ece72 Mon Sep 17 00:00:00 2001 From: dnice Date: Mon, 8 Apr 2019 15:33:52 +0800 Subject: [PATCH 3/3] add MessageType --- src/model/transaction/MessageType.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/model/transaction/MessageType.ts b/src/model/transaction/MessageType.ts index b37220a9cf..a43da5d6b1 100644 --- a/src/model/transaction/MessageType.ts +++ b/src/model/transaction/MessageType.ts @@ -1,5 +1,5 @@ /* - * Copyright 2018 NEM + * Copyright 2019 NEM * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,9 +15,9 @@ */ /** - * The supply type. Supported supply types are: - * 0: Increase in supply. - * 1: Decrease in supply. + * The Message type. Supported supply types are: + * 0: PlainMessage + * 1: EncryptedMessage. */ export enum MessageType { PlainMessage = 0,