@tezos-x/octez.js-michelson-encoder 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -0
- package/dist/lib/constants.js +11 -0
- package/dist/lib/errors.js +38 -0
- package/dist/lib/michelson-map.js +198 -0
- package/dist/lib/octez.js-michelson-encoder.js +36 -0
- package/dist/lib/schema/errors.js +127 -0
- package/dist/lib/schema/event-schema.js +53 -0
- package/dist/lib/schema/model.js +2 -0
- package/dist/lib/schema/parameter.js +113 -0
- package/dist/lib/schema/storage.js +258 -0
- package/dist/lib/schema/types.js +2 -0
- package/dist/lib/schema/view-schema.js +90 -0
- package/dist/lib/tokens/bigmap.js +130 -0
- package/dist/lib/tokens/bls12-381-fr.js +86 -0
- package/dist/lib/tokens/bls12-381-g1.js +76 -0
- package/dist/lib/tokens/bls12-381-g2.js +76 -0
- package/dist/lib/tokens/chain-id.js +78 -0
- package/dist/lib/tokens/chest-key.js +74 -0
- package/dist/lib/tokens/chest.js +74 -0
- package/dist/lib/tokens/comparable/address.js +116 -0
- package/dist/lib/tokens/comparable/bool.js +59 -0
- package/dist/lib/tokens/comparable/bytes.js +90 -0
- package/dist/lib/tokens/comparable/int.js +87 -0
- package/dist/lib/tokens/comparable/key_hash.js +90 -0
- package/dist/lib/tokens/comparable/mutez.js +87 -0
- package/dist/lib/tokens/comparable/nat.js +90 -0
- package/dist/lib/tokens/comparable/string.js +48 -0
- package/dist/lib/tokens/comparable/timestamp.js +66 -0
- package/dist/lib/tokens/constant.js +80 -0
- package/dist/lib/tokens/contract.js +86 -0
- package/dist/lib/tokens/createToken.js +35 -0
- package/dist/lib/tokens/key.js +97 -0
- package/dist/lib/tokens/lambda.js +55 -0
- package/dist/lib/tokens/list.js +85 -0
- package/dist/lib/tokens/map.js +112 -0
- package/dist/lib/tokens/never.js +61 -0
- package/dist/lib/tokens/operation.js +39 -0
- package/dist/lib/tokens/option.js +88 -0
- package/dist/lib/tokens/or.js +276 -0
- package/dist/lib/tokens/pair.js +230 -0
- package/dist/lib/tokens/sapling-state.js +84 -0
- package/dist/lib/tokens/sapling-transaction-deprecated.js +74 -0
- package/dist/lib/tokens/sapling-transaction.js +74 -0
- package/dist/lib/tokens/set.js +82 -0
- package/dist/lib/tokens/signature.js +82 -0
- package/dist/lib/tokens/ticket-deprecated.js +92 -0
- package/dist/lib/tokens/ticket.js +92 -0
- package/dist/lib/tokens/token.js +100 -0
- package/dist/lib/tokens/tokens.js +75 -0
- package/dist/lib/tokens/unit.js +52 -0
- package/dist/lib/version.js +8 -0
- package/dist/octez.js-michelson-encoder.es6.js +3949 -0
- package/dist/octez.js-michelson-encoder.es6.js.map +1 -0
- package/dist/octez.js-michelson-encoder.umd.js +3988 -0
- package/dist/octez.js-michelson-encoder.umd.js.map +1 -0
- package/dist/types/constants.d.ts +8 -0
- package/dist/types/errors.d.ts +17 -0
- package/dist/types/michelson-map.d.ts +82 -0
- package/dist/types/octez.js-michelson-encoder.d.ts +16 -0
- package/dist/types/schema/errors.d.ts +83 -0
- package/dist/types/schema/event-schema.d.ts +12 -0
- package/dist/types/schema/model.d.ts +62 -0
- package/dist/types/schema/parameter.d.ts +54 -0
- package/dist/types/schema/storage.d.ts +97 -0
- package/dist/types/schema/types.d.ts +105 -0
- package/dist/types/schema/view-schema.d.ts +53 -0
- package/dist/types/tokens/bigmap.d.ts +51 -0
- package/dist/types/tokens/bls12-381-fr.d.ts +51 -0
- package/dist/types/tokens/bls12-381-g1.d.ts +47 -0
- package/dist/types/tokens/bls12-381-g2.d.ts +47 -0
- package/dist/types/tokens/chain-id.d.ts +51 -0
- package/dist/types/tokens/chest-key.d.ts +47 -0
- package/dist/types/tokens/chest.d.ts +47 -0
- package/dist/types/tokens/comparable/address.d.ts +61 -0
- package/dist/types/tokens/comparable/bool.d.ts +32 -0
- package/dist/types/tokens/comparable/bytes.d.ts +52 -0
- package/dist/types/tokens/comparable/int.d.ts +56 -0
- package/dist/types/tokens/comparable/key_hash.d.ts +55 -0
- package/dist/types/tokens/comparable/mutez.d.ts +53 -0
- package/dist/types/tokens/comparable/nat.d.ts +55 -0
- package/dist/types/tokens/comparable/string.d.ts +31 -0
- package/dist/types/tokens/comparable/timestamp.d.ts +34 -0
- package/dist/types/tokens/constant.d.ts +51 -0
- package/dist/types/tokens/contract.d.ts +48 -0
- package/dist/types/tokens/createToken.d.ts +18 -0
- package/dist/types/tokens/key.d.ts +55 -0
- package/dist/types/tokens/lambda.d.ts +24 -0
- package/dist/types/tokens/list.d.ts +46 -0
- package/dist/types/tokens/map.d.ts +47 -0
- package/dist/types/tokens/never.d.ts +41 -0
- package/dist/types/tokens/operation.d.ts +24 -0
- package/dist/types/tokens/option.d.ts +37 -0
- package/dist/types/tokens/or.d.ts +53 -0
- package/dist/types/tokens/pair.d.ts +50 -0
- package/dist/types/tokens/sapling-state.d.ts +44 -0
- package/dist/types/tokens/sapling-transaction-deprecated.d.ts +40 -0
- package/dist/types/tokens/sapling-transaction.d.ts +40 -0
- package/dist/types/tokens/set.d.ts +43 -0
- package/dist/types/tokens/signature.d.ts +53 -0
- package/dist/types/tokens/ticket-deprecated.d.ts +38 -0
- package/dist/types/tokens/ticket.d.ts +38 -0
- package/dist/types/tokens/token.d.ts +68 -0
- package/dist/types/tokens/tokens.d.ts +36 -0
- package/dist/types/tokens/unit.d.ts +34 -0
- package/dist/types/version.d.ts +4 -0
- package/package.json +106 -0
- package/signature.json +582 -0
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# octez.js Michelson Encoder package
|
|
2
|
+
|
|
3
|
+
*Documentation can be found [here](https://octez.js.io/docs/michelson_encoder)*
|
|
4
|
+
*TypeDoc style documentation is available on-line [here](https://octez.js.io/typedoc/modules/_octez.js_michelson_encoder.html)*
|
|
5
|
+
|
|
6
|
+
**Maintained by [Nomadic Labs](https://www.nomadic-labs.com/).**
|
|
7
|
+
|
|
8
|
+
`@tezos-x/octez.js-michelson-encoder` provides a JavaScript abstraction based on Tezos Smart contract code, parameters, storage, and views.
|
|
9
|
+
|
|
10
|
+
## General Information
|
|
11
|
+
|
|
12
|
+
The Michelson-Encoder package aims to create an abstraction over the Michelson Language. It allows converting Michelson data into javascript-looking objects which are easier to use and reason about.
|
|
13
|
+
|
|
14
|
+
Its integration into the main octez.js package makes it easier to write the storage when deploying a contract and the parameter when calling a contract entry-point or executing a view. It also provides an abstraction on the storage read value of the contract.
|
|
15
|
+
|
|
16
|
+
The `Schema` class is intended to represent the contract storage, while the `ParameterSchema` represents its entry points. The `Execute` method takes a parameter in Michelson format and converts it into the JavaScript abstraction. The `Encode` method does the opposite; it takes a JavaScript object as a parameter and converts it into Michelson data.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
```
|
|
20
|
+
npm i --save @tezos-x/octez.js-michelson-encoder
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
## Example
|
|
26
|
+
|
|
27
|
+
The constructor of the `Schema` class takes a Michelson type as a parameter which is retrieved from a Tezos Nodes RPC:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
const storageType = {
|
|
31
|
+
prim: 'pair',
|
|
32
|
+
args: [
|
|
33
|
+
{ prim: 'nat', annots: [ '%stored_counter' ] },
|
|
34
|
+
{
|
|
35
|
+
prim: 'pair',
|
|
36
|
+
args: [
|
|
37
|
+
{ prim: 'nat', annots: [ '%threshold' ] },
|
|
38
|
+
{ prim: 'list', args: [ { prim: 'key' } ], annots: [ '%keys' ] }
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
};
|
|
43
|
+
const storageSchema = new Schema(storageType);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The `Execute` method takes a Michelson value as a parameter and generates an abstraction in the form of a plain old javascript object:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// dataMichelson is the storage of a contract retrieved from the RPC
|
|
50
|
+
const dataMichelson = {
|
|
51
|
+
"prim": "Pair",
|
|
52
|
+
"args": [
|
|
53
|
+
{
|
|
54
|
+
"int": "10"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"prim": "Pair",
|
|
58
|
+
"args": [
|
|
59
|
+
{
|
|
60
|
+
"int": "5"
|
|
61
|
+
},
|
|
62
|
+
[
|
|
63
|
+
{
|
|
64
|
+
"string": "edpkvS5QFv7KRGfa3b87gg9DBpxSm3NpSwnjhUjNBQrRUUR66F7C9g"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"string": "edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t"
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const data = storageSchema.Execute(dataMichelson);
|
|
76
|
+
console.log(data);
|
|
77
|
+
|
|
78
|
+
/* output:
|
|
79
|
+
{
|
|
80
|
+
"stored_counter": "10",
|
|
81
|
+
"threshold": "5",
|
|
82
|
+
"keys": [
|
|
83
|
+
"edpkvS5QFv7KRGfa3b87gg9DBpxSm3NpSwnjhUjNBQrRUUR66F7C9g",
|
|
84
|
+
"edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t"
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
*/
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Note that the Michelson type annotations represent the javascript object's keys.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
The `Encode` method takes a javascript object as a parameter and converts it into Michelson:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const data = storageSchema.Encode({
|
|
97
|
+
"stored_counter": "10",
|
|
98
|
+
"threshold": "5",
|
|
99
|
+
"keys": [
|
|
100
|
+
"edpkvS5QFv7KRGfa3b87gg9DBpxSm3NpSwnjhUjNBQrRUUR66F7C9g",
|
|
101
|
+
"edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t"
|
|
102
|
+
]
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
console.log(data);
|
|
106
|
+
|
|
107
|
+
/* output:
|
|
108
|
+
{
|
|
109
|
+
"prim": "Pair",
|
|
110
|
+
"args": [
|
|
111
|
+
{
|
|
112
|
+
"int": "10"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"prim": "Pair",
|
|
116
|
+
"args": [
|
|
117
|
+
{
|
|
118
|
+
"int": "5"
|
|
119
|
+
},
|
|
120
|
+
[
|
|
121
|
+
{
|
|
122
|
+
"string": "edpkvS5QFv7KRGfa3b87gg9DBpxSm3NpSwnjhUjNBQrRUUR66F7C9g"
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
"string": "edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t"
|
|
126
|
+
}
|
|
127
|
+
]
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
*/
|
|
133
|
+
```
|
|
134
|
+
## Additional info
|
|
135
|
+
|
|
136
|
+
See the top-level [https://github.com/trilitech/octez.js](https://github.com/trilitech/octez.js) file for details on reporting issues, contributing and versioning.
|
|
137
|
+
|
|
138
|
+
## Disclaimer
|
|
139
|
+
|
|
140
|
+
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.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SaplingStateValue = exports.UnitValue = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Symbol representing Unit value in Michelson
|
|
6
|
+
*/
|
|
7
|
+
exports.UnitValue = Symbol();
|
|
8
|
+
/**
|
|
9
|
+
* Sapling state placeholder value
|
|
10
|
+
*/
|
|
11
|
+
exports.SaplingStateValue = {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EncodeTicketError = exports.NatValidationError = exports.MutezValidationError = exports.KeyHashValidationError = exports.IntValidationError = exports.BytesValidationError = exports.AddressValidationError = exports.SignatureValidationError = exports.ContractValidationError = exports.KeyValidationError = exports.ChainIDValidationError = exports.OrValidationError = exports.TokenComparisonError = exports.TokenArgumentValidationError = exports.BigMapValidationError = exports.SetValidationError = exports.MapValidationError = exports.ListValidationError = void 0;
|
|
4
|
+
var list_1 = require("./tokens/list");
|
|
5
|
+
Object.defineProperty(exports, "ListValidationError", { enumerable: true, get: function () { return list_1.ListValidationError; } });
|
|
6
|
+
var map_1 = require("./tokens/map");
|
|
7
|
+
Object.defineProperty(exports, "MapValidationError", { enumerable: true, get: function () { return map_1.MapValidationError; } });
|
|
8
|
+
var set_1 = require("./tokens/set");
|
|
9
|
+
Object.defineProperty(exports, "SetValidationError", { enumerable: true, get: function () { return set_1.SetValidationError; } });
|
|
10
|
+
var bigmap_1 = require("./tokens/bigmap");
|
|
11
|
+
Object.defineProperty(exports, "BigMapValidationError", { enumerable: true, get: function () { return bigmap_1.BigMapValidationError; } });
|
|
12
|
+
var pair_1 = require("./tokens/pair");
|
|
13
|
+
Object.defineProperty(exports, "TokenArgumentValidationError", { enumerable: true, get: function () { return pair_1.TokenArgumentValidationError; } });
|
|
14
|
+
Object.defineProperty(exports, "TokenComparisonError", { enumerable: true, get: function () { return pair_1.TokenComparisonError; } });
|
|
15
|
+
var or_1 = require("./tokens/or");
|
|
16
|
+
Object.defineProperty(exports, "OrValidationError", { enumerable: true, get: function () { return or_1.OrValidationError; } });
|
|
17
|
+
var chain_id_1 = require("./tokens/chain-id");
|
|
18
|
+
Object.defineProperty(exports, "ChainIDValidationError", { enumerable: true, get: function () { return chain_id_1.ChainIDValidationError; } });
|
|
19
|
+
var key_1 = require("./tokens/key");
|
|
20
|
+
Object.defineProperty(exports, "KeyValidationError", { enumerable: true, get: function () { return key_1.KeyValidationError; } });
|
|
21
|
+
var contract_1 = require("./tokens/contract");
|
|
22
|
+
Object.defineProperty(exports, "ContractValidationError", { enumerable: true, get: function () { return contract_1.ContractValidationError; } });
|
|
23
|
+
var signature_1 = require("./tokens/signature");
|
|
24
|
+
Object.defineProperty(exports, "SignatureValidationError", { enumerable: true, get: function () { return signature_1.SignatureValidationError; } });
|
|
25
|
+
var address_1 = require("./tokens/comparable/address");
|
|
26
|
+
Object.defineProperty(exports, "AddressValidationError", { enumerable: true, get: function () { return address_1.AddressValidationError; } });
|
|
27
|
+
var bytes_1 = require("./tokens/comparable/bytes");
|
|
28
|
+
Object.defineProperty(exports, "BytesValidationError", { enumerable: true, get: function () { return bytes_1.BytesValidationError; } });
|
|
29
|
+
var int_1 = require("./tokens/comparable/int");
|
|
30
|
+
Object.defineProperty(exports, "IntValidationError", { enumerable: true, get: function () { return int_1.IntValidationError; } });
|
|
31
|
+
var key_hash_1 = require("./tokens/comparable/key_hash");
|
|
32
|
+
Object.defineProperty(exports, "KeyHashValidationError", { enumerable: true, get: function () { return key_hash_1.KeyHashValidationError; } });
|
|
33
|
+
var mutez_1 = require("./tokens/comparable/mutez");
|
|
34
|
+
Object.defineProperty(exports, "MutezValidationError", { enumerable: true, get: function () { return mutez_1.MutezValidationError; } });
|
|
35
|
+
var nat_1 = require("./tokens/comparable/nat");
|
|
36
|
+
Object.defineProperty(exports, "NatValidationError", { enumerable: true, get: function () { return nat_1.NatValidationError; } });
|
|
37
|
+
var ticket_1 = require("./tokens/ticket");
|
|
38
|
+
Object.defineProperty(exports, "EncodeTicketError", { enumerable: true, get: function () { return ticket_1.EncodeTicketError; } });
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.MichelsonMap = exports.MapTypecheckError = exports.InvalidMapTypeError = void 0;
|
|
5
|
+
const storage_1 = require("./schema/storage");
|
|
6
|
+
const fast_json_stable_stringify_1 = require("fast-json-stable-stringify");
|
|
7
|
+
const octez_js_core_1 = require("@tezos-x/octez.js-core");
|
|
8
|
+
/**
|
|
9
|
+
* @category Error
|
|
10
|
+
* @description Error that indicates an invalid map type being passed or used
|
|
11
|
+
*/
|
|
12
|
+
class InvalidMapTypeError extends octez_js_core_1.TaquitoError {
|
|
13
|
+
constructor(mapType, reason) {
|
|
14
|
+
super();
|
|
15
|
+
this.mapType = mapType;
|
|
16
|
+
this.reason = reason;
|
|
17
|
+
this.message = `The map type '${JSON.stringify(mapType)}' is invalid. Reason: ${reason}.`;
|
|
18
|
+
this.name = 'InvalidMapTypeError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.InvalidMapTypeError = InvalidMapTypeError;
|
|
22
|
+
// Retrieve a unique symbol associated with the key from the environment
|
|
23
|
+
// Used in order to identify all object that are of type MichelsonMap even if they come from different module
|
|
24
|
+
const michelsonMapTypeSymbol = Symbol.for('octez-michelson-map-type-symbol');
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @throws {@link InvalidMapTypeError} when the argument passed to mapType is not a valid map type
|
|
28
|
+
*/
|
|
29
|
+
function validateMapType(value) {
|
|
30
|
+
if (!('prim' in value)) {
|
|
31
|
+
throw new InvalidMapTypeError(value, `Missing 'prim' field`);
|
|
32
|
+
}
|
|
33
|
+
if (!['map', 'big_map'].includes(value.prim)) {
|
|
34
|
+
throw new InvalidMapTypeError(value, `The prim field should be 'map' or 'big_map'`);
|
|
35
|
+
}
|
|
36
|
+
if (!('args' in value)) {
|
|
37
|
+
throw new InvalidMapTypeError(value, `Missing 'args' field`);
|
|
38
|
+
}
|
|
39
|
+
if (!Array.isArray(value.args)) {
|
|
40
|
+
throw new InvalidMapTypeError(value, `The 'args' field should be an array`);
|
|
41
|
+
}
|
|
42
|
+
if (value.args.length !== 2) {
|
|
43
|
+
throw new InvalidMapTypeError(value, `The 'args' field should have 2 elements`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @category Error
|
|
48
|
+
* @description Error that indicates a map type mismatch, where an attempt to set a key or value in a Map doesn't match the defined type of the Map
|
|
49
|
+
*/
|
|
50
|
+
class MapTypecheckError extends octez_js_core_1.TaquitoError {
|
|
51
|
+
constructor(value, type, objectType, reason) {
|
|
52
|
+
super();
|
|
53
|
+
this.value = value;
|
|
54
|
+
this.type = type;
|
|
55
|
+
this.reason = reason;
|
|
56
|
+
this.name = 'MapTypecheckError';
|
|
57
|
+
this.message = `The ${objectType} provided: ${JSON.stringify(value)} is not compatible with the expected michelson type: ${JSON.stringify(type)}. Reason: ${JSON.stringify(reason)}.`;
|
|
58
|
+
this.name = 'MapTypecheckError';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.MapTypecheckError = MapTypecheckError;
|
|
62
|
+
/**
|
|
63
|
+
* @description Michelson Map is an abstraction over the michelson native map. It supports complex Pair as key
|
|
64
|
+
*/
|
|
65
|
+
class MichelsonMap {
|
|
66
|
+
// Used to check if an object is a michelson map.
|
|
67
|
+
// Using instanceof was not working for project that had multiple instance of octez.js dependencies
|
|
68
|
+
// as the class constructor is different
|
|
69
|
+
static isMichelsonMap(obj) {
|
|
70
|
+
return obj && obj[michelsonMapTypeSymbol] === true;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @param mapType If specified key and value will be type-checked before being added to the map
|
|
74
|
+
*
|
|
75
|
+
* @example new MichelsonMap({ prim: "map", args: [{prim: "string"}, {prim: "int"}]})
|
|
76
|
+
*/
|
|
77
|
+
constructor(mapType) {
|
|
78
|
+
this.valueMap = new Map();
|
|
79
|
+
this.keyMap = new Map();
|
|
80
|
+
this[_a] = true;
|
|
81
|
+
if (mapType) {
|
|
82
|
+
this.setType(mapType);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
setType(mapType) {
|
|
86
|
+
validateMapType(mapType);
|
|
87
|
+
this.keySchema = new storage_1.Schema(mapType.args[0]);
|
|
88
|
+
this.valueSchema = new storage_1.Schema(mapType.args[1]);
|
|
89
|
+
}
|
|
90
|
+
removeType() {
|
|
91
|
+
this.keySchema = undefined;
|
|
92
|
+
this.valueSchema = undefined;
|
|
93
|
+
}
|
|
94
|
+
static fromLiteral(obj, mapType) {
|
|
95
|
+
const map = new MichelsonMap(mapType);
|
|
96
|
+
Object.keys(obj).forEach((key) => {
|
|
97
|
+
map.set(key, obj[key]);
|
|
98
|
+
});
|
|
99
|
+
return map;
|
|
100
|
+
}
|
|
101
|
+
typecheckKey(key) {
|
|
102
|
+
if (!this.keySchema) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (!this.keySchema.Typecheck(key)) {
|
|
106
|
+
throw new MapTypecheckError(key, this.keySchema, 'key', 'Type validation failed');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
typecheckValue(value) {
|
|
110
|
+
if (!this.valueSchema) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (!this.valueSchema.Typecheck(value)) {
|
|
114
|
+
throw new MapTypecheckError(value, this.valueSchema, 'value', 'Type validation failed');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @throws {@link MapTypecheckError} when the argument passed does not match the expected schema for value
|
|
119
|
+
*/
|
|
120
|
+
assertTypecheckValue(value) {
|
|
121
|
+
this.typecheckValue(value);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @throws {@link MapTypecheckError} when the argument passed does not match the expected schema for key
|
|
125
|
+
*/
|
|
126
|
+
assertTypecheckKey(key) {
|
|
127
|
+
this.typecheckKey(key);
|
|
128
|
+
}
|
|
129
|
+
serializeDeterministically(key) {
|
|
130
|
+
return (0, fast_json_stable_stringify_1.default)(key);
|
|
131
|
+
}
|
|
132
|
+
*keys() {
|
|
133
|
+
for (const [key] of this.entries()) {
|
|
134
|
+
yield key;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
*values() {
|
|
138
|
+
for (const [, value] of this.entries()) {
|
|
139
|
+
yield value;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
*entries() {
|
|
143
|
+
for (const key of this.valueMap.keys()) {
|
|
144
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
145
|
+
yield [this.keyMap.get(key), this.valueMap.get(key)];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
get(key) {
|
|
149
|
+
this.assertTypecheckKey(key);
|
|
150
|
+
const strKey = this.serializeDeterministically(key);
|
|
151
|
+
return this.valueMap.get(strKey);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
*
|
|
155
|
+
* @description Set a key and a value in the MichelsonMap. If the key already exists, override the current value.
|
|
156
|
+
*
|
|
157
|
+
* @example map.set("myKey", "myValue") // Using a string as key
|
|
158
|
+
*
|
|
159
|
+
* @example map.set({0: "test", 1: "test1"}, "myValue") // Using a pair as key
|
|
160
|
+
*
|
|
161
|
+
* @warn The same key can be represented in multiple ways, depending on the type of the key. This duplicate key situation will cause a runtime error (duplicate key) when sending the map data to the Tezos RPC node.
|
|
162
|
+
*
|
|
163
|
+
* For example, consider a contract with a map whose key is of type boolean. If you set the following values in MichelsonMap: map.set(false, "myValue") and map.set(null, "myValue").
|
|
164
|
+
*
|
|
165
|
+
* You will get two unique entries in the MichelsonMap. These values will both be evaluated as falsy by the MichelsonEncoder and ultimately rejected by the Tezos RPC.
|
|
166
|
+
*/
|
|
167
|
+
set(key, value) {
|
|
168
|
+
this.assertTypecheckKey(key);
|
|
169
|
+
this.assertTypecheckValue(value);
|
|
170
|
+
const strKey = this.serializeDeterministically(key);
|
|
171
|
+
this.keyMap.set(strKey, key);
|
|
172
|
+
this.valueMap.set(strKey, value);
|
|
173
|
+
}
|
|
174
|
+
delete(key) {
|
|
175
|
+
this.assertTypecheckKey(key);
|
|
176
|
+
this.keyMap.delete(this.serializeDeterministically(key));
|
|
177
|
+
this.valueMap.delete(this.serializeDeterministically(key));
|
|
178
|
+
}
|
|
179
|
+
has(key) {
|
|
180
|
+
this.assertTypecheckKey(key);
|
|
181
|
+
const strKey = this.serializeDeterministically(key);
|
|
182
|
+
return this.keyMap.has(strKey) && this.valueMap.has(strKey);
|
|
183
|
+
}
|
|
184
|
+
clear() {
|
|
185
|
+
this.keyMap.clear();
|
|
186
|
+
this.valueMap.clear();
|
|
187
|
+
}
|
|
188
|
+
get size() {
|
|
189
|
+
return this.keyMap.size;
|
|
190
|
+
}
|
|
191
|
+
forEach(cb) {
|
|
192
|
+
for (const [key, value] of this.entries()) {
|
|
193
|
+
cb(value, key, this);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.MichelsonMap = MichelsonMap;
|
|
198
|
+
_a = michelsonMapTypeSymbol;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
* @module @tezos-x/octez.js-michelson-encoder
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
18
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.Token = exports.VERSION = exports.SaplingStateValue = exports.UnitValue = void 0;
|
|
22
|
+
__exportStar(require("./schema/storage"), exports);
|
|
23
|
+
__exportStar(require("./schema/parameter"), exports);
|
|
24
|
+
__exportStar(require("./schema/view-schema"), exports);
|
|
25
|
+
__exportStar(require("./schema/event-schema"), exports);
|
|
26
|
+
__exportStar(require("./schema/errors"), exports);
|
|
27
|
+
__exportStar(require("./schema/types"), exports);
|
|
28
|
+
__exportStar(require("./errors"), exports);
|
|
29
|
+
var constants_1 = require("./constants");
|
|
30
|
+
Object.defineProperty(exports, "UnitValue", { enumerable: true, get: function () { return constants_1.UnitValue; } });
|
|
31
|
+
Object.defineProperty(exports, "SaplingStateValue", { enumerable: true, get: function () { return constants_1.SaplingStateValue; } });
|
|
32
|
+
__exportStar(require("./michelson-map"), exports);
|
|
33
|
+
var version_1 = require("./version");
|
|
34
|
+
Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return version_1.VERSION; } });
|
|
35
|
+
var token_1 = require("./tokens/token");
|
|
36
|
+
Object.defineProperty(exports, "Token", { enumerable: true, get: function () { return token_1.Token; } });
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.MissingArgumentError = exports.StorageEncodingError = exports.BigMapEncodingError = exports.InvalidBigMapDiffError = exports.InvalidBigMapSchemaError = exports.InvalidRpcResponseError = exports.InvalidScriptError = exports.ParameterEncodingError = void 0;
|
|
5
|
+
const octez_js_core_1 = require("@tezos-x/octez.js-core");
|
|
6
|
+
/**
|
|
7
|
+
* @category Error
|
|
8
|
+
* @description Error that indicates a failure when encoding (transforming JS parameter into JSON Michelson) the parameter of the view
|
|
9
|
+
*/
|
|
10
|
+
class ParameterEncodingError extends octez_js_core_1.InvalidViewParameterError {
|
|
11
|
+
constructor(viewName, sigs, args, cause) {
|
|
12
|
+
super(viewName, sigs, args, cause);
|
|
13
|
+
this.viewName = viewName;
|
|
14
|
+
this.sigs = sigs;
|
|
15
|
+
this.args = args;
|
|
16
|
+
this.cause = cause;
|
|
17
|
+
this.name = 'ParameterEncodingError';
|
|
18
|
+
this.message = `Could not encode parameter ${JSON.stringify(args)} received for name "${viewName}" expecting one of the following signatures ${JSON.stringify(sigs)}`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.ParameterEncodingError = ParameterEncodingError;
|
|
22
|
+
/**
|
|
23
|
+
* @category Error
|
|
24
|
+
* @description Error that indicates an invalid on-chain view found on the script
|
|
25
|
+
*/
|
|
26
|
+
class InvalidScriptError extends octez_js_core_1.TaquitoError {
|
|
27
|
+
constructor(script, reason) {
|
|
28
|
+
super();
|
|
29
|
+
this.script = script;
|
|
30
|
+
this.reason = reason;
|
|
31
|
+
this.name = 'InvalidScriptError';
|
|
32
|
+
let message = `Invalid on-chain view found in the following script.`;
|
|
33
|
+
if (reason) {
|
|
34
|
+
message += ` Reason: ${reason}.`;
|
|
35
|
+
}
|
|
36
|
+
message += `Script: ${JSON.stringify(script)}`;
|
|
37
|
+
this.message = message;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.InvalidScriptError = InvalidScriptError;
|
|
41
|
+
/**
|
|
42
|
+
* @category Error
|
|
43
|
+
* @description Error that indicates an invalid RPC response being passed or used
|
|
44
|
+
*/
|
|
45
|
+
class InvalidRpcResponseError extends octez_js_core_1.TaquitoError {
|
|
46
|
+
constructor(script, reason) {
|
|
47
|
+
super();
|
|
48
|
+
this.script = script;
|
|
49
|
+
this.reason = reason;
|
|
50
|
+
this.name = 'InvalidRpcResponseError';
|
|
51
|
+
let message = `Invalid RPC response passed as argument(s).`;
|
|
52
|
+
if (reason) {
|
|
53
|
+
message += ` Reason: ${reason}.`;
|
|
54
|
+
}
|
|
55
|
+
message += ` Received: ${JSON.stringify(script)}`;
|
|
56
|
+
this.message = message;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.InvalidRpcResponseError = InvalidRpcResponseError;
|
|
60
|
+
/**
|
|
61
|
+
* @category Error
|
|
62
|
+
* @description Error that indicates an invalid big map schema being passed or used
|
|
63
|
+
*/
|
|
64
|
+
class InvalidBigMapSchemaError extends octez_js_core_1.TaquitoError {
|
|
65
|
+
constructor(message) {
|
|
66
|
+
super(message);
|
|
67
|
+
this.message = message;
|
|
68
|
+
this.name = 'InvalidBigMapSchemaError';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.InvalidBigMapSchemaError = InvalidBigMapSchemaError;
|
|
72
|
+
/**
|
|
73
|
+
* @category Error
|
|
74
|
+
* @description Error that indicates an invalid big map diff being passed or used
|
|
75
|
+
*/
|
|
76
|
+
class InvalidBigMapDiffError extends octez_js_core_1.TaquitoError {
|
|
77
|
+
constructor(message, value) {
|
|
78
|
+
super(message);
|
|
79
|
+
this.message = message;
|
|
80
|
+
this.value = value;
|
|
81
|
+
this.name = 'InvalidBigMapDiffError';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.InvalidBigMapDiffError = InvalidBigMapDiffError;
|
|
85
|
+
/**
|
|
86
|
+
* @category Error
|
|
87
|
+
* @description Error that indicates a failure when trying to encode big maps
|
|
88
|
+
*/
|
|
89
|
+
class BigMapEncodingError extends octez_js_core_1.TaquitoError {
|
|
90
|
+
constructor(obj, details, schema, value) {
|
|
91
|
+
super();
|
|
92
|
+
this.details = details;
|
|
93
|
+
this.schema = schema;
|
|
94
|
+
this.value = value;
|
|
95
|
+
this.name = 'BigMapEncodingError';
|
|
96
|
+
this.message = `Unable to encode the big map ${obj}. Schema is: ${JSON.stringify(schema)}. The ${obj} is: ${JSON.stringify(value)}. Error details: ${details}`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.BigMapEncodingError = BigMapEncodingError;
|
|
100
|
+
/**
|
|
101
|
+
* @category Error
|
|
102
|
+
* @description Error that indicates a failure when trying to encode storage
|
|
103
|
+
*/
|
|
104
|
+
class StorageEncodingError extends octez_js_core_1.TaquitoError {
|
|
105
|
+
constructor(obj, details, schema, value, semantics) {
|
|
106
|
+
super();
|
|
107
|
+
this.details = details;
|
|
108
|
+
this.schema = schema;
|
|
109
|
+
this.value = value;
|
|
110
|
+
this.semantics = semantics;
|
|
111
|
+
this.name = 'StorageEncodingError';
|
|
112
|
+
this.message = `Unable to encode ${obj}. The schema is: ${JSON.stringify(schema)}, the value is: ${JSON.stringify(value)}.${semantics ? `And the semantic is: ${JSON.stringify(semantics)}` : ''}. Error details: ${details}`;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.StorageEncodingError = StorageEncodingError;
|
|
116
|
+
/**
|
|
117
|
+
* @category Error
|
|
118
|
+
* @description General error that indicates a function not being passed a necessary argument
|
|
119
|
+
*/
|
|
120
|
+
class MissingArgumentError extends octez_js_core_1.TaquitoError {
|
|
121
|
+
constructor(message) {
|
|
122
|
+
super(message);
|
|
123
|
+
this.message = message;
|
|
124
|
+
this.name = 'MissingArgumentError';
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.MissingArgumentError = MissingArgumentError;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventSchema = void 0;
|
|
4
|
+
const storage_1 = require("./storage");
|
|
5
|
+
class EventSchema {
|
|
6
|
+
constructor(tag, type) {
|
|
7
|
+
this.tag = tag;
|
|
8
|
+
this.type = type;
|
|
9
|
+
}
|
|
10
|
+
static fromMichelineValue(val) {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
return new EventSchema((_a = val.annots) === null || _a === void 0 ? void 0 : _a[0], (_b = val.args) === null || _b === void 0 ? void 0 : _b[0]);
|
|
13
|
+
}
|
|
14
|
+
static fromRPCResponse(val) {
|
|
15
|
+
const allEventSchema = [];
|
|
16
|
+
val.script.code.forEach((code) => {
|
|
17
|
+
if (!('prim' in code) ||
|
|
18
|
+
code.prim !== 'code' ||
|
|
19
|
+
!('args' in code) ||
|
|
20
|
+
!code.args) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
allEventSchema.push(...EventSchema.extractEventsRecursively(code.args));
|
|
24
|
+
});
|
|
25
|
+
return EventSchema.removeDuplicates(allEventSchema);
|
|
26
|
+
}
|
|
27
|
+
static removeDuplicates(events) {
|
|
28
|
+
const uniqueEvents = [];
|
|
29
|
+
events.forEach((event) => {
|
|
30
|
+
const idx = uniqueEvents.findIndex((e) => e.tag === event.tag && (0, storage_1.deepEqual)(e.type, event.type));
|
|
31
|
+
if (idx === -1) {
|
|
32
|
+
uniqueEvents.push(event);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return uniqueEvents;
|
|
36
|
+
}
|
|
37
|
+
static extractEventsRecursively(code) {
|
|
38
|
+
if (Array.isArray(code)) {
|
|
39
|
+
return code.flatMap((c) => EventSchema.extractEventsRecursively(c));
|
|
40
|
+
}
|
|
41
|
+
if (!('prim' in code)) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
if (code.prim === 'EMIT') {
|
|
45
|
+
return [EventSchema.fromMichelineValue(code)];
|
|
46
|
+
}
|
|
47
|
+
if (!('args' in code) || !code.args) {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
return code.args.flatMap((c) => EventSchema.extractEventsRecursively(c));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.EventSchema = EventSchema;
|