|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
| 17 | +import { Convert } from '../../core/format/Convert'; |
| 18 | +import { RawAddress } from '../../core/format/RawAddress'; |
| 19 | +import { GeneratorUtils } from '../../infrastructure/catbuffer/GeneratorUtils'; |
17 | 20 | import { Address } from '../account/Address'; |
18 | 21 | import { PublicAccount } from '../account/PublicAccount'; |
19 | 22 | import { MosaicId } from '../mosaic/MosaicId'; |
@@ -60,4 +63,39 @@ export class BalanceTransferReceipt extends Receipt { |
60 | 63 | size?: number) { |
61 | 64 | super(version, type, size); |
62 | 65 | } |
| 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(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); |
| 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 | + } |
63 | 101 | } |
0 commit comments