Skip to content

Commit a68b9c6

Browse files
committed
Fixed #426
- Added ephemeral keypair
1 parent c3797e0 commit a68b9c6

10 files changed

+31
-54
lines changed

e2e/service/MosaicRestrictionTransactionService.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ describe('MosaicRestrictionTransactionService', () => {
7474
UInt64.fromUint(1000),
7575
networkType, helper.maxFee,
7676
);
77-
console.log(mosaicId.toHex());
7877
const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash);
7978
return helper.announce(signedTransaction);
8079
});

src/model/message/MessageMarker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
export class MessageMarker {
1818
/**
19-
* 8-byte marker: FE CC 71 C7 64 BF E5 98 for PersistentDelegationRequestTransaction message
19+
* 8-byte marker: FE2A8061577301E2 for PersistentDelegationRequestTransaction message
2020
*/
21-
public static readonly PersistentDelegationUnlock = 'FECC71C764BFE598';
21+
public static readonly PersistentDelegationUnlock = 'FE2A8061577301E2';
2222
}

src/model/message/PersistentHarvestingDelegationMessage.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
import {Crypto, SHA3Hasher} from '../../core/crypto';
1818
import { Convert } from '../../core/format/Convert';
19+
import { Account } from '../account/Account';
1920
import { NetworkType } from '../blockchain/NetworkType';
2021
import { Message } from './Message';
2122
import { MessageMarker } from './MessageMarker';
2223
import { MessageType } from './MessageType';
23-
import { PlainMessage } from './PlainMessage';
2424

2525
export class PersistentHarvestingDelegationMessage extends Message {
2626
constructor(payload: string) {
@@ -33,18 +33,17 @@ export class PersistentHarvestingDelegationMessage extends Message {
3333
/**
3434
*
3535
* @param delegatedPrivateKey - Private key of delegated account
36-
* @param senderPrivateKey - Sender private key
3736
* @param recipientPublicKey - Recipient public key
3837
* @param {NetworkType} networkType - Catapult network type
3938
* @return {PersistentHarvestingDelegationMessage}
4039
*/
4140
public static create(delegatedPrivateKey: string,
42-
senderPrivateKey: string,
4341
recipientPublicKey: string,
4442
networkType: NetworkType): PersistentHarvestingDelegationMessage {
4543
const signSchema = SHA3Hasher.resolveSignSchema(networkType);
46-
const encrypted = MessageMarker.PersistentDelegationUnlock +
47-
Crypto.encode(senderPrivateKey, recipientPublicKey, delegatedPrivateKey, signSchema, true).toUpperCase();
44+
const ephemeralKeypair = Account.generateNewAccount(networkType);
45+
const encrypted = MessageMarker.PersistentDelegationUnlock + ephemeralKeypair.publicKey +
46+
Crypto.encode(ephemeralKeypair.privateKey, recipientPublicKey, delegatedPrivateKey, signSchema, true).toUpperCase();
4847
return new PersistentHarvestingDelegationMessage(encrypted);
4948
}
5049

@@ -61,17 +60,17 @@ export class PersistentHarvestingDelegationMessage extends Message {
6160
*
6261
* @param encryptMessage - Encrypted message to be decrypted
6362
* @param privateKey - Recipient private key
64-
* @param senderPublicKey - Sender public key
6563
* @param {NetworkType} networkType - Catapult network type
6664
* @return {string}
6765
*/
6866
public static decrypt(encryptMessage: PersistentHarvestingDelegationMessage,
6967
privateKey: string,
70-
senderPublicKey: string,
7168
networkType: NetworkType): string {
7269
const signSchema = SHA3Hasher.resolveSignSchema(networkType);
73-
const payload = encryptMessage.payload.substring(MessageMarker.PersistentDelegationUnlock.length);
74-
const decrypted = Crypto.decode(privateKey, senderPublicKey, payload, signSchema);
70+
const markerLength = MessageMarker.PersistentDelegationUnlock.length;
71+
const ephemeralPublicKey = encryptMessage.payload.substring(markerLength, markerLength + 64);
72+
const payload = encryptMessage.payload.substring(markerLength + ephemeralPublicKey.length);
73+
const decrypted = Crypto.decode(privateKey, ephemeralPublicKey, payload, signSchema);
7574
return decrypted.toUpperCase();
7675
}
7776
}

src/model/transaction/PersistentDelegationRequestTransaction.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export class PersistentDelegationRequestTransaction extends TransferTransaction
2828
* @param deadline - The deadline to include the transaction.
2929
* @param delegatedPrivateKey - The private key of delegated account
3030
* @param recipientPublicKey - The recipient public key
31-
* @param senderPrivateKey - The sender's private key
3231
* @param networkType - The network type.
3332
* @param maxFee - (Optional) Max fee defined by the sender
3433
* @returns {TransferTransaction}
@@ -37,11 +36,10 @@ export class PersistentDelegationRequestTransaction extends TransferTransaction
3736
deadline: Deadline,
3837
delegatedPrivateKey: string,
3938
recipientPublicKey: string,
40-
senderPrivateKey: string,
4139
networkType: NetworkType,
4240
maxFee: UInt64 = new UInt64([0, 0])): PersistentDelegationRequestTransaction {
4341
const message = PersistentHarvestingDelegationMessage
44-
.create(delegatedPrivateKey, senderPrivateKey, recipientPublicKey, networkType);
42+
.create(delegatedPrivateKey, recipientPublicKey, networkType);
4543
return super.create(deadline, Address.createFromPublicKey(recipientPublicKey, networkType), [], message, networkType, maxFee);
4644
}
4745
}

src/model/transaction/TransferTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class TransferTransaction extends Transaction {
158158
if (this.message.type === MessageType.PersistentHarvestingDelegationMessage) {
159159
if (this.mosaics.length > 0) {
160160
throw new Error('PersistentDelegationRequestTransaction should be created without Mosaic');
161-
} else if (!/^[0-9a-fA-F]{208}$/.test(this.message.payload)) {
161+
} else if (!/^[0-9a-fA-F]{272}$/.test(this.message.payload)) {
162162
throw new Error('PersistentDelegationRequestTransaction message is invalid');
163163
}
164164
}

src/service/MosaicRestrictionTransactionService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ export class MosaicRestrictionTransactionService {
168168
private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable<MosaicGlobalRestrictionItem | undefined> {
169169
return this.restrictionMosaicRepository.getMosaicGlobalRestriction(mosaicId).pipe(
170170
map((mosaicRestriction: MosaicGlobalRestriction) => {
171-
console.log(mosaicRestriction.restrictions);
172171
return mosaicRestriction.restrictions.get(restrictionKey.toString());
173172
}),
174173
catchError((err: Error) => {

test/model/message/EncryptedMessage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('EncryptedMessage', () => {
5656
expect(plainMessage.payload).to.be.equal('Testing simple transfer');
5757
});
5858

59-
it('should return should return decrepted message reading from message payload', () => {
59+
it('should return decrepted message reading from message payload', () => {
6060
const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6';
6161
const transferTransaction = TransferTransaction.create(
6262
Deadline.create(),

test/model/message/PersistentHarvestingDelegationMessage.spec.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ describe('PersistentHarvestingDelegationMessage', () => {
4545
it('should create a PersistentHarvestingDelegation message', () => {
4646
const encryptedMessage =
4747
PersistentHarvestingDelegationMessage
48-
.create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST);
49-
expect(encryptedMessage.payload.length).to.be.equal(208);
48+
.create(delegatedPrivateKey, recipient.publicKey, NetworkType.MIJIN_TEST);
49+
expect(encryptedMessage.payload.length).to.be.equal(272);
5050
expect(encryptedMessage.type).to.be.equal(MessageType.PersistentHarvestingDelegationMessage);
5151
});
5252

@@ -69,39 +69,37 @@ describe('PersistentHarvestingDelegationMessage', () => {
6969
it('should create and decrypt message', () => {
7070
const encryptedMessage =
7171
PersistentHarvestingDelegationMessage
72-
.create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST);
72+
.create(delegatedPrivateKey, recipient.publicKey, NetworkType.MIJIN_TEST);
7373
const plainMessage =
74-
PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST);
74+
PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient.privateKey, NetworkType.MIJIN_TEST);
7575
expect(plainMessage).to.be.equal(delegatedPrivateKey);
7676
});
7777

78-
it('should return should return decrepted message reading from message payload', () => {
78+
it('return decrepted message reading from message payload', () => {
7979
const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6';
8080
const tx =
8181
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
8282
Deadline.create(),
8383
delegatedPrivateKey,
8484
recipient.publicKey,
85-
sender.privateKey,
8685
NetworkType.MIJIN_TEST,
8786
);
8887
const signedTransaction = tx.signWith(sender, generationHash);
8988
const encryptMessage =
9089
PersistentHarvestingDelegationMessage
9190
.createFromPayload(signedTransaction.payload.substring(322, signedTransaction.payload.length));
9291
const plainMessage =
93-
PersistentHarvestingDelegationMessage.decrypt(encryptMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST);
92+
PersistentHarvestingDelegationMessage.decrypt(encryptMessage, recipient.privateKey, NetworkType.MIJIN_TEST);
9493
expect(plainMessage).to.be.equal(delegatedPrivateKey);
9594
});
9695

9796
it('should encrypt and decrypt message using NIS1 schema', () => {
9897
const encryptedMessage =
9998
PersistentHarvestingDelegationMessage
100-
.create(delegatedPrivateKey, sender_nis.privateKey, recipient_nis.publicKey, NetworkType.TEST_NET);
99+
.create(delegatedPrivateKey, recipient_nis.publicKey, NetworkType.TEST_NET);
101100
const plainMessage =
102101
PersistentHarvestingDelegationMessage.decrypt(encryptedMessage,
103102
recipient_nis.privateKey,
104-
sender_nis.publicKey,
105103
NetworkType.TEST_NET);
106104
expect(plainMessage).to.be.equal(delegatedPrivateKey);
107105
});

test/model/transaction/PersistentDelegationRequestTransaction.spec.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { expect } from 'chai';
1818
import { Account } from '../../../src/model/account/Account';
1919
import { Address } from '../../../src/model/account/Address';
2020
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
21+
import { MessageMarker } from '../../../src/model/message/MessageMarker';
2122
import { Deadline } from '../../../src/model/transaction/Deadline';
2223
import {
2324
PersistentDelegationRequestTransaction,
@@ -30,7 +31,7 @@ describe('PersistentDelegationRequestTransaction', () => {
3031
const delegatedPrivateKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A';
3132
const recipientPublicKey = '9DBF67474D6E1F8B131B4EB1F5BA0595AFFAE1123607BC1048F342193D7E669F';
3233
const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6';
33-
const messageMarker = 'FECC71C764BFE598';
34+
const messageMarker = MessageMarker.PersistentDelegationUnlock;
3435

3536
before(() => {
3637
account = TestingAccount;
@@ -42,7 +43,6 @@ describe('PersistentDelegationRequestTransaction', () => {
4243
Deadline.create(),
4344
delegatedPrivateKey,
4445
recipientPublicKey,
45-
account.privateKey,
4646
NetworkType.MIJIN_TEST,
4747
);
4848

@@ -56,7 +56,6 @@ describe('PersistentDelegationRequestTransaction', () => {
5656
Deadline.create(),
5757
delegatedPrivateKey,
5858
recipientPublicKey,
59-
account.privateKey,
6059
NetworkType.MIJIN_TEST,
6160
new UInt64([1, 0]),
6261
);
@@ -71,11 +70,10 @@ describe('PersistentDelegationRequestTransaction', () => {
7170
Deadline.create(),
7271
delegatedPrivateKey,
7372
recipientPublicKey,
74-
account.privateKey,
7573
NetworkType.MIJIN_TEST,
7674
);
7775

78-
expect(persistentDelegationRequestTransaction.message.payload.length).to.be.equal(192 + messageMarker.length);
76+
expect(persistentDelegationRequestTransaction.message.payload.length).to.be.equal(192 + messageMarker.length + 64);
7977
expect(persistentDelegationRequestTransaction.message.payload.includes(messageMarker)).to.be.true;
8078
expect(persistentDelegationRequestTransaction.mosaics.length).to.be.equal(0);
8179
expect(persistentDelegationRequestTransaction.recipientAddress).to.be.instanceof(Address);
@@ -96,20 +94,6 @@ describe('PersistentDelegationRequestTransaction', () => {
9694
Deadline.create(),
9795
'abc',
9896
recipientPublicKey,
99-
account.privateKey,
100-
NetworkType.MIJIN_TEST,
101-
new UInt64([1, 0]),
102-
);
103-
}).to.throw();
104-
});
105-
106-
it('should throw exception with invalid sender private key', () => {
107-
expect(() => {
108-
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
109-
Deadline.create(),
110-
delegatedPrivateKey,
111-
recipientPublicKey,
112-
'abc',
11397
NetworkType.MIJIN_TEST,
11498
new UInt64([1, 0]),
11599
);

test/model/transaction/TransferTransaction.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { CreateTransactionFromPayload } from '../../../src/infrastructure/transa
2020
import { Account } from '../../../src/model/account/Account';
2121
import { Address } from '../../../src/model/account/Address';
2222
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
23+
import { MessageMarker } from '../../../src/model/message/MessageMarker';
2324
import { MessageType } from '../../../src/model/message/MessageType';
2425
import { PersistentHarvestingDelegationMessage } from '../../../src/model/message/PersistentHarvestingDelegationMessage';
2526
import { PlainMessage } from '../../../src/model/message/PlainMessage';
@@ -40,7 +41,7 @@ describe('TransferTransaction', () => {
4041
const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6';
4142
const delegatedPrivateKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A';
4243
const recipientPublicKey = '9DBF67474D6E1F8B131B4EB1F5BA0595AFFAE1123607BC1048F342193D7E669F';
43-
const messageMarker = 'FECC71C764BFE598';
44+
const messageMarker = MessageMarker.PersistentDelegationUnlock;
4445
let statement: Statement;
4546
const unresolvedAddress = new NamespaceId('address');
4647
const unresolvedMosaicId = new NamespaceId('mosaic');
@@ -252,7 +253,7 @@ describe('TransferTransaction', () => {
252253
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
253254
[],
254255
PersistentHarvestingDelegationMessage
255-
.create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
256+
.create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
256257
NetworkType.MIJIN_TEST,
257258
);
258259

@@ -265,11 +266,10 @@ describe('TransferTransaction', () => {
265266
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
266267
[],
267268
PersistentHarvestingDelegationMessage
268-
.create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
269+
.create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
269270
NetworkType.MIJIN_TEST,
270271
);
271-
272-
expect(transferTransaction.message.payload.length).to.be.equal(192 + messageMarker.length);
272+
expect(transferTransaction.message.payload.length).to.be.equal(192 + messageMarker.length + 64);
273273
expect(transferTransaction.message.payload.includes(messageMarker)).to.be.true;
274274
expect(transferTransaction.mosaics.length).to.be.equal(0);
275275
expect(transferTransaction.recipientAddress).to.be.instanceof(Address);
@@ -291,7 +291,7 @@ describe('TransferTransaction', () => {
291291
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
292292
[NetworkCurrencyMosaic.createRelative(100)],
293293
PersistentHarvestingDelegationMessage
294-
.create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
294+
.create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
295295
NetworkType.MIJIN_TEST,
296296
);
297297
}).to.throw(Error, 'PersistentDelegationRequestTransaction should be created without Mosaic');
@@ -303,7 +303,7 @@ describe('TransferTransaction', () => {
303303
Deadline.create(),
304304
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
305305
[NetworkCurrencyMosaic.createRelative(100)],
306-
PersistentHarvestingDelegationMessage.create('abc', account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
306+
PersistentHarvestingDelegationMessage.create('abc', recipientPublicKey, NetworkType.MIJIN_TEST),
307307
NetworkType.MIJIN_TEST,
308308
);
309309
}).to.throw();
@@ -315,7 +315,7 @@ describe('TransferTransaction', () => {
315315
Deadline.create(),
316316
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
317317
[NetworkCurrencyMosaic.createRelative(100)],
318-
PersistentHarvestingDelegationMessage.create(delegatedPrivateKey, 'abc', recipientPublicKey, NetworkType.MIJIN_TEST),
318+
PersistentHarvestingDelegationMessage.create(delegatedPrivateKey, recipientPublicKey, NetworkType.MIJIN_TEST),
319319
NetworkType.MIJIN_TEST,
320320
);
321321
}).to.throw();

0 commit comments

Comments
 (0)