|
| 1 | +import { convert } from 'nem2-library'; |
| 2 | +import { CreateTransactionFromDTO } from '../../infrastructure/transaction/CreateTransactionFromDTO'; |
| 3 | +import { PublicAccount } from '../../model/account/PublicAccount'; |
| 4 | +import { NetworkType } from '../../model/blockchain/NetworkType'; |
| 5 | +import { AccountPropertyModification } from '../../model/transaction/AccountPropertyModification'; |
| 6 | +import { Deadline } from '../../model/transaction/Deadline'; |
| 7 | +import { ModifyAccountPropertyAddressTransaction } from '../../model/transaction/ModifyAccountPropertyAddressTransaction'; |
| 8 | +import { ModifyAccountPropertyEntityTypeTransaction } from '../../model/transaction/ModifyAccountPropertyEntityTypeTransaction'; |
| 9 | +import { ModifyAccountPropertyMosaicTransaction } from '../../model/transaction/ModifyAccountPropertyMosaicTransaction'; |
| 10 | +import { Transaction } from '../../model/transaction/Transaction'; |
| 11 | +import { TransactionInfo } from '../../model/transaction/TransactionInfo'; |
| 12 | +import { TransactionType } from '../../model/transaction/TransactionType'; |
| 13 | +import { UInt64 } from '../../model/UInt64'; |
| 14 | + |
| 15 | +/* |
| 16 | + * Copyright 2019 NEM |
| 17 | + * |
| 18 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 | + * you may not use this file except in compliance with the License. |
| 20 | + * You may obtain a copy of the License at |
| 21 | + * |
| 22 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | + * |
| 24 | + * Unless required by applicable law or agreed to in writing, software |
| 25 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 26 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | + * See the License for the specific language governing permissions and |
| 28 | + * limitations under the License. |
| 29 | + */ |
| 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 => { |
| 48 | + // Transaction byte size data |
| 49 | + const sizeLength = 8; |
| 50 | + const signatureLength = 128; |
| 51 | + const publicKeyLength = 64; |
| 52 | + const versionLength = 4; |
| 53 | + const typeLength = 4; |
| 54 | + const feeLength = 16; |
| 55 | + const deadlineLength = 16; |
| 56 | + |
| 57 | + // Transaction byte data positions |
| 58 | + const signatureOffset = sizeLength; |
| 59 | + const publicKeyOffset = signatureOffset + signatureLength; |
| 60 | + const versionOffset = publicKeyOffset + publicKeyLength; |
| 61 | + const typeOffset = versionOffset + versionLength; |
| 62 | + const feeOffset = typeOffset + typeLength; |
| 63 | + const deadlineOffset = feeOffset + feeLength; |
| 64 | + const transactionOffset = deadlineOffset + deadlineLength; |
| 65 | + |
| 66 | + // 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); |
| 79 | +}; |
| 80 | + |
| 81 | +const createTransaction = (type: number, transactionData: string, networkType: NetworkType, |
| 82 | + version: number, deadline: number[], fee: UInt64, signature?: string, |
| 83 | + signer?: PublicAccount, transactionInfo?: TransactionInfo): Transaction | undefined => { |
| 84 | + switch (type) { |
| 85 | + case TransactionType.MODIFY_ACCOUNT_PROPERTY_ADDRESS: |
| 86 | + case TransactionType.MODIFY_ACCOUNT_PROPERTY_ENTITY_TYPE: |
| 87 | + case TransactionType.MODIFY_ACCOUNT_PROPERTY_MOSAIC: |
| 88 | + const propertyTypeLength = 2; |
| 89 | + |
| 90 | + const modificationCountOffset = propertyTypeLength; |
| 91 | + const modificationArrayOffset = modificationCountOffset + propertyTypeLength; |
| 92 | + |
| 93 | + // read bytes |
| 94 | + const propertyType = transactionData.substring(0, propertyTypeLength); |
| 95 | + const modifications = transactionData.substring(modificationArrayOffset, transactionData.length); |
| 96 | + const modificationArray = modifications.match(/.{1,52}/g); |
| 97 | + |
| 98 | + switch (type) { |
| 99 | + case TransactionType.MODIFY_ACCOUNT_PROPERTY_ADDRESS: |
| 100 | + return new ModifyAccountPropertyAddressTransaction( |
| 101 | + networkType, |
| 102 | + version, |
| 103 | + Deadline.createFromDTO(deadline), |
| 104 | + fee, |
| 105 | + parseInt(convert.uint8ToHex(convert.hexToUint8(propertyType).reverse()), 16), |
| 106 | + modificationArray ? modificationArray.map((modification) => new AccountPropertyModification( |
| 107 | + parseInt(convert.uint8ToHex(convert.hexToUint8(modification.substring(0, 2)).reverse()), 16), |
| 108 | + modification.substring(2, modification.length), |
| 109 | + )) : [], |
| 110 | + signature, |
| 111 | + signer, |
| 112 | + transactionInfo, |
| 113 | + ); |
| 114 | + case TransactionType.MODIFY_ACCOUNT_PROPERTY_MOSAIC: |
| 115 | + return new ModifyAccountPropertyMosaicTransaction( |
| 116 | + networkType, |
| 117 | + version, |
| 118 | + Deadline.createFromDTO(deadline), |
| 119 | + fee, |
| 120 | + parseInt(convert.uint8ToHex(convert.hexToUint8(propertyType).reverse()), 16), |
| 121 | + modificationArray ? modificationArray.map((modification) => new AccountPropertyModification( |
| 122 | + parseInt(convert.uint8ToHex(convert.hexToUint8(modification.substring(0, 2)).reverse()), 16), |
| 123 | + UInt64.fromHex(modification.substring(2, modification.length)).toDTO(), |
| 124 | + )) : [], |
| 125 | + signature, |
| 126 | + signer, |
| 127 | + transactionInfo, |
| 128 | + ); |
| 129 | + case TransactionType.MODIFY_ACCOUNT_PROPERTY_ENTITY_TYPE: |
| 130 | + return new ModifyAccountPropertyEntityTypeTransaction( |
| 131 | + networkType, |
| 132 | + version, |
| 133 | + Deadline.createFromDTO(deadline), |
| 134 | + fee, |
| 135 | + parseInt(convert.uint8ToHex(convert.hexToUint8(propertyType).reverse()), 16), |
| 136 | + modificationArray ? modificationArray.map((modification) => new AccountPropertyModification( |
| 137 | + parseInt(convert.uint8ToHex(convert.hexToUint8(modification.substring(0, 2)).reverse()), 16), |
| 138 | + parseInt(convert.uint8ToHex(convert.hexToUint8(modification.substring(2, modification.length)).reverse()), 16), |
| 139 | + )) : [], |
| 140 | + signature, |
| 141 | + signer, |
| 142 | + transactionInfo, |
| 143 | + ); |
| 144 | + default: |
| 145 | + break; |
| 146 | + } |
| 147 | + break; |
| 148 | + case TransactionType.MOSAIC_ALIAS: |
| 149 | + |
| 150 | + default: |
| 151 | + throw new Error ('Transaction type not implemented yet.'); |
| 152 | + } |
| 153 | + |
| 154 | +}; |
| 155 | + |
| 156 | +const extractTransactionTypeFromHex = (hexValue: string): number => { |
| 157 | + return parseInt(convert.uint8ToHex(convert.hexToUint8(hexValue).reverse()), 16); |
| 158 | +}; |
| 159 | + |
| 160 | +const extractTransactionVersionFromHex = (hexValue: string): number => { |
| 161 | + return convert.hexToUint8(hexValue)[0]; |
| 162 | +}; |
| 163 | + |
| 164 | +const extractNetwork = (versionHex: string): NetworkType => { |
| 165 | + const networkType = convert.hexToUint8(versionHex)[1]; |
| 166 | + if (networkType === NetworkType.MAIN_NET) { |
| 167 | + return NetworkType.MAIN_NET; |
| 168 | + } else if (networkType === NetworkType.TEST_NET) { |
| 169 | + return NetworkType.TEST_NET; |
| 170 | + } else if (networkType === NetworkType.MIJIN) { |
| 171 | + return NetworkType.MIJIN; |
| 172 | + } else if (networkType === NetworkType.MIJIN_TEST) { |
| 173 | + return NetworkType.MIJIN_TEST; |
| 174 | + } |
| 175 | + throw new Error('Unimplemented network type'); |
| 176 | +}; |
0 commit comments