Skip to content

Commit 0e316e4

Browse files
committed
JAV-41 #51 Receipt merkle proof
1 parent d6e3263 commit 0e316e4

File tree

11 files changed

+74
-51
lines changed

11 files changed

+74
-51
lines changed

src/infrastructure/receipt/CreateReceiptFromDTO.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,21 @@ const createResolutionStatement = (statementDTO, resolutionType): ResolutionStat
9393
switch (resolutionType) {
9494
case ResolutionType.Address:
9595
return new ResolutionStatement(
96+
ResolutionType.Address,
9697
UInt64.fromNumericString(statementDTO.height),
9798
Address.createFromEncoded(statementDTO.unresolved),
9899
statementDTO.resolutionEntries.map((entry) => {
99-
return new ResolutionEntry(new AddressAlias(Address.createFromEncoded(entry.resolved)),
100+
return new ResolutionEntry(Address.createFromEncoded(entry.resolved),
100101
new ReceiptSource(entry.source.primaryId, entry.source.secondaryId));
101102
}),
102103
);
103104
case ResolutionType.Mosaic:
104105
return new ResolutionStatement(
106+
ResolutionType.Mosaic,
105107
UInt64.fromNumericString(statementDTO.height),
106108
new MosaicId(statementDTO.unresolved),
107109
statementDTO.resolutionEntries.map((entry) => {
108-
return new ResolutionEntry(new MosaicAlias(new MosaicId(entry.resolved)),
110+
return new ResolutionEntry(new MosaicId(entry.resolved),
109111
new ReceiptSource(entry.source.primaryId, entry.source.secondaryId));
110112
}),
111113
);

src/model/namespace/Alias.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,4 @@ export abstract class Alias {
5656
* @return {boolean}
5757
*/
5858
protected abstract equals(alias: any): boolean;
59-
60-
/**
61-
* @internal
62-
* Generate alias buffer
63-
* @return {Uint8Array}
64-
*/
65-
protected abstract serialize(): Uint8Array;
6659
}

src/model/namespace/MosaicAlias.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { Convert } from '../../core/format/Convert';
1716
import {MosaicId} from '../mosaic/MosaicId';
1817
import {Alias} from './Alias';
1918
import { AliasType } from './AliasType';
@@ -55,12 +54,4 @@ export class MosaicAlias extends Alias {
5554
public toHex(): string {
5655
return this.mosaicId.toHex();
5756
}
58-
59-
/**
60-
* Generate alias buffer
61-
* @return {Uint8Array}
62-
*/
63-
public serialize(): Uint8Array {
64-
return Convert.hexToUint8(this.mosaicId.toHex());
65-
}
6657
}

src/model/receipt/ArtifactExpiryReceipt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Convert } from '../../core/format/Convert';
1818
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
1919
import { MosaicId } from '../mosaic/MosaicId';
2020
import { NamespaceId } from '../namespace/NamespaceId';
21+
import { UInt64 } from '../UInt64';
2122
import { Receipt } from './Receipt';
2223
import { ReceiptType } from './ReceiptType';
2324
import { ReceiptVersion } from './ReceiptVersion';
@@ -50,7 +51,7 @@ export class ArtifactExpiryReceipt extends Receipt {
5051
const buffer = new Uint8Array(12);
5152
buffer.set(GeneratorUtils.uintToBuffer(ReceiptVersion.ARTIFACT_EXPIRY, 2));
5253
buffer.set(GeneratorUtils.uintToBuffer(this.type, 2), 2);
53-
buffer.set(Convert.hexToUint8(this.artifactId.toHex()), 4);
54+
buffer.set(GeneratorUtils.uint64ToBuffer(UInt64.fromHex(this.artifactId.toHex()).toDTO()), 4);
5455
return buffer;
5556
}
5657
}

src/model/receipt/BalanceChangeReceipt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export class BalanceChangeReceipt extends Receipt {
6666
buffer.set(GeneratorUtils.uintToBuffer(ReceiptVersion.BALANCE_CHANGE, 2));
6767
buffer.set(GeneratorUtils.uintToBuffer(this.type, 2), 2);
6868
buffer.set(Convert.hexToUint8(this.targetPublicAccount.publicKey), 4);
69-
buffer.set(Convert.hexToUint8(this.mosaicId.toHex()), 36);
70-
buffer.set(Convert.hexToUint8(this.amount.toHex()), 44);
69+
buffer.set(GeneratorUtils.uint64ToBuffer(UInt64.fromHex(this.mosaicId.toHex()).toDTO()), 36);
70+
buffer.set(GeneratorUtils.uint64ToBuffer(UInt64.fromHex(this.amount.toHex()).toDTO()), 44);
7171
return buffer;
7272
}
7373
}

src/model/receipt/BalanceTransferReceipt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ export class BalanceTransferReceipt extends Receipt {
7676
buffer.set(GeneratorUtils.uintToBuffer(this.type, 2), 2);
7777
buffer.set(Convert.hexToUint8(this.sender.publicKey), 4);
7878
buffer.set(recipient, 36);
79-
buffer.set(Convert.hexToUint8(this.mosaicId.toHex()), 36 + recipient.length);
80-
buffer.set(Convert.hexToUint8(this.amount.toHex()), 44 + recipient.length);
79+
buffer.set(GeneratorUtils.uint64ToBuffer(UInt64.fromHex(this.mosaicId.toHex()).toDTO()), 36 + recipient.length);
80+
buffer.set(GeneratorUtils.uint64ToBuffer(UInt64.fromHex(this.amount.toHex()).toDTO()), 44 + recipient.length);
8181
return buffer;
8282
}
8383

src/model/receipt/InflationReceipt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export class InflationReceipt extends Receipt {
5959
const buffer = new Uint8Array(20);
6060
buffer.set(GeneratorUtils.uintToBuffer(ReceiptVersion.INFLATION_RECEIPT, 2));
6161
buffer.set(GeneratorUtils.uintToBuffer(this.type, 2), 2);
62-
buffer.set(Convert.hexToUint8(this.mosaicId.toHex()), 4);
63-
buffer.set(Convert.hexToUint8(this.amount.toHex()), 12);
62+
buffer.set(GeneratorUtils.uint64ToBuffer(UInt64.fromHex(this.mosaicId.toHex()).toDTO()), 4);
63+
buffer.set(GeneratorUtils.uint64ToBuffer(UInt64.fromHex(this.amount.toHex()).toDTO()), 12);
6464
return buffer;
6565
}
6666
}

src/model/receipt/ResolutionEntry.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
18-
import { AddressAlias } from '../namespace/AddressAlias';
19-
import { MosaicAlias } from '../namespace/MosaicAlias';
20-
import { ReceiptSource } from './ReceiptSource';
17+
import { RawAddress } from '../../core/format/RawAddress';
18+
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
19+
import { Address } from '../account/Address';
20+
import { MosaicId } from '../mosaic/MosaicId';
21+
import { UInt64 } from '../UInt64';
22+
import { ReceiptSource } from './ReceiptSource';
2123

2224
/**
2325
* The receipt source object.
2426
*/
25-
export class ResolutionEntry {
27+
export class ResolutionEntry {
2628

2729
/**
2830
* @constructor
@@ -33,7 +35,7 @@
3335
/**
3436
* A resolved address or resolved mosaicId (alias).
3537
*/
36-
public readonly resolved: AddressAlias | MosaicAlias,
38+
public readonly resolved: Address | MosaicId,
3739
/**
3840
* The receipt source.
3941
*/
@@ -46,7 +48,12 @@
4648
* @return {Uint8Array}
4749
*/
4850
public serialize(): Uint8Array {
49-
const resolvedBytes = this.resolved.serialize();
51+
let resolvedBytes: Uint8Array;
52+
if (this.resolved instanceof Address) {
53+
resolvedBytes = RawAddress.stringToAddress((this.resolved as Address).plain());
54+
} else {
55+
resolvedBytes = GeneratorUtils.uint64ToBuffer(UInt64.fromHex((this.resolved as MosaicId).toHex()).toDTO());
56+
}
5057
const sourceBytes = this.source.serialize();
5158
return GeneratorUtils.concatTypedArrays(resolvedBytes, sourceBytes);
5259
}

src/model/receipt/ResolutionStatement.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import { RawAddress } from '../../core/format/RawAddress';
2020
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
2121
import { Address } from '../account/Address';
2222
import { MosaicId } from '../mosaic/MosaicId';
23+
import { NamespaceId } from '../namespace/NamespaceId';
2324
import { UInt64 } from '../UInt64';
2425
import { ReceiptType } from './ReceiptType';
2526
import { ReceiptVersion } from './ReceiptVersion';
2627
import { ResolutionEntry } from './ResolutionEntry';
28+
import { ResolutionType } from './ResolutionType';
2729

2830
/**
2931
* When a transaction includes an alias, a so called resolution statement reflects the resolved value for that block:
@@ -34,19 +36,24 @@ export class ResolutionStatement {
3436

3537
/**
3638
* Receipt - resolution statement object
39+
* @param resolutionType - The resolution type
3740
* @param height - The block height
3841
* @param unresolved - An unresolved address or unresolved mosaicId.
3942
* @param resolutionEntries - The array of resolution entries.
4043
*/
4144
constructor(
45+
/**
46+
* Resolution type
47+
*/
48+
public readonly resolutionType: ResolutionType,
4249
/**
4350
* The block height.
4451
*/
4552
public readonly height: UInt64,
4653
/**
4754
* An unresolved address or unresolved mosaicId.
4855
*/
49-
public readonly unresolved: Address | MosaicId,
56+
public readonly unresolved: Address | MosaicId | NamespaceId,
5057
/**
5158
* The array of resolution entries.
5259
*/
@@ -58,10 +65,9 @@ export class ResolutionStatement {
5865
* @return {string} receipt hash in hex
5966
*/
6067
public generateHash(): string {
61-
const type = this.unresolved instanceof Address ? ReceiptType.Address_Alias_Resolution
62-
: ReceiptType.Mosaic_Alias_Resolution;
63-
const unresolvedBytes = this.unresolved instanceof Address ? RawAddress.stringToAddress(this.unresolved.plain())
64-
: Convert.hexToUint8(this.unresolved.toHex());
68+
const type = this.resolutionType === ResolutionType.Address ? ReceiptType.Address_Alias_Resolution
69+
: ReceiptType.Mosaic_Alias_Resolution;
70+
const unresolvedBytes = this.getUnresolvedBytes(this.resolutionType);
6571
const hasher = sha3_256.create();
6672
hasher.update(GeneratorUtils.uintToBuffer(ReceiptVersion.RESOLUTION_STATEMENT, 2));
6773
hasher.update(GeneratorUtils.uintToBuffer(type, 2));
@@ -76,4 +82,27 @@ export class ResolutionStatement {
7682
hasher.update(entryBytes);
7783
return hasher.hex().toUpperCase();
7884
}
85+
86+
/**
87+
* @internal
88+
* Generate buffer for unresulved
89+
* @param - The resolution Type
90+
* @return {Uint8Array}
91+
*/
92+
private getUnresolvedBytes(resolutionType: ResolutionType): Uint8Array {
93+
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+
}
104+
}
105+
106+
return GeneratorUtils.uint64ToBuffer(UInt64.fromHex((this.unresolved as MosaicId | NamespaceId).toHex()).toDTO());
107+
}
79108
}

test/infrastructure/receipt/CreateReceiptFromDTO.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ describe('Receipt - CreateStatementFromDTO', () => {
139139
deepEqual(unresolvedAddress.plain(),
140140
Address.createFromEncoded('9103B60AAF2762688300000000000000000000000000000000').plain());
141141
expect(statement.addressResolutionStatements[0].resolutionEntries.length).to.be.equal(1);
142-
expect((statement.addressResolutionStatements[0].resolutionEntries[0].resolved as AddressAlias)
143-
.address.plain()).to.be.equal(Address.createFromEncoded('917E7E29A01014C2F300000000000000000000000000000000').plain());
142+
expect((statement.addressResolutionStatements[0].resolutionEntries[0].resolved as Address).plain())
143+
.to.be.equal(Address.createFromEncoded('917E7E29A01014C2F300000000000000000000000000000000').plain());
144144

145145
deepEqual(statement.mosaicResolutionStatements[0].height, UInt64.fromNumericString('1506'));
146146
deepEqual(unresolvedMosaicId.toHex(), '85BBEA6CC462B244');
147147
expect(statement.mosaicResolutionStatements[0].resolutionEntries.length).to.be.equal(1);
148-
deepEqual((statement.mosaicResolutionStatements[0].resolutionEntries[0].resolved as MosaicAlias)
149-
.mosaicId.id.toHex(), '941299B2B7E1291C');
148+
deepEqual((statement.mosaicResolutionStatements[0].resolutionEntries[0].resolved as MosaicId)
149+
.toHex(), '941299B2B7E1291C');
150150
});
151151
});

0 commit comments

Comments
 (0)