-
Notifications
You must be signed in to change notification settings - Fork 23
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
Fix plutus double CBOR encoding bug #720
base: master
Are you sure you want to change the base?
Conversation
36cb43d
to
b2d6cf8
Compare
Right (_, needToEncode) -> | ||
case CBOR.deserialiseFromBytes CBOR.decodeBytes $ LBS.fromStrict needToEncode of | ||
Left _ -> NotDoubleEncoded | ||
Right (_, final) -> IsDoubleEncoded $ CBOR.toStrictByteString $ CBOR.encodeBytes final |
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.
Why is the script getting encoded here again? Are you expecting needToEncode
to be different than CBOR.encodeBytes final
You're encoding final
before returning. Why is that? Doesn't that make it double encoded again?
data IsDoubleEncoded | ||
= -- | Original plutus script bytes | ||
IsDoubleEncoded | ||
Crypto.ByteString | ||
| NotDoubleEncoded | ||
|
||
isPlutusScriptDoubleEncoded :: LBS.ByteString -> IsDoubleEncoded |
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.
data IsDoubleEncoded
= -- | Original plutus script bytes
IsDoubleEncoded
Crypto.ByteString
| NotDoubleEncoded
isPlutusScriptDoubleEncoded :: LBS.ByteString -> IsDoubleEncoded
I think those types are a little bit unwieldy. It forces the user to pattern match on the type to extract the bytes. I know it's only used internally in the tests and in the deserialisation instance, but I think it could be improved.
What do you think about something like that:
data IsPlutusScriptDoubleEncoded
= DoubleEncodedPlutusScript
| NotDoubleEncodedPlutusScript
normalisePlutusScriptCborEncoding
:: LBS.ByteString -- ^ original plutus script, possibly doubly encoded
-> (IsPlutusScriptDoubleEncoded, LBS.ByteString)
-- ^ singly encoded plutus script - if the original script was singly encoded,
-- returns original bytes
this way if the user is not interested in the encoding problem, they can ignore the first element of the tuple and just use the resulting bytes
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.
It forces the user to pattern match on the type to extract the bytes
I think we should get rid of the IsPlutusScriptDoubleEncoded
type. I'll push some changes.
8eaa6a5
to
f9fd11d
Compare
b77b45f
to
3b0a1ac
Compare
(Script lang) instance Implement removePlutusScriptDoubleEncoding function Utilize removePlutusScriptDoubleEncoding in the SerialiseAsCBOR (PlutusScript lang)
When decoding plutus script bytes to this type the validity of those bytes are checked and the decoder will fail if they are invalid This type is used in testing to double check we can still decode double encoded plutus script bytes and that the resulting bytes are valid
This function decodes a double encoded plutus script and checks if the bytes are valid. It essentially tests if the existing SerialiseAsCBOR instance for PlutusScript lang can decode a double encoded plutus script correctly.
scripts correctly
3b0a1ac
to
ed0a681
Compare
Resolves: #685
Changelog
Context
Additional context for the PR goes here. If the PR fixes a particular issue please provide a link to the issue.
How to trust this PR
Highlight important bits of the PR that will make the review faster. If there are commands the reviewer can run to observe the new behavior, describe them.
Checklist