Skip to content

Commit 45c1187

Browse files
authored
Merge branch 'master' into task/g316_expose_internal_classes
2 parents b97d961 + bf02cea commit 45c1187

File tree

9 files changed

+74
-73
lines changed

9 files changed

+74
-73
lines changed

src/core/format/RawAddress.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ export class RawAddress {
4949
/**
5050
* Format a namespaceId *alias* into a valid recipient field value.
5151
* @param {Uint8Array} namespaceId The namespaceId
52+
* @param {networkType} the network type serialized in the output.
5253
* @returns {Uint8Array} The padded notation of the alias
5354
*/
54-
public static aliasToRecipient = (namespaceId: Uint8Array): Uint8Array => {
55+
public static aliasToRecipient = (namespaceId: Uint8Array, networkType: NetworkType): Uint8Array => {
5556
// 0x91 | namespaceId on 8 bytes | 16 bytes 0-pad = 25 bytes
5657
const padded = new Uint8Array(1 + 8 + 16);
57-
padded.set([0x91], 0);
58+
padded.set([networkType.valueOf() | 0x01], 0);
5859
padded.set(namespaceId.reverse(), 1);
5960
padded.set(Convert.hexToUint8('00'.repeat(16)), 9);
6061
return padded;
@@ -130,7 +131,6 @@ export class RawAddress {
130131
if (RawAddress.constants.sizes.addressEncoded !== encoded.length) {
131132
return false;
132133
}
133-
134134
try {
135135
const decoded = RawAddress.stringToAddress(encoded);
136136
return RawAddress.isValidAddress(decoded, networkType);

src/core/utils/UnresolvedMapping.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { MosaicId } from '../../model/mosaic/MosaicId';
1818
import { NamespaceId } from '../../model/namespace/NamespaceId';
1919
import { Convert } from '../format/Convert';
2020
import { RawAddress } from '../format/RawAddress';
21+
import { NetworkType } from "../../model/blockchain/NetworkType";
2122

2223
/**
2324
* @internal
@@ -72,12 +73,13 @@ export class UnresolvedMapping {
7273
* Return unresolved address bytes of the unresolved address
7374
* @internal
7475
* @param {Address | NamespaceId} unresolvedAddress The unresolved address
76+
* @param {networkType} the network type serialized in the output.
7577
* @return {Uint8Array}
7678
*/
77-
public static toUnresolvedAddressBytes(unresolvedAddress: Address | NamespaceId): Uint8Array {
79+
public static toUnresolvedAddressBytes(unresolvedAddress: Address | NamespaceId, networkType: NetworkType): Uint8Array {
7880
if (unresolvedAddress instanceof NamespaceId) {
7981
// received hexadecimal notation of namespaceId (alias)
80-
return RawAddress.aliasToRecipient(Convert.hexToUint8((unresolvedAddress as NamespaceId).toHex()));
82+
return RawAddress.aliasToRecipient(Convert.hexToUint8((unresolvedAddress as NamespaceId).toHex()), networkType);
8183
} else {
8284
// received recipient address
8385
return RawAddress.stringToAddress((unresolvedAddress as Address).plain());

src/model/receipt/BalanceTransferReceipt.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { Convert } from '../../core/format/Convert';
18-
import { RawAddress } from '../../core/format/RawAddress';
18+
import { UnresolvedMapping } from "../../core/utils/UnresolvedMapping";
1919
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
2020
import { Address } from '../account/Address';
2121
import { PublicAccount } from '../account/PublicAccount';
@@ -87,15 +87,6 @@ export class BalanceTransferReceipt extends Receipt {
8787
* @return {Uint8Array}
8888
*/
8989
private getRecipientBytes(): Uint8Array {
90-
const recipientString =
91-
this.recipientAddress instanceof NamespaceId ? (this.recipientAddress as NamespaceId).toHex()
92-
: (this.recipientAddress as Address).plain();
93-
if (/^[0-9a-fA-F]{16}$/.test(recipientString)) {
94-
// received hexadecimal notation of namespaceId (alias)
95-
return RawAddress.aliasToRecipient(Convert.hexToUint8(recipientString));
96-
} else {
97-
// received recipient address
98-
return RawAddress.stringToAddress(recipientString);
99-
}
90+
return UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.sender.address.networkType);
10091
}
10192
}

src/model/receipt/ResolutionStatement.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616

1717
import { sha3_256 } from 'js-sha3';
18-
import { Convert } from '../../core/format/Convert';
19-
import { RawAddress } from '../../core/format/RawAddress';
2018
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
2119
import { Address } from '../account/Address';
2220
import { MosaicId } from '../mosaic/MosaicId';
@@ -26,6 +24,8 @@ import { ReceiptType } from './ReceiptType';
2624
import { ReceiptVersion } from './ReceiptVersion';
2725
import { ResolutionEntry } from './ResolutionEntry';
2826
import { ResolutionType } from './ResolutionType';
27+
import { NetworkType } from "../blockchain/NetworkType";
28+
import { UnresolvedMapping } from "../../core/utils/UnresolvedMapping";
2929

3030
/**
3131
* When a transaction includes an alias, a so called resolution statement reflects the resolved value for that block:
@@ -62,12 +62,13 @@ export class ResolutionStatement {
6262

6363
/**
6464
* Generate receipt hash
65+
* @param {networkType} the network type serialized in the output.
6566
* @return {string} receipt hash in hex
6667
*/
67-
public generateHash(): string {
68+
public generateHash(networkType: NetworkType): string {
6869
const type = this.resolutionType === ResolutionType.Address ? ReceiptType.Address_Alias_Resolution
69-
: ReceiptType.Mosaic_Alias_Resolution;
70-
const unresolvedBytes = this.getUnresolvedBytes(this.resolutionType);
70+
: ReceiptType.Mosaic_Alias_Resolution;
71+
const unresolvedBytes = this.getUnresolvedBytes(this.resolutionType, networkType);
7172
const hasher = sha3_256.create();
7273
hasher.update(GeneratorUtils.uintToBuffer(ReceiptVersion.RESOLUTION_STATEMENT, 2));
7374
hasher.update(GeneratorUtils.uintToBuffer(type, 2));
@@ -86,23 +87,14 @@ export class ResolutionStatement {
8687
/**
8788
* @internal
8889
* Generate buffer for unresulved
89-
* @param - The resolution Type
90+
* @param {resolutionType} The resolution Type
91+
* @param {networkType} the network type serialized in the output.
9092
* @return {Uint8Array}
9193
*/
92-
private getUnresolvedBytes(resolutionType: ResolutionType): Uint8Array {
94+
private getUnresolvedBytes(resolutionType: ResolutionType, networkType: NetworkType): Uint8Array {
9395
if (resolutionType === ResolutionType.Address) {
94-
const recipientString =
95-
this.unresolved instanceof NamespaceId ? (this.unresolved as NamespaceId).toHex()
96-
: (this.unresolved as Address).plain();
97-
if (/^[0-9a-fA-F]{16}$/.test(recipientString)) {
98-
// received hexadecimal notation of namespaceId (alias)
99-
return RawAddress.aliasToRecipient(Convert.hexToUint8(recipientString));
100-
} else {
101-
// received recipient address
102-
return RawAddress.stringToAddress(recipientString);
103-
}
96+
return UnresolvedMapping.toUnresolvedAddressBytes(this.unresolved as Address | NamespaceId, networkType);
10497
}
105-
10698
return GeneratorUtils.uint64ToBuffer(UInt64.fromHex((this.unresolved as MosaicId | NamespaceId).toHex()).toDTO());
10799
}
108100
}

src/model/transaction/MosaicAddressRestrictionTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class MosaicAddressRestrictionTransaction extends Transaction {
207207
new TimestampDto(this.deadline.toDTO()),
208208
new UnresolvedMosaicIdDto(this.mosaicId.id.toDTO()),
209209
this.restrictionKey.toDTO(),
210-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.targetAddress)),
210+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.targetAddress, this.networkType)),
211211
this.previousRestrictionValue.toDTO(),
212212
this.newRestrictionValue.toDTO(),
213213
);
@@ -225,7 +225,7 @@ export class MosaicAddressRestrictionTransaction extends Transaction {
225225
TransactionType.MOSAIC_ADDRESS_RESTRICTION.valueOf(),
226226
new UnresolvedMosaicIdDto(this.mosaicId.id.toDTO()),
227227
this.restrictionKey.toDTO(),
228-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.targetAddress)),
228+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.targetAddress, this.networkType)),
229229
this.previousRestrictionValue.toDTO(),
230230
this.newRestrictionValue.toDTO(),
231231
);

src/model/transaction/TransferTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class TransferTransaction extends Transaction {
240240
TransactionType.TRANSFER.valueOf(),
241241
new AmountDto(this.maxFee.toDTO()),
242242
new TimestampDto(this.deadline.toDTO()),
243-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress)),
243+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
244244
this.getMessageBuffer(),
245245
this.sortMosaics().map((mosaic) => {
246246
return new UnresolvedMosaicBuilder(new UnresolvedMosaicIdDto(mosaic.id.id.toDTO()),
@@ -259,7 +259,7 @@ export class TransferTransaction extends Transaction {
259259
new KeyDto(Convert.hexToUint8(this.signer!.publicKey)),
260260
this.versionToDTO(),
261261
TransactionType.TRANSFER.valueOf(),
262-
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress)),
262+
new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.recipientAddress, this.networkType)),
263263
this.getMessageBuffer(),
264264
this.sortMosaics().map((mosaic) => {
265265
return new UnresolvedMosaicBuilder(new UnresolvedMosaicIdDto(mosaic.id.id.toDTO()),

test/core/utils/UnresolvedMapping.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { UnresolvedMapping } from '../../../src/core/utils/UnresolvedMapping';
1919
import { Address } from '../../../src/model/account/Address';
2020
import { MosaicId } from '../../../src/model/mosaic/MosaicId';
2121
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
22+
import { NetworkType } from "../../../src/model/blockchain/NetworkType";
2223

2324
describe('UnresolvedMapping', () => {
2425
let mosaicId: MosaicId;
@@ -74,16 +75,23 @@ describe('UnresolvedMapping', () => {
7475

7576
describe('toUnresolvedAddressBytes', () => {
7677
it('can map Address to buffer', () => {
77-
const buffer = UnresolvedMapping.toUnresolvedAddressBytes(address);
78+
const buffer = UnresolvedMapping.toUnresolvedAddressBytes(address, NetworkType.MIJIN_TEST);
7879
expect(buffer instanceof Uint8Array).to.be.true;
7980
expect(Convert.uint8ToHex(buffer)).to.be.equal(Convert.uint8ToHex(RawAddress.stringToAddress(address.plain())));
8081
});
8182

82-
it('can map hex string to NamespaceId', () => {
83-
const buffer = UnresolvedMapping.toUnresolvedAddressBytes(namespacId);
83+
it('can map hex string to NamespaceId using MIJIN_TEST', () => {
84+
const buffer = UnresolvedMapping.toUnresolvedAddressBytes(namespacId, NetworkType.MIJIN_TEST);
8485
expect(buffer instanceof Uint8Array).to.be.true;
85-
expect(buffer[0]).to.be.equal(145);
86+
expect(buffer[0]).to.be.equal(NetworkType.MIJIN_TEST | 1);
8687
expect(Convert.uint8ToHex(buffer)).to.be.equal('91C51FB4C93FCA509500000000000000000000000000000000');
8788
});
89+
90+
it('can map hex string to NamespaceId using MAIN_NET', () => {
91+
const buffer = UnresolvedMapping.toUnresolvedAddressBytes(namespacId, NetworkType.MAIN_NET);
92+
expect(buffer instanceof Uint8Array).to.be.true;
93+
expect(buffer[0]).to.be.equal(NetworkType.MAIN_NET | 1);
94+
expect(Convert.uint8ToHex(buffer)).to.be.equal('69C51FB4C93FCA509500000000000000000000000000000000');
95+
});
8896
});
8997
});

test/infrastructure/Listener.spec.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {expect} from 'chai';
18-
import {Listener} from '../../src/infrastructure/Listener';
19-
import {Address} from "../../src/model/account/Address";
20-
import {deepEqual} from "assert";
21-
import {UInt64} from "../../src/model/UInt64";
22-
import {timeout} from "rxjs/operators";
17+
import { expect } from 'chai';
18+
import { Listener } from '../../src/infrastructure/Listener';
19+
import { Address } from "../../src/model/account/Address";
20+
import { deepEqual } from "assert";
21+
import { UInt64 } from "../../src/model/UInt64";
22+
import { timeout } from "rxjs/operators";
23+
import { TransactionStatusError } from "../../src/model/transaction/TransactionStatusError";
2324

2425
describe('Listener', () => {
2526
it('should createComplete a WebSocket instance given url parameter', () => {
@@ -37,7 +38,7 @@ describe('Listener', () => {
3738
});
3839

3940
describe('onStatusWhenAddressIsTheSame', () => {
40-
it('Should forward status', (done) => {
41+
it('Should forward status', () => {
4142

4243

4344
const errorEncodedAddress = '906415867F121D037AF447E711B0F5E4D52EBBF066D96860EB';
@@ -64,24 +65,27 @@ describe('Listener', () => {
6465

6566
listener.open();
6667

67-
listener.status(errorAddress).pipe(timeout(2000)).subscribe((transactionStatusError) => {
68-
expect(transactionStatusError.address).to.deep.equal(errorAddress);
69-
expect(transactionStatusError.hash).to.be.equal(statusInfoErrorDTO.hash);
70-
expect(transactionStatusError.status).to.be.equal(statusInfoErrorDTO.status);
71-
deepEqual(transactionStatusError.deadline.toDTO(), UInt64.fromNumericString(statusInfoErrorDTO.deadline).toDTO());
72-
done();
73-
}, err => {
74-
done('Should have not timed out!');
68+
const reportedStatus = new Array<TransactionStatusError>();
69+
70+
listener.status(errorAddress).subscribe((transactionStatusError) => {
71+
reportedStatus.push(transactionStatusError);
7572
});
7673

7774
listener.handleMessage(statusInfoErrorDTO, null);
7875

76+
expect(reportedStatus.length).to.be.equal(1);
77+
const transactionStatusError = reportedStatus[0];
78+
expect(transactionStatusError.address).to.deep.equal(errorAddress);
79+
expect(transactionStatusError.hash).to.be.equal(statusInfoErrorDTO.hash);
80+
expect(transactionStatusError.status).to.be.equal(statusInfoErrorDTO.status);
81+
deepEqual(transactionStatusError.deadline.toDTO(), UInt64.fromNumericString(statusInfoErrorDTO.deadline).toDTO());
82+
7983

8084
});
8185
});
8286

8387
describe('onStatusWhenAddressIsDifferentAddress', () => {
84-
it('Should not forward status', (done) => {
88+
it('Should not forward status', () => {
8589

8690

8791
const errorEncodedAddress = '906415867F121D037AF447E711B0F5E4D52EBBF066D96860EB';
@@ -110,14 +114,15 @@ describe('Listener', () => {
110114

111115
listener.open();
112116

113-
listener.status(subscribedAddress).pipe(timeout(100)).subscribe(status => {
114-
done('Should have timed out!');
115-
}, err => {
116-
expect(err.name).to.be.eq('TimeoutError');
117-
done();
117+
const reportedStatus = new Array<TransactionStatusError>();
118+
119+
listener.status(subscribedAddress).subscribe((transactionStatusError) => {
120+
reportedStatus.push(transactionStatusError);
118121
});
119122

120123
listener.handleMessage(statusInfoErrorDTO, null);
124+
125+
expect(reportedStatus.length).to.be.equal(0);
121126

122127

123128
});
@@ -127,12 +132,12 @@ describe('Listener', () => {
127132
it('should reject because of wrong server url', async () => {
128133
const listener = new Listener('https://notcorrecturl:0000');
129134
await listener.open()
130-
.then((result) => {
131-
throw new Error('This should not be called when expecting error');
132-
})
133-
.catch((error) => {
134-
expect(error.message.toString()).not.to.be.equal('');
135-
});
135+
.then((result) => {
136+
throw new Error('This should not be called when expecting error');
137+
})
138+
.catch((error) => {
139+
expect(error.message.toString()).not.to.be.equal('');
140+
});
136141
});
137142
});
138143
});

test/model/receipt/Receipt.spec.ts

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

1717
import { deepEqual } from 'assert';
1818
import { expect } from 'chai';
19-
import { CreateReceiptFromDTO, CreateStatementFromDTO } from '../../../src/infrastructure/receipt/CreateReceiptFromDTO';
20-
import {Account} from '../../../src/model/account/Account';
19+
import {
20+
CreateReceiptFromDTO,
21+
CreateStatementFromDTO
22+
} from '../../../src/infrastructure/receipt/CreateReceiptFromDTO';
23+
import { Account } from '../../../src/model/account/Account';
2124
import { Address } from '../../../src/model/account/Address';
2225
import { PublicAccount } from '../../../src/model/account/PublicAccount';
2326
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
@@ -343,14 +346,14 @@ describe('Receipt', () => {
343346
it('should generate hash for MosaicResolutionStatement', () => {
344347
const statement = CreateStatementFromDTO(statementDTO, netWorkType);
345348
const receipt = statement.mosaicResolutionStatements[0];
346-
const hash = receipt.generateHash();
349+
const hash = receipt.generateHash(NetworkType.MAIN_NET);
347350
expect(hash).to.be.equal('99381CE398D3AAE110FC97E984D7D35A710A5C525A4F959EC8916B382DE78A63');
348351
});
349352

350353
it('should generate hash for AddressResolutionStatement', () => {
351354
const statement = CreateStatementFromDTO(statementDTO, netWorkType);
352355
const receipt = statement.addressResolutionStatements[0];
353-
const hash = receipt.generateHash();
356+
const hash = receipt.generateHash(NetworkType.MAIN_NET);
354357
expect(hash).to.be.equal('6967470641BC527768CDC29998F4A3350813FDF2E40D1C97AB0BBA36B9AF649E');
355358
});
356359

0 commit comments

Comments
 (0)