@taquito/tzip16 25.2.0-beta0 → 25.2.0-beta2

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,140 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Tzip16ContractAbstraction = void 0;
7
- const taquito_1 = require("@taquito/taquito");
8
- const utils_1 = require("@taquito/utils");
9
- const errors_1 = require("./errors");
10
- const bignumber_js_1 = __importDefault(require("bignumber.js"));
11
- const michelson_encoder_1 = require("@taquito/michelson-encoder");
12
- const viewFactory_1 = require("./viewKind/viewFactory");
13
- const metadataBigMapType = {
14
- prim: 'big_map',
15
- args: [{ prim: 'string' }, { prim: 'bytes' }],
16
- annots: ['%metadata'],
17
- };
18
- class Tzip16ContractAbstraction {
19
- constructor(constractAbstraction, context) {
20
- this.constractAbstraction = constractAbstraction;
21
- this.context = context;
22
- this._viewFactory = new viewFactory_1.ViewFactory();
23
- this._metadataViewsObject = {};
24
- this._metadataProvider = context.metadataProvider;
25
- }
26
- async findMetadataBigMap() {
27
- const metadataBigMapId = this.constractAbstraction.schema.FindFirstInTopLevelPair(this.constractAbstraction.readBlock === 'head'
28
- ? await this.context.readProvider.getStorage(this.constractAbstraction.address, 'head')
29
- : this.constractAbstraction.script.storage, metadataBigMapType);
30
- if (!metadataBigMapId || !metadataBigMapId.int) {
31
- throw new errors_1.BigMapContractMetadataNotFoundError(metadataBigMapId);
32
- }
33
- return new taquito_1.BigMapAbstraction(new bignumber_js_1.default(metadataBigMapId['int']), new michelson_encoder_1.Schema(metadataBigMapType), this.context.contract, this.constractAbstraction.readBlock);
34
- }
35
- async getUriOrFail() {
36
- const metadataBigMap = await this.findMetadataBigMap();
37
- const uri = await metadataBigMap.get('');
38
- if (!uri) {
39
- throw new errors_1.UriNotFoundError();
40
- }
41
- return uri;
42
- }
43
- /**
44
- * Return an object containing the metadata, the uri, an optional integrity check result and an optional sha256 hash
45
- */
46
- async getMetadata() {
47
- if (!this._metadataProvider) {
48
- throw new errors_1.UnconfiguredContractMetadataProviderError();
49
- }
50
- if (!this._metadataEnvelope) {
51
- const uri = await this.getUriOrFail();
52
- this._metadataEnvelope = await this._metadataProvider.provideMetadata(this.constractAbstraction, (0, utils_1.bytesToString)(uri), this.context);
53
- }
54
- return this._metadataEnvelope;
55
- }
56
- async metadataName() {
57
- if (!this._metadataEnvelope) {
58
- await this.getMetadata();
59
- }
60
- return this._metadataEnvelope.metadata.name;
61
- }
62
- async metadataDescription() {
63
- if (!this._metadataEnvelope)
64
- await this.getMetadata();
65
- return this._metadataEnvelope.metadata.description;
66
- }
67
- async metadataVersion() {
68
- if (!this._metadataEnvelope)
69
- await this.getMetadata();
70
- return this._metadataEnvelope.metadata.version;
71
- }
72
- async metadataLicense() {
73
- if (!this._metadataEnvelope)
74
- await this.getMetadata();
75
- return this._metadataEnvelope.metadata.license;
76
- }
77
- async metadataAuthors() {
78
- if (!this._metadataEnvelope)
79
- await this.getMetadata();
80
- return this._metadataEnvelope.metadata.authors;
81
- }
82
- async metadataHomepage() {
83
- if (!this._metadataEnvelope)
84
- await this.getMetadata();
85
- return this._metadataEnvelope.metadata.homepage;
86
- }
87
- async metadataSource() {
88
- if (!this._metadataEnvelope)
89
- await this.getMetadata();
90
- return this._metadataEnvelope.metadata.source;
91
- }
92
- async metadataInterfaces() {
93
- if (!this._metadataEnvelope)
94
- await this.getMetadata();
95
- return this._metadataEnvelope.metadata.interfaces;
96
- }
97
- async metadataErrors() {
98
- if (!this._metadataEnvelope)
99
- await this.getMetadata();
100
- return this._metadataEnvelope.metadata.errors;
101
- }
102
- async metadataViews() {
103
- if (Object.keys(this._metadataViewsObject).length === 0) {
104
- await this.initializeMetadataViewsList();
105
- }
106
- return this._metadataViewsObject;
107
- }
108
- async initializeMetadataViewsList() {
109
- const { metadata } = await this.getMetadata();
110
- const metadataViews = {};
111
- metadata.views?.forEach((view) => this.createViewImplementations(view, metadataViews));
112
- this._metadataViewsObject = metadataViews;
113
- }
114
- generateIndexedViewName(viewName, metadataViews) {
115
- let i = 1;
116
- if (viewName in metadataViews) {
117
- while (`${viewName}${i}` in metadataViews) {
118
- i++;
119
- }
120
- viewName = `${viewName}${i}`;
121
- }
122
- return viewName;
123
- }
124
- createViewImplementations(view, metadataViews) {
125
- for (const viewImplementation of view?.implementations ?? []) {
126
- if (view.name) {
127
- // when views have the same name, add an index at the end of the name
128
- const viewName = this.generateIndexedViewName(view.name, metadataViews);
129
- const metadataView = this._viewFactory.getView(viewName, this.context.rpc, this.context.readProvider, this.constractAbstraction, viewImplementation);
130
- if (metadataView) {
131
- metadataViews[viewName] = metadataView;
132
- }
133
- else {
134
- console.warn(`Skipped generating ${viewName} because the view has an unsupported type: ${this._viewFactory.getImplementationType(viewImplementation)}`);
135
- }
136
- }
137
- }
138
- }
139
- }
140
- exports.Tzip16ContractAbstraction = Tzip16ContractAbstraction;
@@ -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,10 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.calculateSHA256Hash = calculateSHA256Hash;
7
- const crypto_js_1 = __importDefault(require("crypto-js"));
8
- function calculateSHA256Hash(preimage) {
9
- return crypto_js_1.default.SHA256(preimage).toString(crypto_js_1.default.enc.Hex);
10
- }
@@ -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": "78ca0b9cb430f230c3230cd28b7e89a310e84c15",
7
- "version": "25.2.0-beta0"
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;