Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTC Address validation #7799

Merged
merged 3 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion app/assets/v2/js/pages/new_bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ let appFormBounty;

window.addEventListener('dataWalletReady', function(e) {
appFormBounty.network = networkName;
appFormBounty.form.funderAddress = selectedAccount;
if (appFormBounty.chainId == 0) {
appFormBounty.form.funderAddress = "";
} else {
appFormBounty.form.funderAddress = selectedAccount;
}

}, false);

Vue.component('v-select', VueSelect.VueSelect);
Expand Down Expand Up @@ -145,6 +150,19 @@ Vue.mixin({
vm.submitted = true;
vm.errors = {};

// validate BTC address
if (appFormBounty.chainId == 0) {
let ADDRESS_REGEX = new RegExp("^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$");
let BECH32_REGEX = new RegExp("^bc1[ac-hj-np-zAC-HJ-NP-Z02-9]{11,71}$");
let valid_legacy = ADDRESS_REGEX.test(vm.form.funderAddress);
let valid_segwit = BECH32_REGEX.test(vm.form.funderAddress);
if (valid_legacy == true || valid_segwit == true){
// valid
} else {
vm.$set(vm.errors, 'funderAddress', 'Please enter a valid BTC address');
}
}

if (!vm.form.keywords.length) {
vm.$set(vm.errors, 'keywords', 'Please select the prize keywords');
}
Expand Down
37 changes: 22 additions & 15 deletions app/dashboard/sync/btc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import requests
from dashboard.sync.helpers import record_payout_activity, txn_already_used
from oogway import Net
from oogway import Net, validate
import logging

logger = logging.getLogger(__name__)

def find_txn_on_btc_explorer(fulfillment, network='mainnet'):
funderAddress = fulfillment.bounty.bounty_owner_address
Expand All @@ -13,21 +15,26 @@ def find_txn_on_btc_explorer(fulfillment, network='mainnet'):
n = Net(provider='Blockstream', network=network)
txlist = n.txs(funderAddress)

if network == 'mainnet':
blockstream_url = 'https://blockstream.info/api/tx/'
# validate BTC address
is_valid = validate.is_valid_address(funderAddress)
if is_valid == False:
logger.error(f'error: invalid BTC address - {funderAddress}')
else:
blockstream_url = 'https://blockstream.info/testnet/api/tx/'

if txlist != []:
for txn in txlist:
blockstream_response = requests.get(blockstream_url+txn).json()
if (
blockstream_response['vin'][0]['prevout']['scriptpubkey_address'] == str(funderAddress) and
blockstream_response['vout'][0]['scriptpubkey_address'] == str(payeeAddress) and
float(blockstream_response['vout'][0]['value']) == float(amount) and
not txn_already_used(txn, 'BTC')
):
return txn
if network == 'mainnet':
blockstream_url = 'https://blockstream.info/api/tx/'
else:
blockstream_url = 'https://blockstream.info/testnet/api/tx/'

if txlist != []:
for txn in txlist:
blockstream_response = requests.get(blockstream_url+txn).json()
if (
blockstream_response['vin'][0]['prevout']['scriptpubkey_address'] == str(funderAddress) and
blockstream_response['vout'][0]['scriptpubkey_address'] == str(payeeAddress) and
float(blockstream_response['vout'][0]['value']) == float(amount) and
not txn_already_used(txn, 'BTC')
):
return txn
return None


Expand Down