|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { GeneratorUtils } from 'catbuffer'; |
| 17 | +import { AddressDto, |
| 18 | + AddressResolutionEntryBuilder, |
| 19 | + AddressResolutionStatementBuilder, |
| 20 | + MosaicIdDto, MosaicResolutionEntryBuilder, |
| 21 | + MosaicResolutionStatementBuilder, |
| 22 | + ReceiptSourceBuilder, |
| 23 | + UnresolvedAddressDto, |
| 24 | + UnresolvedMosaicIdDto } from 'catbuffer'; |
18 | 25 | import { sha3_256 } from 'js-sha3'; |
| 26 | +import { RawAddress } from '../../core/format/RawAddress'; |
19 | 27 | import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping'; |
20 | 28 | import { Address } from '../account/Address'; |
21 | 29 | import { NetworkType } from '../blockchain/NetworkType'; |
@@ -68,19 +76,23 @@ export class ResolutionStatement { |
68 | 76 | public generateHash(networkType: NetworkType): string { |
69 | 77 | const type = this.resolutionType === ResolutionType.Address ? ReceiptType.Address_Alias_Resolution |
70 | 78 | : ReceiptType.Mosaic_Alias_Resolution; |
71 | | - const unresolvedBytes = this.getUnresolvedBytes(this.resolutionType, networkType); |
| 79 | + const builder = this.resolutionType === ResolutionType.Address ? new AddressResolutionStatementBuilder( |
| 80 | + ReceiptVersion.RESOLUTION_STATEMENT, type.valueOf(), |
| 81 | + new UnresolvedAddressDto(UnresolvedMapping.toUnresolvedAddressBytes(this.unresolved as Address | NamespaceId, networkType)), |
| 82 | + this.resolutionEntries.map((entry) => new AddressResolutionEntryBuilder( |
| 83 | + new ReceiptSourceBuilder(entry.source.primaryId, entry.source.secondaryId), |
| 84 | + new AddressDto(RawAddress.stringToAddress((entry.resolved as Address).plain())), |
| 85 | + )), |
| 86 | + ) : new MosaicResolutionStatementBuilder(ReceiptVersion.RESOLUTION_STATEMENT, |
| 87 | + type.valueOf(), |
| 88 | + new UnresolvedMosaicIdDto(UInt64.fromHex((this.unresolved as MosaicId | NamespaceId).toHex()).toDTO()), |
| 89 | + this.resolutionEntries.map((entry) => new MosaicResolutionEntryBuilder( |
| 90 | + new ReceiptSourceBuilder(entry.source.primaryId, entry.source.secondaryId), |
| 91 | + new MosaicIdDto((entry.resolved as MosaicId).toDTO()), |
| 92 | + )), |
| 93 | + ); |
72 | 94 | const hasher = sha3_256.create(); |
73 | | - hasher.update(GeneratorUtils.uintToBuffer(ReceiptVersion.RESOLUTION_STATEMENT, 2)); |
74 | | - hasher.update(GeneratorUtils.uintToBuffer(type, 2)); |
75 | | - hasher.update(unresolvedBytes); |
76 | | - |
77 | | - let entryBytes = Uint8Array.from([]); |
78 | | - this.resolutionEntries.forEach((entry) => { |
79 | | - const bytes = entry.serialize(); |
80 | | - entryBytes = GeneratorUtils.concatTypedArrays(entryBytes, bytes); |
81 | | - }); |
82 | | - |
83 | | - hasher.update(entryBytes); |
| 95 | + hasher.update(builder.serialize()); |
84 | 96 | return hasher.hex().toUpperCase(); |
85 | 97 | } |
86 | 98 |
|
@@ -195,18 +207,4 @@ export class ResolutionStatement { |
195 | 207 | return Math.max(...this.resolutionEntries |
196 | 208 | .map((entry) => primaryId >= entry.source.primaryId ? entry.source.primaryId : 0)); |
197 | 209 | } |
198 | | - |
199 | | - /** |
200 | | - * @internal |
201 | | - * Generate buffer for unresulved |
202 | | - * @param {resolutionType} The resolution Type |
203 | | - * @param {networkType} the network type serialized in the output. |
204 | | - * @return {Uint8Array} |
205 | | - */ |
206 | | - private getUnresolvedBytes(resolutionType: ResolutionType, networkType: NetworkType): Uint8Array { |
207 | | - if (resolutionType === ResolutionType.Address) { |
208 | | - return UnresolvedMapping.toUnresolvedAddressBytes(this.unresolved as Address | NamespaceId, networkType); |
209 | | - } |
210 | | - return GeneratorUtils.uint64ToBuffer(UInt64.fromHex((this.unresolved as MosaicId | NamespaceId).toHex()).toDTO()); |
211 | | - } |
212 | 210 | } |
0 commit comments