Skip to content

Commit c3f2056

Browse files
author
Greg S
committed
issue #89: use auxiliary class for recipient extraction
1 parent 7c7a78c commit c3f2056

File tree

5 files changed

+22
-247
lines changed

5 files changed

+22
-247
lines changed

src/infrastructure/transaction/CreateTransactionFromDTO.ts

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

1717
import {Address} from '../../model/account/Address';
1818
import {PublicAccount} from '../../model/account/PublicAccount';
19-
import {Recipient} from '../../model/account/Recipient';
2019
import {NetworkType} from '../../model/blockchain/NetworkType';
2120
import {Mosaic} from '../../model/mosaic/Mosaic';
2221
import {MosaicId} from '../../model/mosaic/MosaicId';
@@ -117,13 +116,12 @@ export const CreateTransactionFromDTO = (transactionDTO): Transaction => {
117116
*/
118117
const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Transaction => {
119118
if (transactionDTO.type === TransactionType.TRANSFER) {
120-
const recipient = Recipient.createFromEncoded(transactionDTO.recipient);
121119
return new TransferTransaction(
122120
extractNetworkType(transactionDTO.version),
123121
extractTransactionVersion(transactionDTO.version),
124122
Deadline.createFromDTO(transactionDTO.deadline),
125123
new UInt64(transactionDTO.fee || [0, 0]),
126-
recipient.value,
124+
extractRecipient(transactionDTO.recipient),
127125
transactionDTO.mosaics === undefined ? [] :
128126
transactionDTO.mosaics
129127
.map((mosaicDTO) => new Mosaic(new MosaicId(mosaicDTO.id), new UInt64(mosaicDTO.amount))),
@@ -341,3 +339,19 @@ const extractNetworkType = (version: number): NetworkType => {
341339
const extractTransactionVersion = (version: number): number => {
342340
return parseInt(version.toString(16).substr(2, 2), 16);
343341
};
342+
343+
const extractRecipient = (recipient: string): Address | NamespaceId => {
344+
// If bit 0 of byte 0 is not set (like in 0x90), then it is a regular address.
345+
// Else (e.g. 0x91) it represents a namespace id which starts at byte 1.
346+
const fstByteBit0 = recipient.substr(1, 1);
347+
348+
if (parseInt(fstByteBit0, 2) === 1) {
349+
// namespaceId encoded hexadecimal notation provided
350+
// only 8 bytes are relevant to resolve the NamespaceId
351+
const relevantPart = recipient.substr(2, 16);
352+
return NamespaceId.createFromEncoded(relevantPart);
353+
}
354+
355+
// read address from encoded hexadecimal notation
356+
return Address.createFromEncoded(recipient);
357+
};

src/model/account/Recipient.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/model/transaction/TransferTransaction.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import { TransferTransaction as TransferTransactionLibrary, VerifiableTransaction } from 'nem2-library';
1818
import { Address } from '../account/Address';
1919
import { PublicAccount } from '../account/PublicAccount';
20-
import { Recipient } from '../account/Recipient';
2120
import { NetworkType } from '../blockchain/NetworkType';
2221
import { Mosaic } from '../mosaic/Mosaic';
2322
import { NamespaceId } from '../namespace/NamespaceId';
@@ -43,7 +42,7 @@ export class TransferTransaction extends Transaction {
4342
* @returns {TransferTransaction}
4443
*/
4544
public static create(deadline: Deadline,
46-
recipient: Recipient | Address | NamespaceId,
45+
recipient: Address | NamespaceId,
4746
mosaics: Mosaic[],
4847
message: Message,
4948
networkType: NetworkType): TransferTransaction {
@@ -75,7 +74,7 @@ export class TransferTransaction extends Transaction {
7574
/**
7675
* The address of the recipient.
7776
*/
78-
public readonly recipient: Recipient | Address | NamespaceId,
77+
public readonly recipient: Address | NamespaceId,
7978
/**
8079
* The array of Mosaic objects.
8180
*/
@@ -97,19 +96,13 @@ export class TransferTransaction extends Transaction {
9796
*/
9897
public recipientToString(): string {
9998

100-
// handle `Recipient` wrapper class
101-
let recipient = this.recipient;
102-
if (recipient instanceof Recipient) {
103-
recipient = (this.recipient as Recipient).value;
104-
}
105-
106-
if (recipient instanceof NamespaceId) {
99+
if (this.recipient instanceof NamespaceId) {
107100
// namespaceId recipient, return hexadecimal notation
108-
return (recipient as NamespaceId).toHex();
101+
return (this.recipient as NamespaceId).toHex();
109102
}
110103

111104
// address recipient
112-
return (recipient as Address).plain();
105+
return (this.recipient as Address).plain();
113106
}
114107

115108
/**

test/model/account/Recipient.spec.ts

Lines changed: 0 additions & 102 deletions
This file was deleted.

test/model/transaction/TransferTransaction.spec.ts

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import { expect } from 'chai';
1818
import { Account } from '../../../src/model/account/Account';
1919
import { Address } from '../../../src/model/account/Address';
20-
import { Recipient } from '../../../src/model/account/Recipient';
2120
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
2221
import { NetworkCurrencyMosaic } from '../../../src/model/mosaic/NetworkCurrencyMosaic';
2322
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
@@ -108,63 +107,6 @@ describe('TransferTransaction', () => {
108107
'44B262C46CEABB8500E1F50500000000');
109108
});
110109

111-
it('should createComplete an TransferTransaction object with Recipient given address recipient', () => {
112-
const addressRecipient = new Recipient(Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'));
113-
const transferTransaction = TransferTransaction.create(
114-
Deadline.create(),
115-
addressRecipient,
116-
[
117-
NetworkCurrencyMosaic.createRelative(100),
118-
],
119-
PlainMessage.create('test-message'),
120-
NetworkType.MIJIN_TEST,
121-
);
122-
123-
expect(transferTransaction.message.payload).to.be.equal('test-message');
124-
expect(transferTransaction.mosaics.length).to.be.equal(1);
125-
expect(transferTransaction.recipient).to.be.instanceof(Recipient);
126-
127-
const address = ((transferTransaction.recipient as Recipient).value as Address);
128-
expect(address.plain()).to.be.equal('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC');
129-
130-
const signedTransaction = transferTransaction.signWith(account);
131-
132-
expect(signedTransaction.payload.substring(
133-
240,
134-
signedTransaction.payload.length,
135-
)).to.be.equal('9050B9837EFAB4BBE8A4B9BB32D812F9885C00D8FC1650E1420D000100746573742D6D657373616765' +
136-
'44B262C46CEABB8500E1F50500000000');
137-
});
138-
139-
it('should createComplete an TransferTransaction object with Recipient given namespaceId recipient', () => {
140-
const namespaceId = new NamespaceId('nem.owner');
141-
const namespaceRecipient = new Recipient(namespaceId);
142-
const transferTransaction = TransferTransaction.create(
143-
Deadline.create(),
144-
namespaceRecipient,
145-
[
146-
NetworkCurrencyMosaic.createRelative(100),
147-
],
148-
PlainMessage.create('test-message'),
149-
NetworkType.MIJIN_TEST,
150-
);
151-
152-
expect(transferTransaction.message.payload).to.be.equal('test-message');
153-
expect(transferTransaction.mosaics.length).to.be.equal(1);
154-
expect(transferTransaction.recipient).to.be.instanceof(Recipient);
155-
156-
const actualNamespaceId = ((transferTransaction.recipient as Recipient).value as NamespaceId);
157-
expect(actualNamespaceId.toHex()).to.be.equal(namespaceId.toHex());
158-
159-
const signedTransaction = transferTransaction.signWith(account);
160-
161-
expect(signedTransaction.payload.substring(
162-
240,
163-
signedTransaction.payload.length,
164-
)).to.be.equal('9151776168D24257D8000000000000000000000000000000000D000100746573742D6D657373616765' +
165-
'44B262C46CEABB8500E1F50500000000');
166-
});
167-
168110
it('should format TransferTransaction payload with 25 bytes binary address', () => {
169111
const transferTransaction = TransferTransaction.create(
170112
Deadline.create(),

0 commit comments

Comments
 (0)