From 0d24af3b468aeec9eb526ab4dd747c8830e607e8 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Sat, 12 Oct 2019 19:23:12 +0100 Subject: [PATCH] JAV-67 [Github #300] Fixed PersistentDelegationRequestTransaction --- .../PersistentHarvestingDelegationMessage.ts | 12 +++++++----- .../PersistentDelegationRequestTransaction.ts | 11 +++++++---- ...rsistentHarvestingDelegationMessage.spec.ts | 17 +++++++++++------ ...sistentDelegationRequestTransaction.spec.ts | 18 ++++++++++++------ .../transaction/TransferTransaction.spec.ts | 16 ++++++++++------ 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/model/message/PersistentHarvestingDelegationMessage.ts b/src/model/message/PersistentHarvestingDelegationMessage.ts index a015b032d3..15db8ca45c 100644 --- a/src/model/message/PersistentHarvestingDelegationMessage.ts +++ b/src/model/message/PersistentHarvestingDelegationMessage.ts @@ -32,17 +32,19 @@ export class PersistentHarvestingDelegationMessage extends Message { /** * - * @param harvesterPublicKey - Haverster account public key - * @param privateKey - Sender private key + * @param delegatedPrivateKey - Private key of delegated account + * @param senderPrivateKey - Sender private key + * @param recipientPrivateKey - Recipient public key * @param {NetworkType} networkType - Catapult network type * @return {PersistentHarvestingDelegationMessage} */ - public static create(harvesterPublicKey: string, - privateKey: string, + public static create(delegatedPrivateKey: string, + senderPrivateKey: string, + recipientPublicKey: string, networkType: NetworkType): PersistentHarvestingDelegationMessage { const signSchema = SHA3Hasher.resolveSignSchema(networkType); const encrypted = MessageMarker.PersistentDelegationUnlock + - Crypto.encode(privateKey, harvesterPublicKey, harvesterPublicKey, signSchema, true).toUpperCase(); + Crypto.encode(senderPrivateKey, recipientPublicKey, delegatedPrivateKey, signSchema, true).toUpperCase(); return new PersistentHarvestingDelegationMessage(encrypted); } diff --git a/src/model/transaction/PersistentDelegationRequestTransaction.ts b/src/model/transaction/PersistentDelegationRequestTransaction.ts index e66f151a83..20db9d713a 100644 --- a/src/model/transaction/PersistentDelegationRequestTransaction.ts +++ b/src/model/transaction/PersistentDelegationRequestTransaction.ts @@ -26,7 +26,8 @@ export class PersistentDelegationRequestTransaction extends TransferTransaction * Create a PersistentDelegationRequestTransaction with special message payload * for presistent harvesting delegation unlocking * @param deadline - The deadline to include the transaction. - * @param HarvestePublicKey - The harvester public key + * @param delegatedPrivateKey - The private key of delegated account + * @param recipientPublicKey - The recipient public key * @param senderPrivateKey - The sender's private key * @param networkType - The network type. * @param maxFee - (Optional) Max fee defined by the sender @@ -34,11 +35,13 @@ export class PersistentDelegationRequestTransaction extends TransferTransaction */ public static createPersistentDelegationRequestTransaction( deadline: Deadline, - HarvestePublicKey: string, + delegatedPrivateKey: string, + recipientPublicKey: string, senderPrivateKey: string, networkType: NetworkType, maxFee: UInt64 = new UInt64([0, 0])): PersistentDelegationRequestTransaction { - const message = PersistentHarvestingDelegationMessage.create(HarvestePublicKey, senderPrivateKey, networkType); - return super.create(deadline, Address.createFromPublicKey(HarvestePublicKey, networkType), [], message, networkType, maxFee); + const message = PersistentHarvestingDelegationMessage + .create(delegatedPrivateKey, senderPrivateKey, recipientPublicKey, networkType); + return super.create(deadline, Address.createFromPublicKey(recipientPublicKey, networkType), [], message, networkType, maxFee); } } diff --git a/test/model/message/PersistentHarvestingDelegationMessage.spec.ts b/test/model/message/PersistentHarvestingDelegationMessage.spec.ts index 9ddcd69579..7aee611e0b 100644 --- a/test/model/message/PersistentHarvestingDelegationMessage.spec.ts +++ b/test/model/message/PersistentHarvestingDelegationMessage.spec.ts @@ -29,6 +29,7 @@ describe('PersistentHarvestingDelegationMessage', () => { let sender_nis: Account; let recipient_nis: Account; + const delegatedPrivateKey = 'F0AB1010EFEE19EE5373719881DF5123C13E643C519655F7E97347BFF77175BF'; before(() => { sender = Account.createFromPrivateKey('2602F4236B199B3DF762B2AAB46FC3B77D8DDB214F0B62538D3827576C46C108', NetworkType.MIJIN_TEST); @@ -43,7 +44,8 @@ describe('PersistentHarvestingDelegationMessage', () => { it('should create a PersistentHarvestingDelegation message', () => { const encryptedMessage = - PersistentHarvestingDelegationMessage.create(recipient.publicKey, sender.privateKey, NetworkType.MIJIN_TEST); + PersistentHarvestingDelegationMessage + .create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST); expect(encryptedMessage.payload.length).to.be.equal(208); expect(encryptedMessage.type).to.be.equal(MessageType.PersistentHarvestingDelegationMessage); }); @@ -66,10 +68,11 @@ describe('PersistentHarvestingDelegationMessage', () => { it('should create and decrypt message', () => { const encryptedMessage = - PersistentHarvestingDelegationMessage.create(recipient.publicKey, sender.privateKey, NetworkType.MIJIN_TEST); + PersistentHarvestingDelegationMessage + .create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST); const plainMessage = PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST); - expect(plainMessage).to.be.equal(recipient.publicKey); + expect(plainMessage).to.be.equal(delegatedPrivateKey); }); it('should return should return decrepted message reading from message payload', () => { @@ -77,6 +80,7 @@ describe('PersistentHarvestingDelegationMessage', () => { const tx = PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( Deadline.create(), + delegatedPrivateKey, recipient.publicKey, sender.privateKey, NetworkType.MIJIN_TEST, @@ -87,18 +91,19 @@ describe('PersistentHarvestingDelegationMessage', () => { .createFromPayload(signedTransaction.payload.substring(298, signedTransaction.payload.length)); const plainMessage = PersistentHarvestingDelegationMessage.decrypt(encryptMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST); - expect(plainMessage).to.be.equal(recipient.publicKey); + expect(plainMessage).to.be.equal(delegatedPrivateKey); }); it('should encrypt and decrypt message using NIS1 schema', () => { const encryptedMessage = - PersistentHarvestingDelegationMessage.create(recipient_nis.publicKey, sender_nis.privateKey, NetworkType.TEST_NET); + PersistentHarvestingDelegationMessage + .create(delegatedPrivateKey, sender_nis.privateKey, recipient_nis.publicKey, NetworkType.TEST_NET); const plainMessage = PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient_nis.privateKey, sender_nis.publicKey, NetworkType.TEST_NET); - expect(plainMessage).to.be.equal(recipient_nis.publicKey); + expect(plainMessage).to.be.equal(delegatedPrivateKey); }); }); diff --git a/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts b/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts index a11455650b..ac5753c78c 100644 --- a/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts +++ b/test/model/transaction/PersistentDelegationRequestTransaction.spec.ts @@ -27,7 +27,8 @@ import { TestingAccount } from '../../conf/conf.spec'; describe('PersistentDelegationRequestTransaction', () => { let account: Account; - const harvesterPublicKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A'; + const delegatedPrivateKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A'; + const recipientPublicKey = '9DBF67474D6E1F8B131B4EB1F5BA0595AFFAE1123607BC1048F342193D7E669F'; const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; const messageMarker = 'FECC71C764BFE598'; @@ -39,7 +40,8 @@ describe('PersistentDelegationRequestTransaction', () => { const persistentDelegationRequestTransaction = PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( Deadline.create(), - harvesterPublicKey, + delegatedPrivateKey, + recipientPublicKey, account.privateKey, NetworkType.MIJIN_TEST, ); @@ -52,7 +54,8 @@ describe('PersistentDelegationRequestTransaction', () => { const persistentDelegationRequestTransaction = PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( Deadline.create(), - harvesterPublicKey, + delegatedPrivateKey, + recipientPublicKey, account.privateKey, NetworkType.MIJIN_TEST, new UInt64([1, 0]), @@ -66,7 +69,8 @@ describe('PersistentDelegationRequestTransaction', () => { const persistentDelegationRequestTransaction = PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( Deadline.create(), - harvesterPublicKey, + delegatedPrivateKey, + recipientPublicKey, account.privateKey, NetworkType.MIJIN_TEST, ); @@ -76,7 +80,7 @@ describe('PersistentDelegationRequestTransaction', () => { expect(persistentDelegationRequestTransaction.mosaics.length).to.be.equal(0); expect(persistentDelegationRequestTransaction.recipientAddress).to.be.instanceof(Address); expect((persistentDelegationRequestTransaction.recipientAddress as Address).plain()) - .to.be.equal('SAMA2UEQNAQ45DWYDNJVLPWKQJDAHFZIVLWACIGN'); + .to.be.equal('SDBC4JE7GTJAKN2XJCQWWRJMYA35AFOYQBATXOUA'); const signedTransaction = persistentDelegationRequestTransaction.signWith(account, generationHash); @@ -91,6 +95,7 @@ describe('PersistentDelegationRequestTransaction', () => { PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( Deadline.create(), 'abc', + recipientPublicKey, account.privateKey, NetworkType.MIJIN_TEST, new UInt64([1, 0]), @@ -102,7 +107,8 @@ describe('PersistentDelegationRequestTransaction', () => { expect(() => { PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction( Deadline.create(), - harvesterPublicKey, + delegatedPrivateKey, + recipientPublicKey, 'abc', NetworkType.MIJIN_TEST, new UInt64([1, 0]), diff --git a/test/model/transaction/TransferTransaction.spec.ts b/test/model/transaction/TransferTransaction.spec.ts index a8e1648616..03f775a182 100644 --- a/test/model/transaction/TransferTransaction.spec.ts +++ b/test/model/transaction/TransferTransaction.spec.ts @@ -31,7 +31,8 @@ import { TestingAccount } from '../../conf/conf.spec'; describe('TransferTransaction', () => { let account: Account; const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6'; - const harvesterPublicKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A'; + const delegatedPrivateKey = '8A78C9E9B0E59D0F74C0D47AB29FBD523C706293A3FA9CD9FE0EEB2C10EA924A'; + const recipientPublicKey = '9DBF67474D6E1F8B131B4EB1F5BA0595AFFAE1123607BC1048F342193D7E669F'; const messageMarker = 'FECC71C764BFE598'; before(() => { account = TestingAccount; @@ -229,7 +230,8 @@ describe('TransferTransaction', () => { Deadline.create(), Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [], - PersistentHarvestingDelegationMessage.create(harvesterPublicKey, account.privateKey, NetworkType.MIJIN_TEST), + PersistentHarvestingDelegationMessage + .create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); @@ -241,7 +243,8 @@ describe('TransferTransaction', () => { Deadline.create(), Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [], - PersistentHarvestingDelegationMessage.create(harvesterPublicKey, account.privateKey, NetworkType.MIJIN_TEST), + PersistentHarvestingDelegationMessage + .create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); @@ -266,7 +269,8 @@ describe('TransferTransaction', () => { Deadline.create(), Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [NetworkCurrencyMosaic.createRelative(100)], - PersistentHarvestingDelegationMessage.create(harvesterPublicKey, account.privateKey, NetworkType.MIJIN_TEST), + PersistentHarvestingDelegationMessage + .create(delegatedPrivateKey, account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); }).to.throw(Error, 'PersistentDelegationRequestTransaction should be created without Mosaic'); @@ -278,7 +282,7 @@ describe('TransferTransaction', () => { Deadline.create(), Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [NetworkCurrencyMosaic.createRelative(100)], - PersistentHarvestingDelegationMessage.create('abc', account.privateKey, NetworkType.MIJIN_TEST), + PersistentHarvestingDelegationMessage.create('abc', account.privateKey, recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); }).to.throw(); @@ -290,7 +294,7 @@ describe('TransferTransaction', () => { Deadline.create(), Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'), [NetworkCurrencyMosaic.createRelative(100)], - PersistentHarvestingDelegationMessage.create(harvesterPublicKey, 'abc', NetworkType.MIJIN_TEST), + PersistentHarvestingDelegationMessage.create(delegatedPrivateKey, 'abc', recipientPublicKey, NetworkType.MIJIN_TEST), NetworkType.MIJIN_TEST, ); }).to.throw();