-
Notifications
You must be signed in to change notification settings - Fork 2
Trying to deserialize Signed Transaction with Program #1
Comments
You can refer this:
https://github.com/yuan-xy/libra-client/blob/master/libra/transaction.py
and the test code:
https://github.com/yuan-xy/libra-client/blob/master/test/test_canoser.py
…________________________________
发件人: Gie Katon <[email protected]>
发送时间: Thursday, September 26, 2019 5:13:34 PM
收件人: yuan-xy/canoser-python <[email protected]>
抄送: Subscribed <[email protected]>
主题: [yuan-xy/canoser-python] Trying to deserialize Signed Transaction with Proof (#1)
Hi and thanks for the library.
I'm trying to deserialize a signed transaction with the program, but getting the following error, maybe you see what's wrong and can help me.
Error:
...Python\Python37\site-packages\canoser\rust_enum.py", line 69, in decode
_name, datatype = cls._enums[index]
IndexError: list index out of range
My signed txn is this:
signed_txn = "200000003A24A61E05D129CACE9E0EFC8BC9E33831FEC9A9BE66F50FD352A2638A49B9EE200000000000000000000000040000006D6F766502000000020000000900000043414645204430304402000000090000006361666520643030640300000001000000CA02000000FED0010000000D1027000000000000204E0000000000008051010000000000"
I have defined structs like this:
class Addr(Struct):
_fields = [
('addr', [Uint8, 32])
]
class TransactionArgument(RustEnum):
_enums = [
('U64', Uint64),
('Address', Addr),
('ByteArray', [Uint8]),
('String', str),
]
class TransactionProgram(Struct):
_fields = [
( 'code', [Uint8]),
( 'args', TransactionArgument),
( 'modules', [Uint8] ),
]
class Raw(Struct):
_fields = [
('sender', Addr),
('sequence_number', Uint64),
('payload', TransactionProgram),
('max_gas_amount', Uint64),
('gas_unit_price', Uint64),
('expiration_time', Uint64),
]
And the rest of the code:
def test():
txn_bytes = bytes.fromhex(signed_txn)
result = Raw.deserialize(txn_bytes)
print(result)
test()
―
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#1?email_source=notifications&email_token=AAC55MWO5NXTJCDHYPPIVPTQLR4L5A5CNFSM4I2XI272YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HN2PIQQ>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAC55MVAAPDCJUPAWO4F22TQLR4L5ANCNFSM4I2XI27Q>.
|
Added detailed doc here: https://github.com/yuan-xy/canoser-python#must-define-canoser-struct-by-serialized-fields-and-sequence-not-the-definition-in-the-rust-struct |
Hi, thank you for the references and documentation updates. I'm learning and trying to run the LCS, but not quite there yet. I see that in your So I'm using your
When I run it, I get the error:
Maybe it's because my txn is a RawTransaction with a Program (as in Libra LCS specification), and not with a WriteSet. Do I need to specify this in structs? If not, any ideas what I'm doing wrong? |
The above |
Maybe you can tell me the transaction id, so I can figure out what's the problem there. |
Please check my previous comment. It's an example transaction from the readme. At the very bottom of that page it's deserialized. |
You are right. The above signed_txn is a RawTransaction.
It works fine. |
It works with RawTransaction.deserial... (without "libra." at the beginning) When I print the deserialized result, I get the below message. How can I turn it into an object like representation, as it is in the LCS readme? I'm a Python newbie ツ I will need to return it to my Node.js client from a spawned Python module.
|
I'm writing a str method to pretty print this object, comming soon |
Thank you very much. Looking forward to it! Also, maybe this is something you know, and it's an easy answer. To query a custom txn by range, I use a grpc client and the returned signed_txn looks like this:
This doesn't look like the LCS format and it doesn't work with Does it need to be first decoded into an LCS representation, or is it more likely that my grpc client is returning it in the wrong format? When querying directly from Rust CLI, the same txn looks like below:
|
Update canoser to 0.3.4, add simple pretty print support. |
after get a proto class
you can refer to this code: |
Pretty printing works great. 非常感谢你. And thanks for the reference regarding the deserialization of the signed_txn. I think I will finish everything tomorrow, now time sleep. |
Hi, regarding my second question, in your file
The Any ideas? |
The input of deserialize method should be bytes type, not str type.
在 2019年9月27日,16:43,Gie Katon <[email protected]<mailto:[email protected]>> 写道:
Hi, regarding my second question, in your file test_client.py, I see that your client gets the signed_txn and decodes it here:
def test_get_transaction():
c = libra.Client("testnet")
txn = c.get_transaction(1)
assert len(txn.signed_txn) > 0
stx = SignedTransaction.deserialize(txn.signed_txn)
The signed_txn returned from my Node.js grpc client is in the format as I mentioned above and if I try to deserialize it with SignedTransaction.deserialize(signed_txn), I get this error: TypeError: a bytes-like object is required, not 'str'.
Any ideas?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#1?email_source=notifications&email_token=AAC55MTKUSUXO36WBJ3363LQLXBSZA5CNFSM4I2XI272YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7YG6JY#issuecomment-535850791>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAC55MRDUGL7IBYO5JCMRXDQLXBSZANCNFSM4I2XI27Q>.
|
Can I convert it to bytes with Python, or should the grpc client return it in the right format? Below in the comments you can see the errors I get when trying to decode it from string.
|
It’s not a hex str, so you can’t call bytes.fromhex. Hex str only contain 0-9a-f. Where did you get this signed_txn? You can use struct.unpack to parse it with correct format.
在 2019年9月27日,17:42,Gie Katon <[email protected]<mailto:[email protected]>> 写道:
Can I convert it to bytes with Python, or should the grpc client return it in the right format?
Below in the comments you can see the errors I get when trying to decode it from string.
signed_txn = "IAAAAB/HpLI25vah9LY90dFahBXFrtJYvHxrTT2+jxUQSw6SAQAAAAAAAAACAAAAuAAAAExJQlJBVk0KAQAHAUoAAAAEAAAAA04AAAAGAAAADVQAAAAGAAAADloAAAAGAAAABWAAAAApAAAABIkAAAAgAAAACKkAAAAPAAAAAAAAAQACAAEDAAIAAgQCAAMCBAIDAAY8U0VMRj4MTGlicmFBY2NvdW50BG1haW4PcGF5X2Zyb21fc2VuZGVyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQACAAQADAAMARMBAQICAAAAAQAAACAAAACbF/h+UR+X4dxOXN5KqU17lMJVNBEMJsl85hiXNVgvwAAAAABAQg8AAAAAAOAiAgAAAAAAAAAAAAAAAAB5KotdAAAAACAAAADGMHrvNmuIrxuW5cmzptu7rmo/bCOohwQSSpRMv/zmdkAAAADKZ+SsmtKC07wa7vK3nYVdkkeoLEmrpxCr/zw6T50jmB6TNp0XNpCT8D1p6JSQ5TlOYEyPvjZjl3yS0lHmps0D"
signed_txn_bytes = bytes.fromhex(signed_txn)
# ERROR: ValueError: non-hexadecimal number found in fromhex() arg at position 0
# signed_txn_bytes = signed_txn.encode()
# The deserializer accepts the encoded string, but it is not in the LCS format, so returns the ERROR: TypeError: 1094795593 is not equal to predefined value: 32
des = SignedTransaction.deserialize(signed_txn_bytes)
print(des)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#1?email_source=notifications&email_token=AAC55MQLPLQK3NPOCK3GL2DQLXIRNA5CNFSM4I2XI272YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7YL2JA#issuecomment-535870756>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAC55MWDU4COM23AGKTFLZDQLXIRNANCNFSM4I2XI27Q>.
|
That's how my Node.js libra-grpc client returns it. I updated my .proto files, but maybe something is still missing, I need to double-check then, or maybe switch to your client. I will also double-check the |
I rebuilt the .proto files from another Libra branch, but the In your code, regarding
So for my
|
It seems to be a base64 encoded str.
base64.b64decode("IAAAAB/HpLI2")
b' \x00\x00\x00\x1f\xc7\xa4\xb26'
在 2019年9月27日,23:00,Gie Katon <[email protected]<mailto:[email protected]>> 写道:
I rebuilt the .proto files from another Libra branch, but the signed_txn is returned in the same format as before. I guess grpc client works fine, but the unpacking is what's necessary.
In your code, regarding struct.unpack there is only bytes to int_list unpacking:
def bytes_to_int_list(bytes_str):
tp = struct.unpack("<{}B".format(len(bytes_str)), bytes_str)
return list(tp)
So for my signed_txn unpacking, any ideas?
def unpack_signed_txn(???):
txn = struct.unpack( ???, ??? )
return print(txn)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#1?email_source=notifications&email_token=AAC55MRP6JEVWDWIKVUGU73QLYNZJA5CNFSM4I2XI272YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7ZFVUQ#issuecomment-535976658>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAC55MS3P6E45DASGV25NELQLYNZJANCNFSM4I2XI27Q>.
|
It works!!! Thanks a lot. I just need to finish with the decoding.
|
To decode But the |
I will do it, maybe need a few days.
在 2019年9月28日,17:49,Gie Katon <[email protected]<mailto:[email protected]>> 写道:
To decode sender, it works like this: print(int_list_to_hex(txn.raw_txn.sender))
But the args also need to be pretty-printed. If you can update your code, so that it pretty prints also the args, that would be great.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#1?email_source=notifications&email_token=AAC55MSK2G3WNAGL4BZCOSTQL4SAVA5CNFSM4I2XI272YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD72VFRY#issuecomment-536171207>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAC55MULXNWJOMJMUZEYNO3QL4SAVANCNFSM4I2XI27Q>.
|
Thanks, looking forward to it, that's the only part I'm missing. Meanwhile, I will finish integrating this LCS as a spawned child process in my Node.js grpc client. It already works, just need to run it on the real testnet. I will later share the code on GitHub. |
The code of address formatting is updated. |
Hi, thanks a lot, works great. I have already integrated your LCS into Libra Checker. I still need to finish some things, then will update the GitHub. As far as I know, there is yet no LCS written in JavaScript, so your Python LCS running in Node.js as a child process is a cool workaround. |
Maybe I can port Canoser from python to JavaScript. |
There are no solutions yet: So I guess LCS in JS would be very useful for the community. |
Hi Yuan, I'm still using Canoser and following your code on libra-client. Great work, much appreciated. Can you help me to decode the deserialized txn payload's "code" field? My current python file looks like this: Example query: Transaction nr 44 (the result from Canoser is below) I need to extract the transaction_type from the
|
I'm currently writing an openAPI for Libra, you may use the API to solve the problem later. But now, you can do like this:
|
It works and I learned a lot from your code snippet. Thanks. Looking forward to the openAPI. |
Hi and thanks for the library.
I'm trying to deserialize a signed transaction with the program, but getting the following error, maybe you see what's wrong and can help me.
Error:
My signed txn is this:
signed_txn = "200000003A24A61E05D129CACE9E0EFC8BC9E33831FEC9A9BE66F50FD352A2638A49B9EE200000000000000000000000040000006D6F766502000000020000000900000043414645204430304402000000090000006361666520643030640300000001000000CA02000000FED0010000000D1027000000000000204E0000000000008051010000000000"
I have defined structs like this:
And the rest of the code:
The text was updated successfully, but these errors were encountered: