Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @taquito/michel-codec

pkgsign status

Taquito michel-codec package

@taquito/michel-codec Converts and validates Michelson expressions between JSON based Michelson and Micheline.

This package can:

  • Retrieve Michelson in JSON form from the Tezos Node RPC and convert it to plain Michelson.
  • Parse plain Michelson (including Macros) and expand/convert it to JSON Michelson suitable for injection into the Tezo Blockchain.
  • Validate Michelson to ensure correctness

See the top-level project https://github.com/ecadlabs/taquito for details on reporting issues, contributing and versioning.

Examples

Michelson expression to JSON

    const code = `(Pair
                     (Pair { Elt 1 (Pair (Pair "tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx") 0x0501000000026869)}
                           10000000)
                     (Pair 2 333)
                  )`;

    const p = new Parser()

    const result = p.parseMichelineExpression(code)
    console.log(JSON.stringify(result))

Output:

{"prim":"Pair","args":[{"prim":"Pair","args":[[{"prim":"Elt","args":[{"int":"1"},{"prim":"Pair","args":[{"prim":"Pair","args":[{"string":"tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN"},{"string":"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx"}]},{"bytes":"0501000000026869"}]}]}],{"int":"10000000"}]},{"prim":"Pair","args":[{"int":"2"},{"int":"333"}]}]}

Pretty Print a Michelson contract

    const contract = await Tezos.contract.at('KT1EctCuorV2NfVb1XTQgvzJ88MQtWP8cMMv')
    const p = new Parser()

    const michelsonCode = p.parseJSON(contract.script.code as JSON[])
    console.log(emitMicheline(michelsonCode, {indent:"    ", newline: "\n",}))

API Documentation

TypeDoc style documentation is available on-line here

Disclaimer

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

References

BytesLiteral

Re-exports BytesLiteral

Expr

Re-exports Expr

FormatOptions

Re-exports FormatOptions

IntLiteral

Re-exports IntLiteral

JSONParseError

Re-exports JSONParseError

MacroError

Re-exports MacroError

MichelineParseError

Re-exports MichelineParseError

MichelsonCode

Re-exports MichelsonCode

MichelsonComparableType

Re-exports MichelsonComparableType

MichelsonData

Re-exports MichelsonData

MichelsonDataId

Re-exports MichelsonDataId

MichelsonInstruction

Re-exports MichelsonInstruction

MichelsonInstructionId

Re-exports MichelsonInstructionId

MichelsonMapElt

Re-exports MichelsonMapElt

MichelsonParameter

Re-exports MichelsonParameter

MichelsonScript

Re-exports MichelsonScript

MichelsonSimpleComparableType

Re-exports MichelsonSimpleComparableType

MichelsonSimpleComparableTypeId

Re-exports MichelsonSimpleComparableTypeId

MichelsonStorage

Re-exports MichelsonStorage

MichelsonType

Re-exports MichelsonType

MichelsonTypeId

Re-exports MichelsonTypeId

MichelsonUnaryInstructionId

Re-exports MichelsonUnaryInstructionId

Parser

Re-exports Parser

ParserOptions

Re-exports ParserOptions

Prim

Re-exports Prim

StringLiteral

Re-exports StringLiteral

ValidationError

Re-exports ValidationError

assertMichelsonCode

Re-exports assertMichelsonCode

assertMichelsonData

Re-exports assertMichelsonData

assertMichelsonScript

Re-exports assertMichelsonScript

assertMichelsonType

Re-exports assertMichelsonType

emitMicheline

Re-exports emitMicheline

Type aliases

Expr

An AST node representing valid Michelson expression. Directly corresponds to JSON-encoded Michelson node

MichelsonCode

MichelsonCode: SectionPrim<"code", [MichelsonInstruction[]]>

MichelsonComparableType

MichelsonData

MichelsonData: IntLiteral | StringLiteral | BytesLiteral | NoArgs<DataPrim<"Unit" | "True" | "False" | "None">> | ReqArgs<DataPrim<"Pair", [MichelsonData, MichelsonData]>> | ReqArgs<DataPrim<"Left" | "Right" | "Some", [MichelsonData]>> | MichelsonData[] | MichelsonMapElt[] | MichelsonInstruction

MichelsonDataId

MichelsonDataId: "Unit" | "True" | "False" | "None" | "Pair" | "Left" | "Right" | "Some"

MichelsonInstruction

MichelsonInstruction: MichelsonInstruction[] | NoArgs<InstrPrim<MichelsonUnaryInstructionId>> | ReqArgs<InstrPrim<"DIG" | "DUG", [IntLiteral]>> | InstrPrim<"DROP", [IntLiteral]> | ReqArgs<InstrPrim<"NONE" | "LEFT" | "RIGHT" | "NIL" | "UNPACK" | "CONTRACT" | "CAST", [MichelsonType]>> | ReqArgs<InstrPrim<"IF_NONE" | "IF_LEFT" | "IF_CONS" | "IF", [MichelsonInstruction[], MichelsonInstruction[]]>> | ReqArgs<InstrPrim<"MAP" | "ITER" | "LOOP" | "LOOP_LEFT" | "DIP" | "CREATE_CONTRACT", [MichelsonInstruction[]]>> | ReqArgs<InstrPrim<"PUSH", [MichelsonType, MichelsonData]>> | ReqArgs<InstrPrim<"EMPTY_SET", [MichelsonComparableType]>> | ReqArgs<InstrPrim<"EMPTY_MAP" | "EMPTY_BIG_MAP", [MichelsonComparableType, MichelsonType]>> | ReqArgs<InstrPrim<"LAMBDA", [MichelsonType, MichelsonType, MichelsonInstruction[]]>> | ReqArgs<InstrPrim<"DIP", [IntLiteral, MichelsonInstruction[]] | [MichelsonInstruction[]]>>

MichelsonInstructionId

MichelsonInstructionId: MichelsonUnaryInstructionId | "DROP" | "DIG" | "DUG" | "NONE" | "LEFT" | "RIGHT" | "NIL" | "UNPACK" | "CONTRACT" | "CAST" | "IF_NONE" | "IF_LEFT" | "IF_CONS" | "IF" | "MAP" | "ITER" | "LOOP" | "LOOP_LEFT" | "DIP" | "CREATE_CONTRACT" | "PUSH" | "EMPTY_SET" | "EMPTY_MAP" | "EMPTY_BIG_MAP" | "LAMBDA"

MichelsonMapElt

MichelsonMapElt: ReqArgs<Prim<"Elt", [MichelsonData, MichelsonData]>>

MichelsonParameter

MichelsonParameter: SectionPrim<"parameter", [MichelsonType]>

MichelsonScript

MichelsonSimpleComparableType

MichelsonSimpleComparableType: NoArgs<TypePrim<MichelsonSimpleComparableTypeId>>

MichelsonSimpleComparableTypeId

MichelsonSimpleComparableTypeId: "int" | "nat" | "string" | "bytes" | "mutez" | "bool" | "key_hash" | "timestamp" | "address"

MichelsonStorage

MichelsonStorage: SectionPrim<"storage", [MichelsonType]>

MichelsonType

MichelsonType: MichelsonComparableType | NoArgs<TypePrim<"key" | "unit" | "signature" | "operation" | "chain_id">> | ReqArgs<TypePrim<"option" | "list" | "contract", [MichelsonType]>> | ReqArgs<TypePrim<"pair" | "or" | "lambda", [MichelsonType, MichelsonType]>> | ReqArgs<TypePrim<"set", [MichelsonComparableType]>> | ReqArgs<TypePrim<"map" | "big_map", [MichelsonComparableType, MichelsonType]>>

MichelsonTypeId

MichelsonTypeId: MichelsonSimpleComparableTypeId | "key" | "unit" | "signature" | "operation" | "chain_id" | "option" | "list" | "contract" | "pair" | "or" | "lambda" | "set" | "map" | "big_map"

MichelsonUnaryInstructionId

MichelsonUnaryInstructionId: "DUP" | "SWAP" | "SOME" | "UNIT" | "PAIR" | "CAR" | "CDR" | "CONS" | "SIZE" | "MEM" | "GET" | "UPDATE" | "EXEC" | "FAILWITH" | "RENAME" | "CONCAT" | "SLICE" | "PACK" | "ADD" | "SUB" | "MUL" | "EDIV" | "ABS" | "ISNAT" | "INT" | "NEG" | "LSL" | "LSR" | "OR" | "AND" | "XOR" | "NOT" | "COMPARE" | "EQ" | "NEQ" | "LT" | "GT" | "LE" | "GE" | "SELF" | "TRANSFER_TOKENS" | "SET_DELEGATE" | "CREATE_ACCOUNT" | "IMPLICIT_ACCOUNT" | "NOW" | "AMOUNT" | "BALANCE" | "CHECK_SIGNATURE" | "BLAKE2B" | "SHA256" | "SHA512" | "HASH_KEY" | "STEPS_TO_QUOTA" | "SOURCE" | "SENDER" | "ADDRESS" | "CHAIN_ID"

NoAnnots

NoAnnots<T>: OmitProp<T, "annots">

Type parameters

NoArgs

NoArgs<T>: OmitProp<T, "args">

Type parameters

ReqArgs

ReqArgs<T>: RequiredProp<T, "args">

Type parameters

TokenType

TokenType: "(" | ")" | "{" | "}" | ";" | Literal

Tuple

Tuple<T, N>: N extends 1 ? [T] : N extends 2 ? [T, T] : N extends 3 ? [T, T, T] : N extends 4 ? [T, T, T, T] : never

Type parameters

  • T

  • N: number

Functions

assertMichelsonCode

  • assertMichelsonCode(ex: Expr[]): ex is MichelsonInstruction[]

assertMichelsonData

  • assertMichelsonData(ex: Expr): ex is MichelsonData

assertMichelsonScript

  • assertMichelsonScript(ex: Expr): ex is MichelsonScript
  • Checks if the node is a valid Michelson smart contract source containing all required and valid properties such as parameter, storage and code. This is a type guard function which either returns true of throws an exception.

    Parameters

    • ex: Expr

      An AST node

    Returns ex is MichelsonScript

assertMichelsonType

  • assertMichelsonType(ex: Expr): ex is MichelsonType

emitMicheline

expandMacros

scan

  • scan(src: string, scanComments?: boolean): Generator<Token, void>

Generated using TypeDoc