Skip to content

Commit

Permalink
jey
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed May 15, 2024
1 parent d9b3f86 commit 5839ce6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 44 deletions.
11 changes: 9 additions & 2 deletions commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ def ticket2address(self, ticket, **kwargs):
def signature2address(self, sig, **kwargs):
return self.verify(sig, return_address=True, **kwargs)
sig2addy = signature2address


def verify(self,
data: Union[ScaleBytes, bytes, str, dict],
Expand All @@ -972,6 +973,7 @@ def verify(self,
seperator = seperator,
ss58_format = 42,
max_age = None,
address = None,
**kwargs
) -> bool:
"""
Expand Down Expand Up @@ -1005,9 +1007,14 @@ def verify(self,

if not isinstance(data, str):
data = c.python2str(data)


if address != None:
public_key = c.ss58_decode(address)
if public_key == None:
public_key = self.public_key
public_key = public_key or self.public_key
else:
if self.is_ss58(public_key):
public_key = c.ss58_decode(public_key)

if isinstance(public_key, str):
public_key = bytes.fromhex(public_key.replace('0x', ''))
Expand Down
28 changes: 0 additions & 28 deletions commune/key/ticket.py

This file was deleted.

1 change: 1 addition & 0 deletions commune/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ def get_module(cls,

module = c.import_object(object_path)
except Exception as e:
c.print(e)
if trials == 0:
raise Exception(f'Could not find {path} in {c.modules(path)} modules')
c.print(f'Could not find {path} in {c.modules(path)} modules, so we are updating the tree', color='red')
Expand Down
28 changes: 14 additions & 14 deletions commune/ticket/ticket.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import commune as c

class Ticket(c.Module):
seperator='<DATA::SIGNATURE>'
def create(self, key, seperator=seperator, tag=None, **kwargs):
seperator='<SIGNATURE>'
def create(self, key=None, seperator=seperator, tag=None, **kwargs):
key = c.get_key(key) if key else self.key
timestamp = str(c.time())
key = c.get_key(key)
c.print(key)
if tag:
timestamp += f'::{tag}'
return key.sign(timestamp, return_string=True, seperator=seperator)
return key.sign(timestamp, return_string=True, seperator=seperator) + '<ADDRESS>' + key.ss58_address

def verify(self, ticket, seperator=seperator):
assert seperator in ticket, f'No seperator found in {ticket}'
ticket, address = ticket.split('<ADDRESS>')
return c.verify(ticket, address=address, seperator=seperator)

@classmethod
def test(cls, key='test'):
self = cls()
ticket = self.create(key)
key = self.key
key = c.get_key(key)
c.print(ticket)
assert c.verify(ticket, key=key)
ticket2signer = self.ticket2signer(ticket)
c.print(ticket2signer)
assert ticket2signer == key.ss58_address
return {'ticket': ticket, 'key': key.ss58_address}
assert self.verify(ticket), 'Failed to verify'
return {'success': True, 'ticket': ticket, 'key': str(key)}

def ticket2signer(self, ticket = None, seperator=seperator, **kwargs):

return c.verify(ticket, return_address=True, seperator=seperator, **kwargs)
def qr(self,filename='ticket.png'):
return c.module('qrcode').text2qrcode(self.ticket(), filename=filename)
30 changes: 30 additions & 0 deletions modules/qrcode/qrcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import commune as c
# This code example demonstartes how to generate a QR code from Text.
# Initialize the BarcodeGenerator
# Specify Encode type

class Barcode(c.Module):
def __init__(self, a=1):
self.a = a
def call(self, b = 1):
return self.a + b

def text2qrcode(self, text='whadup', filename='barcode.png'):
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save(filename)
return filename

def install(self):
c.cmd('pip3 install qrcode[pil]', verbose=True)


0 comments on commit 5839ce6

Please sign in to comment.