|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| - |
| 16 | +import {convert} from 'nem2-library'; |
| 17 | +import {uint64 as UInt64Library} from 'nem2-library'; |
17 | 18 | import {Address} from '../../model/account/Address';
|
18 | 19 | import {PublicAccount} from '../../model/account/PublicAccount';
|
19 | 20 | import {NetworkType} from '../../model/blockchain/NetworkType';
|
| 21 | +import {Id} from '../../model/Id'; |
20 | 22 | import {Mosaic} from '../../model/mosaic/Mosaic';
|
21 | 23 | import {MosaicId} from '../../model/mosaic/MosaicId';
|
22 | 24 | import {MosaicProperties} from '../../model/mosaic/MosaicProperties';
|
@@ -121,10 +123,8 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
|
121 | 123 | extractTransactionVersion(transactionDTO.version),
|
122 | 124 | Deadline.createFromDTO(transactionDTO.deadline),
|
123 | 125 | new UInt64(transactionDTO.fee || [0, 0]),
|
124 |
| - Address.createFromEncoded(transactionDTO.recipient), |
125 |
| - transactionDTO.mosaics === undefined ? [] : |
126 |
| - transactionDTO.mosaics |
127 |
| - .map((mosaicDTO) => new Mosaic(new MosaicId(mosaicDTO.id), new UInt64(mosaicDTO.amount))), |
| 126 | + extractRecipient(transactionDTO.recipient), |
| 127 | + extractMosaics(transactionDTO.mosaics), |
128 | 128 | transactionDTO.message !== undefined ?
|
129 | 129 | PlainMessage.createFromDTO(transactionDTO.message.payload) : EmptyMessage,
|
130 | 130 | transactionDTO.signature,
|
@@ -339,3 +339,60 @@ const extractNetworkType = (version: number): NetworkType => {
|
339 | 339 | const extractTransactionVersion = (version: number): number => {
|
340 | 340 | return parseInt(version.toString(16).substr(2, 2), 16);
|
341 | 341 | };
|
| 342 | + |
| 343 | +/** |
| 344 | + * Extract recipient value from encoded hexadecimal notation. |
| 345 | + * |
| 346 | + * If bit 0 of byte 0 is not set (e.g. 0x90), then it is a regular address. |
| 347 | + * Else (e.g. 0x91) it represents a namespace id which starts at byte 1. |
| 348 | + * |
| 349 | + * @param recipient {string} Encoded hexadecimal recipient notation |
| 350 | + * @return {Address | NamespaceId} |
| 351 | + */ |
| 352 | +const extractRecipient = (recipient: string): Address | NamespaceId => { |
| 353 | + // If bit 0 of byte 0 is not set (like in 0x90), then it is a regular address. |
| 354 | + // Else (e.g. 0x91) it represents a namespace id which starts at byte 1. |
| 355 | + const bit0 = convert.hexToUint8(recipient.substr(1, 2))[0]; |
| 356 | + |
| 357 | + if ((bit0 & 16) === 16) { |
| 358 | + // namespaceId encoded hexadecimal notation provided |
| 359 | + // only 8 bytes are relevant to resolve the NamespaceId |
| 360 | + const relevantPart = recipient.substr(2, 16); |
| 361 | + return NamespaceId.createFromEncoded(relevantPart); |
| 362 | + } |
| 363 | + |
| 364 | + // read address from encoded hexadecimal notation |
| 365 | + return Address.createFromEncoded(recipient); |
| 366 | +}; |
| 367 | + |
| 368 | +/** |
| 369 | + * Extract mosaics from encoded UInt64 notation. |
| 370 | + * |
| 371 | + * If most significant bit of byte 0 is set, then it is a namespaceId. |
| 372 | + * If most significant bit of byte 0 is not set, then it is a mosaicId. |
| 373 | + * |
| 374 | + * @param mosaics {Array | undefined} The DTO array of mosaics (with UInt64 Id notation) |
| 375 | + * @return {Mosaic[]} |
| 376 | + */ |
| 377 | +const extractMosaics = (mosaics: any): Mosaic[] => { |
| 378 | + |
| 379 | + if (mosaics === undefined) { |
| 380 | + return []; |
| 381 | + } |
| 382 | + |
| 383 | + return mosaics.map((mosaicDTO) => { |
| 384 | + |
| 385 | + // convert ID to UInt8 bytes array and get first byte (most significant byte) |
| 386 | + const uint64 = new Id(mosaicDTO.id); |
| 387 | + const bytes = convert.hexToUint8(UInt64Library.toHex(uint64.toDTO())); |
| 388 | + const byte0 = bytes[0]; |
| 389 | + |
| 390 | + // if most significant bit of byte 0 is set, then we have a namespaceId |
| 391 | + if ((byte0 & 128) === 128) { |
| 392 | + return new Mosaic(new NamespaceId(mosaicDTO.id), new UInt64(mosaicDTO.amount)); |
| 393 | + } |
| 394 | + |
| 395 | + // most significant bit of byte 0 is not set => mosaicId |
| 396 | + return new Mosaic(new MosaicId(mosaicDTO.id), new UInt64(mosaicDTO.amount)); |
| 397 | + }); |
| 398 | +}; |
0 commit comments