PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@celo/contractkit/lib/wrappers

Просмотр файла: MetaTransactionWallet.js

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildMetaTxTypedData = exports.toTransactionBatch = exports.toRawTransaction = exports.MetaTransactionWalletWrapper = void 0;
var address_1 = require("@celo/base/lib/address");
var connect_1 = require("@celo/connect");
var BaseWrapper_1 = require("./BaseWrapper");
/**
 * Class that wraps the MetaTransactionWallet
 */
var MetaTransactionWalletWrapper = /** @class */ (function (_super) {
    __extends(MetaTransactionWalletWrapper, _super);
    function MetaTransactionWalletWrapper() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getMetaTransactionDigestParams = function (tx, nonce) {
            var rawTx = (0, exports.toRawTransaction)(tx);
            return [rawTx.destination, rawTx.value, rawTx.data, nonce];
        };
        _this.getMetaTransactionDigest = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.getMetaTransactionDigest, _this.getMetaTransactionDigestParams, BaseWrapper_1.stringIdentity);
        _this.getMetaTransactionSignerParams = function (tx, nonce, signature) {
            var rawTx = (0, exports.toRawTransaction)(tx);
            return [
                rawTx.destination,
                rawTx.value,
                rawTx.data,
                nonce,
                signature.v,
                signature.r,
                signature.s,
            ];
        };
        _this.getMetaTransactionSigner = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.getMetaTransactionSigner, _this.getMetaTransactionSignerParams, BaseWrapper_1.stringIdentity);
        _this.eip712DomainSeparator = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.eip712DomainSeparator);
        _this.isOwner = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.isOwner);
        _this.nonce = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.nonce, undefined, BaseWrapper_1.valueToInt);
        _this.getSigner = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.signer, undefined, BaseWrapper_1.stringIdentity);
        _this.transferOwnership = (0, BaseWrapper_1.proxySend)(_this.connection, _this.contract.methods.transferOwnership);
        _this.setSigner = (0, BaseWrapper_1.proxySend)(_this.connection, _this.contract.methods.setSigner);
        _this.setEip712DomainSeparator = (0, BaseWrapper_1.proxySend)(_this.connection, _this.contract.methods.setEip712DomainSeparator);
        return _this;
    }
    /**
     * Execute a transaction originating from the MTW
     * Reverts if the caller is not a signer
     * @param tx a TransactionInput
     */
    MetaTransactionWalletWrapper.prototype.executeTransaction = function (tx) {
        var rawTx = (0, exports.toRawTransaction)(tx);
        return (0, connect_1.toTransactionObject)(this.connection, this.contract.methods.executeTransaction(rawTx.destination, rawTx.value, rawTx.data));
    };
    /**
     * Execute a batch of transactions originating from the MTW
     * Reverts if the caller is not a signer
     * @param txs An array of TransactionInput
     */
    MetaTransactionWalletWrapper.prototype.executeTransactions = function (txs) {
        var _a = (0, exports.toTransactionBatch)(txs), destinations = _a.destinations, values = _a.values, callData = _a.callData, callDataLengths = _a.callDataLengths;
        return (0, connect_1.toTransactionObject)(this.connection, this.contract.methods.executeTransactions(destinations, values, callData, callDataLengths));
    };
    /**
     * Execute a signed meta transaction
     * Reverts if meta-tx signer is not a signer for the wallet
     * @param tx a TransactionInput
     * @param signature a Signature
     */
    MetaTransactionWalletWrapper.prototype.executeMetaTransaction = function (tx, signature) {
        var rawTx = (0, exports.toRawTransaction)(tx);
        return (0, connect_1.toTransactionObject)(this.connection, this.contract.methods.executeMetaTransaction(rawTx.destination, rawTx.value, rawTx.data, signature.v, signature.r, signature.s));
    };
    /**
     * Signs a meta transaction as EIP712 typed data
     * @param tx a TransactionWrapper
     * @param nonce Optional -- will query contract state if not passed
     * @returns signature a Signature
     */
    MetaTransactionWalletWrapper.prototype.signMetaTransaction = function (tx, nonce) {
        return __awaiter(this, void 0, void 0, function () {
            var typedData, _a, _b, signer;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        if (!(nonce === undefined)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.nonce()];
                    case 1:
                        nonce = _c.sent();
                        _c.label = 2;
                    case 2:
                        _a = exports.buildMetaTxTypedData;
                        _b = [this.address,
                            (0, exports.toRawTransaction)(tx),
                            nonce];
                        return [4 /*yield*/, this.chainId()];
                    case 3:
                        typedData = _a.apply(void 0, _b.concat([_c.sent()]));
                        return [4 /*yield*/, this.signer()];
                    case 4:
                        signer = _c.sent();
                        return [2 /*return*/, this.connection.signTypedData(signer, typedData)];
                }
            });
        });
    };
    /**
     * Execute a signed meta transaction
     * Reverts if meta-tx signer is not a signer for the wallet
     * @param tx a TransactionInput
     */
    MetaTransactionWalletWrapper.prototype.signAndExecuteMetaTransaction = function (tx) {
        return __awaiter(this, void 0, void 0, function () {
            var signature;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.signMetaTransaction(tx)];
                    case 1:
                        signature = _a.sent();
                        return [2 /*return*/, this.executeMetaTransaction(tx, signature)];
                }
            });
        });
    };
    MetaTransactionWalletWrapper.prototype.chainId = function () {
        var _a;
        return __awaiter(this, void 0, void 0, function () {
            var _b, _c, _d;
            return __generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        _b = this;
                        if (!((_a = this._chainId) !== null && _a !== void 0)) return [3 /*break*/, 1];
                        _c = _a;
                        return [3 /*break*/, 3];
                    case 1: return [4 /*yield*/, this.connection.chainId()];
                    case 2:
                        _c = (_e.sent());
                        _e.label = 3;
                    case 3:
                        _b._chainId = _c;
                        if (!(this._chainId === undefined)) return [3 /*break*/, 5];
                        _d = this;
                        return [4 /*yield*/, this.connection.chainId()];
                    case 4:
                        _d._chainId = _e.sent();
                        _e.label = 5;
                    case 5: return [2 /*return*/, this._chainId];
                }
            });
        });
    };
    MetaTransactionWalletWrapper.prototype.signer = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if (!(this._signer === undefined)) return [3 /*break*/, 2];
                        _a = this;
                        return [4 /*yield*/, this.getSigner()];
                    case 1:
                        _a._signer = _b.sent();
                        _b.label = 2;
                    case 2: return [2 /*return*/, this._signer];
                }
            });
        });
    };
    return MetaTransactionWalletWrapper;
}(BaseWrapper_1.BaseWrapper));
exports.MetaTransactionWalletWrapper = MetaTransactionWalletWrapper;
/**
 * Turns any possible way to pass in a transaction into the raw values
 * that are actually required. This is used both internally to normalize
 * ways in which transactions are passed in but also public in order
 * for one instance of ContractKit to serialize a meta transaction to
 * send over the wire and be consumed somewhere else.
 * @param tx TransactionInput<any> union of all the ways we expect transactions
 * @returns a RawTransactions that's serializable
 */
var toRawTransaction = function (tx) {
    if ('destination' in tx) {
        return tx;
    }
    else if ('value' in tx) {
        return {
            destination: tx.txo._parent.options.address,
            data: tx.txo.encodeABI(),
            value: (0, BaseWrapper_1.valueToString)(tx.value),
        };
    }
    else {
        return {
            destination: tx._parent.options.address,
            data: tx.encodeABI(),
            value: '0',
        };
    }
};
exports.toRawTransaction = toRawTransaction;
/**
 * Turns an array of transaction inputs into the argument that
 * need to be passed to the executeTransactions call.
 * Main transformation is that all the `data` parts of each
 * transaction in the batch are concatenated and an array
 * of lengths is constructed.
 * This is a gas optimisation on the contract.
 * @param txs Array<TransactionInput<any>> array of txs
 * @returns Params for the executeTransactions method call
 */
var toTransactionBatch = function (txs) {
    var rawTxs = txs.map(exports.toRawTransaction);
    return {
        destinations: rawTxs.map(function (rtx) { return rtx.destination; }),
        values: rawTxs.map(function (rtx) { return rtx.value; }),
        callData: (0, address_1.ensureLeading0x)(rawTxs.map(function (rtx) { return (0, address_1.trimLeading0x)(rtx.data); }).join('')),
        callDataLengths: rawTxs.map(function (rtx) { return (0, address_1.trimLeading0x)(rtx.data).length / 2; }),
    };
};
exports.toTransactionBatch = toTransactionBatch;
var buildMetaTxTypedData = function (walletAddress, tx, nonce, chainId) {
    return {
        types: {
            EIP712Domain: [
                { name: 'name', type: 'string' },
                { name: 'version', type: 'string' },
                { name: 'chainId', type: 'uint256' },
                { name: 'verifyingContract', type: 'address' },
            ],
            ExecuteMetaTransaction: [
                { name: 'destination', type: 'address' },
                { name: 'value', type: 'uint256' },
                { name: 'data', type: 'bytes' },
                { name: 'nonce', type: 'uint256' },
            ],
        },
        primaryType: 'ExecuteMetaTransaction',
        domain: {
            name: 'MetaTransactionWallet',
            version: '1.1',
            chainId: chainId,
            verifyingContract: walletAddress,
        },
        message: tx ? __assign(__assign({}, tx), { nonce: nonce }) : {},
    };
};
exports.buildMetaTxTypedData = buildMetaTxTypedData;
//# sourceMappingURL=MetaTransactionWallet.js.map

Выполнить команду


Для локальной разработки. Не используйте в интернете!