def derive_keypair(seed: str, validator: bool = False) -> Tuple[str, str]: """ Derive the public and private keys from a given seed value. Args: seed: Seed to derive the key pair from. Use :func:`generate_seed() <xrpl.core.keypairs.generate_seed>` to generate an appropriate value. validator: Whether the keypair is a validator keypair. Returns: A (public key, private key) pair derived from the given seed. Raises: XRPLKeypairsException: If the derived keypair did not generate a verifiable signature. """ decoded_seed, algorithm = addresscodec.decode_seed(seed) module = _ALGORITHM_TO_MODULE_MAP[algorithm] public_key, private_key = module.derive_keypair(decoded_seed, validator) signature = module.sign(_VERIFICATION_MESSAGE, private_key) if not module.is_valid_message(_VERIFICATION_MESSAGE, signature, public_key): raise XRPLKeypairsException( "Derived keypair did not generate verifiable signature", ) return public_key, private_key
def derive_keypair(seed: str, validator: bool = False) -> Tuple[str, str]: """ Given seed, which can be generated via `generate_seed`, returns public and private keypair. Args: seed: Value from which to derive keypair validator: Whether the keypair is a validator keypair. Returns: A public and private keypair. Raises: XRPLKeypairsException: If the derived keypair did not generate a verifiable signature. """ decoded_seed, algorithm = addresscodec.decode_seed(seed) module = _ALGORITHM_TO_MODULE_MAP[algorithm] public_key, private_key = module.derive_keypair(decoded_seed, validator) signature = module.sign(_VERIFICATION_MESSAGE, private_key) if not module.is_valid_message(_VERIFICATION_MESSAGE, signature, public_key): raise XRPLKeypairsException( "Derived keypair did not generate verifiable signature", ) return public_key, private_key
def test_seed_encode_decode_secp256k1(self): hex_string = "CF2DE378FBDD7E2EE87D486DFB5A7BFF" encoded_string = "sn259rEFXrQrWyx3Q7XneWcwV6dfL" hex_string_bytes = bytes.fromhex(hex_string) encode_result = addresscodec.encode_seed(hex_string_bytes, CryptoAlgorithm.SECP256K1) self.assertEqual(encode_result, encoded_string) decode_result, encoding_type = addresscodec.decode_seed(encoded_string) self.assertEqual(decode_result, hex_string_bytes) self.assertEqual(encoding_type, CryptoAlgorithm.SECP256K1)
def test_seed_encode_decode_ed25519_high(self): hex_string = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" encoded_string = "sEdV19BLfeQeKdEXyYA4NhjPJe6XBfG" hex_string_bytes = bytes.fromhex(hex_string) encode_result = addresscodec.encode_seed(hex_string_bytes, CryptoAlgorithm.ED25519) self.assertEqual(encode_result, encoded_string) decode_result, encoding_type = addresscodec.decode_seed(encoded_string) self.assertEqual(decode_result, hex_string_bytes) self.assertEqual(encoding_type, CryptoAlgorithm.ED25519)
def test_seed_encode_decode_ed25519_low(self): hex_string = "00000000000000000000000000000000" encoded_string = "sEdSJHS4oiAdz7w2X2ni1gFiqtbJHqE" hex_string_bytes = bytes.fromhex(hex_string) encode_result = addresscodec.encode_seed(hex_string_bytes, CryptoAlgorithm.ED25519) self.assertEqual(encode_result, encoded_string) decode_result, encoding_type = addresscodec.decode_seed(encoded_string) self.assertEqual(decode_result, hex_string_bytes) self.assertEqual(encoding_type, CryptoAlgorithm.ED25519)
def test_seed_encode_decode_ed25519(self): hex_string = "4C3A1D213FBDFB14C7C28D609469B341" encoded_string = "sEdTM1uX8pu2do5XvTnutH6HsouMaM2" hex_string_bytes = bytes.fromhex(hex_string) encode_result = addresscodec.encode_seed(hex_string_bytes, CryptoAlgorithm.ED25519) self.assertEqual(encode_result, encoded_string) decode_result, encoding_type = addresscodec.decode_seed(encoded_string) self.assertEqual(decode_result, hex_string_bytes) self.assertEqual(encoding_type, CryptoAlgorithm.ED25519)
def test_seed_encode_decode_secp256k1_high(self): hex_string = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" encoded_string = "saGwBRReqUNKuWNLpUAq8i8NkXEPN" hex_string_bytes = bytes.fromhex(hex_string) encode_result = addresscodec.encode_seed(hex_string_bytes, CryptoAlgorithm.SECP256K1) self.assertEqual(encode_result, encoded_string) decode_result, encoding_type = addresscodec.decode_seed(encoded_string) self.assertEqual(decode_result, hex_string_bytes) self.assertEqual(encoding_type, CryptoAlgorithm.SECP256K1)
def test_seed_encode_decode_secp256k1_low(self): hex_string = "00000000000000000000000000000000" encoded_string = "sp6JS7f14BuwFY8Mw6bTtLKWauoUs" hex_string_bytes = bytes.fromhex(hex_string) encode_result = addresscodec.encode_seed(hex_string_bytes, CryptoAlgorithm.SECP256K1) self.assertEqual(encode_result, encoded_string) decode_result, encoding_type = addresscodec.decode_seed(encoded_string) self.assertEqual(decode_result, hex_string_bytes) self.assertEqual(encoding_type, CryptoAlgorithm.SECP256K1)