Skip to content

Instantly share code, notes, and snippets.

@mfornos
Last active October 17, 2022 15:47
Show Gist options
  • Select an option

  • Save mfornos/7848c7d9d67eefe0e41418981a52212d to your computer and use it in GitHub Desktop.

Select an option

Save mfornos/7848c7d9d67eefe0e41418981a52212d to your computer and use it in GitHub Desktop.

Revisions

  1. mfornos revised this gist Oct 17, 2022. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion contract-lang-detect.py
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ def hasName(name):
    # Ask!
    # * has a start section
    # * has custom section named 'sourceMappingURL'
    # * imports declare memory after functions
    isAsk = bool(
    imports[0].kind != 'memory'
    and start
    @@ -36,6 +37,8 @@ def hasName(name):

    # Ink!
    # * no custom sections
    # * no start section
    # * imports declare memory after functions
    isInk = bool(
    imports[0].kind != 'memory'
    and not start
    @@ -49,4 +52,3 @@ def hasName(name):
    Ask! = {isAsk}
    Sol = {isSolang}
    """)

  2. mfornos revised this gist Oct 17, 2022. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions example-output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    ❯ python3 contract-lang-detect.py < blobs/flipper.ink.wasm

    Detection results
    -----------------
    Ink! = True
    Ask! = False
    Sol = False

    ❯ python3 contract-lang-detect.py < blobs/flipper.ask.wasm

    Detection results
    -----------------
    Ink! = False
    Ask! = True
    Sol = False

    ❯ python3 contract-lang-detect.py < blobs/flipper.sol.wasm

    Detection results
    -----------------
    Ink! = False
    Ask! = False
    Sol = True
  3. mfornos renamed this gist Oct 17, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. mfornos revised this gist Oct 17, 2022. 1 changed file with 0 additions and 0 deletions.
    Binary file added blobs.zip
    Binary file not shown.
  5. mfornos created this gist Oct 17, 2022.
    52 changes: 52 additions & 0 deletions contract-lang-detect.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import sys
    from ppci import wasm

    m = wasm.Module(sys.stdin.buffer.read())
    sections = m.get_definitions_per_section()
    imports = sections['import']
    start = sections['start']
    customs = sections['custom']
    mem = next(filter(lambda x : x.kind == 'memory', imports))

    def hasName(name):
    lambda x : x.name == name

    # Tentative heuristics
    # (!) NOT tested with a reasonable sample set of binaries

    # Solang
    # * emits memory as the first import
    # * has custom sections named 'name' and 'producers'
    isSolang = bool(
    imports[0].kind == 'memory'
    and not start
    and customs
    and any(filter(hasName('name'), customs))
    )

    # Ask!
    # * has a start section
    # * has custom section named 'sourceMappingURL'
    isAsk = bool(
    imports[0].kind != 'memory'
    and start
    and customs
    and any(filter(hasName('sourceMappingURL'), customs))
    )

    # Ink!
    # * no custom sections
    isInk = bool(
    imports[0].kind != 'memory'
    and not start
    and not customs
    )

    print(f"""
    Detection results
    -----------------
    Ink! = {isInk}
    Ask! = {isAsk}
    Sol = {isSolang}
    """)