Add bitcoin RPC methods for onchain address validation (#198)

* Added params to connect to bitcoin core daemon. Needed by api.utils.validate_onchain_address()

* Fixes issue#194

* Modified BITCOIND_RPCUSER and BITCOIND_RPCPASSWORD to default development environment

* Modified BITCOIND_RPCURL port number to default development environment
This commit is contained in:
zx9r 2022-07-31 16:35:12 +02:00 committed by GitHub
parent 8e451da5d3
commit 3ae6087a87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 35 deletions

View File

@ -8,6 +8,11 @@ LND_CERT_BASE64='LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNLVENDQWRDZ0F3SUJBZ0l
# base64 ~/.lnd/data/chain/bitcoin/testnet/admin.macaroon | tr -d '\n'
LND_MACAROON_BASE64='AgEDbG5kAvgBAwoQsyI+PK+fyb7F2UyTeZ4seRIBMBoWCgdhZGRyZXNzEgRyZWFkEgV3cml0ZRoTCgRpbmZvEgRyZWFkEgV3cml0ZRoXCghpbnZvaWNlcxIEcmVhZBIFd3JpdGUaIQoIbWFjYXJvb24SCGdlbmVyYXRlEgRyZWFkEgV3cml0ZRoWCgdtZXNzYWdlEgRyZWFkEgV3cml0ZRoXCghvZmZjaGFpbhIEcmVhZBIFd3JpdGUaFgoHb25jaGFpbhIEcmVhZBIFd3JpdGUaFAoFcGVlcnMSBHJlYWQSBXdyaXRlGhgKBnNpZ25lchIIZ2VuZXJhdGUSBHJlYWQAAAYgMt90uD6v4truTadWCjlppoeJ4hZrL1SBb09Y+4WOiI0='
# Bitcoin Core Daemon RPC, used to validate addresses
BITCOIND_RPCURL = 'http://127.0.0.1:18332'
BITCOIND_RPCUSER = 'robodev'
BITCOIND_RPCPASSWORD = 'robodev'
# Postgresql Database
POSTGRES_NAME='postgres'
POSTGRES_USER='postgres'

View File

@ -1,9 +1,14 @@
import requests, ring, os
from decouple import config
import json
import os
import numpy as np
import coinaddrvalidator as addr
import requests
import ring
from decouple import config
from api.models import Order
def get_tor_session():
session = requests.session()
# Tor uses the 9050 port as the default socks port
@ -11,36 +16,48 @@ def get_tor_session():
'https': 'socks5://127.0.0.1:9050'}
return session
def bitcoind_rpc(method, params=None):
"""
Makes a RPC call to bitcoin core daemon
:param method: RPC method to call
:param params: list of params required by the calling RPC method
:return:
"""
BITCOIND_RPCURL = config('BITCOIND_RPCURL')
BITCOIND_RPCUSER = config('BITCOIND_RPCUSER')
BITCOIND_RPCPASSWORD = config('BITCOIND_RPCPASSWORD')
if params is None:
params = []
payload = json.dumps(
{
"jsonrpc": "2.0",
"id": "robosats",
"method": method,
"params": params
}
)
return requests.post(BITCOIND_RPCURL, auth=(BITCOIND_RPCUSER, BITCOIND_RPCPASSWORD), data=payload).json()['result']
def validate_onchain_address(address):
'''
"""
Validates an onchain address
'''
"""
validation = addr.validate('btc', address.encode('utf-8'))
try:
validation = bitcoind_rpc('validateaddress', [address])
if not validation['isvalid']:
return False, {"bad_address": validation['error']}
except:
# TODO: log the exception ?
return False, {"bad_address": 'Unable to validate address, check bitcoind backend'}
if not validation.valid:
return False, {
"bad_address":
"Does not look like a valid address"
}
return True, None
NETWORK = str(config('NETWORK'))
if NETWORK == 'mainnet':
if validation.network == 'main':
return True, None
else:
return False, {
"bad_address":
"This is not a bitcoin mainnet address"
}
elif NETWORK == 'testnet':
if validation.network == 'test':
return True, None
else:
return False, {
"bad_address":
"This is not a bitcoin testnet address"
}
market_cache = {}
@ring.dict(market_cache, expire=3) # keeps in cache for 3 seconds