From e0641ce85a603f8460701613d3814dffbc645c9f Mon Sep 17 00:00:00 2001 From: bferenc <117105626+bferenc@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:54:06 +0100 Subject: [PATCH] feat: switch to v4 (#802) * feat: switch to v4 * ci: add codeql analysis BREAKING CHANGE: switch to new API --- .env | 18 -- examples/evm/base58Decoder.ts | 23 --- examples/evm/bech32AddressDecoder.ts | 31 ---- examples/evm/bech32AddressEncoder.ts | 26 --- examples/evm/blockExtraDataDecoder.ts | 250 -------------------------- 5 files changed, 348 deletions(-) delete mode 100644 .env delete mode 100644 examples/evm/base58Decoder.ts delete mode 100644 examples/evm/bech32AddressDecoder.ts delete mode 100644 examples/evm/bech32AddressEncoder.ts delete mode 100644 examples/evm/blockExtraDataDecoder.ts diff --git a/.env b/.env deleted file mode 100644 index e4677080a..000000000 --- a/.env +++ /dev/null @@ -1,18 +0,0 @@ -# public Avalanche API -IP = "api.avax.network" -PORT = 443 -PROTOCOL = "https" -NETWORK_ID = 1 - -# localhost -LOCAL_IP= "127.0.0.1" -LOCAL_PORT = 9650 -LOCAL_PROTOCOL = "http" -LOCAL_NETWORK_ID = 1337 - -# index API -IP_INDEXER = "indexer-demo.avax.network" - -# socket -PROTOCOL_WS = "ws" -HOST = "localhost" diff --git a/examples/evm/base58Decoder.ts b/examples/evm/base58Decoder.ts deleted file mode 100644 index db6db1759..000000000 --- a/examples/evm/base58Decoder.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Serialization } from "../../src/utils" -import { SerializedType } from "../../src/utils" -import { SerializedEncoding } from "../../src/utils" - - -const base58Decoder = (item: string) => { - const hexVal: string = serialization.decoder(item, base58 , cb58, hex ) - return hexVal - } - -const cb58: SerializedType = "cb58" -const base58: SerializedEncoding = "base58" -const hex: SerializedType = "hex" -const serialization: Serialization = Serialization.getInstance() - -const main = async (): Promise => { - const base58: string = "2MJd1pvSzdvvFKk3aa6qCa4trgnRfjzNfkTCWHha6TxtthsNfd" - - const decoded: string = base58Decoder(base58) - console.log("0x" + decoded) -} - -main() \ No newline at end of file diff --git a/examples/evm/bech32AddressDecoder.ts b/examples/evm/bech32AddressDecoder.ts deleted file mode 100644 index 14886554b..000000000 --- a/examples/evm/bech32AddressDecoder.ts +++ /dev/null @@ -1,31 +0,0 @@ -import * as bech32 from "bech32" -import { decode } from "base32-encoding" -import { Buffer } from "../../src" - -const fromDecToHex = (item: number) => { - const hexVal = item.toString(16) - return hexVal.length < 2 ? "0" + hexVal : hexVal -} -const bech32Decoder = (item: string) => { - const bech32Val = item.length === 45 ? item.split("C-").join("") : item - //decode bech32 address into hrp and words - const toWords = bech32.bech32.decode(bech32Val) - //get buffer from words - const bufFromWords = Buffer.from(toWords.words) - //convert words array of unsigned integers from base32 (5-bit) to base256 (8-bit) - const base256Converted = decode(bufFromWords) - //get an array of unsigned integers from the buffer obtained - const arrBase256Converted = [...base256Converted] - //convert each integer to its hex value - const hexValue = arrBase256Converted.map((item) => fromDecToHex(item)) - return "0x" + hexValue.toString().split(",").join("") -} - -const main = async (): Promise => { - //can be used for P and X addresses too - const address: string = "C-avax1hwgqh0s6yrdy636xv6me5haxecfx99enh5278a" - const bech32DecodedAddress = bech32Decoder(address) - console.log("Decoded address (0x format): " + bech32DecodedAddress) -} - -main() diff --git a/examples/evm/bech32AddressEncoder.ts b/examples/evm/bech32AddressEncoder.ts deleted file mode 100644 index fac67b76d..000000000 --- a/examples/evm/bech32AddressEncoder.ts +++ /dev/null @@ -1,26 +0,0 @@ -import * as bech32 from "bech32" -import { Buffer } from "../../src" - -const bech32Encoder = (item: string) => { - const hrp = "avax" - const bufFromHex = Buffer.from(item.slice(2), "hex") - const arrBuf = [...bufFromHex] - const bech32Address = bech32.bech32.encode(hrp, bech32.bech32.toWords(arrBuf)) - const errorMessage = - item.length > 42 - ? "Address too long. Enter a valid address" - : item.length < 40 - ? "Address too short. Enter a valid address" - : "Add 0x prefix to the address" - if (item.length === 42) return "C-" + bech32Address - //to get P and X chains format, just change the C- prefix to P- or X- - else throw new Error(errorMessage) -} - -const main = async (): Promise => { - const address: string = "0xBB900BbE1A20dA4d474666B79a5fa6CE12629733" - const encodedAddress = bech32Encoder(address) - console.log("Bech32 encoded address: " + encodedAddress) -} - -main() diff --git a/examples/evm/blockExtraDataDecoder.ts b/examples/evm/blockExtraDataDecoder.ts deleted file mode 100644 index b8227fb7e..000000000 --- a/examples/evm/blockExtraDataDecoder.ts +++ /dev/null @@ -1,250 +0,0 @@ -import { Buffer } from "../../src" -import { Tx } from "../../src/apis/evm" -import { Serialization } from "../../src/utils" -import { SerializedType } from "../../src/utils" -import * as bech32 from "bech32" - -const cb58: SerializedType = "cb58" -const serialization: Serialization = Serialization.getInstance() - -const getTxData = (item: string) => { - const txSplit = item.split("0x000000000001") - const prefix = "0x0000" - const txData = prefix + txSplit[1] - return txData -} - -const fromDecToHex = (item: number) => { - let hexVal = item.toString(16) - let hexString = hexVal.length < 2 ? "0" + hexVal : hexVal - return hexString -} -const fromHexToDec = (item: string) => { - let hexString = item.split("0x").join("") - let decNumber = parseInt(hexString, 16) - let value = decNumber / 10 ** 9 - return value -} -const toHexThenDec = (item: number) => { - let toHex = fromDecToHex(item).split(",").join("") - let hexString = toHex.split("0x").join("") - let decNumber = parseInt(hexString, 16) - return decNumber -} -const bufToHex = (item: string) => { - let valueFromJSON = item - let bufValueFromJson = Buffer.from(valueFromJSON) - let arrValueFromJSON = [...bufValueFromJson] - let hexValueFromJSON = arrValueFromJSON.map((item) => fromDecToHex(item)) - return "0x" + hexValueFromJSON.toString().split(",").join("") -} -const bech32Encoder = (item: string) => { - const hrp = "avax" - let valueFromJSON = item - let bufValueFromJson = Buffer.from(valueFromJSON) - let arrValueFromJSON = [...bufValueFromJson] - let bech32Address = bech32.bech32.encode( - hrp, - bech32.bech32.toWords(arrValueFromJSON) - ) - return "C-" + bech32Address -} -const base58Encoder = (item: string) => { - let valToBeEncoded = Buffer.from(item) - let base58Val: string = serialization.bufferToType(valToBeEncoded, cb58) - return base58Val -} -const chainName = (item: string) => { - const chainID = base58Encoder(item) - let name: string = "null" - const cchainID = "2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5" - const pchainID = "11111111111111111111111111111111LpoYY" - chainID == "11111111111111111111111111111111LpoYY" - ? (name = "P-Chain") - : (name = "X-Chain") - chainID == cchainID - ? (name = "C-Chain") - : chainID == pchainID - ? name == "P-Chain" - : (name = "X-Chain") - return name -} - -const main = async (): Promise => { - const blockExtraData: string = - "0x00000000000100000001000000010427d4b22a2a78bcddd456742caf91b56badbff985ee19aef14573e7343fd652000000000000000000000000000000000000000000000000000000000000000000000001bb900bbe1a20da4d474666b79a5fa6ce1262973300000000009dba8421e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000000000000370000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000070000000000989680000000000000000000000001000000015feaa6c211cc8376e16211a76eff1e88bad8079d000000010000000900000001f526c9a38a2da08291583bf86e5160bd8b49df585b3fc2fb57884390c673f748428c58e95c6514b9d6a27d273550c63070ab64d257798e8d07f8a208489ebb2100" - const txData = getTxData(blockExtraData) - const buf: Buffer = new Buffer(txData.slice(2), "hex") - const tx: Tx = new Tx() - tx.fromBuffer(buf) - const txString: string = JSON.stringify(tx) - const txToObject = JSON.parse(txString) - - let displayExportTx = () => { - //exportTx - let exportedTxInputs = txToObject.unsignedTx.transaction.inputs.map( - (input) => ({ - Address: bufToHex(input.address.data), - Amount: bufToHex(input.amount.data), - AmountValue: "0x" + input.amountValue, - DecimalAmountValue: fromHexToDec(input.amountValue) + " AVAX", - AssetID: base58Encoder(input.assetID.data), - Nonce: bufToHex(input.nonce.data), - NonceValue: input.nonceValue, - SignaturesCount: toHexThenDec(input.sigCount.data), - SignaturesIDs: input.sigIdxs - }) - ) - let exportedTxExpOutputs = - txToObject.unsignedTx.transaction.exportedOutputs.map((out) => ({ - Type: out._typeName, - AssetID: base58Encoder(out.assetID.data), - Output: { - Type: out.output._typeName, - TypeID: out.output._typeID, - Locktime: toHexThenDec(out.output.locktime.data), - Threshold: toHexThenDec(out.output.threshold.data), - NumberOfAddresses: toHexThenDec(out.output.numaddrs.data), - Addresses: out.output.addresses.map((address) => ({ - Type: address._typeName, - Bytes: bufToHex(address.bytes.data), - BytesSize: address.bsize, - Bech32Format: bech32Encoder(address.bytes.data) - })), - Amount: bufToHex(out.output.amount), - AmountValue: "0x" + out.output.amountValue, - DecimalAmountValue: fromHexToDec(out.output.amountValue) + " AVAX" - } - })) - let exportedTxCredentials = txToObject.credentials.map((credential) => ({ - Type: credential._typeName, - TypeID: credential._typeID, - Signatures: credential.sigArray.map((signature) => ({ - Type: signature._typeName, - Bytes: bufToHex(signature.bytes.data), - BytesSize: signature.bsize - })) - })) - let exportTx = { - Type: txToObject._typeName, - UnsignedTx: { - Type: txToObject.unsignedTx._typeName, - CodecID: txToObject.unsignedTx.codecID, - Transaction: { - Type: txToObject.unsignedTx.transaction._typeName, - TypeID: txToObject.unsignedTx.transaction._typeID, - NetworkID: toHexThenDec( - txToObject.unsignedTx.transaction.networkID.data - ), - BlockchainID: base58Encoder( - txToObject.unsignedTx.transaction.blockchainID.data - ), - BlockchainIDName: chainName( - txToObject.unsignedTx.transaction.blockchainID.data - ), - DestinationChain: base58Encoder( - txToObject.unsignedTx.transaction.destinationChain.data - ), - DestinationChainName: chainName( - txToObject.unsignedTx.transaction.destinationChain.data - ), - NumberOfInputs: toHexThenDec( - txToObject.unsignedTx.transaction.numInputs.data - ), - Inputs: exportedTxInputs, - NumberOfExportedOutputs: toHexThenDec( - txToObject.unsignedTx.transaction.numExportedOutputs.data - ), - ExportedOutputs: exportedTxExpOutputs - } - }, - Credentials: exportedTxCredentials - } - console.log(require("util").inspect(exportTx, true, 10)) - } - - let displayImportTx = () => { - //importTX - let importedTxImpInputs = txToObject.unsignedTx.transaction.importIns.map( - (inp) => ({ - Type: inp._typeName, - TransactionId: base58Encoder(inp.txid.data), - OutputId: toHexThenDec(inp.outputidx.data), - AssetID: base58Encoder(inp.assetID.data), - Input: { - Type: inp.input._typeName, - TypeID: inp.input._typeID, - SignaturesIds: inp.input.sigIdxs.map((signature) => ({ - Type: signature._typeName, - Source: bufToHex(signature.source), - Bytes: bufToHex(signature.bytes.data), - BytesSize: signature.bsize - })), - Amount: bufToHex(inp.input.amount), - AmountValue: "0x" + inp.input.amountValue, - DecimalAmountValue: fromHexToDec(inp.input.amountValue) + " AVAX" - } - }) - ) - let importedTxOutputs = txToObject.unsignedTx.transaction.outs.map( - (out) => ({ - Address: bufToHex(out.address.data), - Amount: bufToHex(out.amount.data), - AmountValue: "0x" + out.amountValue, - DecimalAmountValue: fromHexToDec(out.amountValue) + " AVAX", - AssetID: base58Encoder(out.assetID.data) - }) - ) - let importedTxCredentials = txToObject.credentials.map((credential) => ({ - Type: credential._typeName, - TypeID: credential._typeID, - Signatures: credential.sigArray.map((signature) => ({ - Type: signature._typeName, - Bytes: bufToHex(signature.bytes.data), - BytesSize: signature.bsize - })) - })) - let importTx = { - Type: txToObject._typeName, - UnsignedTx: { - Type: txToObject.unsignedTx._typeName, - CodecID: txToObject.unsignedTx.codecID, - Transaction: { - Type: txToObject.unsignedTx.transaction._typeName, - TypeID: txToObject.unsignedTx.transaction._typeID, - NetworkID: toHexThenDec( - txToObject.unsignedTx.transaction.networkID.data - ), - BlockchainID: base58Encoder( - txToObject.unsignedTx.transaction.blockchainID.data - ), - BlockchainIDName: chainName( - txToObject.unsignedTx.transaction.blockchainID.data - ), - SourceChain: base58Encoder( - txToObject.unsignedTx.transaction.sourceChain.data - ), - SourceChainName: chainName( - txToObject.unsignedTx.transaction.sourceChain.data - ), - NumberOfImportedInputs: toHexThenDec( - txToObject.unsignedTx.transaction.numIns.data - ), - ImportedInputs: importedTxImpInputs, - NumberOfOutputs: toHexThenDec( - txToObject.unsignedTx.transaction.numOuts.data - ), - Outputs: importedTxOutputs - } - }, - Credentials: importedTxCredentials - } - console.log(require("util").inspect(importTx, true, 10)) - } - - txToObject.unsignedTx.transaction._typeName == "ExportTx" - ? displayExportTx() - : displayImportTx() -} - -main()