From e978d1810d6d617bed4c1cd29d234ff5a5afe6c3 Mon Sep 17 00:00:00 2001 From: Bishal Das Date: Fri, 5 Jul 2024 00:08:34 +0530 Subject: [PATCH 1/2] btc, eth, bch address validator can use this function to validate btc, eth and bch address while user is trying to --- website/static/js/validate-btc.js | 119 ++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 website/static/js/validate-btc.js diff --git a/website/static/js/validate-btc.js b/website/static/js/validate-btc.js new file mode 100644 index 0000000000..36dfda7b72 --- /dev/null +++ b/website/static/js/validate-btc.js @@ -0,0 +1,119 @@ + +function validateBCH(address) { + /*** + * Params: BCH Address + * ***/ + if(address == null || address == "" || address == " "){ + return false; + } + if(address.startsWith("bitcoincash:")){ + address = address.slice(12); + } + let regex = new RegExp(/^[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{42}$/); + + if(!regex.test(address) == true){ + return false; + } + return true; +} + + +function validateEthereum(address) { + /*** + * Params: Ethereum Address + * ***/ + + let regex = new RegExp(/^(0x)?[0-9a-fA-F]{40}$/); + + if(address == null || address == "" || address == " "){ + return false; + }else if(regex.test(address) == true){ + return true; + }else{ + return false; + } + +} + +function validateBitCoin(address) { + /*** + * Params: BitCoin Address + * ***/ + let regex = new RegExp(/^(bc1|[13])[a-km-zA-HJ-NP-Z1-9]{25,34}$/); + if (address == null || address == "" || address == " ") { + return false; + } else if (regex.test(address) == true) { + return true; + } else { + return false; + } +} +// TEST THE VALIDATORS +// BitCoin VALIDATOR +// Uncomment the below code + +// var btc = [ +// '1BoatSLRHtKNngkdXEeobR76b53LETtpyT', +// '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', +// '2QLbGuc3GWptSpWLKwJfaV8z6Z1k7ydfGr', +// '1PeChKY22Zq8Kipj6nKzf8xVRmXo5q3Ne', +// '1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp', +// 'ACounterpartyXXXXXXXXXXXXXXXUWLpVr', +// '3ELzJkt9A1sp8ysTTz9TnL5KszYQmBpdr5', +// 'aBitcoinEaterAddressDontSendf59kuE', +// '1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX', +// '1AGRx1kJhx8ZgB4jQDL3Ka5Mf9xSKYtL2p', +// '1GDbUgo6X2i29K1jZ4WDEz6qczdVqzK7oa', +// '1MZ8Rjkt8Tgk5n98dRwj29s5LZY2zp8mcK', +// '1HoDW3sJv5X4xmtPvknm78X3pYYUGTkJK ', +// '1AE1LoNUouPjqaAcdRFLednhrHiDRJX6W3', +// '1FfmbHfnpaZjKFvyi1okTjJJusN455paPH', +// '1HCKjUpRGcrrRAtFaaCAUaGjsPx9oYmLaZ', +// '1L8meqhHjBckTnCvFkg1aeDdWxqa5i5a8n', +// '1ice7DUtRURKToyD8fX4abRekiYnoVHTa', +// '1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp ', +// '1Ch5cMc8s8QzCx9vjvcu6zG8gTNDzYf9ZT', +// ] +// for(var i=0; i Date: Fri, 5 Jul 2024 01:11:29 +0530 Subject: [PATCH 2/2] crypto validator added to profile page --- website/static/js/validate-btc.js | 44 ++++++++++++++++++++++++------- website/templates/profile.html | 13 ++++++--- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/website/static/js/validate-btc.js b/website/static/js/validate-btc.js index 36dfda7b72..6fdf6d97c8 100644 --- a/website/static/js/validate-btc.js +++ b/website/static/js/validate-btc.js @@ -1,17 +1,41 @@ +function validateCrypto() { + let selectedCrypto = document.forms["cryptoForm"]["selected_crypto"].value; + let address = document.forms["cryptoForm"]["new_address"].value; + var isValid; + if (selectedCrypto == "Bitcoin") { + isValid = validateBitCoin(address); + } else if (selectedCrypto == "Ethereum") { + isValid = validateEthereum(address); + } else if (selectedCrypto == "BitcoinCash") { + isValid = validateBCH(address) + } else { + $.notify("Select a Crypto to Continue!", { + style: "custom", + className: "danger" + }); + } + if (!isValid) { + $.notify("Enter a valid Crypto Address!", { + style: "custom", + className: "danger" + }); + } + return isValid; +} function validateBCH(address) { /*** * Params: BCH Address * ***/ - if(address == null || address == "" || address == " "){ + if (address == null || address == "" || address == " ") { return false; } - if(address.startsWith("bitcoincash:")){ + if (address.startsWith("bitcoincash:")) { address = address.slice(12); } let regex = new RegExp(/^[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{42}$/); - if(!regex.test(address) == true){ + if (!regex.test(address) == true) { return false; } return true; @@ -19,20 +43,20 @@ function validateBCH(address) { function validateEthereum(address) { - /*** - * Params: Ethereum Address - * ***/ + /*** + * Params: Ethereum Address + * ***/ let regex = new RegExp(/^(0x)?[0-9a-fA-F]{40}$/); - if(address == null || address == "" || address == " "){ + if (address == null || address == "" || address == " ") { return false; - }else if(regex.test(address) == true){ + } else if (regex.test(address) == true) { return true; - }else{ + } else { return false; } - + } function validateBitCoin(address) { diff --git a/website/templates/profile.html b/website/templates/profile.html index 3e611cd0f2..4cc7515b07 100644 --- a/website/templates/profile.html +++ b/website/templates/profile.html @@ -351,14 +351,20 @@

{{ user.username }}

-
+ id="update-form" + onsubmit="return validateCrypto()"> {% csrf_token %}
- + {% comment %} set default to index 0 or Bitcoin {% endcomment %} + {{ user.username }} }); }); + {% endblock content %}