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

TokenSymbol length check mismatch #616

Closed
dkuehr opened this issue Nov 30, 2022 · 1 comment · Fixed by #618
Closed

TokenSymbol length check mismatch #616

dkuehr opened this issue Nov 30, 2022 · 1 comment · Fixed by #618
Assignees

Comments

@dkuehr
Copy link

dkuehr commented Nov 30, 2022

In JavaScript the string length property returns the size in number of characters, however String.length in OCaml returns the size in bytes.

Length check in SnarkyJS:

class TokenSymbol extends Struct(TokenSymbolPure) {
  static get empty() {
    return { symbol: '', field: Field(0) };
  }

  static from(symbol: string): TokenSymbol {
    if (symbol.length > 6)
      throw Error('Token symbol length should be a maximum of 6');
    let field = prefixToField(symbol);
    return { symbol, field };
  }
}

Length check in OCaml:

module Token_symbol = struct
  [%%versioned_binable
  module Stable = struct
    module V1 = struct
      module T = struct
        type t = string [@@deriving sexp, equal, compare, hash, yojson]

        let to_latest = Fn.id

        let max_length = 6

        let check (x : t) = assert (String.length x <= max_length)

This can end in an inconsistent size check if non-ASCII characters are used, for example:
node:

> a = '₿₿₿₿₿₿';
'₿₿₿₿₿₿'
> a.length
6
> (new TextEncoder().encode(a)).length
18

utop:

# String.length "₿₿₿₿₿₿";;
- : int = 18
@mitschabaude
Copy link
Collaborator

good catch, thanks! @Trivo25 small guardian task?

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 a pull request may close this issue.

3 participants