Skip to content

Commit

Permalink
package & converter fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vasylcode committed Oct 28, 2023
1 parent e9f37ec commit 1ce6a7a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 67 deletions.
27 changes: 21 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
133 changes: 74 additions & 59 deletions src/components/AddressConverter/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
// <div>
// <div className="flex items-center gap-3 mb-3">
// <input
// type="text"
// value={hrp}
// onChange={handleHrpChange}
// placeholder="HRP (e.g., dydx)"
// className="bg-yellow-800 dark:bg-red-800"
// />
// <input
// type="text"
// value={address}
// onChange={handleAddressChange}
// placeholder="0x or Bech32 address"
// className="bg-yellow-800 dark:bg-red-800"
// />

// <button type="button" onClick={handleConvert} className="hover:dark:border-green-500">
// Convert
// </button>
// </div>
// <p>Converted address: {convertedAddress}</p>
// </div>
// );
// };

// 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<HTMLInputElement>) => {
setAddress(e.target.value);
};

const handleNetworkChange = (e: React.ChangeEvent<HTMLInputElement>) => {
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 (
<div>
<div className="flex items-center gap-3 mb-3">
<input
type="text"
value={network}
onChange={handleNetworkChange}
placeholder="Network (e.g., dydx)"
className="bg-yellow-800 dark:bg-red-800"
/>
<input
type="text"
value={address}
onChange={handleAddressChange}
placeholder="0x or Bech32 address"
className="bg-yellow-800 dark:bg-red-800"
/>

<button type="button" onClick={handleConvert} className="hover:dark:border-green-500">
Convert
</button>
</div>
<p>Converted address: {convertedAddress}</p>
</div>
);
};

export default AddressConverter;

0 comments on commit 1ce6a7a

Please sign in to comment.