Skip to content

Commit 077d4f1

Browse files
author
Grégory Saive
authored
Merge branch 'master' into task/g38_raise_error_adding_agg_tx_as_inner_tx
2 parents a8d5d7b + 6770480 commit 077d4f1

File tree

6 files changed

+126
-19
lines changed

6 files changed

+126
-19
lines changed

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/infrastructure/Listener.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as WebSocket from 'ws';
2020
import {Address} from '../model/account/Address';
2121
import {PublicAccount} from '../model/account/PublicAccount';
2222
import {BlockInfo} from '../model/blockchain/BlockInfo';
23+
import {NamespaceId} from '../model/namespace/NamespaceId';
2324
import {AggregateTransaction} from '../model/transaction/AggregateTransaction';
2425
import {AggregateTransactionCosignature} from '../model/transaction/AggregateTransactionCosignature';
2526
import {CosignatureSignedTransaction} from '../model/transaction/CosignatureSignedTransaction';
@@ -375,8 +376,16 @@ export class Listener {
375376
* @param address
376377
* @returns {boolean}
377378
*/
378-
private transactionHasSignerOrReceptor(transaction: Transaction, address: Address): boolean {
379-
return transaction.signer!.address.equals(address) ||
380-
(transaction instanceof TransferTransaction && transaction.recipient.equals(address));
379+
private transactionHasSignerOrReceptor(transaction: Transaction, address: Address | NamespaceId): boolean {
380+
381+
if (address instanceof NamespaceId) {
382+
return transaction instanceof TransferTransaction
383+
&& (transaction.recipient as NamespaceId).equals(address);
384+
}
385+
386+
return transaction.signer!.address.equals(address) || (
387+
transaction instanceof TransferTransaction
388+
&& (transaction.recipient as Address).equals(address)
389+
);
381390
}
382391
}

src/model/namespace/MosaicAlias.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ export class MosaicAlias implements Alias {
5151
}
5252
return false;
5353
}
54+
55+
/**
56+
* Get string value of mosaicId
57+
* @returns {string}
58+
*/
59+
public toHex(): string {
60+
return this.mosaicId.toHex();
61+
}
5462
}

src/model/transaction/AddressAliasTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class AddressAliasTransaction extends Transaction {
4848
address: Address,
4949
networkType: NetworkType): AddressAliasTransaction {
5050
return new AddressAliasTransaction(networkType,
51-
TransactionVersion.MOSAIC_ALIAS,
51+
TransactionVersion.ADDRESS_ALIAS,
5252
deadline,
5353
new UInt64([0, 0]),
5454
actionType,
@@ -88,7 +88,7 @@ export class AddressAliasTransaction extends Transaction {
8888
signature?: string,
8989
signer?: PublicAccount,
9090
transactionInfo?: TransactionInfo) {
91-
super(TransactionType.MOSAIC_ALIAS, networkType, version, deadline, fee, signature, signer, transactionInfo);
91+
super(TransactionType.ADDRESS_ALIAS, networkType, version, deadline, fee, signature, signer, transactionInfo);
9292
}
9393

9494
/**

src/model/transaction/TransferTransaction.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Address } from '../account/Address';
1919
import { PublicAccount } from '../account/PublicAccount';
2020
import { NetworkType } from '../blockchain/NetworkType';
2121
import { Mosaic } from '../mosaic/Mosaic';
22+
import { NamespaceId } from '../namespace/NamespaceId';
2223
import { UInt64 } from '../UInt64';
2324
import { Deadline } from './Deadline';
2425
import { Message } from './Message';
@@ -41,7 +42,7 @@ export class TransferTransaction extends Transaction {
4142
* @returns {TransferTransaction}
4243
*/
4344
public static create(deadline: Deadline,
44-
recipient: Address,
45+
recipient: Address | NamespaceId,
4546
mosaics: Mosaic[],
4647
message: Message,
4748
networkType: NetworkType): TransferTransaction {
@@ -73,7 +74,7 @@ export class TransferTransaction extends Transaction {
7374
/**
7475
* The address of the recipient.
7576
*/
76-
public readonly recipient: Address,
77+
public readonly recipient: Address | NamespaceId,
7778
/**
7879
* The array of Mosaic objects.
7980
*/
@@ -88,6 +89,21 @@ export class TransferTransaction extends Transaction {
8889
super(TransactionType.TRANSFER, networkType, version, deadline, fee, signature, signer, transactionInfo);
8990
}
9091

92+
/**
93+
* Return the string notation for the set recipient
94+
* @internal
95+
* @returns {string}
96+
*/
97+
public recipientToString(): string {
98+
if (this.recipient instanceof NamespaceId) {
99+
// namespaceId available, return hexadecimal notation
100+
return (this.recipient as NamespaceId).toHex();
101+
}
102+
103+
// address available
104+
return (this.recipient as Address).plain();
105+
}
106+
91107
/**
92108
* @internal
93109
* @returns {VerifiableTransaction}
@@ -97,7 +113,7 @@ export class TransferTransaction extends Transaction {
97113
.addDeadline(this.deadline.toDTO())
98114
.addFee(this.fee.toDTO())
99115
.addVersion(this.versionToDTO())
100-
.addRecipient(this.recipient.plain())
116+
.addRecipient(this.recipientToString())
101117
.addMosaics(this.mosaics.map((mosaic) => mosaic.toDTO()))
102118
.addMessage(this.message)
103119
.build();

test/model/transaction/TransferTransaction.spec.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Account } from '../../../src/model/account/Account';
1919
import { Address } from '../../../src/model/account/Address';
2020
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
2121
import { NetworkCurrencyMosaic } from '../../../src/model/mosaic/NetworkCurrencyMosaic';
22+
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
2223
import { Deadline } from '../../../src/model/transaction/Deadline';
2324
import { PlainMessage } from '../../../src/model/transaction/PlainMessage';
2425
import { TransferTransaction } from '../../../src/model/transaction/TransferTransaction';
@@ -42,7 +43,8 @@ describe('TransferTransaction', () => {
4243

4344
expect(transferTransaction.message.payload).to.be.equal('test-message');
4445
expect(transferTransaction.mosaics.length).to.be.equal(0);
45-
expect(transferTransaction.recipient.plain()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
46+
expect(transferTransaction.recipient).to.be.instanceof(Address);
47+
expect((transferTransaction.recipient as Address).plain()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
4648

4749
const signedTransaction = transferTransaction.signWith(account);
4850

@@ -65,7 +67,8 @@ describe('TransferTransaction', () => {
6567

6668
expect(transferTransaction.message.payload).to.be.equal('test-message');
6769
expect(transferTransaction.mosaics.length).to.be.equal(1);
68-
expect(transferTransaction.recipient.plain()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
70+
expect(transferTransaction.recipient).to.be.instanceof(Address);
71+
expect((transferTransaction.recipient as Address).plain()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
6972

7073
const signedTransaction = transferTransaction.signWith(account);
7174

@@ -76,4 +79,75 @@ describe('TransferTransaction', () => {
7679
'9050B9837EFAB4BBE8A4B9BB32D812F9885C00D8FC1650E1420D000100746573742D6D657373616765' +
7780
'44B262C46CEABB8500E1F50500000000');
7881
});
82+
83+
it('should createComplete an TransferTransaction object with NamespaceId recipient', () => {
84+
const addressAlias = new NamespaceId('nem.owner');
85+
const transferTransaction = TransferTransaction.create(
86+
Deadline.create(),
87+
addressAlias,
88+
[
89+
NetworkCurrencyMosaic.createRelative(100),
90+
],
91+
PlainMessage.create('test-message'),
92+
NetworkType.MIJIN_TEST,
93+
);
94+
95+
expect(transferTransaction.message.payload).to.be.equal('test-message');
96+
expect(transferTransaction.mosaics.length).to.be.equal(1);
97+
expect(transferTransaction.recipient).to.be.instanceof(NamespaceId);
98+
expect(transferTransaction.recipient).to.be.equal(addressAlias);
99+
expect((transferTransaction.recipient as NamespaceId).toHex()).to.be.equal(addressAlias.toHex());
100+
101+
const signedTransaction = transferTransaction.signWith(account);
102+
103+
expect(signedTransaction.payload.substring(
104+
240,
105+
signedTransaction.payload.length,
106+
)).to.be.equal('9151776168D24257D8000000000000000000000000000000000D000100746573742D6D657373616765' +
107+
'44B262C46CEABB8500E1F50500000000');
108+
});
109+
110+
it('should format TransferTransaction payload with 25 bytes binary address', () => {
111+
const transferTransaction = TransferTransaction.create(
112+
Deadline.create(),
113+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
114+
[
115+
NetworkCurrencyMosaic.createRelative(100),
116+
],
117+
PlainMessage.create('test-message'),
118+
NetworkType.MIJIN_TEST,
119+
);
120+
121+
// test recipientToString with Address recipient
122+
expect(transferTransaction.recipientToString()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
123+
124+
const signedTransaction = transferTransaction.signWith(account);
125+
126+
expect(signedTransaction.payload.substring(
127+
240,
128+
290,
129+
)).to.be.equal('9050B9837EFAB4BBE8A4B9BB32D812F9885C00D8FC1650E142');
130+
});
131+
132+
it('should format TransferTransaction payload with 8 bytes binary namespaceId', () => {
133+
const transferTransaction = TransferTransaction.create(
134+
Deadline.create(),
135+
new NamespaceId('nem.owner'),
136+
[
137+
NetworkCurrencyMosaic.createRelative(100),
138+
],
139+
PlainMessage.create('test-message'),
140+
NetworkType.MIJIN_TEST,
141+
);
142+
143+
// test recipientToString with NamespaceId recipient
144+
expect(transferTransaction.recipientToString()).to.be.equal('d85742d268617751');
145+
146+
const signedTransaction = transferTransaction.signWith(account);
147+
148+
expect(signedTransaction.payload.substring(
149+
240,
150+
290,
151+
)).to.be.equal('9151776168D24257D800000000000000000000000000000000');
152+
});
79153
});

0 commit comments

Comments
 (0)