def test_from_seed_keypair_invalid_seed(self): with self.assertRaises(nkeys.ErrInvalidSeed): seed = "AUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" nkeys.from_seed(bytearray(seed.encode())) with self.assertRaises(nkeys.ErrInvalidSeed): seed = "" nkeys.from_seed(bytearray(seed.encode()))
def test_from_seed_keypair_valid_prefix_byte(self): seeds = [ "SNAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU", "SCAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU", "SOAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU", "SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" ] for seed in seeds: nkeys.from_seed(bytearray(seed.encode()))
def test_from_seed_keypair_invalid_public_prefix_byte(self): seeds = [ b'SBAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU', b'SDAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU', b'PWAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU', b'PMAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU' ] with self.assertRaises(nkeys.ErrInvalidPrefixByte): for seed in seeds: nkeys.from_seed(bytearray(seed))
def test_keypair_sign_nonce(self): seed = "SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" kp = nkeys.from_seed(bytearray(seed.encode())) raw = kp.sign(b"PXoWU7zWAMt75FY") sig = base64.b64encode(raw) self.assertEqual( sig, b'ZaAiVDgB5CeYoXoQ7cBCmq+ZllzUnGUoDVb8C7PilWvCs8XKfUchAUhz2P4BYAF++Dg3w05CqyQFRDiGL6LrDw==' )
def test_keypair_use_seed_to_verify_signature(self): seed = "SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" encoded_seed = bytearray(seed.encode()) kp = nkeys.from_seed(encoded_seed) nonce = b'NcMQZSlX2lZ3Y4w' sig = kp.sign(nonce) self.assertTrue(kp.verify(nonce, sig)) with self.assertRaises(nkeys.ErrInvalidSignature): kp.verify(nonce + b'asdf', sig)
def test_keypair_wipe(self): seed = "SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" kp = nkeys.from_seed(bytearray(seed.encode())) self.assertTrue(kp._keys is not None) kp.wipe() with self.assertRaises(AttributeError): kp._keys with self.assertRaises(AttributeError): kp._seed
def run(): parser = argparse.ArgumentParser() parser.add_argument('--inkey', default='') parser.add_argument('--sign', default='') parser.add_argument('--sigfile', default='') parser.add_argument('--verify', default='') args = parser.parse_args() if len(args.inkey) <= 0: show_usage_and_die() # Create keypair from seed. seed = None with open(args.inkey, 'rb', buffering=0) as f: seed = bytearray(os.fstat(f.fileno()).st_size) f.readinto(seed) if len(args.sign) > 0: data = '' with open(args.sign, 'rb', buffering=0) as f: data = f.read() user = nkeys.from_seed(seed) signed = user.sign(data) result = base64.b64encode(signed) print(result.decode()) sys.exit(0) if len(args.verify) > 0: data = '' with open(args.verify, 'rb', buffering=0) as f: data = f.read() signed_data = '' with open(args.sigfile, 'rb', buffering=0) as f: encoded_data = f.read() signed_data = base64.b64decode(encoded_data) user = nkeys.from_seed(seed) if user.verify(data, signed_data): print("Verified OK") sys.exit(0)
def test_keypair_public_key(self): seed = "SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" encoded_seed = bytearray(seed.encode()) kp = nkeys.from_seed(encoded_seed) self.assertEqual(None, kp._public_key) self.assertEqual( "UCK5N7N66OBOINFXAYC2ACJQYFSOD4VYNU6APEJTAVFZB2SVHLKGEW7L", kp.public_key) # Confirm that the public key is wiped as well. kp.wipe() with self.assertRaises(AttributeError): kp._public_key
def test_keypair_seed_property(self): seed = bytearray( b"SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU") kp = nkeys.from_seed(seed) self.assertEqual( kp.seed, bytearray( b"SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU")) # Throw away the seed. kp.wipe() with self.assertRaises(nkeys.ErrInvalidSeed): kp.seed
def test_keypair_private_key(self): seed = "SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" encoded_seed = bytearray(seed.encode()) kp = nkeys.from_seed(encoded_seed) self.assertEqual(None, kp._public_key) priv = kp.private_key self.assertEqual( b"PDC2WWLK67NUTFW7ZH5A7FOPZC32VXYZYWYNQMQ6RQWP2FEEF6KDVFOW7W7PHAXEGS3QMBNABEYMCZHB6K4G2PAHSEZQKS4Q5JKTVVDCJORA", priv) # Confirm that the private_key is wiped as well. kp.wipe() with self.assertRaises(AttributeError): kp._keys with self.assertRaises(AttributeError): kp._private_key
def test_from_seed_keypair(self): seed = "SUAMLK2ZNL35WSMW37E7UD4VZ7ELPKW7DHC3BWBSD2GCZ7IUQQXZIORRBU" kp = nkeys.from_seed(bytearray(seed.encode())) self.assertTrue(type(kp) is nkeys.KeyPair)