forked from nathanwoodburn/firewalletbrowser
feat: Add send page
This commit is contained in:
89
account.py
89
account.py
@@ -2,6 +2,9 @@ from handywrapper import api
|
||||
import os
|
||||
import dotenv
|
||||
import requests
|
||||
import re
|
||||
import domainLookup
|
||||
import json
|
||||
|
||||
|
||||
dotenv.load_dotenv()
|
||||
@@ -16,6 +19,9 @@ response = hsd.getInfo()
|
||||
|
||||
|
||||
def check_account(cookie: str):
|
||||
if cookie is None:
|
||||
return False
|
||||
|
||||
# Check the account
|
||||
if cookie.count(":") < 1:
|
||||
return False
|
||||
@@ -81,4 +87,85 @@ def getTransactions(account):
|
||||
info = hsw.getWalletTxHistory(account)
|
||||
if 'error' in info:
|
||||
return []
|
||||
return info
|
||||
return info
|
||||
|
||||
|
||||
def check_address(address: str, allow_name: bool = True, return_address: bool = False):
|
||||
# Check if the address is valid
|
||||
if address.startswith('@'):
|
||||
# Check if the address is a name
|
||||
if not allow_name and not return_address:
|
||||
return 'Invalid address'
|
||||
elif not allow_name and return_address:
|
||||
return False
|
||||
return check_hip2(address[1:])
|
||||
|
||||
# Check if the address is a valid HNS address
|
||||
response = requests.post(f"http://x:{APIKEY}@127.0.0.1:12037",json={
|
||||
"method": "validateaddress",
|
||||
"params": [address]
|
||||
}).json()
|
||||
if response['error'] is not None:
|
||||
if return_address:
|
||||
return False
|
||||
return 'Invalid address'
|
||||
|
||||
if response['result']['isvalid'] == True:
|
||||
if return_address:
|
||||
return address
|
||||
return 'Valid address'
|
||||
|
||||
if return_address:
|
||||
return False
|
||||
return 'Invalid address'
|
||||
|
||||
|
||||
def check_hip2(domain: str):
|
||||
# Check if the domain is valid
|
||||
domain = domain.lower()
|
||||
|
||||
if re.match(r'^[a-zA-Z0-9\-\.]{1,63}$', domain) is None:
|
||||
return 'Invalid address'
|
||||
|
||||
address = domainLookup.hip2(domain)
|
||||
if not check_address(address, False,True):
|
||||
return 'Hip2: Lookup succeeded but address is invalid'
|
||||
return address
|
||||
|
||||
|
||||
|
||||
def send(account,address,amount):
|
||||
account_name = check_account(account)
|
||||
password = ":".join(account.split(":")[1:])
|
||||
|
||||
|
||||
# Unlock the account
|
||||
response = requests.post(f"http://x:{APIKEY}@127.0.0.1:12039/wallet/{account_name}/unlock",
|
||||
json={"passphrase": password,"timeout": 10})
|
||||
|
||||
if response.status_code != 200:
|
||||
return {
|
||||
"error": "Failed to unlock account"
|
||||
}
|
||||
if 'success' not in response.json():
|
||||
return {
|
||||
"error": "Failed to unlock account"
|
||||
}
|
||||
|
||||
# Send the transaction
|
||||
response = requests.post(f"http://x:{APIKEY}@127.0.0.1:12039",json={
|
||||
"method": "sendtoaddress",
|
||||
"params": [address,amount]
|
||||
})
|
||||
if response.status_code != 200:
|
||||
return {
|
||||
"error": "Failed to send transaction"
|
||||
}
|
||||
response = response.json()
|
||||
if 'error' in response:
|
||||
return {
|
||||
"error": json.dumps(response['error'])
|
||||
}
|
||||
return {
|
||||
"tx": response['result']
|
||||
}
|
||||
Reference in New Issue
Block a user