|
| 1 | +import { Buffer } from "../../src" |
| 2 | +import { Tx } from "../../src/apis/evm" |
| 3 | +import { Serialization } from "../../src/utils" |
| 4 | +import { SerializedType } from "../../src/utils" |
| 5 | +import * as bech32 from "bech32" |
| 6 | + |
| 7 | +const cb58: SerializedType = "cb58" |
| 8 | +const serialization: Serialization = Serialization.getInstance() |
| 9 | + |
| 10 | +const getTxData = (item: string) => { |
| 11 | + const txSplit = item.split("0x000000000001") |
| 12 | + const prefix = "0x0000" |
| 13 | + const txData = prefix + txSplit[1] |
| 14 | + return txData |
| 15 | +} |
| 16 | + |
| 17 | +const fromDecToHex = (item: number) => { |
| 18 | + let hexVal = item.toString(16) |
| 19 | + let hexString = hexVal.length < 2 ? "0" + hexVal : hexVal |
| 20 | + return hexString |
| 21 | +} |
| 22 | +const fromHexToDec = (item: string) => { |
| 23 | + let hexString = item.split("0x").join("") |
| 24 | + let decNumber = parseInt(hexString, 16) |
| 25 | + let value = decNumber / 10 ** 9 |
| 26 | + return value |
| 27 | +} |
| 28 | +const toHexThenDec = (item: number) => { |
| 29 | + let toHex = fromDecToHex(item).split(",").join("") |
| 30 | + let hexString = toHex.split("0x").join("") |
| 31 | + let decNumber = parseInt(hexString, 16) |
| 32 | + return decNumber |
| 33 | +} |
| 34 | +const bufToHex = (item: string) => { |
| 35 | + let valueFromJSON = item |
| 36 | + let bufValueFromJson = Buffer.from(valueFromJSON) |
| 37 | + let arrValueFromJSON = [...bufValueFromJson] |
| 38 | + let hexValueFromJSON = arrValueFromJSON.map((item) => fromDecToHex(item)) |
| 39 | + return "0x" + hexValueFromJSON.toString().split(",").join("") |
| 40 | +} |
| 41 | +const bech32Encoder = (item: string) => { |
| 42 | + const hrp = "avax" |
| 43 | + let valueFromJSON = item |
| 44 | + let bufValueFromJson = Buffer.from(valueFromJSON) |
| 45 | + let arrValueFromJSON = [...bufValueFromJson] |
| 46 | + let bech32Address = bech32.bech32.encode( |
| 47 | + hrp, |
| 48 | + bech32.bech32.toWords(arrValueFromJSON) |
| 49 | + ) |
| 50 | + return "C-" + bech32Address |
| 51 | +} |
| 52 | +const base58Encoder = (item: string) => { |
| 53 | + let valToBeEncoded = Buffer.from(item) |
| 54 | + let base58Val: string = serialization.bufferToType(valToBeEncoded, cb58) |
| 55 | + return base58Val |
| 56 | +} |
| 57 | +const chainName = (item: string) => { |
| 58 | + const chainID = base58Encoder(item) |
| 59 | + let name: string = "null" |
| 60 | + const cchainID = "2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5" |
| 61 | + const pchainID = "11111111111111111111111111111111LpoYY" |
| 62 | + chainID == "11111111111111111111111111111111LpoYY" |
| 63 | + ? (name = "P-Chain") |
| 64 | + : (name = "X-Chain") |
| 65 | + chainID == cchainID |
| 66 | + ? (name = "C-Chain") |
| 67 | + : chainID == pchainID |
| 68 | + ? name == "P-Chain" |
| 69 | + : (name = "X-Chain") |
| 70 | + return name |
| 71 | +} |
| 72 | + |
| 73 | +const main = async (): Promise<any> => { |
| 74 | + const blockExtraData: string = |
| 75 | + "0x00000000000100000001000000010427d4b22a2a78bcddd456742caf91b56badbff985ee19aef14573e7343fd652000000000000000000000000000000000000000000000000000000000000000000000001bb900bbe1a20da4d474666b79a5fa6ce1262973300000000009dba8421e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000000000000370000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000070000000000989680000000000000000000000001000000015feaa6c211cc8376e16211a76eff1e88bad8079d000000010000000900000001f526c9a38a2da08291583bf86e5160bd8b49df585b3fc2fb57884390c673f748428c58e95c6514b9d6a27d273550c63070ab64d257798e8d07f8a208489ebb2100" |
| 76 | + const txData = getTxData(blockExtraData) |
| 77 | + const buf: Buffer = new Buffer(txData.slice(2), "hex") |
| 78 | + const tx: Tx = new Tx() |
| 79 | + tx.fromBuffer(buf) |
| 80 | + const txString: string = JSON.stringify(tx) |
| 81 | + const txToObject = JSON.parse(txString) |
| 82 | + |
| 83 | + let displayExportTx = () => { |
| 84 | + //exportTx |
| 85 | + let exportedTxInputs = txToObject.unsignedTx.transaction.inputs.map( |
| 86 | + (input) => ({ |
| 87 | + Address: bufToHex(input.address.data), |
| 88 | + Amount: bufToHex(input.amount.data), |
| 89 | + AmountValue: "0x" + input.amountValue, |
| 90 | + DecimalAmountValue: fromHexToDec(input.amountValue) + " AVAX", |
| 91 | + AssetID: base58Encoder(input.assetID.data), |
| 92 | + Nonce: bufToHex(input.nonce.data), |
| 93 | + NonceValue: input.nonceValue, |
| 94 | + SignaturesCount: toHexThenDec(input.sigCount.data), |
| 95 | + SignaturesIDs: input.sigIdxs |
| 96 | + }) |
| 97 | + ) |
| 98 | + let exportedTxExpOutputs = |
| 99 | + txToObject.unsignedTx.transaction.exportedOutputs.map((out) => ({ |
| 100 | + Type: out._typeName, |
| 101 | + AssetID: base58Encoder(out.assetID.data), |
| 102 | + Output: { |
| 103 | + Type: out.output._typeName, |
| 104 | + TypeID: out.output._typeID, |
| 105 | + Locktime: toHexThenDec(out.output.locktime.data), |
| 106 | + Threshold: toHexThenDec(out.output.threshold.data), |
| 107 | + NumberOfAddresses: toHexThenDec(out.output.numaddrs.data), |
| 108 | + Addresses: out.output.addresses.map((address) => ({ |
| 109 | + Type: address._typeName, |
| 110 | + Bytes: bufToHex(address.bytes.data), |
| 111 | + BytesSize: address.bsize, |
| 112 | + Bech32Format: bech32Encoder(address.bytes.data) |
| 113 | + })), |
| 114 | + Amount: bufToHex(out.output.amount), |
| 115 | + AmountValue: "0x" + out.output.amountValue, |
| 116 | + DecimalAmountValue: fromHexToDec(out.output.amountValue) + " AVAX" |
| 117 | + } |
| 118 | + })) |
| 119 | + let exportedTxCredentials = txToObject.credentials.map((credential) => ({ |
| 120 | + Type: credential._typeName, |
| 121 | + TypeID: credential._typeID, |
| 122 | + Signatures: credential.sigArray.map((signature) => ({ |
| 123 | + Type: signature._typeName, |
| 124 | + Bytes: bufToHex(signature.bytes.data), |
| 125 | + BytesSize: signature.bsize |
| 126 | + })) |
| 127 | + })) |
| 128 | + let exportTx = { |
| 129 | + Type: txToObject._typeName, |
| 130 | + UnsignedTx: { |
| 131 | + Type: txToObject.unsignedTx._typeName, |
| 132 | + CodecID: txToObject.unsignedTx.codecID, |
| 133 | + Transaction: { |
| 134 | + Type: txToObject.unsignedTx.transaction._typeName, |
| 135 | + TypeID: txToObject.unsignedTx.transaction._typeID, |
| 136 | + NetworkID: toHexThenDec( |
| 137 | + txToObject.unsignedTx.transaction.networkID.data |
| 138 | + ), |
| 139 | + BlockchainID: base58Encoder( |
| 140 | + txToObject.unsignedTx.transaction.blockchainID.data |
| 141 | + ), |
| 142 | + BlockchainIDName: chainName( |
| 143 | + txToObject.unsignedTx.transaction.blockchainID.data |
| 144 | + ), |
| 145 | + DestinationChain: base58Encoder( |
| 146 | + txToObject.unsignedTx.transaction.destinationChain.data |
| 147 | + ), |
| 148 | + DestinationChainName: chainName( |
| 149 | + txToObject.unsignedTx.transaction.destinationChain.data |
| 150 | + ), |
| 151 | + NumberOfInputs: toHexThenDec( |
| 152 | + txToObject.unsignedTx.transaction.numInputs.data |
| 153 | + ), |
| 154 | + Inputs: exportedTxInputs, |
| 155 | + NumberOfExportedOutputs: toHexThenDec( |
| 156 | + txToObject.unsignedTx.transaction.numExportedOutputs.data |
| 157 | + ), |
| 158 | + ExportedOutputs: exportedTxExpOutputs |
| 159 | + } |
| 160 | + }, |
| 161 | + Credentials: exportedTxCredentials |
| 162 | + } |
| 163 | + console.log(require("util").inspect(exportTx, true, 10)) |
| 164 | + } |
| 165 | + |
| 166 | + let displayImportTx = () => { |
| 167 | + //importTX |
| 168 | + let importedTxImpInputs = txToObject.unsignedTx.transaction.importIns.map( |
| 169 | + (inp) => ({ |
| 170 | + Type: inp._typeName, |
| 171 | + TransactionId: base58Encoder(inp.txid.data), |
| 172 | + OutputId: toHexThenDec(inp.outputidx.data), |
| 173 | + AssetID: base58Encoder(inp.assetID.data), |
| 174 | + Input: { |
| 175 | + Type: inp.input._typeName, |
| 176 | + TypeID: inp.input._typeID, |
| 177 | + SignaturesIds: inp.input.sigIdxs.map((signature) => ({ |
| 178 | + Type: signature._typeName, |
| 179 | + Source: bufToHex(signature.source), |
| 180 | + Bytes: bufToHex(signature.bytes.data), |
| 181 | + BytesSize: signature.bsize |
| 182 | + })), |
| 183 | + Amount: bufToHex(inp.input.amount), |
| 184 | + AmountValue: "0x" + inp.input.amountValue, |
| 185 | + DecimalAmountValue: fromHexToDec(inp.input.amountValue) + " AVAX" |
| 186 | + } |
| 187 | + }) |
| 188 | + ) |
| 189 | + let importedTxOutputs = txToObject.unsignedTx.transaction.outs.map( |
| 190 | + (out) => ({ |
| 191 | + Address: bufToHex(out.address.data), |
| 192 | + Amount: bufToHex(out.amount.data), |
| 193 | + AmountValue: "0x" + out.amountValue, |
| 194 | + DecimalAmountValue: fromHexToDec(out.amountValue) + " AVAX", |
| 195 | + AssetID: base58Encoder(out.assetID.data) |
| 196 | + }) |
| 197 | + ) |
| 198 | + let importedTxCredentials = txToObject.credentials.map((credential) => ({ |
| 199 | + Type: credential._typeName, |
| 200 | + TypeID: credential._typeID, |
| 201 | + Signatures: credential.sigArray.map((signature) => ({ |
| 202 | + Type: signature._typeName, |
| 203 | + Bytes: bufToHex(signature.bytes.data), |
| 204 | + BytesSize: signature.bsize |
| 205 | + })) |
| 206 | + })) |
| 207 | + let importTx = { |
| 208 | + Type: txToObject._typeName, |
| 209 | + UnsignedTx: { |
| 210 | + Type: txToObject.unsignedTx._typeName, |
| 211 | + CodecID: txToObject.unsignedTx.codecID, |
| 212 | + Transaction: { |
| 213 | + Type: txToObject.unsignedTx.transaction._typeName, |
| 214 | + TypeID: txToObject.unsignedTx.transaction._typeID, |
| 215 | + NetworkID: toHexThenDec( |
| 216 | + txToObject.unsignedTx.transaction.networkID.data |
| 217 | + ), |
| 218 | + BlockchainID: base58Encoder( |
| 219 | + txToObject.unsignedTx.transaction.blockchainID.data |
| 220 | + ), |
| 221 | + BlockchainIDName: chainName( |
| 222 | + txToObject.unsignedTx.transaction.blockchainID.data |
| 223 | + ), |
| 224 | + SourceChain: base58Encoder( |
| 225 | + txToObject.unsignedTx.transaction.sourceChain.data |
| 226 | + ), |
| 227 | + SourceChainName: chainName( |
| 228 | + txToObject.unsignedTx.transaction.sourceChain.data |
| 229 | + ), |
| 230 | + NumberOfImportedInputs: toHexThenDec( |
| 231 | + txToObject.unsignedTx.transaction.numIns.data |
| 232 | + ), |
| 233 | + ImportedInputs: importedTxImpInputs, |
| 234 | + NumberOfOutputs: toHexThenDec( |
| 235 | + txToObject.unsignedTx.transaction.numOuts.data |
| 236 | + ), |
| 237 | + Outputs: importedTxOutputs |
| 238 | + } |
| 239 | + }, |
| 240 | + Credentials: importedTxCredentials |
| 241 | + } |
| 242 | + console.log(require("util").inspect(importTx, true, 10)) |
| 243 | + } |
| 244 | + |
| 245 | + txToObject.unsignedTx.transaction._typeName == "ExportTx" |
| 246 | + ? displayExportTx() |
| 247 | + : displayImportTx() |
| 248 | +} |
| 249 | + |
| 250 | +main() |
0 commit comments