Skip to content

Commit 9000c02

Browse files
committed
Merge branch 'task/g295_namespace_id_in_metadata_mosaic_restriction_transaction' of github.com:NEMStudios/nem2-sdk-typescript-javascript into task/g295_namespace_id_in_metadata_mosaic_restriction_transaction
2 parents 830c753 + b12b046 commit 9000c02

13 files changed

+119
-39
lines changed

src/model/message/PersistentHarvestingDelegationMessage.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ export class PersistentHarvestingDelegationMessage extends Message {
3232

3333
/**
3434
*
35-
* @param harvesterPublicKey - Haverster account public key
36-
* @param privateKey - Sender private key
35+
* @param delegatedPrivateKey - Private key of delegated account
36+
* @param senderPrivateKey - Sender private key
37+
* @param recipientPrivateKey - Recipient public key
3738
* @param {NetworkType} networkType - Catapult network type
3839
* @return {PersistentHarvestingDelegationMessage}
3940
*/
40-
public static create(harvesterPublicKey: string,
41-
privateKey: string,
41+
public static create(delegatedPrivateKey: string,
42+
senderPrivateKey: string,
43+
recipientPublicKey: string,
4244
networkType: NetworkType): PersistentHarvestingDelegationMessage {
4345
const signSchema = SHA3Hasher.resolveSignSchema(networkType);
4446
const encrypted = MessageMarker.PersistentDelegationUnlock +
45-
Crypto.encode(privateKey, harvesterPublicKey, harvesterPublicKey, signSchema, true).toUpperCase();
47+
Crypto.encode(senderPrivateKey, recipientPublicKey, delegatedPrivateKey, signSchema, true).toUpperCase();
4648
return new PersistentHarvestingDelegationMessage(encrypted);
4749
}
4850

src/model/transaction/AccountRestrictionTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class AccountRestrictionTransaction {
102102
): AccountOperationRestrictionTransaction {
103103
if (![AccountRestrictionType.AllowIncomingTransactionType,
104104
AccountRestrictionType.AllowOutgoingTransactionType,
105-
AccountRestrictionType.BlockOutgoingTransactionType,
105+
AccountRestrictionType.BlockIncomingTransactionType,
106106
AccountRestrictionType.BlockOutgoingTransactionType].includes(restrictionType)) {
107107
throw new Error ('Restriction type is not allowed.');
108108
}

src/model/transaction/AddressAliasTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class AddressAliasTransaction extends Transaction {
9393
*/
9494
public readonly namespaceId: NamespaceId,
9595
/**
96-
* The mosaic id.
96+
* The address.
9797
*/
9898
public readonly address: Address,
9999
signature?: string,

src/model/transaction/AggregateTransaction.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,19 @@ export class AggregateTransaction extends Transaction {
289289
* @internal
290290
* @returns {Uint8Array}
291291
*/
292-
protected generateBytes(): Uint8Array {
292+
protected generateBytes(signer?: PublicAccount): Uint8Array {
293293
const signerBuffer = new Uint8Array(32);
294294
const signatureBuffer = new Uint8Array(64);
295-
296295
let transactions = Uint8Array.from([]);
297296
this.innerTransactions.forEach((transaction) => {
297+
if (!transaction.signer) {
298+
if (this.type === TransactionType.AGGREGATE_COMPLETE) {
299+
transaction = Object.assign({__proto__: Object.getPrototypeOf(transaction)}, transaction, {signer});
300+
} else {
301+
throw new Error(
302+
'InnerTransaction signer must be provide. Only AggregateComplete transaction can use delegated signer.');
303+
}
304+
}
298305
const transactionByte = transaction.toAggregateTransactionBytes();
299306
transactions = GeneratorUtils.concatTypedArrays(transactions, transactionByte);
300307
});

src/model/transaction/InnerTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ import {Transaction} from './Transaction';
2020
/**
2121
* Transaction with signer included, used when adding signer to transactions included in an aggregate transaction.
2222
*/
23-
export type InnerTransaction = Transaction & {signer: PublicAccount};
23+
export type InnerTransaction = Transaction & {signer?: PublicAccount};

src/model/transaction/PersistentDelegationRequestTransaction.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@ export class PersistentDelegationRequestTransaction extends TransferTransaction
2626
* Create a PersistentDelegationRequestTransaction with special message payload
2727
* for presistent harvesting delegation unlocking
2828
* @param deadline - The deadline to include the transaction.
29-
* @param HarvestePublicKey - The harvester public key
29+
* @param delegatedPrivateKey - The private key of delegated account
30+
* @param recipientPublicKey - The recipient public key
3031
* @param senderPrivateKey - The sender's private key
3132
* @param networkType - The network type.
3233
* @param maxFee - (Optional) Max fee defined by the sender
3334
* @returns {TransferTransaction}
3435
*/
3536
public static createPersistentDelegationRequestTransaction(
3637
deadline: Deadline,
37-
HarvestePublicKey: string,
38+
delegatedPrivateKey: string,
39+
recipientPublicKey: string,
3840
senderPrivateKey: string,
3941
networkType: NetworkType,
4042
maxFee: UInt64 = new UInt64([0, 0])): PersistentDelegationRequestTransaction {
41-
const message = PersistentHarvestingDelegationMessage.create(HarvestePublicKey, senderPrivateKey, networkType);
42-
return super.create(deadline, Address.createFromPublicKey(HarvestePublicKey, networkType), [], message, networkType, maxFee);
43+
const message = PersistentHarvestingDelegationMessage
44+
.create(delegatedPrivateKey, senderPrivateKey, recipientPublicKey, networkType);
45+
return super.create(deadline, Address.createFromPublicKey(recipientPublicKey, networkType), [], message, networkType, maxFee);
4346
}
4447
}

src/model/transaction/Transaction.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ export abstract class Transaction {
105105

106106
/**
107107
* @internal
108+
* @param singer Optional singer for delegated aggregate complete transaction only.
108109
*/
109-
protected abstract generateBytes(): Uint8Array;
110+
protected abstract generateBytes(signer?: PublicAccount): Uint8Array;
110111

111112
/**
112113
* @internal
@@ -123,7 +124,7 @@ export abstract class Transaction {
123124
public signWith(account: Account, generationHash: string): SignedTransaction {
124125
const generationHashBytes = Array.from(Convert.hexToUint8(generationHash));
125126
const signSchema = SHA3Hasher.resolveSignSchema(account.networkType);
126-
const byteBuffer = Array.from(this.generateBytes());
127+
const byteBuffer = Array.from(this.generateBytes(account.publicAccount));
127128
const signingBytes = generationHashBytes.concat(byteBuffer.slice(4 + 64 + 32));
128129
const keyPairEncoded = KeyPair.createKeyPairFromPrivateKeyString(account.privateKey, signSchema);
129130
const signature = Array.from(KeyPair.sign(account, new Uint8Array(signingBytes), signSchema));
@@ -162,10 +163,12 @@ export abstract class Transaction {
162163

163164
/**
164165
* Convert an aggregate transaction to an inner transaction including transaction signer.
165-
* @param signer - Transaction signer.
166+
* Signer is optional for `AggregateComplete` transaction `ONLY`.
167+
* If no signer provided, aggregate transaction signer will be delegated on signing
168+
* @param signer - Innre transaction signer. (Optional for `AggregateComplete` transaction `ONLY`)
166169
* @returns InnerTransaction
167170
*/
168-
public toAggregate(signer: PublicAccount): InnerTransaction {
171+
public toAggregate(signer?: PublicAccount): InnerTransaction {
169172
if (this.type === TransactionType.AGGREGATE_BONDED || this.type === TransactionType.AGGREGATE_COMPLETE) {
170173
throw new Error('Inner transaction cannot be an aggregated transaction.');
171174
}

src/service/AggregateTransactionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class AggregateTransactionService {
5353
signers.push(signedTransaction.signerPublicKey);
5454
}
5555
return observableFrom(aggregateTransaction.innerTransactions).pipe(
56-
mergeMap((innerTransaction) => this.accountHttp.getMultisigAccountInfo(innerTransaction.signer.address)
56+
mergeMap((innerTransaction) => this.accountHttp.getMultisigAccountInfo(innerTransaction.signer!.address)
5757
.pipe(
5858
/**
5959
* For multisig account, we need to get the graph info in case it has multiple levels

test/model/message/PersistentHarvestingDelegationMessage.spec.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('PersistentHarvestingDelegationMessage', () => {
2929

3030
let sender_nis: Account;
3131
let recipient_nis: Account;
32+
const delegatedPrivateKey = 'F0AB1010EFEE19EE5373719881DF5123C13E643C519655F7E97347BFF77175BF';
3233
before(() => {
3334
sender = Account.createFromPrivateKey('2602F4236B199B3DF762B2AAB46FC3B77D8DDB214F0B62538D3827576C46C108',
3435
NetworkType.MIJIN_TEST);
@@ -43,7 +44,8 @@ describe('PersistentHarvestingDelegationMessage', () => {
4344

4445
it('should create a PersistentHarvestingDelegation message', () => {
4546
const encryptedMessage =
46-
PersistentHarvestingDelegationMessage.create(recipient.publicKey, sender.privateKey, NetworkType.MIJIN_TEST);
47+
PersistentHarvestingDelegationMessage
48+
.create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST);
4749
expect(encryptedMessage.payload.length).to.be.equal(208);
4850
expect(encryptedMessage.type).to.be.equal(MessageType.PersistentHarvestingDelegationMessage);
4951
});
@@ -66,17 +68,19 @@ describe('PersistentHarvestingDelegationMessage', () => {
6668

6769
it('should create and decrypt message', () => {
6870
const encryptedMessage =
69-
PersistentHarvestingDelegationMessage.create(recipient.publicKey, sender.privateKey, NetworkType.MIJIN_TEST);
71+
PersistentHarvestingDelegationMessage
72+
.create(delegatedPrivateKey, sender.privateKey, recipient.publicKey, NetworkType.MIJIN_TEST);
7073
const plainMessage =
7174
PersistentHarvestingDelegationMessage.decrypt(encryptedMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST);
72-
expect(plainMessage).to.be.equal(recipient.publicKey);
75+
expect(plainMessage).to.be.equal(delegatedPrivateKey);
7376
});
7477

7578
it('should return should return decrepted message reading from message payload', () => {
7679
const generationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6';
7780
const tx =
7881
PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
7982
Deadline.create(),
83+
delegatedPrivateKey,
8084
recipient.publicKey,
8185
sender.privateKey,
8286
NetworkType.MIJIN_TEST,
@@ -87,18 +91,19 @@ describe('PersistentHarvestingDelegationMessage', () => {
8791
.createFromPayload(signedTransaction.payload.substring(298, signedTransaction.payload.length));
8892
const plainMessage =
8993
PersistentHarvestingDelegationMessage.decrypt(encryptMessage, recipient.privateKey, sender.publicKey, NetworkType.MIJIN_TEST);
90-
expect(plainMessage).to.be.equal(recipient.publicKey);
94+
expect(plainMessage).to.be.equal(delegatedPrivateKey);
9195
});
9296

9397
it('should encrypt and decrypt message using NIS1 schema', () => {
9498
const encryptedMessage =
95-
PersistentHarvestingDelegationMessage.create(recipient_nis.publicKey, sender_nis.privateKey, NetworkType.TEST_NET);
99+
PersistentHarvestingDelegationMessage
100+
.create(delegatedPrivateKey, sender_nis.privateKey, recipient_nis.publicKey, NetworkType.TEST_NET);
96101
const plainMessage =
97102
PersistentHarvestingDelegationMessage.decrypt(encryptedMessage,
98103
recipient_nis.privateKey,
99104
sender_nis.publicKey,
100105
NetworkType.TEST_NET);
101-
expect(plainMessage).to.be.equal(recipient_nis.publicKey);
106+
expect(plainMessage).to.be.equal(delegatedPrivateKey);
102107
});
103108

104109
});

test/model/transaction/AccountRestrictionTransaction.spec.ts

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

1717
import {expect} from 'chai';
1818
import {Account} from '../../../src/model/account/Account';
19-
import { AccountRestrictionModificationAction } from '../../../src/model/restriction/AccountRestrictionModificationAction';
20-
import { AccountRestrictionType } from '../../../src/model/restriction/AccountRestrictionType';
2119
import {Address} from '../../../src/model/account/Address';
2220
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
2321
import {MosaicId} from '../../../src/model/mosaic/MosaicId';
22+
import { AccountRestrictionModificationAction } from '../../../src/model/restriction/AccountRestrictionModificationAction';
23+
import { AccountRestrictionType } from '../../../src/model/restriction/AccountRestrictionType';
2424
import { AccountRestrictionModification } from '../../../src/model/transaction/AccountRestrictionModification';
2525
import {AccountRestrictionTransaction} from '../../../src/model/transaction/AccountRestrictionTransaction';
2626
import {Deadline} from '../../../src/model/transaction/Deadline';

0 commit comments

Comments
 (0)