Skip to content

Instantly share code, notes, and snippets.

@dmitry-vsl
Created March 10, 2021 11:40
Show Gist options
  • Save dmitry-vsl/317f9f17931a9501c98ab0d15893f6c7 to your computer and use it in GitHub Desktop.
Save dmitry-vsl/317f9f17931a9501c98ab0d15893f6c7 to your computer and use it in GitHub Desktop.
Perform Polkadot setKeys tx from python
import sys, json
from substrateinterface import SubstrateInterface, Keypair
from scalecodec.base import ScaleDecoder, RuntimeConfiguration, ScaleBytes
from scalecodec.type_registry import load_type_registry_preset
import requests
URL = 'wss://fullnode-relay.chachacha.centrifuge.io'
keysencoded = '...your keys from rotate keys here...'
# Decode session keys
RuntimeConfiguration().update_type_registry(load_type_registry_preset("default"))
RuntimeConfiguration().update_type_registry(load_type_registry_preset("polkadot"))
obj = ScaleDecoder.get_decoder_class('Keys', ScaleBytes(keysencoded))
obj.decode()
sessionkeys = obj.value
print('Session keys: ' + str(sessionkeys))
# Submit session keys as validator
with open('./validator_secret') as file:
validator_secret = file.read().strip()
keypair = Keypair.create_from_mnemonic(validator_secret)
substrate = SubstrateInterface(
url=URL,
ss58_format=42,
type_registry_preset='rococo',
)
call = substrate.compose_call(
call_module='Session',
call_function='set_keys',
call_params=dict(
keys=sessionkeys,
proof=''
)
)
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=keypair)
receipt = substrate.submit_extrinsic(extrinsic, wait_for_inclusion=True)
print('extrinsic_hash', receipt.extrinsic_hash, 'block_hash', receipt.block_hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment