Skip to content

Commit

Permalink
Merge ca60371 into f1672b4
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymoussprocket committed Jul 13, 2020
2 parents f1672b4 + ca60371 commit b57888c
Show file tree
Hide file tree
Showing 21 changed files with 741 additions and 266 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TBD

```html
<script src="https://cdn.jsdelivr.net/gh/cryptonomic/conseiljs/dist-web/conseiljs.min.js"
integrity="sha384-CDpCW4rM4qw7oEyOr5wSQNqnPmQw9vtCoqWyKSDIys50jRONU+j+uxk/aEy0QzRJ"
integrity="sha384-D5Exnkq8hYVfQqoWvZOQsohtT45+lj0YGbHmwdeJPeqCcM0zxXjmCRRsaf+Ch2eg"
crossorigin="anonymous"></script>
```

Expand Down
2 changes: 1 addition & 1 deletion dist-web/conseiljs.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ We have a complete [React application example](https://github.com/Cryptonomic/Co

```html
<script src="https://cdn.jsdelivr.net/gh/cryptonomic/conseiljs/dist-web/conseiljs.min.js"
integrity="sha384-CDpCW4rM4qw7oEyOr5wSQNqnPmQw9vtCoqWyKSDIys50jRONU+j+uxk/aEy0QzRJ"
integrity="sha384-D5Exnkq8hYVfQqoWvZOQsohtT45+lj0YGbHmwdeJPeqCcM0zxXjmCRRsaf+Ch2eg"
crossorigin="anonymous"></script>
```

Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "conseiljs",
"version": "5.0.2-beta.3",
"version": "5.0.3-beta.2",
"description": "Client-side library for dApp development.",
"browser": "dist/index-web.js",
"main": "dist/index.js",
Expand Down
164 changes: 155 additions & 9 deletions src/chain/tezos/TezosLanguageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ export namespace TezosLanguageUtil {
break;
}
case '03': { // bare primitive
code += `{ "prim": ${michelineHexToKeyword(hex, offset)} }`;
code += `{ "prim": ${hexToMichelineKeyword(hex, offset)} }`;
offset += 2;
break;
}
case '04': { // primitive with a set of annotations
code += `{ "prim": ${michelineHexToKeyword(hex, offset)}, `;
code += `{ "prim": ${hexToMichelineKeyword(hex, offset)}, `;
offset += 2;

const annEnvelope = michelineHexToAnnotations(hex.substring(offset));
Expand All @@ -69,15 +69,15 @@ export namespace TezosLanguageUtil {
break;
}
case '05': { // primitive with an argument
code += `{ "prim": ${michelineHexToKeyword(hex, offset)}, `;
code += `{ "prim": ${hexToMichelineKeyword(hex, offset)}, `;
offset += 2;
const envelope = hexToMicheline(hex.substring(offset));
code += `"args": [ ${envelope.code} ] }`;
offset += envelope.consumed;
break;
}
case '06': { // primitive with an argument an a set of annotations
code += `{ "prim": ${michelineHexToKeyword(hex, offset)}, `;
code += `{ "prim": ${hexToMichelineKeyword(hex, offset)}, `;
offset += 2;

const args = hexToMicheline(hex.substring(offset));
Expand All @@ -90,7 +90,7 @@ export namespace TezosLanguageUtil {
break;
}
case '07': { // primitive with two arguments
code += `{ "prim": ${michelineHexToKeyword(hex, offset)}, `;
code += `{ "prim": ${hexToMichelineKeyword(hex, offset)}, `;
offset += 2;

let buffer: string[] = [];
Expand All @@ -104,8 +104,8 @@ export namespace TezosLanguageUtil {
code += `"args": [ ${buffer.join(', ')} ] }`;
break;
}
case '08': { // primitive with two arguments and an anotation set
code += `{ "prim": ${michelineHexToKeyword(hex, offset)}, `;
case '08': { // primitive with two arguments and an annotation set
code += `{ "prim": ${hexToMichelineKeyword(hex, offset)}, `;
offset += 2;

const arg0 = hexToMicheline(hex.substring(offset));
Expand All @@ -120,7 +120,7 @@ export namespace TezosLanguageUtil {
break;
}
case '09': { // primitive with an argument array and an optional anotation set
code += `{ "prim": ${michelineHexToKeyword(hex, offset)}, `;
code += `{ "prim": ${hexToMichelineKeyword(hex, offset)}, `;
offset += 2;

let envelope = hexToMicheline('02' + hex.substring(offset)); // fake an array to re-use the parsing code
Expand Down Expand Up @@ -152,6 +152,142 @@ export namespace TezosLanguageUtil {
return { code: code, consumed: offset };
}

export function hexToMichelson(hex: string): codeEnvelope {
if (hex.length < 2) { throw new Error(`Malformed Michelson fragment '${hex}'`); }
let code = '';
let offset = 0;
let fieldType = hex.substring(offset, offset + 2);
offset += 2;

switch (fieldType) {
case '00': { // literal int or nat
const value = TezosMessageUtils.findInt(hex.substring(offset), 0, true);
code += ` ${value.value} `;
offset += value.length;
break;
}
case '01': { // literal string
const stringEnvelope = michelineHexToString(hex.substring(offset));
code += ` "${stringEnvelope.code}" `;
offset += stringEnvelope.consumed;
break;
}
case '02': { // array
const length = parseInt(hex.substring(offset, offset + 8), 16);
offset += 8;
let buffer: string[] = [];
let consumed = 0;
while (consumed < length) {
let envelope = hexToMichelson(hex.substring(offset));
buffer.push(envelope.code);
consumed += envelope.consumed / 2; // plain bytes
offset += envelope.consumed; // hex-encoded two-char bytes
}
if (length === 0) {
code += '[]';
} else {
code += `[ ${buffer.join(' ')} ]`;
}
break;
}
case '03': { // bare primitive
code += `( ${hexToMichelsonKeyword(hex, offset)} )`;
offset += 2;
break;
}
case '04': { // primitive with a set of annotations
code += `( ${hexToMichelsonKeyword(hex, offset)} `;
offset += 2;

const annEnvelope = michelineHexToAnnotations(hex.substring(offset));
code += ` ${annEnvelope.code} )`;
offset += annEnvelope.consumed;
break;
}
case '05': { // primitive with an argument
code += `( ${hexToMichelsonKeyword(hex, offset)} `;
offset += 2;
const envelope = hexToMichelson(hex.substring(offset));
code += ` ${envelope.code} )`;
offset += envelope.consumed;
break;
}
case '06': { // primitive with an argument an a set of annotations
code += `( ${hexToMichelsonKeyword(hex, offset)} `;
offset += 2;

const args = hexToMichelson(hex.substring(offset));
code += ` ${args.code} `;
offset += args.consumed;

const anns = michelineHexToAnnotations(hex.substring(offset));
code += ` ${anns.code} )`;
offset += anns.consumed;
break;
}
case '07': { // primitive with two arguments
code += `( ${hexToMichelsonKeyword(hex, offset)} `;
offset += 2;

let buffer: string[] = [];
let envelope = hexToMichelson(hex.substring(offset));
buffer.push(envelope.code);
offset += envelope.consumed;
envelope = hexToMichelson(hex.substring(offset));
buffer.push(envelope.code);
offset += envelope.consumed;

code += ` ${buffer.join(' ')} )`;
break;
}
case '08': { // primitive with two arguments and an annotation set
code += `( ${hexToMichelsonKeyword(hex, offset)} `;
offset += 2;

const arg0 = hexToMichelson(hex.substring(offset));
offset += arg0.consumed;
const arg1 = hexToMichelson(hex.substring(offset));
offset += arg1.consumed;
code += ` ${arg0.code} ${arg1.code} `;

const anns = michelineHexToAnnotations(hex.substring(offset));
code += ` ${anns.code} )`;
offset += anns.consumed;
break;
}
case '09': { // primitive with an argument array and an optional anotation set
code += `( ${hexToMichelsonKeyword(hex, offset)} `;
offset += 2;

let envelope = hexToMichelson('02' + hex.substring(offset)); // fake an array to re-use the parsing code
code += `"args": ${envelope.code}`;
offset += envelope.consumed - 2; // account for the inserted '02' above

if (hex.substring(offset, offset + 8) !== '00000000') {
const annEnvelope = michelineHexToAnnotations(hex.substring(offset));
if (annEnvelope.code.length > 2) { // more than empty quotes
code += ` ${annEnvelope.code} )`;
}
offset += annEnvelope.consumed;
} else {
code += ' )';
offset += 8;
}
break;
}
case '0a': { // raw bytes
const length = parseInt(hex.substring(offset, offset + 8), 16);
offset += 8;
code += ` 0x${hex.substring(offset, offset + length * 2)} `;
offset += length * 2;
break;
}
default: { throw new Error(`Unknown Micheline field type '${fieldType}'`); }
}

return { code: code, consumed: offset };
}

/**
* Converts Michelson to Micheline and wraps the result in a script property.
*/
Expand Down Expand Up @@ -233,10 +369,14 @@ export namespace TezosLanguageUtil {
* @param offset Offset to read one byte from
* @returns {string} Michelson/Micheline keyword
*/
function michelineHexToKeyword(hex: string, offset: number): string {
function hexToMichelineKeyword(hex: string, offset: number): string {
return MichelineKeywords[parseInt(hex.substring(offset, offset + 2), 16)];
}

function hexToMichelsonKeyword(hex: string, offset: number): string {
return MichelineKeywords[parseInt(hex.substring(offset, offset + 2), 16)].slice(1, -1);
}

/**
* Translates hex-encoded stream into a collection of annotations. Determines the length internally. Annotations are prefixed with ':', '@', or '%' for type, variable, and field annotations.
*
Expand Down Expand Up @@ -292,6 +432,12 @@ export namespace TezosLanguageUtil {
.trim();
}

export function normalizeMichelsonWhiteSpace(fragment: string): string {
return fragment.replace(/\s{2,}/g, ' ')
.replace(/\( /g, '(')
.replace(/ \)/g, ')')
}

/**
*
*
Expand Down

0 comments on commit b57888c

Please sign in to comment.