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

2922 michel codec doc #2972

Merged
merged 38 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
81f82f7
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Nov 7, 2023
eab82bb
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Nov 10, 2023
0259cdd
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Nov 21, 2023
b1a2679
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Nov 27, 2023
45223bd
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Nov 28, 2023
a82b5cd
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Dec 1, 2023
3b08821
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Dec 5, 2023
bfd1f49
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Dec 5, 2023
5d0a7cc
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Dec 13, 2023
7b9d4c1
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Dec 14, 2023
37e14e4
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Dec 28, 2023
dc68be9
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Jan 9, 2024
f259810
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Jan 16, 2024
6fdda51
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 6, 2024
298b413
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 12, 2024
06af53b
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 14, 2024
e4853cf
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 16, 2024
fc2e2b0
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 20, 2024
a64564e
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 21, 2024
ae561ab
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 22, 2024
0a2974b
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Feb 29, 2024
708d150
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Mar 1, 2024
e018e9e
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Mar 8, 2024
4523ffb
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Mar 19, 2024
3c386ad
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Mar 26, 2024
dffb59c
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Apr 5, 2024
b58a460
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Apr 5, 2024
8fa69af
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Apr 8, 2024
a896a23
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Apr 9, 2024
fd47258
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Apr 11, 2024
00892e8
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Apr 11, 2024
a39aaa5
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang Apr 17, 2024
82f5a89
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang May 9, 2024
bbaba4b
Merge branch 'master' of https://github.com/ecadlabs/taquito
hui-an-yang May 13, 2024
729c977
Merge branch 'master' of https://github.com/ecadlabs/taquito into 292…
hui-an-yang May 22, 2024
20e1521
docs: adding michel-codec page to website
hui-an-yang May 24, 2024
daa592b
docs: address review comments
hui-an-yang May 27, 2024
6010777
docs: addressed review comments
hui-an-yang May 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions docs/michel_codec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Michel Codec
author: Hui-An Yang
---

The `taquito/michel-codec` package converts and validates Michelson expressions between JSON-based Michelson and Micheline. It also comes with various functions like `packData`, `packDataBytes`, `unpackData` and `unpackDataBytes` to serialize any value of packable type to its optimized binary representation locally and vice versa, like Michelson instructions `PACK` and `UNPACK`.

## Parser class
To use the parser class, import and initialize it as follows.

```ts
import { Parser } from '@taquito/michel-codec'
const p = new Parser()
```
### Configuration
You can configure the parser class by passing `ParserOptions` through initialization with `expandMacros` and `expandGlobalConstant` properties.

* `expandMacros` - defaults to true unless you don't want `Parser` class to expand them; you can pass `{ expandMacros: false }` to disable it. ref: Expand [Michelson macros](https://tezos.gitlab.io/whitedoc/michelson.html#macros) during parsing
* `expandGlobalConstant` - expects an object where the keys are global constant hashes and the values are the corresponding JSON Micheline expressions.

for example

```ts
import { Parser } from '@taquito/michel-codec'

const parserOptions: ParserOptions = {
expandMacros: true,
expandGlobalConstant: {
'expr...': { prim: 'DROP', args: [{ int: '2' }] }
}
}
const p = new Parser(parserOptions);
```

### parseJSON & emitMicheline - Parse JSON Michelson and convert it to Micheline
Copy link
Contributor

Choose a reason for hiding this comment

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

redundant title, remove 'parseJSON & emitMicheline`

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

they are in place so the outline of the doc display the function names

Copy link
Contributor

Choose a reason for hiding this comment

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

you listed them right below it, I would remove it

Copy link
Contributor

Choose a reason for hiding this comment

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

Heading titles for documentation should be concise and clear

Copy link
Contributor

@dsawali dsawali May 27, 2024

Choose a reason for hiding this comment

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

also having the title of methods as a preface to what it does is not a very good habit when writing technical documentation (it does work in some exceptions, but not in this case)

When writing a heading for a section, you want to explain what this section describes:

Parsing JSON Michelson and converting to Micheline

and then you would describe what methods to use in the content of said section

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i did some reading on writing technical docs outlines and it says it's good idea to have both method name with concise description and I also reference ethereum taquito equivalent tool docs, their outline have method name only. I think this is a preference and i decided to keep them.

* `parseJSON` - takes a JSON-encoded Michelson, validates it, strips away unneeded properties and expands macros based on your configuration.
* `emitMicheline` takes a parsed JSON Michelson object and converts it to a Micheline expression with formatting options.

```js live noInline
// import { Parser, emitMicheline } from '@taquito/michel-codec'

const p = new Parser();
Tezos.contract
.at("KT1BJadpDyLCACMH7Tt9xtpx4dQZVKw9cDF7")
.then(contract => {
const code = p.parseJSON(contract.script.code);
println("Pretty print Michelson smart contract:");
println(emitMicheline(code, {indent:" ", newline: "\n",}));

const storage = p.parseJSON(contract.script.storage);
println("Pretty print Storage:");
println(emitMicheline(storage, {indent:" ", newline: "\n",}));
})
.catch((error) => println(`Error: ${JSON.stringify(error, null, 2)}`));
```

### parseMichelineExpression - Parse Micheline and convert it to JSON Michelson
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, remove 'parseMichelineExpression - '

Takes a Micheline expression in the form of script or data and converts it to JSON Michelson

```js live noInline
// import { Parser } from '@taquito/michel-codec'

const p = new Parser();

const michelineScript = `{parameter unit; storage unit; code {CDR; NIL operation; PAIR};}`
const script = p.parseMichelineExpression(michelineScript);
println('JSON Michelson script: ' + JSON.stringify(script) + '\n');

const michelineData = `(IF_LEFT { IF_LEFT { SWAP ; SUB } { ADD } })`;
const data = p.parseMichelineExpression(michelineData);
println('JSON Michelson data: ' + JSON.stringify(data));
```

## PACK and UNPACK locally

### packData & packDataBytes - Pack Michelson data
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Serialize any value of packable type to its optimized binary representation identical to the one used by PACK Michelson instructions.
Without a type definition (not recommended), `packData` and `packDataBytes` will encode the data as a binary form of a generic Michelson expression.
Type definition allows types like `timestamp`, `address` and other base58 representable types to be encoded to corresponding optimized binary forms borrowed from the Tezos protocol.

```ts
// import { packData, packDataBytes } from '@taquito/michel-codec'

const data: MichelsonData = { string: 'KT1RvkwF4F7pz1gCoxkyZrG1RkrxQy3gmFTv%foo' };
const typ: MichelsonType = { prim: 'address' };

const packed = packData(data, typ);
// 050a0000001901be41ee922ddd2cf33201e49d32da0afec571dce300666f6f

const packedBytes = packDataBytes(data, typ);
// { bytes: "050a0000001901be41ee922ddd2cf33201e49d32da0afec571dce300666f6f" }
```

### unpackData & unpackDataBytes - Unpack Michelson data
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Deserialize a byte array into its corresponding Michelson value.
Without a type definition (not recommended), the binary data will be treated as a binary form of a generic Michelson expression and returned as is.
Type definition allows some types, like `timestamp` and `address` and others, usually encoded in optimized binary forms, to be transformed back to their string representations like base58 and ISO timestamps.

```ts
// import { unpackData, unpackDataBytes } from '@taquito/michel-codec'
const type: MichelsonType = { prim: 'timestamp' };

const src1 = [0x05, 0x00, 0xa7, 0xe8, 0xe4, 0xd8, 0x0b];
const data1 = unpackData(src1, type);
// { string: "2019-09-26T10:59:51Z" }

const src2 = { bytes: '0500a7e8e4d80b' };
const data2 = unpackDataBytes(src2, type);
// { string: "2019-09-26T10:59:51Z" }
```

Alternatively, the same binary data without passing a type definition to `unpackData`, `unpackDataBytes` will not be deserialized correctly
```ts
// import { unpackData, unpackDataBytes } from '@taquito/michel-codec'

const src1 = [0x05, 0x00, 0xa7, 0xe8, 0xe4, 0xd8, 0x0b];
const data1 = unpackData(src1);
// { int: "1569495591" }

const src2 = { bytes: '0500a7e8e4d80b' };
const data2 = unpackDataBytes(src2);
// { int: "1569495591" }
```
102 changes: 51 additions & 51 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const sidebars = {
items: [
'rpc_package',
'michelson_encoder',
'michel_codec',
'contracts-library',
'timelock',
'taquito_utils',
Expand Down
4 changes: 3 additions & 1 deletion website/src/theme/CodeBlock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default ({
const { Tzip16Module, tzip16, MichelsonStorageView } = await import('@taquito/tzip16')
const { Tzip12Module, tzip12 } = await import("@taquito/tzip12");
const { Schema, ParameterSchema } = await import("@taquito/michelson-encoder");
const { Parser, packDataBytes } = await import('@taquito/michel-codec');
const { Parser, packDataBytes, emitMicheline } = await import('@taquito/michel-codec');
const { RpcClient } = await import('@taquito/rpc');
const TransportWebHID = (await import("@ledgerhq/hw-transport-webhid")).default;

Expand Down Expand Up @@ -132,6 +132,7 @@ export default ({
verifySignature,
Parser,
packDataBytes,
emitMicheline,
RpcReadAdapter,
RpcClient,
Ed25519,
Expand Down Expand Up @@ -188,6 +189,7 @@ export default ({
verifySignature: dependencies?.verifySignature,
Parser: dependencies?.Parser,
packDataBytes: dependencies?.packDataBytes,
emitMicheline: dependencies?.emitMicheline,
RpcReadAdapter: dependencies?.RpcReadAdapter,
RpcClient: dependencies?.RpcClient,
InMemorySpendingKey: dependencies?.InMemorySpendingKey,
Expand Down
Loading
Loading