@@ -1800,10 +1800,96 @@ async def sign(
1800
1800
)
1801
1801
1802
1802
signed_message = keypair .sign (message .encode ("utf-8" )).hex ()
1803
- console .print ("[dark_sea_green3]Message signed successfully:" )
1803
+ signer_address = keypair .ss58_address
1804
+ console .print ("[dark_sea_green3]Message signed successfully!\n " )
1805
+
1804
1806
if json_output :
1805
- json_console .print (json .dumps ({"signed_message" : signed_message }))
1806
- console .print (signed_message )
1807
+ json_console .print (
1808
+ json .dumps (
1809
+ {"signed_message" : signed_message , "signer_address" : signer_address }
1810
+ )
1811
+ )
1812
+ else :
1813
+ console .print (f"[yellow]Signature:[/yellow]\n { signed_message } " )
1814
+ console .print (f"[yellow]Signer address:[/yellow] { signer_address } " )
1815
+
1816
+
1817
+ async def verify (
1818
+ message : str ,
1819
+ signature : str ,
1820
+ public_key_or_ss58 : str ,
1821
+ json_output : bool = False ,
1822
+ ):
1823
+ """Verify a message signature using a public key or SS58 address."""
1824
+
1825
+ if is_valid_ss58_address (public_key_or_ss58 ):
1826
+ print_verbose (f"[blue]SS58 address detected:[/blue] { public_key_or_ss58 } " )
1827
+ keypair = Keypair (ss58_address = public_key_or_ss58 )
1828
+ signer_address = public_key_or_ss58
1829
+ else :
1830
+ try :
1831
+ public_key_hex = public_key_or_ss58 .strip ().lower ()
1832
+ if public_key_hex .startswith ("0x" ):
1833
+ public_key_hex = public_key_hex [2 :]
1834
+ if len (public_key_hex ) == 64 :
1835
+ bytes .fromhex (public_key_hex )
1836
+ print_verbose ("[blue]Hex public key detected[/blue] (64 characters)" )
1837
+ keypair = Keypair (public_key = public_key_hex )
1838
+ signer_address = keypair .ss58_address
1839
+ print_verbose (
1840
+ f"[blue]Corresponding SS58 address:[/blue] { signer_address } "
1841
+ )
1842
+ else :
1843
+ raise ValueError ("Public key must be 32 bytes (64 hex characters)" )
1844
+
1845
+ except (ValueError , TypeError ) as e :
1846
+ if json_output :
1847
+ json_console .print (
1848
+ json .dumps (
1849
+ {
1850
+ "verified" : False ,
1851
+ "error" : f"Invalid public key or SS58 address: { str (e )} " ,
1852
+ }
1853
+ )
1854
+ )
1855
+ else :
1856
+ err_console .print (
1857
+ f":cross_mark: Invalid SS58 address or hex public key (64 chars, with or without 0x prefix)- { str (e )} "
1858
+ )
1859
+ return False
1860
+
1861
+ try :
1862
+ signature_bytes = bytes .fromhex (signature .strip ().lower ().replace ("0x" , "" ))
1863
+ except ValueError as e :
1864
+ if json_output :
1865
+ json_console .print (
1866
+ json .dumps (
1867
+ {
1868
+ "verified" : False ,
1869
+ "error" : f"Invalid signature format: { str (e )} " ,
1870
+ }
1871
+ )
1872
+ )
1873
+ else :
1874
+ err_console .print (f"[red]:cross_mark: Invalid signature format: { str (e )} " )
1875
+ return False
1876
+
1877
+ is_valid = keypair .verify (message .encode ("utf-8" ), signature_bytes )
1878
+
1879
+ if json_output :
1880
+ json_console .print (
1881
+ json .dumps (
1882
+ {"verified" : is_valid , "signer" : signer_address , "message" : message }
1883
+ )
1884
+ )
1885
+ else :
1886
+ if is_valid :
1887
+ console .print ("[dark_sea_green3]Signature is valid!\n " )
1888
+ console .print (f"[yellow]Signer:[/yellow] { signer_address } " )
1889
+ else :
1890
+ err_console .print (":cross_mark: [red]Signature verification failed!" )
1891
+
1892
+ return is_valid
1807
1893
1808
1894
1809
1895
async def schedule_coldkey_swap (
0 commit comments