-
Notifications
You must be signed in to change notification settings - Fork 11.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encode data using base64, encode ids using Hex for human readable serialisation #1304
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1304 +/- ##
==========================================
+ Coverage 81.70% 81.94% +0.24%
==========================================
Files 99 100 +1
Lines 20766 20927 +161
==========================================
+ Hits 16966 17148 +182
+ Misses 3800 3779 -21
Continue to review full report at Codecov.
|
c5dc349
to
d5eb5cf
Compare
Added @Clay-Mysten to track required doc changes. |
sui_types/src/readable_serde.rs
Outdated
let s = String::deserialize(deserializer)?; | ||
encoding | ||
.decode(s) | ||
.map_err(|_| D::Error::custom("byte deserialization failed")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the reason for throwing away the error from decoding, wouldn't it be useful to include that in the error message (via the error's Display
impl) so that a user can get a better idea of why the deserialization failed? Eg from this error I don't know why it failed, did i give it hex when it wanted base64 or vice versa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍thanks for pointing this out, I will add more info to the error
cc68395
to
89dc1ac
Compare
I edited the code comments in readable_serde.rs directly. I assume you mean this will affect a bunch of examples/output in our existing docs. Let's talk first when done with this then so I do the right thing. Hit me up in chat. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Is it accurate to say everything will be serialized/deserialized using Base64 except SuiAddress?
sui_types/src/readable_serde.rs
Outdated
byte_or_hex!(20,); | ||
byte_or_base64!(32, 96,); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we use Constants(e.g., OBJECT_DIGEST_LENGTH
) from other files to improve readability?
@@ -169,6 +169,80 @@ fn test_object_id_serde_json() { | |||
assert_eq!(obj_id, json_obj_id); | |||
} | |||
|
|||
#[test] | |||
fn test_object_id_serde_not_human_readable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently object id is passed in the rest server in hex:
sui/sui/src/rest_gateway/requests.rs
Line 57 in 7c30c0a
/// Required; Hex code as string representing Move module location |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not changing ObjectID in this PR, ObjectID already have it's own serialiser implemented earlier and it's doing similar things : Hex for human-readable, and tuple array (which doesn't contain array length compare to Bytes) for non-human-readable serialisation, we might want to do a refactoring in the future though to make them consistent.
Thanks! The rules is Hex for IDs, so we can preserve the ordering, and base64 for data/signature/digest |
@patrickkuo hey this is a great PR, it does allow a way to serialize to human readable and bytes at the same time. The only ting I don't like, and this is not meant to be a discouragement, is that the API of @bmwill I think implementing base64 and hex as specific methods |
Serialise to bytes instead of tuplearray
Hyphenate compound modifiers, remove caps where not needed
66695fe
to
db3b2e1
Compare
One reason of shoehorning the encoding to serde is to avoid having to do the conversion manually in the RPC layer; we can achieve the same result by create a different set of all the return types and convert them in the RPC layer, but that's quite a bit of work as our return objects are quite complex. |
Would you mind pointing out where the RPC code is? Just want to have a look and see how much impact this would be... |
For example in execute_transaction function, we will need to handle/convert the TransactionResponse manually to JSON without the original serde. Also for other functions we will need to convert the input SuiAddress/ObjectID manually. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack on direction, I pointed at a couple issues in the PR.
sui_types/src/readable_serde.rs
Outdated
Encoding::Base64 => base64::decode(s)?, | ||
Encoding::Hex => hex::decode(s)?, | ||
}) | ||
} | ||
|
||
fn encode<T: AsRef<[u8]>>(&self, data: T) -> String { | ||
match self { | ||
Encoding::Base64 => base64::encode(data), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are issues with the base64 crate in cryptographic contexts (I don't think you're serializing private keys, but you're serializing public ones at least). Please make sure you contact @kchalkias for advice.
base64ct does not have those issues, you may want to use it instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have replaced base64 with base64ct
* replace base64 with base64ct
@awelc and @patrickkuo Please help me understand the docs needing updating as a result of this change. I am happy to take it from there. Thanks! |
The changes here mainly impacting the JSON output, I had a quick look it seems like we do not include JSON output in our docs? @Clay-Mysten |
What about here? Thanks, Patrick! |
@awelc for guidance on whether these need updates: |
@Clay-Mysten sorry for late reply, I skimmed through the docs and seems like we didn't include JSON output samples in them, so nothing to update.... @awelc to confirm |
No worries, @patrickkuo! Thanks for double checking. |
This PR created 2 custom de/serialiser
BytesOrHex
,BytesOrBase64
for :SuiAddress
,ObjectDigest
,PublicKeyBytes
,Signature
,TransactionDigest
, andAuthoritySignature
, data will be serialised as Bytes for non-human readable serialiser and encoded with base64/Hex for human readable serialiser.Here are the json output of a transfer transaction after the changes: