1+
12/*
23 * Copyright 2019 NEM
34 *
1516 */
1617
1718import { convert } from 'nem2-library' ;
18- import { CreateTransactionFromDTO } from '../../infrastructure/transaction/CreateTransactionFromDTO' ;
19- import { PublicAccount } from '../../model/account/PublicAccount' ;
20- import { NetworkType } from '../../model/blockchain/NetworkType' ;
21- import { AccountPropertyModification } from '../../model/transaction/AccountPropertyModification' ;
22- import { Deadline } from '../../model/transaction/Deadline' ;
23- import { ModifyAccountPropertyAddressTransaction } from '../../model/transaction/ModifyAccountPropertyAddressTransaction' ;
24- import { ModifyAccountPropertyEntityTypeTransaction } from '../../model/transaction/ModifyAccountPropertyEntityTypeTransaction' ;
25- import { ModifyAccountPropertyMosaicTransaction } from '../../model/transaction/ModifyAccountPropertyMosaicTransaction' ;
26- import { Transaction } from '../../model/transaction/Transaction' ;
27- import { TransactionInfo } from '../../model/transaction/TransactionInfo' ;
28- import { TransactionType } from '../../model/transaction/TransactionType' ;
29- import { UInt64 } from '../../model/UInt64' ;
30-
31- export const createFromJson = ( dataJson : object ) : Transaction => {
32- return CreateTransactionFromDTO ( dataJson ) ;
33- } ;
34-
35- export const createFromBinary = ( dataString : string ) : Transaction => {
36- throw new Error ( ) ;
37- } ;
38-
39- export const serialize = ( ) : string => {
40- throw new Error ( ) ;
41- } ;
42-
43- export const serializeJson = ( ) : object => {
44- throw new Error ( ) ;
45- } ;
46-
47- const readTransactionHexadecimal = ( bytes : string ) : any => {
19+ import { PublicAccount } from '../model/account/PublicAccount' ;
20+ import { NetworkType } from '../model/blockchain/NetworkType' ;
21+ import { AccountPropertyModification } from '../model/transaction/AccountPropertyModification' ;
22+ import { Deadline } from '../model/transaction/Deadline' ;
23+ import { ModifyAccountPropertyAddressTransaction } from '../model/transaction/ModifyAccountPropertyAddressTransaction' ;
24+ import { ModifyAccountPropertyEntityTypeTransaction } from '../model/transaction/ModifyAccountPropertyEntityTypeTransaction' ;
25+ import { ModifyAccountPropertyMosaicTransaction } from '../model/transaction/ModifyAccountPropertyMosaicTransaction' ;
26+ import { Transaction } from '../model/transaction/Transaction' ;
27+ import { TransactionInfo } from '../model/transaction/TransactionInfo' ;
28+ import { TransactionType } from '../model/transaction/TransactionType' ;
29+ import { UInt64 } from '../model/UInt64' ;
30+
31+ /**
32+ * @internal
33+ * @param transactionBinary
34+ * @returns {Transaction }
35+ * @constructor
36+ */
37+ export const CreateTransactionFromBinary = ( transactionBinary ) : Transaction => {
4838 // Transaction byte size data
4939 const sizeLength = 8 ;
5040 const signatureLength = 128 ;
@@ -64,30 +54,28 @@ const readTransactionHexadecimal = (bytes: string): any => {
6454 const transactionOffset = deadlineOffset + deadlineLength ;
6555
6656 // Transaction byte data
67- const networkType = extractNetwork ( bytes . substring ( versionOffset , typeOffset ) ) ;
68- const sizeBytes = bytes . substring ( 0 , sizeLength ) ;
69- const signatureBytes = bytes . substring ( signatureOffset , publicKeyOffset ) ;
70- const signer = PublicAccount . createFromPublicKey ( bytes . substring ( publicKeyOffset , versionOffset ) , networkType ) ;
71- const version = extractTransactionVersionFromHex ( bytes . substring ( versionOffset , typeOffset ) ) ;
72- const type = extractTransactionTypeFromHex ( bytes . substring ( typeOffset , feeOffset ) ) ;
73- const fee = UInt64 . fromHex ( bytes . substring ( feeOffset , deadlineOffset ) ) ;
74- const deadline = UInt64 . fromHex ( bytes . substring ( deadlineOffset , transactionOffset ) ) . toDTO ( ) ;
75- const transactionBytes = bytes . substring ( transactionOffset ) ;
76- const transactionData = this . readTransactionBytes ( type , transactionBytes ) ;
77-
78- return createTransaction ( type , transactionData , networkType , version , deadline , fee , signatureBytes , signer ) ;
57+ const networkType = extractNetwork ( transactionBinary . substring ( versionOffset , typeOffset ) ) ;
58+ const signatureBytes = reverse ( transactionBinary . substring ( signatureOffset , publicKeyOffset ) ) ;
59+ const signer = PublicAccount . createFromPublicKey ( reverse ( transactionBinary . substring ( publicKeyOffset , versionOffset ) ) , networkType ) ;
60+ const version = extractTransactionVersionFromHex ( transactionBinary . substring ( versionOffset , typeOffset ) ) ;
61+ const type = extractTransactionTypeFromHex ( transactionBinary . substring ( typeOffset , feeOffset ) ) ;
62+ const fee = UInt64 . fromHex ( reverse ( transactionBinary . substring ( feeOffset , deadlineOffset ) ) ) ;
63+ const deadline = UInt64 . fromHex ( reverse ( transactionBinary . substring ( deadlineOffset , transactionOffset ) ) ) . toDTO ( ) ;
64+ const transactionData = transactionBinary . substring ( transactionOffset ) ;
65+
66+ return CreateTransaction ( type , transactionData , networkType , version , deadline , fee , signatureBytes , signer ) ;
7967} ;
8068
81- const createTransaction = ( type : number , transactionData : string , networkType : NetworkType ,
69+ const CreateTransaction = ( type : number , transactionData : string , networkType : NetworkType ,
8270 version : number , deadline : number [ ] , fee : UInt64 , signature ?: string ,
83- signer ?: PublicAccount , transactionInfo ?: TransactionInfo ) : Transaction | undefined => {
71+ signer ?: PublicAccount , transactionInfo ?: TransactionInfo ) : Transaction => {
8472 switch ( type ) {
8573 case TransactionType . MODIFY_ACCOUNT_PROPERTY_ADDRESS :
8674 case TransactionType . MODIFY_ACCOUNT_PROPERTY_ENTITY_TYPE :
8775 case TransactionType . MODIFY_ACCOUNT_PROPERTY_MOSAIC :
8876 const propertyTypeLength = 2 ;
8977
90- const modificationCountOffset = propertyTypeLength ;
78+ const modificationCountOffset = propertyTypeLength ;
9179 const modificationArrayOffset = modificationCountOffset + propertyTypeLength ;
9280
9381 // read bytes
@@ -97,20 +85,21 @@ const createTransaction = (type: number, transactionData: string, networkType: N
9785
9886 switch ( type ) {
9987 case TransactionType . MODIFY_ACCOUNT_PROPERTY_ADDRESS :
100- return new ModifyAccountPropertyAddressTransaction (
88+ const t = new ModifyAccountPropertyAddressTransaction (
10189 networkType ,
10290 version ,
10391 Deadline . createFromDTO ( deadline ) ,
10492 fee ,
10593 parseInt ( convert . uint8ToHex ( convert . hexToUint8 ( propertyType ) . reverse ( ) ) , 16 ) ,
10694 modificationArray ? modificationArray . map ( ( modification ) => new AccountPropertyModification (
10795 parseInt ( convert . uint8ToHex ( convert . hexToUint8 ( modification . substring ( 0 , 2 ) ) . reverse ( ) ) , 16 ) ,
108- modification . substring ( 2 , modification . length ) ,
96+ reverse ( modification . substring ( 2 , modification . length ) ) ,
10997 ) ) : [ ] ,
11098 signature ,
11199 signer ,
112100 transactionInfo ,
113101 ) ;
102+ return t ;
114103 case TransactionType . MODIFY_ACCOUNT_PROPERTY_MOSAIC :
115104 return new ModifyAccountPropertyMosaicTransaction (
116105 networkType ,
@@ -135,22 +124,20 @@ const createTransaction = (type: number, transactionData: string, networkType: N
135124 parseInt ( convert . uint8ToHex ( convert . hexToUint8 ( propertyType ) . reverse ( ) ) , 16 ) ,
136125 modificationArray ? modificationArray . map ( ( modification ) => new AccountPropertyModification (
137126 parseInt ( convert . uint8ToHex ( convert . hexToUint8 ( modification . substring ( 0 , 2 ) ) . reverse ( ) ) , 16 ) ,
138- parseInt ( convert . uint8ToHex ( convert . hexToUint8 ( modification . substring ( 2 , modification . length ) ) . reverse ( ) ) , 16 ) ,
127+ parseInt ( convert . uint8ToHex ( convert . hexToUint8 (
128+ modification . substring ( 2 , modification . length ) ) . reverse ( ) ) , 16 ) ,
139129 ) ) : [ ] ,
140130 signature ,
141131 signer ,
142132 transactionInfo ,
143133 ) ;
144- default :
145- break ;
146134 }
147- break ;
135+ throw new Error ( 'Account property transaction type not recognised.' ) ;
148136 case TransactionType . MOSAIC_ALIAS :
149-
137+ throw new Error ( 'Transaction type not implemented yet.' ) ;
150138 default :
151139 throw new Error ( 'Transaction type not implemented yet.' ) ;
152140 }
153-
154141} ;
155142
156143const extractTransactionTypeFromHex = ( hexValue : string ) : number => {
@@ -174,3 +161,7 @@ const extractNetwork = (versionHex: string): NetworkType => {
174161 }
175162 throw new Error ( 'Unimplemented network type' ) ;
176163} ;
164+
165+ const reverse = ( hex : string ) : string => {
166+ return convert . uint8ToHex ( convert . hexToUint8 ( hex ) . reverse ( ) ) ;
167+ } ;
0 commit comments