eth_keys 如何获取字符串 public 键以便我可以将其放入 JSON
eth_keys how to get a string public key so that I can put it into JSON
我有一个私人的 - public 密钥签名可以正常工作:
print(signature.verify_msg(bytes(transaction_hash.hexdigest().encode('utf-8')), first_wallet['public_key']))
verify_signature:
0xf9671943931afb93e1613eb5d73a77ed961653f70562884709d45ffa774fa7f756b7e8b8a4bd11ffe71448eedddfc10e7dbcd4b069477c7665e6a978347b6e42
True
但是我需要json 序列化 public 密钥以便节点可以进行验证,我已经尝试了很多东西。第一个错误是:
TypeError: Object of type 'PublicKey' is not JSON serializable
然后,如果我用 first_wallet['public_key'].to_hex()
序列化它,当节点使用那个 public 密钥来验证我得到的消息时:
eth_keys.exceptions.ValidationError: Unexpected uncompressed public
key length: Expected 64, but got 130 bytes
你能不能把它序列化成字节,它可能正在读取十六进制的长度。我在下面有几个示例是 json 可序列化的,服务器可能除外。在不知道你在另一端连接哪个库的情况下,我决定展示几个例子。
In [587]: bytes.fromhex(hex(first_wallet['public_key'])[2:])
Out[587]: b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB'
In [572]: len('0xf9671943931afb93e1613eb5d73a77ed961653f70562884709d45ffa774fa7f756b7e8b8a4bd11ffe71448eedddfc10e7dbcd4b069477c7665e6a978347b6e42')
Out[572]: 130
In [573]: len(b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB')
Out[573]: 64
encoded = base64.encodebytes( b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB')
encoded
b'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n'
encoded.decode('ascii')
'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n'
In [12]: json.dumps('+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n')
Out[12]: '"+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n"'
或
base64.b64encode(b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\x
...: ee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB')
b'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg=='
json.dumps(b'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg==')
In [22]: json.dumps('+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg==')
Out[22]: '"+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg=="'
In [24]: base64.b64decode('"+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg=="')
Out[24]: b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB'
我有一个私人的 - public 密钥签名可以正常工作:
print(signature.verify_msg(bytes(transaction_hash.hexdigest().encode('utf-8')), first_wallet['public_key']))
verify_signature: 0xf9671943931afb93e1613eb5d73a77ed961653f70562884709d45ffa774fa7f756b7e8b8a4bd11ffe71448eedddfc10e7dbcd4b069477c7665e6a978347b6e42
True
但是我需要json 序列化 public 密钥以便节点可以进行验证,我已经尝试了很多东西。第一个错误是:
TypeError: Object of type 'PublicKey' is not JSON serializable
然后,如果我用 first_wallet['public_key'].to_hex()
序列化它,当节点使用那个 public 密钥来验证我得到的消息时:
eth_keys.exceptions.ValidationError: Unexpected uncompressed public key length: Expected 64, but got 130 bytes
你能不能把它序列化成字节,它可能正在读取十六进制的长度。我在下面有几个示例是 json 可序列化的,服务器可能除外。在不知道你在另一端连接哪个库的情况下,我决定展示几个例子。
In [587]: bytes.fromhex(hex(first_wallet['public_key'])[2:])
Out[587]: b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB'
In [572]: len('0xf9671943931afb93e1613eb5d73a77ed961653f70562884709d45ffa774fa7f756b7e8b8a4bd11ffe71448eedddfc10e7dbcd4b069477c7665e6a978347b6e42')
Out[572]: 130
In [573]: len(b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB')
Out[573]: 64
encoded = base64.encodebytes( b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB')
encoded
b'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n'
encoded.decode('ascii')
'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n'
In [12]: json.dumps('+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n')
Out[12]: '"+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl\n5ql4NHtuQg==\n"'
或
base64.b64encode(b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\x
...: ee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB')
b'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg=='
json.dumps(b'+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg==')
In [22]: json.dumps('+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg==')
Out[22]: '"+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg=="'
In [24]: base64.b64decode('"+WcZQ5Ma+5PhYT611zp37ZYWU/cFYohHCdRf+ndPp/dWt+i4pL0R/+cUSO7d38EOfbzUsGlHfHZl5ql4NHtuQg=="')
Out[24]: b'\xf9g\x19C\x93\x1a\xfb\x93\xe1a>\xb5\xd7:w\xed\x96\x16S\xf7\x05b\x88G\t\xd4_\xfawO\xa7\xf7V\xb7\xe8\xb8\xa4\xbd\x11\xff\xe7\x14H\xee\xdd\xdf\xc1\x0e}\xbc\xd4\xb0iG|ve\xe6\xa9x4{nB'