Wot adjustments, blockdb plugin events

This commit is contained in:
Kevin F 2022-10-01 04:25:46 +00:00
parent 20393a547e
commit 4572f255fb
10 changed files with 46 additions and 21 deletions

View File

@ -1,6 +1,7 @@
from typing import Callable, Generator, List from typing import Callable, Generator, List
from onionrblocks import Block from onionrblocks import Block
from onionrplugins import onionrevents
import db import db
@ -15,8 +16,9 @@ block_storage_observers: List[Callable] = []
def add_block_to_db(block: Block): def add_block_to_db(block: Block):
# Raises db.DuplicateKey if dupe onionrevents.event('before_block_db_add', block, threaded=False)
db.set_if_new(block_db_path, block.id, block.raw) db.set_if_new(block_db_path, block.id, block.raw) # Raises db.DuplicateKey if dupe
onionrevents.event('after_block_db_add', block, threaded=False)
def has_block(block_hash): def has_block(block_hash):

View File

@ -0,0 +1 @@
import tty

View File

@ -14,6 +14,7 @@ from gossip.peerset import gossip_peer_set
from logger import log as logging from logger import log as logging
import onionrplugins import onionrplugins
from onionrplugins.pluginapis import plugin_apis
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
@ -34,15 +35,24 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
plugin_name = 'wot' plugin_name = 'wot'
PLUGIN_VERSION = '0.0.0' PLUGIN_VERSION = '0.0.1'
from wot.identity import identities from wot.identity import identities
from wot.loadfromblocks import load_identities_from_blocks from wot.loadfromblocks import load_identities_from_blocks
from wot.identity import get_distance
def on_init(api, data=None): def on_init(api, data=None):
logging.info( logging.info(
f"Web of Trust Plugin v{PLUGIN_VERSION} enabled") f"Web of Trust Plugin v{PLUGIN_VERSION} enabled")
#onionrplugins.plugin_apis['wot'] = wot_test #onionrplugins.plugin_apis['wot'] = wot_test
plugin_apis['wot.get_distance'] = get_distance
list(map(lambda x: identities.add(x), load_identities_from_blocks())) list(
map(
lambda x: identities.add(x),
load_identities_from_blocks())
)
def on_wot_cmd(api, data=None):
return

View File

@ -1,20 +1,11 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from enum import IntEnum, auto from .identityprocessing import process_identity_announce
import struct
import msgpack
if TYPE_CHECKING: if TYPE_CHECKING:
from onionrblocks import Block from onionrblocks import Block
from wot.exceptions import InvalidWotBlock from wot.exceptions import InvalidWotBlock
from wot.wotcommand import WotCommand
class WotCommand(IntEnum):
TRUST = 1
REVOKE_TRUST = auto()
ANNOUNCE = auto()
REVOKE = auto()
class WotPayload: class WotPayload:
@ -26,6 +17,14 @@ class WotPayload:
match wot_command(WotCommand): match wot_command(WotCommand):
case WotCommand.TRUST: case WotCommand.TRUST:
pass pass
case WotCommand.REVOKE_TRUST:
pass
case WotCommand.ANNOUNCE:
process_identity_announce(block_data[1:])
case WotCommand.REVOKE:
pass
case _:
raise InvalidWotBlock('Invalid WOT command')
def process_block(bl: 'Block'): def process_block(bl: 'Block'):

View File

@ -2,7 +2,7 @@ from logger import log as logging
from nacl.signing import VerifyKey from nacl.signing import VerifyKey
from wot.blockprocessingevent import WotCommand from wot.wotcommand import WotCommand
from wot.identity import Identity from wot.identity import Identity
from wot.identity.identityset import identities from wot.identity.identityset import identities

View File

@ -2,7 +2,7 @@ from logger import log as logging
from nacl.signing import VerifyKey from nacl.signing import VerifyKey
from wot.blockprocessingevent import WotCommand from wot.wotcommand import WotCommand
from wot.identity import Identity from wot.identity import Identity
from wot.identity.identityset import identities from wot.identity.identityset import identities

View File

@ -5,7 +5,9 @@ from nacl.signing import VerifyKey
from logger import log as logging from logger import log as logging
from wot.getbykey import get_identity_by_key from wot.getbykey import get_identity_by_key
from wot.blockprocessingevent import WotCommand from wot.wotcommand import WotCommand
from utils import identifyhome
def process_revoke_signature(revoke_signature_payload): def process_revoke_signature(revoke_signature_payload):

View File

@ -4,7 +4,7 @@ from logger import log as logging
from nacl.signing import VerifyKey from nacl.signing import VerifyKey
from wot.getbykey import get_identity_by_key from wot.getbykey import get_identity_by_key
from wot.blockprocessingevent import WotCommand from wot.wotcommand import WotCommand
def process_trust_signature(sig_payload: bytes): def process_trust_signature(sig_payload: bytes):

View File

@ -0,0 +1,7 @@
from enum import IntEnum, auto
class WotCommand(IntEnum):
TRUST = 1
REVOKE_TRUST = auto()
ANNOUNCE = auto()
REVOKE = auto()

View File

@ -5,6 +5,7 @@ from enum import IntEnum, auto
from nacl.signing import SigningKey, VerifyKey from nacl.signing import SigningKey, VerifyKey
from nacl.exceptions import BadSignatureError from nacl.exceptions import BadSignatureError
import nacl import nacl
import nacl.exceptions
import secrets import secrets
import onionrblocks import onionrblocks
@ -43,8 +44,11 @@ class TestRevokeIdentityPayload(unittest.TestCase):
signed = signing_key.sign(wot_cmd + bytes(main_iden.key)) signed = signing_key.sign(wot_cmd + bytes(main_iden.key))
revoke_payload = wot_cmd + bytes(signing_key.verify_key) + signed.signature revoke_payload = wot_cmd + bytes(signing_key.verify_key) + signed.signature
revoke_payload = bytearray(revoke_payload)
revoke_payload[63] = revoke_payload[63] + 1
revoke_payload = bytes(revoke_payload)
self.assertRaises(nacl.exceptions.Inv process_identity_revoke(revoke_payload) self.assertRaises(nacl.exceptions.BadSignatureError, process_identity_revoke, revoke_payload)
self.assertEqual(len(identities), 1) self.assertEqual(len(identities), 1)