Skip to content

Commit

Permalink
Merge pull request #56 from xian-network/bugfix-validator
Browse files Browse the repository at this point in the history
Bugfix validator
  • Loading branch information
crosschainer authored Apr 1, 2024
2 parents acafc52 + a0edf76 commit f0c1439
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
43 changes: 27 additions & 16 deletions src/xian/tools/validator_file_gen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from argparse import ArgumentParser
from nacl.public import PrivateKey
from base64 import b64encode
from nacl.signing import SigningKey
from nacl.encoding import HexEncoder, Base64Encoder
import json
import hashlib

Expand All @@ -15,30 +15,41 @@ def __init__(self):
self.args = self.parser.parse_args()

def generate_keys(self):
validator_sk_hex = bytes.fromhex(self.args.validator_privkey)
validator_sk = PrivateKey(validator_sk_hex)
pk_hex = self.args.validator_privkey

validator_pub_key = validator_sk.public_key

# Convert hex private key to bytes and generate signing key object
signing_key = SigningKey(pk_hex, encoder=HexEncoder)

# Obtain the verify key (public key) from the signing key
verify_key = signing_key.verify_key

validator_pub_key_b64 = b64encode(validator_pub_key._public_key).decode('utf-8')
validator_sk_b64 = b64encode(validator_sk._private_key + validator_pub_key._public_key).decode('utf-8')
# Concatenate private and public key bytes
priv_key_with_pub = signing_key.encode() + verify_key.encode()

validator_pub_key_hash = hashlib.sha256(validator_pub_key._public_key).digest()[:20]
validator_address = validator_pub_key_hash.hex().upper()
# Encode concatenated private and public keys in Base64 for the output
priv_key_with_pub_b64 = Base64Encoder.encode(priv_key_with_pub).decode('utf-8')

# Encode public key in Base64 for the output
public_key_b64 = verify_key.encode(encoder=Base64Encoder).decode('utf-8')

return {
'address': validator_address,
'pub_key': {
'type': 'tendermint/PubKeyEd25519',
'value': validator_pub_key_b64
# Hash the public key using SHA-256 and take the first 20 bytes for the address
address_bytes = hashlib.sha256(verify_key.encode()).digest()[:20]
address = address_bytes.hex().upper()

output = {
"address": address,
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": public_key_b64
},
'priv_key': {
'type': 'tendermint/PrivKeyEd25519',
'value': validator_sk_b64
'value': priv_key_with_pub_b64
}
}

return output

def main(self):

if len(self.args.validator_privkey) != 64:
Expand Down
4 changes: 3 additions & 1 deletion src/xian/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ def tx_hash_from_tx(tx):

def hash_from_validator_updates(validator_updates):
h = hashlib.sha3_256()
encoded_validator_updates = encode(validator_updates).encode()
# Sort the list of validator updates to ensure that the hash is consistent
validator_updates = sorted(validator_updates)
encoded_validator_updates = str(validator_updates).encode()
h.update(encoded_validator_updates)
return h.hexdigest()

Expand Down

0 comments on commit f0c1439

Please sign in to comment.