-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
latentvector
committed
May 15, 2024
1 parent
d9b3f86
commit 5839ce6
Showing
5 changed files
with
54 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|