Skip to content

Commit 05260cb

Browse files
committed
JAV-41 [Github #51] Added receipt serializer
1 parent 61b3837 commit 05260cb

14 files changed

+237
-1
lines changed

src/model/namespace/AddressAlias.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { RawAddress } from '../../core/format/RawAddress';
1617
import {Address} from '../account/Address';
1718
import {Alias} from './Alias';
1819
import { AliasType } from './AliasType';
@@ -46,4 +47,12 @@ export class AddressAlias extends Alias {
4647
}
4748
return false;
4849
}
50+
51+
/**
52+
* Generate alias buffer
53+
* @return {Uint8Array}
54+
*/
55+
public serialize(): Uint8Array {
56+
return RawAddress.stringToAddress(this.address.plain());
57+
}
4958
}

src/model/namespace/Alias.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export abstract class Alias {
5353
* @internal
5454
* Compares alias for equality.
5555
* @param alias - MosaicAlias
56+
* @return {boolean}
5657
*/
5758
protected abstract equals(alias: any): boolean;
59+
60+
/**
61+
* @internal
62+
* Generate alias buffer
63+
* @return {Uint8Array}
64+
*/
65+
protected abstract serialize(): Uint8Array;
5866
}

src/model/namespace/EmptyAlias.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ export class EmptyAlias extends Alias {
4545
public equals(alias: any): boolean {
4646
return alias instanceof EmptyAlias || alias.type === 0;
4747
}
48+
49+
/**
50+
* Generate alias buffer
51+
* @return {Uint8Array}
52+
*/
53+
public serialize(): Uint8Array {
54+
return new Uint8Array(0);
55+
}
4856
}

src/model/namespace/MosaicAlias.ts

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

src/model/receipt/ArtifactExpiryReceipt.ts

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

17+
import { Convert } from '../../core/format/Convert';
18+
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
1719
import { MosaicId } from '../mosaic/MosaicId';
1820
import { NamespaceId } from '../namespace/NamespaceId';
1921
import { Receipt } from './Receipt';
@@ -38,4 +40,17 @@ export class ArtifactExpiryReceipt extends Receipt {
3840
size?: number) {
3941
super(version, type, size);
4042
}
43+
44+
/**
45+
* @internal
46+
* Generate buffer
47+
* @return {Uint8Array}
48+
*/
49+
public serialize(): Uint8Array {
50+
const buffer = new Uint8Array(12);
51+
buffer.set(GeneratorUtils.uintToBuffer(ReceiptVersion.ARTIFACT_EXPIRY, 2));
52+
buffer.set(GeneratorUtils.uintToBuffer(this.type, 2), 2);
53+
buffer.set(Convert.hexToUint8(this.artifactId.toHex()), 4);
54+
return buffer;
55+
}
4156
}

src/model/receipt/BalanceChangeReceipt.ts

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

17+
import { Convert } from '../../core/format/Convert';
18+
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
1719
import { PublicAccount } from '../account/PublicAccount';
1820
import { MosaicId } from '../mosaic/MosaicId';
1921
import { UInt64 } from '../UInt64';
@@ -53,4 +55,19 @@ export class BalanceChangeReceipt extends Receipt {
5355
size?: number) {
5456
super(version, type, size);
5557
}
58+
59+
/**
60+
* @internal
61+
* Generate buffer
62+
* @return {Uint8Array}
63+
*/
64+
public serialize(): Uint8Array {
65+
const buffer = new Uint8Array(52);
66+
buffer.set(GeneratorUtils.uintToBuffer(ReceiptVersion.BALANCE_CHANGE, 2));
67+
buffer.set(GeneratorUtils.uintToBuffer(this.type, 2), 2);
68+
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);
71+
return buffer;
72+
}
5673
}

src/model/receipt/BalanceTransferReceipt.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Convert } from '../../core/format/Convert';
18+
import { RawAddress } from '../../core/format/RawAddress';
19+
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
1720
import { Address } from '../account/Address';
1821
import { PublicAccount } from '../account/PublicAccount';
1922
import { MosaicId } from '../mosaic/MosaicId';
@@ -60,4 +63,39 @@ export class BalanceTransferReceipt extends Receipt {
6063
size?: number) {
6164
super(version, type, size);
6265
}
66+
67+
/**
68+
* @internal
69+
* Generate buffer
70+
* @return {Uint8Array}
71+
*/
72+
public serialize(): Uint8Array {
73+
const recipient = this.getRecipientBytes();
74+
const buffer = new Uint8Array(52 + recipient.length);
75+
buffer.set(GeneratorUtils.uintToBuffer(ReceiptVersion.BALANCE_TRANSFER, 2));
76+
buffer.set(GeneratorUtils.uintToBuffer(this.type, 2), 2);
77+
buffer.set(Convert.hexToUint8(this.sender.publicKey), 4);
78+
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);
81+
return buffer;
82+
}
83+
84+
/**
85+
* @internal
86+
* Generate buffer for recipientAddress
87+
* @return {Uint8Array}
88+
*/
89+
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+
}
100+
}
63101
}

src/model/receipt/InflationReceipt.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Convert } from '../../core/format/Convert';
18+
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
1719
import { MosaicId } from '../mosaic/MosaicId';
1820
import { UInt64 } from '../UInt64';
1921
import { Receipt } from './Receipt';
@@ -47,4 +49,18 @@ export class InflationReceipt extends Receipt {
4749
size?: number) {
4850
super(version, type, size);
4951
}
52+
53+
/**
54+
* @internal
55+
* Generate buffer
56+
* @return {Uint8Array}
57+
*/
58+
public serialize(): Uint8Array {
59+
const buffer = new Uint8Array(20);
60+
buffer.set(GeneratorUtils.uintToBuffer(ReceiptVersion.INFLATION_RECEIPT, 2));
61+
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);
64+
return buffer;
65+
}
5066
}

src/model/receipt/Receipt.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,11 @@ export abstract class Receipt {
4242
*/
4343
public readonly size?: number) {
4444
}
45+
46+
/**
47+
* @internal
48+
* Generate buffer
49+
* @return {Uint8Array}
50+
*/
51+
abstract serialize(): Uint8Array;
4552
}

src/model/receipt/ReceiptSource.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Convert } from '../../core/format/Convert';
2+
import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils';
3+
14
/*
25
* Copyright 2019 NEM
36
*
@@ -34,4 +37,14 @@ export class ReceiptSource {
3437
*/
3538
public readonly secondaryId: number) {
3639
}
40+
41+
/**
42+
* @internal
43+
* Generate buffer
44+
* @return {Uint8Array}
45+
*/
46+
public serialize(): Uint8Array {
47+
return GeneratorUtils.concatTypedArrays(
48+
GeneratorUtils.uintToBuffer(this.primaryId, 4), GeneratorUtils.uintToBuffer(this.secondaryId, 4));
49+
}
3750
}

0 commit comments

Comments
 (0)