From 1ce6a7a957c46f2445c2a4081a2d5e8e9cd06e96 Mon Sep 17 00:00:00 2001 From: Vasyl Date: Fri, 27 Oct 2023 18:33:26 -0600 Subject: [PATCH] package & converter fixed --- package-lock.json | 27 ++++- package.json | 13 ++- src/components/AddressConverter/index.tsx | 133 ++++++++++++---------- 3 files changed, 106 insertions(+), 67 deletions(-) diff --git a/package-lock.json b/package-lock.json index f501a1ab..1a64221a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { - "name": "f-5-wiki", - "version": "0.0.0", + "name": "f5wiki", + "version": "0.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "f-5-wiki", - "version": "0.0.0", + "name": "f5wiki", + "version": "0.0.1", "dependencies": { "@docusaurus/core": "2.3.1", "@docusaurus/preset-classic": "2.3.1", "@mdx-js/react": "^1.6.22", + "bech32": "^2.0.0", "clsx": "^1.2.1", "prism-react-renderer": "^1.3.5", "react": "^17.0.2", @@ -3794,7 +3795,9 @@ "version": "0.0.8", "resolved": "https://registry.npmjs.org/ansi-html-community/-/ansi-html-community-0.0.8.tgz", "integrity": "sha512-1APHAyr3+PCamwNw3bXCPp4HFLONZt/yIH0sZp0/469KWNTEy+qN5jQ3GVX6DMZ1UXAi34yVwtTeaG/HpBuuzw==", - "engines": ["node >= 0.8.0"], + "engines": [ + "node >= 0.8.0" + ], "bin": { "ansi-html": "bin/ansi-html" } @@ -4041,6 +4044,11 @@ "resolved": "https://registry.npmjs.org/batch/-/batch-0.6.1.tgz", "integrity": "sha512-x+VAiMRL6UPkx+kudNvxTl6hB2XNNCG2r+7wixVfIYwu/2HKRXimwQyaumLjMveWvT2Hkd/cAJw+QBMfJ/EKVw==" }, + "node_modules/bech32": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz", + "integrity": "sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==" + }, "node_modules/big.js": { "version": "5.2.2", "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz", @@ -6266,7 +6274,9 @@ "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", "hasInstallScript": true, "optional": true, - "os": ["darwin"], + "os": [ + "darwin" + ], "engines": { "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } @@ -15434,6 +15444,11 @@ "resolved": "https://registry.npmjs.org/batch/-/batch-0.6.1.tgz", "integrity": "sha512-x+VAiMRL6UPkx+kudNvxTl6hB2XNNCG2r+7wixVfIYwu/2HKRXimwQyaumLjMveWvT2Hkd/cAJw+QBMfJ/EKVw==" }, + "bech32": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz", + "integrity": "sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==" + }, "big.js": { "version": "5.2.2", "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz", diff --git a/package.json b/package.json index 167b98f6..36be7950 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@docusaurus/core": "2.3.1", "@docusaurus/preset-classic": "2.3.1", "@mdx-js/react": "^1.6.22", + "bech32": "^2.0.0", "clsx": "^1.2.1", "prism-react-renderer": "^1.3.5", "react": "^17.0.2", @@ -29,8 +30,16 @@ "typescript": "^4.7.4" }, "browserslist": { - "production": [">0.5%", "not dead", "not op_mini all"], - "development": ["last 1 chrome version", "last 1 firefox version", "last 1 safari version"] + "production": [ + ">0.5%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] }, "engines": { "node": ">=16.14" diff --git a/src/components/AddressConverter/index.tsx b/src/components/AddressConverter/index.tsx index d7b7fa61..6e2635c0 100644 --- a/src/components/AddressConverter/index.tsx +++ b/src/components/AddressConverter/index.tsx @@ -1,59 +1,74 @@ -// import React, { useState } from "react"; -// import { bech32 } from "bech32"; - -// export const AddressConverter: React.FC<{}> = () => { -// const [address, setAddress] = useState(""); -// const [convertedAddress, setConvertedAddress] = useState(""); -// const [hrp, setHrp] = useState(""); - -// const handleAddressChange = (e: any) => { -// setAddress(e.target.value); -// }; - -// const handleHrpChange = (e: any) => { -// setHrp(e.target.value); -// }; - -// const handleConvert = () => { -// try { -// let converted; -// if (address.startsWith("0x")) { -// const data = Buffer.from(address.substr(2), "hex"); -// converted = bech32.encode(hrp, bech32.toWords(data)); -// } else { -// const decoded = bech32.decode(address); -// converted = "0x" + Buffer.from(bech32.fromWords(decoded.words)).toString("hex"); -// } - -// setConvertedAddress(converted); -// } catch (error) {} -// }; - -// return ( -//
-//
-// -// - -// -//
-//

Converted address: {convertedAddress}

-//
-// ); -// }; - -// export default AddressConverter; +import React, { useState } from "react"; +import { bech32 } from "bech32"; + +export const AddressConverter: React.FC<{}> = () => { + const [address, setAddress] = useState(""); + const [convertedAddress, setConvertedAddress] = useState(""); + const [network, setNetwork] = useState(""); // Default network + + const handleAddressChange = (e: React.ChangeEvent) => { + setAddress(e.target.value); + }; + + const handleNetworkChange = (e: React.ChangeEvent) => { + setNetwork(e.target.value); + }; + + const handleConvert = () => { + try { + let converted; + + if (address.startsWith("0x")) { + // Convert from hexadecimal to Bech32 + const hexData = address.substr(2); + const bytes = new Uint8Array(hexData.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))); + const words = bech32.toWords(bytes); + converted = bech32.encode(network, words); + } else if (address.includes("1")) { + // Convert from Bech32 to hexadecimal + const decoded = bech32.decode(address); + const bytes = new Uint8Array(bech32.fromWords(decoded.words)); + converted = + "0x" + + Array.from(bytes) + .map((byte) => byte.toString(16).padStart(2, "0")) + .join(""); + } else { + // Handle invalid input address format + throw new Error("Invalid address format"); + } + + setConvertedAddress(converted); + } catch (error) { + setConvertedAddress("Error: Invalid address or network"); + } + }; + + return ( +
+
+ + + + +
+

Converted address: {convertedAddress}

+
+ ); +}; + +export default AddressConverter;