|
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'; |
@@ -122,9 +124,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr |
122 | 124 | Deadline.createFromDTO(transactionDTO.deadline), |
123 | 125 | new UInt64(transactionDTO.fee || [0, 0]), |
124 | 126 | extractRecipient(transactionDTO.recipient), |
125 | | - transactionDTO.mosaics === undefined ? [] : |
126 | | - transactionDTO.mosaics |
127 | | - .map((mosaicDTO) => new Mosaic(new MosaicId(mosaicDTO.id), new UInt64(mosaicDTO.amount))), |
| 127 | + extractMosaics(transactionDTO.mosaics), |
128 | 128 | transactionDTO.message !== undefined ? |
129 | 129 | PlainMessage.createFromDTO(transactionDTO.message.payload) : EmptyMessage, |
130 | 130 | transactionDTO.signature, |
@@ -355,3 +355,26 @@ const extractRecipient = (recipient: string): Address | NamespaceId => { |
355 | 355 | // read address from encoded hexadecimal notation |
356 | 356 | return Address.createFromEncoded(recipient); |
357 | 357 | }; |
| 358 | + |
| 359 | +const extractMosaics = (mosaics: any): Mosaic[] => { |
| 360 | + |
| 361 | + if (mosaics === undefined) { |
| 362 | + return []; |
| 363 | + } |
| 364 | + |
| 365 | + return mosaics.map((mosaicDTO) => { |
| 366 | + |
| 367 | + // convert ID to UInt8 bytes array and get first byte (most significant byte) |
| 368 | + const uint64 = new Id(mosaicDTO.id); |
| 369 | + const bytes = convert.hexToUint8(UInt64Library.toHex(uint64.toDTO())); |
| 370 | + const byte0 = bytes[0]; |
| 371 | + |
| 372 | + // if most significant bit of byte 0 is set, then we have a namespaceId |
| 373 | + if ((byte0 & 128) === 128) { |
| 374 | + return new Mosaic(new NamespaceId(mosaicDTO.id), new UInt64(mosaicDTO.amount)); |
| 375 | + } |
| 376 | + |
| 377 | + // most significant bit of byte 0 is not set => mosaicId |
| 378 | + return new Mosaic(new MosaicId(mosaicDTO.id), new UInt64(mosaicDTO.amount)); |
| 379 | + }); |
| 380 | +}; |
0 commit comments