Skip to content

Commit

Permalink
perf: make signature verify/generate polkadotjs compatible (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvis8x7b authored Oct 8, 2024
1 parent 83bb372 commit 0c0c945
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions verify/generate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from substrateinterface import Keypair
from os import getenv, environ
from datetime import datetime

import bittensor

# Hardcode or set the environment variable WALLET_PASS to the password for the wallet
Expand All @@ -14,14 +13,15 @@ def main(args):
timestamp = datetime.now()
timezone = timestamp.astimezone().tzname()

message = f"On {timestamp} {timezone} {args.message}"
# ensure compatiblity with polkadotjs messages, as polkadotjs always wraps message
message = "<Bytes>" + f"On {timestamp} {timezone} {args.message}" + "</Bytes>"
signature = keypair.sign(data=message)

file_contents = f"{message}\n\tSigned by: {keypair.ss58_address}\n\tSignature: {signature.hex()}"
print(file_contents)
open("message_and_signature.txt", "w").write(file_contents)

print(f"Signature generated and saved to message_and_signature.txt")
print("Signature generated and saved to message_and_signature.txt")


if __name__ == "__main__":
Expand Down
9 changes: 5 additions & 4 deletions verify/verify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from substrateinterface import Keypair
from binascii import unhexlify

from substrateinterface import Keypair


def main(args):
file_data = open(args.file).read()
Expand All @@ -16,6 +17,8 @@ def main(args):
keypair = Keypair(ss58_address=address, ss58_format=42)

message = file_split[0]
if not message.startswith("<Bytes>") or not message.endswith("</Bytes>"):
raise ValueError("Message is not properly wrapped in <Bytes>.")

signature_line = file_split[2]
signature_prefix = "Signature: "
Expand All @@ -36,8 +39,6 @@ def main(args):
import argparse

parser = argparse.ArgumentParser(description="Verify a signature")
parser.add_argument(
"--file", help="The file containing the message and signature"
)
parser.add_argument("--file", help="The file containing the message and signature")
args = parser.parse_args()
main(args)

0 comments on commit 0c0c945

Please sign in to comment.