Skip to content

Commit c513cc5

Browse files
committed
add EncryptedMessage class
1 parent a354ca8 commit c513cc5

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

src/model/account/Account.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {NetworkType} from '../blockchain/NetworkType';
1919
import {AggregateTransaction} from '../transaction/AggregateTransaction';
2020
import {CosignatureSignedTransaction} from '../transaction/CosignatureSignedTransaction';
2121
import {CosignatureTransaction} from '../transaction/CosignatureTransaction';
22+
import {EncryptedMessage} from '../transaction/EncryptedMessage';
23+
import {PlainMessage} from '../transaction/PlainMessage';
2224
import {SignedTransaction} from '../transaction/SignedTransaction';
2325
import {Transaction} from '../transaction/Transaction';
2426
import {Address} from './Address';
@@ -79,7 +81,25 @@ export class Account {
7981
const address = Address.createFromPublicKey(convert.uint8ToHex(keyPair.publicKey), networkType);
8082
return new Account(address, keyPair);
8183
}
84+
/**
85+
* Create a new encrypted Message
86+
* @param message
87+
* @param recipientPublicAccount
88+
* @returns {EncryptedMessage}
89+
*/
90+
public encryptMessage(message: string, recipientPublicAccount: PublicAccount): EncryptedMessage {
91+
return EncryptedMessage.create(message, recipientPublicAccount, this.privateKey);
92+
}
8293

94+
/**
95+
* Decrypts an encrypted message
96+
* @param encryptedMessage
97+
* @param recipientPublicAccount
98+
* @returns {PlainMessage}
99+
*/
100+
public decryptMessage(encryptedMessage: EncryptedMessage, recipientPublicAccount: PublicAccount): PlainMessage {
101+
return EncryptedMessage.decrypt(encryptedMessage, this.privateKey, recipientPublicAccount);
102+
}
83103
/**
84104
* Account public key.
85105
* @return {string}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 {crypto} from 'nem2-library';
18+
import {PublicAccount} from '../account/PublicAccount';
19+
import {Message} from './Message';
20+
import {PlainMessage} from './PlainMessage';
21+
22+
/**
23+
* Encrypted Message model
24+
*/
25+
export class EncryptedMessage extends Message {
26+
27+
public readonly recipientPublicAccount?: PublicAccount;
28+
29+
constructor(payload: string,
30+
recipientPublicAccount?: PublicAccount){
31+
super(1,payload);
32+
this.recipientPublicAccount = recipientPublicAccount;
33+
}
34+
35+
/**
36+
*
37+
* @param message
38+
* @param recipientPublicAccount
39+
* @param privateKey
40+
*/
41+
public static create(message: string, recipientPublicAccount: PublicAccount, privateKey) {
42+
return new EncryptedMessage(crypto.encode(privateKey, recipientPublicAccount.publicKey, message), recipientPublicAccount);
43+
}
44+
45+
/**
46+
*
47+
* @param payload
48+
*/
49+
public static createFromDTO(payload: string): EncryptedMessage {
50+
return new EncryptedMessage(payload);
51+
}
52+
53+
/**
54+
*
55+
* @param encryptMessage
56+
* @param privateKey
57+
* @param recipientPublicAccount
58+
*/
59+
public static decrypt(encryptMessage: EncryptedMessage, privateKey, recipientPublicAccount: PublicAccount): PlainMessage {
60+
return new PlainMessage(Message.decodeHex(crypto.decode(privateKey, recipientPublicAccount.publicKey, encryptMessage.payload)));
61+
}
62+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 {expect} from 'chai';
18+
import {Account} from '../../../src/model/account/Account';
19+
20+
import {PublicAccount} from '../../../src/model/account/PublicAccount';
21+
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
22+
import {EncryptedMessage} from '../../../src/model/transaction/EncryptedMessage';
23+
24+
25+
describe('EncryptedMessage', () => {
26+
27+
const accountInformation = {
28+
address: 'SCTVW23D2MN5VE4AQ4TZIDZENGNOZXPRPRLIKCF2',
29+
privateKey: '26b64cb10f005e5988a36744ca19e20d835ccc7c105aaa5f3b212da593180930'.toUpperCase(),
30+
publicKey: 'c2f93346e27ce6ad1a9f8f5e3066f8326593a406bdf357acb041e2f9ab402efe'.toUpperCase(),
31+
};
32+
let recipientPublicAccount:PublicAccount;
33+
34+
before(() => {
35+
recipientPublicAccount = PublicAccount.createFromPublicKey(accountInformation.publicKey,NetworkType.MIJIN_TEST);
36+
});
37+
38+
it("should create a encrypted message from a DTO", () => {
39+
const encryptedMessage = EncryptedMessage.createFromDTO("test transaction");
40+
expect(encryptedMessage.payload).to.be.equal("test transaction");
41+
});
42+
43+
it("should return encrypted message dto", () => {
44+
const account = Account.createFromPrivateKey(accountInformation.privateKey,NetworkType.MIJIN_TEST);
45+
const publicAccount = PublicAccount.createFromPublicKey(account.publicKey,NetworkType.MIJIN_TEST);
46+
const encryptedMessage = account.encryptMessage("test transaction", publicAccount);
47+
const plainMessage = account.decryptMessage(encryptedMessage, publicAccount);
48+
expect(plainMessage.payload).to.be.equal("test transaction");
49+
});
50+
51+
it("should create an encrypted message from a DTO and decrypt it", () => {
52+
const account = Account.createFromPrivateKey(accountInformation.privateKey,NetworkType.MIJIN_TEST);
53+
const publicAccount = PublicAccount.createFromPublicKey("0414fe7647ec008e533aac98a4bf1c5fbf1d236c75b81fdadf1f5d1042fdd2ff",NetworkType.MIJIN_TEST);
54+
const encryptMessage = EncryptedMessage.createFromDTO("02bb332c0fdd445455117882b2bec5e49f5713860d6b34650d0f769159d021a27518ea03539af8913231b9f80f600daae9291bb100a6d32e36b52a6c457fea287ca9942a32368618fe1fd0c185dbf834");
55+
const plainMessage = account.decryptMessage(encryptMessage, publicAccount);
56+
expect(plainMessage.payload).to.be.equal("test transaction");
57+
});
58+
});

0 commit comments

Comments
 (0)