2020-10-15 22:02:51 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-26 06:34:34 +00:00
|
|
|
|
2020-10-15 22:02:51 +00:00
|
|
|
add user keys or transport addresses
|
|
|
|
"""
|
|
|
|
import sqlite3
|
|
|
|
from onionrutils import stringvalidators
|
|
|
|
from . import listkeys
|
|
|
|
from .. import dbfiles
|
|
|
|
import onionrcrypto
|
2022-02-07 01:18:53 +00:00
|
|
|
import onionrvalues
|
2020-10-15 22:02:51 +00:00
|
|
|
"""
|
2019-06-26 06:34:34 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-15 22:02:51 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-29 05:36:56 +00:00
|
|
|
|
2022-01-11 07:20:15 +00:00
|
|
|
def add_pub_key(peerID, name=''):
|
2020-10-15 22:02:51 +00:00
|
|
|
"""Add a public key to the key database (misleading function name)."""
|
2022-01-11 07:20:15 +00:00
|
|
|
if peerID in listkeys.list_pub_keys() or peerID == onionrcrypto.pub_key:
|
2019-06-22 21:16:12 +00:00
|
|
|
raise ValueError("specified id is already known")
|
|
|
|
|
|
|
|
# This function simply adds a peer to the DB
|
2019-06-25 08:21:36 +00:00
|
|
|
if not stringvalidators.validate_pub_key(peerID):
|
2019-06-22 21:16:12 +00:00
|
|
|
return False
|
|
|
|
|
2019-10-29 05:36:56 +00:00
|
|
|
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
|
2019-07-21 16:15:20 +00:00
|
|
|
hashID = ""
|
2019-06-22 21:16:12 +00:00
|
|
|
c = conn.cursor()
|
|
|
|
t = (peerID, name, 'unknown', hashID, 0)
|
|
|
|
|
|
|
|
for i in c.execute("SELECT * FROM peers WHERE id = ?;", (peerID,)):
|
|
|
|
try:
|
|
|
|
if i[0] == peerID:
|
|
|
|
conn.close()
|
|
|
|
return False
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
c.execute('INSERT INTO peers (id, name, dateSeen, hashID, trust) VALUES(?, ?, ?, ?, ?);', t)
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|