github.com/platonnetwork/platon-go@v0.7.6/cases/common/key.py (about) 1 import os 2 import sys 3 from common.abspath import abspath 4 from eth_keys import ( 5 keys, 6 ) 7 from eth_utils.curried import ( 8 keccak, 9 text_if_str, 10 to_bytes, 11 ) 12 13 14 def generate_key(): 15 """ 16 generate node public private key 17 :return: 18 privateKey 19 publicKey 20 """ 21 extra_entropy = '' 22 extra_key_bytes = text_if_str(to_bytes, extra_entropy) 23 key_bytes = keccak(os.urandom(32) + extra_key_bytes) 24 privatekey = keys.PrivateKey(key_bytes) 25 return privatekey.to_hex()[2:], keys.private_key_to_public_key(privatekey).to_hex()[2:] 26 27 28 def generate_blskey(): 29 """ 30 generate bls public and private keys 31 :return: 32 bls_private_key 33 bls_public_key 34 """ 35 if sys.platform in "linux,linux2": 36 tool_file = abspath("tool/linux/keytool") 37 run("chmod +x {}".format(tool_file)) 38 else: 39 tool_file = abspath("tool/win/keytool.exe") 40 keypair = run("{} genblskeypair".format(tool_file)) 41 if not keypair: 42 raise Exception("unable to use generate blskey tool") 43 lines = keypair.split("\n") 44 bls_private_key = "" 45 bls_public_key = "" 46 for l in lines: 47 kv = l.split(":") 48 if kv[0] == "PrivateKey": 49 bls_private_key = kv[1].strip() 50 elif kv[0] == "PublicKey ": 51 bls_public_key = kv[1].strip() 52 if not bls_private_key or not bls_public_key: 53 raise Exception("Blskey cannot be generated") 54 return bls_private_key, bls_public_key 55 56 57 def run(cmd): 58 """ 59 The machine executes the cmd command and gets the result 60 :param cmd: 61 :return: 62 """ 63 r = os.popen(cmd) 64 out = r.read() 65 r.close() 66 return out 67 68 69 def get_pub_key(url, block): 70 """ 71 obtain signature nodes based on block information 72 :param url: node url 73 :param block: block height 74 :return: 75 """ 76 if sys.platform in "linux,linux2": 77 tool_file = abspath("tool/linux/get_pubkey_for_blocknum") 78 run("chmod +x {}".format(tool_file)) 79 else: 80 tool_file = abspath("tool/win/get_pubkey_for_blocknum.exe") 81 output = run("{} -url={} -blockNumber={}".format(tool_file, url, block)) 82 if not output: 83 raise Exception("unable to use get node id tool") 84 if "1111" in output or "2222" in output: 85 raise Exception("get node id exception:{}".format(output)) 86 return output.strip("\n") 87 88 89 def mock_duplicate_sign(dtype, sk, blskey, block_number, epoch=0, view_number=0, block_index=0, index=0): 90 """ 91 forged double sign 92 :param dtype: 93 :param sk: 94 :param blskey: 95 :param block_number: 96 :param epoch: 97 :param view_number: 98 :param block_index: 99 :param index: 100 :return: 101 """ 102 if sys.platform in "linux,linux2": 103 tool_file = abspath("tool/linux/duplicateSign") 104 run("chmod +x {}".format(tool_file)) 105 else: 106 tool_file = abspath("tool/win/duplicateSign.exe") 107 108 output = run("{} -dtype={} -sk={} -blskey={} -blockNumber={} -epoch={} -viewNumber={} -blockIndex={} -vindex={}".format( 109 tool_file, dtype, sk, blskey, block_number, epoch, view_number, block_index, index)) 110 if not output: 111 raise Exception("unable to use double sign tool") 112 return output.strip("\n") 113 114 115 if __name__ == "__main__": 116 print(generate_blskey())