Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/model/message/PersistentHarvestingDelegationMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
11 changes: 7 additions & 4 deletions src/model/transaction/PersistentDelegationRequestTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ 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
* @returns {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);
}
}
17 changes: 11 additions & 6 deletions test/model/message/PersistentHarvestingDelegationMessage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
Expand All @@ -66,17 +68,19 @@ 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', () => {
const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6';
const tx =
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
Deadline.create(),
delegatedPrivateKey,
recipient.publicKey,
sender.privateKey,
NetworkType.MIJIN_TEST,
Expand All @@ -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);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -39,7 +40,8 @@ describe('PersistentDelegationRequestTransaction', () => {
const persistentDelegationRequestTransaction =
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
Deadline.create(),
harvesterPublicKey,
delegatedPrivateKey,
recipientPublicKey,
account.privateKey,
NetworkType.MIJIN_TEST,
);
Expand All @@ -52,7 +54,8 @@ describe('PersistentDelegationRequestTransaction', () => {
const persistentDelegationRequestTransaction =
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
Deadline.create(),
harvesterPublicKey,
delegatedPrivateKey,
recipientPublicKey,
account.privateKey,
NetworkType.MIJIN_TEST,
new UInt64([1, 0]),
Expand All @@ -66,7 +69,8 @@ describe('PersistentDelegationRequestTransaction', () => {
const persistentDelegationRequestTransaction =
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
Deadline.create(),
harvesterPublicKey,
delegatedPrivateKey,
recipientPublicKey,
account.privateKey,
NetworkType.MIJIN_TEST,
);
Expand All @@ -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);

Expand All @@ -91,6 +95,7 @@ describe('PersistentDelegationRequestTransaction', () => {
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
Deadline.create(),
'abc',
recipientPublicKey,
account.privateKey,
NetworkType.MIJIN_TEST,
new UInt64([1, 0]),
Expand All @@ -102,7 +107,8 @@ describe('PersistentDelegationRequestTransaction', () => {
expect(() => {
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
Deadline.create(),
harvesterPublicKey,
delegatedPrivateKey,
recipientPublicKey,
'abc',
NetworkType.MIJIN_TEST,
new UInt64([1, 0]),
Expand Down
16 changes: 10 additions & 6 deletions test/model/transaction/TransferTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
);

Expand All @@ -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,
);

Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -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();
Expand Down