Skip to content

Commit 6eb054e

Browse files
author
Greg S
committed
issue #89: differenciate mosaicId from NamespaceId with most significant bit
1 parent c3f2056 commit 6eb054e

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
16+
import {convert} from 'nem2-library';
17+
import {uint64 as UInt64Library} from 'nem2-library';
1718
import {Address} from '../../model/account/Address';
1819
import {PublicAccount} from '../../model/account/PublicAccount';
1920
import {NetworkType} from '../../model/blockchain/NetworkType';
21+
import {Id} from '../../model/Id';
2022
import {Mosaic} from '../../model/mosaic/Mosaic';
2123
import {MosaicId} from '../../model/mosaic/MosaicId';
2224
import {MosaicProperties} from '../../model/mosaic/MosaicProperties';
@@ -122,9 +124,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
122124
Deadline.createFromDTO(transactionDTO.deadline),
123125
new UInt64(transactionDTO.fee || [0, 0]),
124126
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),
128128
transactionDTO.message !== undefined ?
129129
PlainMessage.createFromDTO(transactionDTO.message.payload) : EmptyMessage,
130130
transactionDTO.signature,
@@ -355,3 +355,26 @@ const extractRecipient = (recipient: string): Address | NamespaceId => {
355355
// read address from encoded hexadecimal notation
356356
return Address.createFromEncoded(recipient);
357357
};
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

Comments
 (0)