def FromSeed(cls, seed_bytes: bytes, coin_type: SubstrateCoins) -> Substrate: """ Create a Substrate object from the specified seed. Args: seed_bytes (bytes) : Seed bytes coin_type (SubstrateCoins): Coin type Returns: Substrate object: Substrate object Raises: TypeError: If coin_type is not of SubstrateCoins enum ValueError: If the seed length is not valid """ if len(seed_bytes) < SubstrateConst.SEED_BYTE_LEN: raise ValueError( f"Seed length is too small, it shall be at least {SubstrateConst.SEED_BYTE_LEN} bytes" ) pub_key_bytes, priv_key_bytes = sr25519.pair_from_seed( seed_bytes[:SubstrateConst.SEED_BYTE_LEN]) return cls(priv_key=priv_key_bytes, pub_key=pub_key_bytes, path=SubstratePath(), coin_conf=SubstrateConfGetter.GetConfig(coin_type))
def test_derive_soft(self): # Get private and public key from seed public_key, private_key = sr25519.pair_from_seed(bytes(self.seed)) # Private derivation child_chain_priv, child_pubkey_priv, child_privkey = sr25519.derive_keypair( (self.chain_code, public_key, private_key), self.child_index ) # Public derivation child_chain_pub, child_pubkey_pub = sr25519.derive_pubkey( (self.chain_code, public_key), self.child_index ) # Assert that the chain code and public key are the same regardless of # derivation method self.assertEqual(child_chain_priv, child_chain_pub) self.assertEqual(child_pubkey_priv, child_pubkey_pub) # Test that signatures with the derived private key are valid signature = sr25519.sign( (child_pubkey_priv, child_privkey), self.message ) self.assertTrue(sr25519.verify(signature, self.message, child_pubkey_pub))
def setup_arbitrator(self, arbitrator_key: str): """ Set up constants required for arbitrator functionality """ self.keypair = sr25519.pair_from_seed(bytes.fromhex(arbitrator_key)) self.arbitrator_account_id = self.keypair[0].hex() self.arbitrator_address = ss58_encode(self.keypair[0], ss58_format=self.address_type)
def test_sign_and_verify_message(self): # Get private and public key from seed public_key, private_key = sr25519.pair_from_seed(bytes(self.seed)) # Generate signature signature = sr25519.sign((public_key, private_key), self.message) # Verify message with signature self.assertTrue(sr25519.verify(signature, self.message, public_key))
def test_sign_0x_payload(self): payload = ( "0x04005a989f0526b8a619b90205b6c3bef293f7b0c38fae7353afe10feae4c4712" "55b0700e40b540200100026040000b0a8d493285c2df73290dfb7e61f870f17b418" "01197a149ca93654499ea3dafeb0a8d493285c2df73290dfb7e61f870f17b418011" "97a149ca93654499ea3dafe" ) signer_hex = "52bb9cee00b1f93a8ff5c022360c97457a7bd1e7c9387002728cac022aedf1b0" keypair = sr25519.pair_from_seed(bytes.fromhex(signer_hex)) result = sign_payload(keypair, payload) assert len(result) == 128
def __init__(self, address_type=0, *, mnemonic=None, hex=None): if mnemonic: seed_bytes = bip39.bip39_to_mini_secret(mnemonic, "") seed_hex = bytearray(seed_bytes).hex() self.hex = seed_hex if hex: self.hex = hex self.keypair = sr25519.pair_from_seed(bytes.fromhex(self.hex)) self.public_key = self.keypair[0].hex() self.private_key = self.keypair[1].hex() self.address = ss58_encode(self.keypair[0], ss58_format=address_type)
def test_derive_hard(self): # Get private and public key from seed public_key, private_key = sr25519.pair_from_seed(bytes(self.seed)) # Private derivation _, child_pubkey, child_privkey = sr25519.hard_derive_keypair( (self.chain_code, public_key, private_key), self.child_index) # Test that signatures with the derived private key are valid signature = sr25519.sign((child_pubkey, child_privkey), self.message) self.assertTrue(sr25519.verify(signature, self.message, child_pubkey))