@taquito/tzip16 25.1.1 → 25.2.0-beta1

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.
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Tzip16Module = exports.DEFAULT_HANDLERS = void 0;
4
- const metadata_provider_1 = require("./metadata-provider");
5
- const http_handler_1 = require("./handlers/http-handler");
6
- const tezos_storage_handler_1 = require("./handlers/tezos-storage-handler");
7
- const ipfs_handler_1 = require("./handlers/ipfs-handler");
8
- exports.DEFAULT_HANDLERS = new Map([
9
- ['http', new http_handler_1.HttpHandler()],
10
- ['https', new http_handler_1.HttpHandler()],
11
- ['tezos-storage', new tezos_storage_handler_1.TezosStorageHandler()],
12
- ['ipfs', new ipfs_handler_1.IpfsHttpHandler()]
13
- ]);
14
- class Tzip16Module {
15
- constructor(metadataProvider) {
16
- this._metadataProvider = metadataProvider ? metadataProvider : new metadata_provider_1.MetadataProvider(exports.DEFAULT_HANDLERS);
17
- }
18
- configureContext(context) {
19
- Object.assign(context, { metadataProvider: this._metadataProvider });
20
- }
21
- }
22
- exports.Tzip16Module = Tzip16Module;
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateSHA256Hash = calculateSHA256Hash;
4
- const crypto_js_1 = require("crypto-js");
5
- function calculateSHA256Hash(preimage) {
6
- return crypto_js_1.default.SHA256(preimage).toString(crypto_js_1.default.enc.Hex);
7
- }
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VERSION = void 0;
4
- // IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT!
5
- exports.VERSION = {
6
- "commitHash": "1cc2663cf397066ce46065a2595aaeaf41c42552",
7
- "version": "25.1.1"
8
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,168 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MichelsonStorageView = void 0;
4
- const michelson_encoder_1 = require("@taquito/michelson-encoder");
5
- const taquito_1 = require("@taquito/taquito");
6
- const errors_1 = require("../errors");
7
- const taquito_2 = require("@taquito/taquito");
8
- const core_1 = require("@taquito/core");
9
- class MichelsonStorageView {
10
- constructor(viewName, contract, rpc, readProvider, returnType, code, viewParameterType) {
11
- this.viewName = viewName;
12
- this.contract = contract;
13
- this.rpc = rpc;
14
- this.readProvider = readProvider;
15
- this.returnType = returnType;
16
- this.code = code;
17
- this.viewParameterType = viewParameterType;
18
- this.validateArgs = (args, schema, viewName) => {
19
- const sigs = schema.ExtractSignatures();
20
- if (!sigs.find((x) => x.length === args.length)) {
21
- throw new core_1.InvalidViewParameterError(viewName, sigs, args);
22
- }
23
- };
24
- }
25
- /**
26
- * According to the tzip-16 standard, the following instructions must not be used in the code of the view:
27
- * 'AMOUNT', 'CREATE_CONTRACT', 'SENDER', 'SET_DELEGATE', 'SOURCE', and 'TRANSFER_TOKENS'
28
- * The method throw an error if an illegal instruction is found
29
- */
30
- findForbiddenInstructionInViewCodeError(code) {
31
- const illegalInstructions = [
32
- 'AMOUNT',
33
- 'CREATE_CONTRACT',
34
- 'SENDER',
35
- 'SET_DELEGATE',
36
- 'SOURCE',
37
- 'TRANSFER_TOKENS',
38
- ];
39
- for (const forbiddenInstruction of illegalInstructions) {
40
- for (const instruction of code) {
41
- if (instruction.prim === forbiddenInstruction) {
42
- throw new errors_1.ForbiddenInstructionInViewCodeError(forbiddenInstruction);
43
- }
44
- if (instruction.args && instruction.args.length !== 0) {
45
- this.findForbiddenInstructionInViewCodeError(instruction.args);
46
- }
47
- }
48
- }
49
- }
50
- /**
51
- * According to the tzip-16 standard, in the first version of the specification, the instruction SELF should only be used before ADDRESS
52
- * The method throws an error is the instruction SELF is present, but not followed by ADDRESS
53
- */
54
- illegalUseOfSelfInstruction(code) {
55
- for (const instruction of code) {
56
- if (instruction.prim === 'SELF') {
57
- const index = code.indexOf(instruction);
58
- const nextInstruction = code[index + 1] ? code[index + 1].prim : undefined;
59
- if (nextInstruction !== 'ADDRESS') {
60
- throw new errors_1.ForbiddenInstructionInViewCodeError('the instruction SELF should only be used before ADDRESS');
61
- }
62
- }
63
- if (instruction.args && instruction.args.length !== 0) {
64
- this.illegalUseOfSelfInstruction(instruction.args);
65
- }
66
- }
67
- }
68
- /**
69
- * Loops through the view's code and replace SELF, BALANCE, NOW, and CHAIN_ID with Michelson expressions that match the current context, if applicable.
70
- */
71
- adaptViewCodeToContext(code, contractBalance, blockTimeStamp, chainId) {
72
- const instructionsToReplace = {
73
- SELF: [
74
- { prim: 'PUSH', args: [{ prim: 'address' }, { string: this.contract.address }] },
75
- { prim: 'CONTRACT', args: [{ prim: 'unit' }] },
76
- { prim: 'IF_NONE', args: [[{ prim: 'UNIT' }, { prim: 'FAILWITH' }], []] },
77
- ],
78
- BALANCE: [{ prim: 'PUSH', args: [{ prim: 'mutez' }, { int: contractBalance }] }],
79
- NOW: [{ prim: 'PUSH', args: [{ prim: 'timestamp' }, { string: blockTimeStamp }] }],
80
- CHAIN_ID: [{ prim: 'PUSH', args: [{ prim: 'string' }, { string: chainId }] }],
81
- };
82
- code.forEach((x, i) => {
83
- for (const instruction in instructionsToReplace) {
84
- if (x.prim === instruction) {
85
- code[i] = Object(instructionsToReplace)[instruction];
86
- }
87
- if (x.args && x.args.length !== 0) {
88
- this.adaptViewCodeToContext(x.args, contractBalance, blockTimeStamp, chainId);
89
- }
90
- }
91
- });
92
- return code;
93
- }
94
- formatArgsAndParameter(argView) {
95
- let args = argView;
96
- let viewParameterType = this.viewParameterType;
97
- if (!viewParameterType) {
98
- viewParameterType = { args: [], prim: 'unit', annots: [] };
99
- }
100
- if (viewParameterType.prim === 'unit') {
101
- if (args.length === 0) {
102
- args = ['Unit'];
103
- }
104
- if (args.length !== 0 && args[0] !== 'Unit') {
105
- throw new errors_1.NoParameterExpectedError(this.viewName, argView);
106
- }
107
- }
108
- const parameterViewSchema = new michelson_encoder_1.ParameterSchema(viewParameterType);
109
- this.validateArgs(args, parameterViewSchema, this.viewName);
110
- const arg = parameterViewSchema.Encode(...args);
111
- return { arg, viewParameterType };
112
- }
113
- async executeView(...args) {
114
- // validate view code against tzip-16 specifications
115
- this.findForbiddenInstructionInViewCodeError(this.code);
116
- this.illegalUseOfSelfInstruction(this.code);
117
- const { arg, viewParameterType } = this.formatArgsAndParameter(args);
118
- const storageType = this.contract.script.code.find((x) => x.prim === 'storage');
119
- const storageArgs = storageType.args[0];
120
- // currentContext
121
- const storageValue = this.contract.readBlock === 'head'
122
- ? await this.readProvider.getStorage(this.contract.address, 'head')
123
- : this.contract.script.storage;
124
- const chainId = await this.readProvider.getChainId();
125
- const contractBalance = (await this.readProvider.getBalance(this.contract.address, this.contract.readBlock)).toString();
126
- const blockTimestamp = await this.readProvider.getBlockTimestamp(this.contract.readBlock);
127
- const code = this.adaptViewCodeToContext(this.code, contractBalance, blockTimestamp, chainId);
128
- if (!this.viewParameterType) {
129
- code.unshift({ prim: 'CDR' });
130
- }
131
- const viewScript = {
132
- script: [
133
- { prim: 'parameter', args: [{ prim: 'pair', args: [viewParameterType, storageArgs] }] },
134
- { prim: 'storage', args: [{ prim: 'option', args: [this.returnType] }] },
135
- {
136
- prim: 'code',
137
- args: [
138
- [
139
- { prim: 'CAR' },
140
- code,
141
- { prim: 'SOME' },
142
- { prim: 'NIL', args: [{ prim: 'operation' }] },
143
- { prim: 'PAIR' },
144
- ],
145
- ],
146
- },
147
- ],
148
- storage: { prim: 'None' },
149
- input: { prim: 'Pair', args: [arg, storageValue] },
150
- amount: '0',
151
- chain_id: chainId,
152
- balance: '0',
153
- };
154
- let result;
155
- try {
156
- result = await this.rpc.runCode(viewScript);
157
- }
158
- catch (error) {
159
- const failWith = (0, taquito_2.validateAndExtractFailwith)(error);
160
- throw failWith
161
- ? new taquito_1.ViewSimulationError(`The simulation of the Michelson view failed with: ${JSON.stringify(failWith)}`, this.viewName, failWith, error)
162
- : error;
163
- }
164
- const viewResultSchema = new michelson_encoder_1.ParameterSchema(this.returnType);
165
- return viewResultSchema.Execute(result.storage.args[0]);
166
- }
167
- }
168
- exports.MichelsonStorageView = MichelsonStorageView;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ViewFactory = void 0;
4
- const metadata_interface_1 = require("../metadata-interface");
5
- const michelson_storage_view_1 = require("./michelson-storage-view");
6
- class ViewFactory {
7
- getView(viewName, rpc, readProvider, contract, viewImplementation) {
8
- if (this.isMichelsonStorageView(viewImplementation)) {
9
- const viewValues = viewImplementation[metadata_interface_1.ViewImplementationType.MICHELSON_STORAGE];
10
- if (!viewValues.returnType || !viewValues.code) {
11
- console.warn(`${viewName} is missing mandatory code or returnType property therefore it will be skipped.`);
12
- return;
13
- }
14
- return () => {
15
- const view = new michelson_storage_view_1.MichelsonStorageView(viewName, contract, rpc, readProvider, viewValues.returnType, viewValues.code, viewValues.parameter);
16
- return view;
17
- };
18
- }
19
- }
20
- getImplementationType(viewImplementation) {
21
- return Object.keys(viewImplementation)[0];
22
- }
23
- isMichelsonStorageView(viewImplementation) {
24
- return (this.getImplementationType(viewImplementation) === metadata_interface_1.ViewImplementationType.MICHELSON_STORAGE);
25
- }
26
- }
27
- exports.ViewFactory = ViewFactory;