如何使用Python对58链ID进行编码?
3 个回答
- 投票数
-
- 2019-02-17
您的magicbyte似乎是错误的.如果您从原始数据中获取十进制字节值,然后将其转换为十六进制,然后用前导零填充,您将得到
>>> struct.unpack('>L', b'\x00\x57\x52\x00')[0] 5722624
此值应产生预期的结果:
>>> payload = '023bb717ee882891d7be5b881cefa98946800e3d67b5d01b4237b3618709defb51ec37c3e100000518ae' >>> mb = struct.unpack('>L', b'\x00\x57\x52\x00')[0] >>> chainid = bytes.fromhex(payload[2:10]) >>> bitcoin.bin_to_b58check(chainid, magicbyte=mb) 'NetXSzLHKwSumh7'
Your magicbyte seems to be wrong. If you take the decimal byte values from the original, convert them to hex, then pad it with a leading zero, you get
>>> struct.unpack('>L', b'\x00\x57\x52\x00')[0] 5722624
This value should produce the expected result:
>>> payload = '023bb717ee882891d7be5b881cefa98946800e3d67b5d01b4237b3618709defb51ec37c3e100000518ae' >>> mb = struct.unpack('>L', b'\x00\x57\x52\x00')[0] >>> chainid = bytes.fromhex(payload[2:10]) >>> bitcoin.bin_to_b58check(chainid, magicbyte=mb) 'NetXSzLHKwSumh7'
-
是的5722624是正确的magicbyte:Yeah 5722624 is the right magicbyte:
- 1
- 2019-02-17
- Stephen Andrews
-
- 2019-02-16
看起来您实际上只是在获取2个字节的数据(4个十六进制字符).我通过解码得到的结果来验证这一点,对于给定的魔术字节,它仅返回两个字节的数据.
尝试进行以下更改:
def get_chain_id(self): chainid = bytes.fromhex(self.payload[2:10]) return bitcoin.bin_to_b58check(chainid, magicbyte=5722583)
It looks like you are only actually grabbing 2 bytes of data (4 hex chars). I verified this by decoding the result you got, and it only returning two bytes of data for the given magic byte.
Try making the following change:
def get_chain_id(self): chainid = bytes.fromhex(self.payload[2:10]) return bitcoin.bin_to_b58check(chainid, magicbyte=5722583)
-
谢谢,这是问题的一部分-我需要获取8个字符(4个十六进制字节)或`self.payload [2:10]`.在Zeronet上,背书有效载荷看起来像这样:023bb717ee882891d7be5b881cefa98946800e3d67b5d01b4237b3618709defb51ec37c3e100000518ae,其中3bb717ee是链ID,但可以转换为Net1BPz7FKbUzsXNetXBPnet7SX,应为Net1BPz7FKbUzwXSNet. 我在上面编辑了问题.再次感谢你的帮助.Thanks, this is part of the problem - I need to grab 8 characters (4 hex bytes), or `self.payload[2:10]`. On zeronet, an endorsement payload looks like this: `023bb717ee882891d7be5b881cefa98946800e3d67b5d01b4237b3618709defb51ec37c3e100000518ae`, where `3bb717ee` is the chain id, but that converts to `Net1BPz7FKbUqsY` and it should be `NetXSzLHKwSumh7` on zeronet. I edited the question above a bit. Thanks again for all your help.
- 1
- 2019-02-17
- Luke Youngblood
-
- 2019-02-26
首先,非常感谢!您已经帮助我解决了区块签名之谜:) 您可以使用pytezos.encoding 包:
来自pytezos.encoding的from pytezos.encoding import base58_encode def get_chain_id(self): chainid = bytes.fromhex(self.payload[2:10]) return base58_encode(chain_id, b'Net')
First of all, many thanks! You've helped me with solving the block signature mystery :) You can use pytezos.encoding package:
from pytezos.encoding import base58_encode def get_chain_id(self): chainid = bytes.fromhex(self.payload[2:10]) return base58_encode(chain_id, b'Net')
当Tezos需要签署烘焙或背书操作有效负载时,字节0对于烘焙操作将为0x01,对于背书操作将为0x02.操作有效负载中的字节1-5包含链ID(也称为网络),可以使用此规范:
生成的链ID将是类似于
NetXdQprcVkpaWU
的字符串(截至2019年2月16日,当前处于活动状态的主网链ID).如何使用Vitalik的pybitcointools模块将字节1-5中的4字节字段转换为base58编码的Net(15)格式?
我尝试使用
scripts/b58_prefix.py
中的脚本来确定要传递给bitcoin.bin_to_b58check
函数的适当魔术字节,但我没有得到适当的结果:这是我的代码:
在零网络上,认可有效载荷如下所示:
023bb717ee882891d7be5b881cefa98946800e3d67b5d01b4237b3618709defb51ec37c3e100000518ae
,其中3bb717ee
是链ID,但会转换为NetbBPz7应该是零网络上的
NetXSzLHKwSumh7
.