Skip to content
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

Convert from hex at compile time #299

Merged
merged 2 commits into from
Sep 6, 2022
Merged

Conversation

lehins
Copy link
Collaborator

@lehins lehins commented Aug 25, 2022

This PR is meant to be a replacement for the approach taken in #294

Implement IsString instances for a Q (TExp (PinnedSizedBytes n)) and Q (TExp (Hash h a))

This allows us to safely construct PinnedSizedBytes and Hash at compile time when we know the value statically (such as unit tests for example). Here are a few examples that were included in the haddock:

>>> import Cardano.Crypto.PinnedSizedBytes
>>> :set -XTemplateHaskell
>>> :set -XOverloadedStrings
>>> :set -XDataKinds
>>> print ($$("0xdeadbeef") :: PinnedSizedBytes 4)
"deadbeef"
>>> print ($$("deadbeef") :: PinnedSizedBytes 4)
"deadbeef"
>>> let bsb = $$("0xdeadbeef") :: PinnedSizedBytes 5
<interactive>:9:14: error:
     <PinnedSizedBytes>: Expected in decoded form to be: 5 bytes, but got: 4
     In the Template Haskell splice $$("0xdeadbeef")
      In the expression: $$("0xdeadbeef") :: PinnedSizedBytes 5
      In an equation for bsb’:
          bsb = $$("0xdeadbeef") :: PinnedSizedBytes 5
>>> let bsb = $$("nogood") :: PinnedSizedBytes 5
<interactive>:11:14: error:
     <PinnedSizedBytes>: Malformed hex: invalid character at offset: 0
     In the Template Haskell splice $$("nogood")
      In the expression: $$("nogood") :: PinnedSizedBytes 5
      In an equation for bsb’: bsb = $$("nogood") :: PinnedSizedBytes 5

and for Hashes

>>> import Cardano.Crypto.Hash.Class (Hash)
>>> import Cardano.Crypto.Hash.Short (ShortHash)
>>> :set -XTemplateHaskell
>>> :set -XOverloadedStrings
>>>  let hash = $$("0xBADC0FFEE0DDF00D") :: Hash ShortHash ()
>>> print hash
"badc0ffee0ddf00d"
>>> let hash = $$("0123456789abcdef") :: Hash ShortHash ()
>>> print hash
"0123456789abcdef"
>>> let hash = $$("deadbeef") :: Hash ShortHash ()
<interactive>:5:15: error:
     <Hash blake2b_prefix_8>: Expected in decoded form to be: 8 bytes, but got: 4
     In the Template Haskell splice $$("deadbeef")
      In the expression: $$("deadbeef") :: Hash ShortHash ()
      In an equation for hash’:
          hash = $$("deadbeef") :: Hash ShortHash ()
>>> let hash = $$("123") :: Hash ShortHash ()
<interactive>:6:15: error:
     <Hash blake2b_prefix_8>: Malformed hex: invalid bytestring size
     In the Template Haskell splice $$("123")
      In the expression: $$("123") :: Hash ShortHash ()
      In an equation for hash’: hash = $$("123") :: Hash ShortHash ()

Other safety fixes:

  • Remove IsString (PinnedSizedBytes n) instance
  • Fix psbFromByteString to fail on invalid input

@lehins lehins force-pushed the lehins/static-from-hex-conversion branch from 29deaf4 to 496abca Compare August 25, 2022 19:29
* Remove `IsString (PinnedSizedBytes n)` instance
* Fix `psbFromByteString` to fail on invalid input
@lehins lehins force-pushed the lehins/static-from-hex-conversion branch from 496abca to 85d351e Compare September 2, 2022 09:08
Copy link
Contributor

@tdammers tdammers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@lehins lehins force-pushed the lehins/static-from-hex-conversion branch from 85d351e to 6f856f0 Compare September 6, 2022 12:34
@lehins lehins merged commit 7e3ddba into master Sep 6, 2022
@lehins lehins deleted the lehins/static-from-hex-conversion branch February 21, 2023 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants