1515 */
1616
1717import { convert } from 'nem2-library' ;
18- import { Address } from '../model/account/Address ' ;
19- import { PublicAccount } from '../model/account/PublicAccount ' ;
20- import { NetworkType } from '../model/blockchain/NetworkType ' ;
21- import { Mosaic } from '../model/mosaic/Mosaic ' ;
22- import { MosaicId } from '../model/mosaic/MosaicId ' ;
23- import { MosaicNonce } from '../model/mosaic/MosaicNonce ' ;
24- import { MosaicProperties } from '../model/mosaic/MosaicProperties ' ;
25- import { NamespaceId } from '../model/namespace/NamespaceId ' ;
26- import { AccountPropertyModification } from '../model/transaction/AccountPropertyModification ' ;
27- import { AddressAliasTransaction } from '../model/transaction/AddressAliasTransaction ' ;
28- import { AggregateTransaction } from '../model/transaction/AggregateTransaction ' ;
29- import { AggregateTransactionCosignature } from '../model/transaction/AggregateTransactionCosignature ' ;
30- import { Deadline } from '../model/transaction/Deadline ' ;
31- import { HashLockTransaction } from '../model/transaction/HashLockTransaction ' ;
32- import { HashType } from '../model/transaction/HashType ' ;
33- import { Message } from '../model/transaction/Message ' ;
34- import { ModifyAccountPropertyAddressTransaction } from '../model/transaction/ModifyAccountPropertyAddressTransaction ' ;
35- import { ModifyAccountPropertyEntityTypeTransaction } from '../model/transaction/ModifyAccountPropertyEntityTypeTransaction ' ;
36- import { ModifyAccountPropertyMosaicTransaction } from '../model/transaction/ModifyAccountPropertyMosaicTransaction ' ;
37- import { ModifyMultisigAccountTransaction } from '../model/transaction/ModifyMultisigAccountTransaction ' ;
38- import { MosaicAliasTransaction } from '../model/transaction/MosaicAliasTransaction ' ;
39- import { MosaicDefinitionTransaction } from '../model/transaction/MosaicDefinitionTransaction ' ;
40- import { MosaicSupplyChangeTransaction } from '../model/transaction/MosaicSupplyChangeTransaction ' ;
41- import { MultisigCosignatoryModification } from '../model/transaction/MultisigCosignatoryModification ' ;
42- import { PlainMessage } from '../model/transaction/PlainMessage ' ;
43- import { RegisterNamespaceTransaction } from '../model/transaction/RegisterNamespaceTransaction ' ;
44- import { SecretLockTransaction } from '../model/transaction/SecretLockTransaction ' ;
45- import { SecretProofTransaction } from '../model/transaction/SecretProofTransaction ' ;
46- import { Transaction } from '../model/transaction/Transaction ' ;
47- import { TransactionInfo } from '../model/transaction/TransactionInfo ' ;
48- import { TransactionType } from '../model/transaction/TransactionType ' ;
49- import { TransferTransaction } from '../model/transaction/TransferTransaction ' ;
50- import { UInt64 } from '../model/UInt64 ' ;
51- import { LockFundsTransaction } from '../model/transaction/LockFundsTransaction ' ;
52- import { SignedTransaction } from '../model/transaction/SignedTransaction ' ;
18+ import { decode } from 'utf8 ' ;
19+ import { Address } from '../../ model/account/Address ' ;
20+ import { PublicAccount } from '../../ model/account/PublicAccount ' ;
21+ import { NetworkType } from '../../ model/blockchain/NetworkType ' ;
22+ import { NamespaceType } from '../../ model/model ' ;
23+ import { Mosaic } from '../../ model/mosaic/Mosaic ' ;
24+ import { MosaicId } from '../../ model/mosaic/MosaicId ' ;
25+ import { MosaicNonce } from '../../ model/mosaic/MosaicNonce ' ;
26+ import { MosaicProperties } from '../../ model/mosaic/MosaicProperties ' ;
27+ import { NamespaceId } from '../../ model/namespace/NamespaceId ' ;
28+ import { AccountPropertyModification } from '../../ model/transaction/AccountPropertyModification ' ;
29+ import { AddressAliasTransaction } from '../../ model/transaction/AddressAliasTransaction ' ;
30+ import { AggregateTransaction } from '../../ model/transaction/AggregateTransaction ' ;
31+ import { AggregateTransactionCosignature } from '../../ model/transaction/AggregateTransactionCosignature ' ;
32+ import { Deadline } from '../../ model/transaction/Deadline ' ;
33+ import { HashType } from '../../ model/transaction/HashType ' ;
34+ import { LockFundsTransaction } from '../../ model/transaction/LockFundsTransaction ' ;
35+ import { Message } from '../../ model/transaction/Message ' ;
36+ import { ModifyAccountPropertyAddressTransaction } from '../../ model/transaction/ModifyAccountPropertyAddressTransaction ' ;
37+ import { ModifyAccountPropertyEntityTypeTransaction } from '../../ model/transaction/ModifyAccountPropertyEntityTypeTransaction ' ;
38+ import { ModifyAccountPropertyMosaicTransaction } from '../../ model/transaction/ModifyAccountPropertyMosaicTransaction ' ;
39+ import { ModifyMultisigAccountTransaction } from '../../ model/transaction/ModifyMultisigAccountTransaction ' ;
40+ import { MosaicAliasTransaction } from '../../ model/transaction/MosaicAliasTransaction ' ;
41+ import { MosaicDefinitionTransaction } from '../../ model/transaction/MosaicDefinitionTransaction ' ;
42+ import { MosaicSupplyChangeTransaction } from '../../ model/transaction/MosaicSupplyChangeTransaction ' ;
43+ import { MultisigCosignatoryModification } from '../../ model/transaction/MultisigCosignatoryModification ' ;
44+ import { PlainMessage } from '../../ model/transaction/PlainMessage ' ;
45+ import { RegisterNamespaceTransaction } from '../../ model/transaction/RegisterNamespaceTransaction ' ;
46+ import { SecretLockTransaction } from '../../ model/transaction/SecretLockTransaction ' ;
47+ import { SecretProofTransaction } from '../../ model/transaction/SecretProofTransaction ' ;
48+ import { SignedTransaction } from '../../ model/transaction/SignedTransaction ' ;
49+ import { Transaction } from '../../ model/transaction/Transaction ' ;
50+ import { TransactionType } from '../../ model/transaction/TransactionType ' ;
51+ import { TransferTransaction } from '../../ model/transaction/TransferTransaction ' ;
52+ import { UInt64 } from '../../ model/UInt64 ' ;
5353
5454/**
5555 * @internal
56- * @param transactionBinary
56+ * @param transactionBinary - The transaction binary data
5757 * @returns {Transaction }
5858 * @constructor
5959 */
60- export const CreateTransactionFromBinary = ( transactionBinary : string ) : Transaction => {
60+ export const CreateTransactionFromPayload = ( transactionBinary : string ) : Transaction => {
6161 // Transaction byte size data
6262 const sizeLength = 8 ;
6363 const signatureLength = 128 ;
@@ -87,14 +87,10 @@ export const CreateTransactionFromBinary = (transactionBinary: string): Transact
8787
8888/**
8989 * @internal
90- * @param type
91- * @param transactionData
92- * @param networkType
93- * @param version
94- * @param deadline
95- * @param signature
96- * @param signer
97- * @param transactionInfo
90+ * @param type - Transaction type
91+ * @param transactionData - Details per specific transaction type
92+ * @param networkType - Network type
93+ * @param deadline - Deadline
9894 * @returns {Transaction }
9995 */
10096const CreateTransaction = ( type : number , transactionData : string , networkType : NetworkType , deadline : number [ ] ) : Transaction => {
@@ -148,11 +144,9 @@ const CreateTransaction = (type: number, transactionData: string, networkType: N
148144 }
149145 throw new Error ( 'Account property transaction type not recognised.' ) ;
150146 case TransactionType . ADDRESS_ALIAS :
151- const addressAliasActionLength = 2 ;
152-
153147 // read bytes
154- const addressAliasAction = transactionData . substring ( 0 , addressAliasActionLength ) ;
155- const addressAliasNamespaceId = transactionData . substring ( addressAliasActionLength , 18 ) ;
148+ const addressAliasAction = transactionData . substring ( 0 , 2 ) ;
149+ const addressAliasNamespaceId = transactionData . substring ( 2 , 18 ) ;
156150 const addressAliasAddress = transactionData . substring ( 18 ) ;
157151
158152 return AddressAliasTransaction . create (
@@ -177,29 +171,26 @@ const CreateTransaction = (type: number, transactionData: string, networkType: N
177171 new MosaicId ( UInt64 . fromHex ( reverse ( mosaicAliasMosaicId ) ) . toDTO ( ) ) ,
178172 networkType ,
179173 ) ;
180- // case TransactionType.REGISTER_NAMESPACE:
181- // const registerNameSpaceTypeLength = 2;
182-
183- // // read bytes
184- // const namespaceType = transactionData.substring(0, registerNameSpaceTypeLength);
185- // const duration = transactionData.substring(registerNameSpaceTypeLength, 18);
186- // const ParentId = transactionData.substring(18, 34);
187- // const NameSpaceId = transactionData.substring(34, 50);
188- // const NameSize = transactionData.substring(50, 52);
189- // const name = transactionData.substring(52);
190-
191- // return new RegisterNamespaceTransaction(
192- // networkType,
193- // version,
194- // Deadline.createFromDTO(deadline),
195- // fee,
196- // parseInt(convert.uint8ToHex(convert.hexToUint8(namespaceType).reverse()), 16),
197- // new NamespaceId(UInt64.fromHex(reverse(mosaicAliasNamespaceId)).toDTO()),
198- // new MosaicId(UInt64.fromHex(reverse(mosaicAliasMosaicId)).toDTO()),
199- // signature,
200- // signer,
201- // transactionInfo,
202- // );
174+ case TransactionType . REGISTER_NAMESPACE :
175+ // read bytes
176+ const namespaceType = parseInt ( convert . uint8ToHex ( convert . hexToUint8 ( transactionData . substring ( 0 , 2 ) ) . reverse ( ) ) , 16 ) ;
177+ const nameSpaceDurationParentId = transactionData . substring ( 2 , 18 ) ;
178+ const nameSpaceId = transactionData . substring ( 18 , 34 ) ;
179+ const nameSize = transactionData . substring ( 34 , 36 ) ;
180+ const nameSpaceName = transactionData . substring ( 36 ) ;
181+
182+ return namespaceType === NamespaceType . RootNamespace ?
183+ RegisterNamespaceTransaction . createRootNamespace (
184+ Deadline . createFromDTO ( deadline ) ,
185+ decodeHex ( nameSpaceName ) ,
186+ UInt64 . fromHex ( reverse ( nameSpaceDurationParentId ) ) ,
187+ networkType ,
188+ ) : RegisterNamespaceTransaction . createSubNamespace (
189+ Deadline . createFromDTO ( deadline ) ,
190+ decodeHex ( nameSpaceName ) ,
191+ new NamespaceId ( UInt64 . fromHex ( reverse ( nameSpaceDurationParentId ) ) . toDTO ( ) ) ,
192+ networkType ,
193+ ) ;
203194 case TransactionType . MOSAIC_DEFINITION :
204195 const mosaicDefMosaicNonceLength = 8 ;
205196 const mosaicDefMosaicIdLength = 16 ;
@@ -408,7 +399,7 @@ const CreateTransaction = (type: number, transactionData: string, networkType: N
408399
409400/**
410401 * @internal
411- * @param hexValue
402+ * @param hexValue - Transaction type in hex
412403 * @returns {number }
413404 */
414405const extractTransactionTypeFromHex = ( hexValue : string ) : number => {
@@ -417,7 +408,7 @@ const extractTransactionTypeFromHex = (hexValue: string): number => {
417408
418409/**
419410 * @internal
420- * @param versionHex
411+ * @param versionHex - Transaction version in hex
421412 * @returns {NetworkType }
422413 */
423414const extractNetwork = ( versionHex : string ) : NetworkType => {
@@ -445,7 +436,7 @@ const reverse = (hex: string): string => {
445436
446437/**
447438 * @internal
448- * @param innerTransactionBinary
439+ * @param innerTransactionBinary - Inner transaction binary data
449440 * @returns {Array }
450441 */
451442const parseInnerTransactionFromBinary = ( innerTransactionBinary : string ) : string [ ] => {
@@ -460,3 +451,20 @@ const parseInnerTransactionFromBinary = (innerTransactionBinary: string): string
460451 }
461452 return embeddedTransaction ;
462453} ;
454+
455+ /**
456+ * @internal
457+ * @param hex - Hex input
458+ * @returns {string }
459+ */
460+ const decodeHex = ( hex : string ) : string => {
461+ let str = '' ;
462+ for ( let i = 0 ; i < hex . length ; i += 2 ) {
463+ str += String . fromCharCode ( parseInt ( hex . substr ( i , 2 ) , 16 ) ) ;
464+ }
465+ try {
466+ return decode ( str ) ;
467+ } catch ( e ) {
468+ return str ;
469+ }
470+ } ;
0 commit comments