|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 12 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 13 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 14 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 15 | + function step(op) { |
| 16 | + if (f) throw new TypeError("Generator is already executing."); |
| 17 | + while (_) try { |
| 18 | + 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; |
| 19 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 20 | + switch (op[0]) { |
| 21 | + case 0: case 1: t = op; break; |
| 22 | + case 4: _.label++; return { value: op[1], done: false }; |
| 23 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 24 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 25 | + default: |
| 26 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 27 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 28 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 29 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 30 | + if (t[2]) _.ops.pop(); |
| 31 | + _.trys.pop(); continue; |
| 32 | + } |
| 33 | + op = body.call(thisArg, _); |
| 34 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 35 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 36 | + } |
| 37 | +}; |
| 38 | +exports.__esModule = true; |
| 39 | +var shim = require("fabric-shim"); |
| 40 | +var helpers_1 = require("./utils/helpers"); |
| 41 | +var StubHelper_1 = require("./StubHelper"); |
| 42 | +var datatransform_1 = require("./utils/datatransform"); |
| 43 | +var ChaincodeError_1 = require("./utils/errors/ChaincodeError"); |
| 44 | +var InternalServerError_1 = require("./utils/errors/InternalServerError"); |
| 45 | +var serialize = require('serialize-error'); |
| 46 | +/** |
| 47 | + * The Chaincode class is a base class containing handlers for the `Invoke()` and `Init()` function which are required |
| 48 | + * by `fabric-shim`. The `Init()` function can be overwritten by just implementing it in your Chaincode implementation |
| 49 | + * class. |
| 50 | + */ |
| 51 | +var Chaincode = /** @class */ (function () { |
| 52 | + function Chaincode(logLevel) { |
| 53 | + this.logger = helpers_1.Helpers.getLoggerInstance(this.name, logLevel); |
| 54 | + } |
| 55 | + Object.defineProperty(Chaincode.prototype, "name", { |
| 56 | + /** |
| 57 | + * the name of the current chaincode. |
| 58 | + * |
| 59 | + * @readonly |
| 60 | + * @type {string} |
| 61 | + * @memberof Chaincode |
| 62 | + */ |
| 63 | + get: function () { |
| 64 | + return this.constructor.name; |
| 65 | + }, |
| 66 | + enumerable: true, |
| 67 | + configurable: true |
| 68 | + }); |
| 69 | + /** |
| 70 | + * The Init method is called when the Smart Contract is instantiated by the blockchain network |
| 71 | + * Best practice is to have any Ledger initialization in separate function -- see initLedger() |
| 72 | + * |
| 73 | + * @param {ChaincodeStub} stub |
| 74 | + * @returns {Promise<ChaincodeResponse>} |
| 75 | + * @memberof Chaincode |
| 76 | + */ |
| 77 | + Chaincode.prototype.Init = function (stub) { |
| 78 | + return __awaiter(this, void 0, void 0, function () { |
| 79 | + var ret; |
| 80 | + return __generator(this, function (_a) { |
| 81 | + switch (_a.label) { |
| 82 | + case 0: |
| 83 | + this.logger.info("=========== Instantiated " + this.name + " chaincode ==========="); |
| 84 | + this.logger.info("Transaction ID: " + stub.getTxID()); |
| 85 | + this.logger.info("Args: " + stub.getArgs().join(',')); |
| 86 | + ret = stub.getFunctionAndParameters(); |
| 87 | + return [4 /*yield*/, this.executeMethod('init', ret.params, stub, true)]; |
| 88 | + case 1: return [2 /*return*/, _a.sent()]; |
| 89 | + } |
| 90 | + }); |
| 91 | + }); |
| 92 | + }; |
| 93 | + /** |
| 94 | + * The Invoke method is called as a result of an application request to run the Smart Contract. |
| 95 | + * The calling application program has also specified the particular smart contract |
| 96 | + * function to be called, with arguments |
| 97 | + * |
| 98 | + * @param {ChaincodeStub} stub |
| 99 | + * @returns {Promise<ChaincodeResponse>} |
| 100 | + * @memberof Chaincode |
| 101 | + */ |
| 102 | + Chaincode.prototype.Invoke = function (stub) { |
| 103 | + return __awaiter(this, void 0, void 0, function () { |
| 104 | + var ret; |
| 105 | + return __generator(this, function (_a) { |
| 106 | + switch (_a.label) { |
| 107 | + case 0: |
| 108 | + this.logger.info("=========== Invoked Chaincode " + this.name + " ==========="); |
| 109 | + this.logger.info("Transaction ID: " + stub.getTxID()); |
| 110 | + this.logger.info("Args: " + stub.getArgs().join(',')); |
| 111 | + ret = stub.getFunctionAndParameters(); |
| 112 | + return [4 /*yield*/, this.executeMethod(ret.fcn, ret.params, stub)]; |
| 113 | + case 1: return [2 /*return*/, _a.sent()]; |
| 114 | + } |
| 115 | + }); |
| 116 | + }); |
| 117 | + }; |
| 118 | + /** |
| 119 | + * Handle custom method execution |
| 120 | + * |
| 121 | + * @param {string} fcn |
| 122 | + * @param {string[]} params |
| 123 | + * @param stub |
| 124 | + * @param {boolean} silent |
| 125 | + * @returns {Promise<any>} |
| 126 | + */ |
| 127 | + Chaincode.prototype.executeMethod = function (fcn, params, stub, silent) { |
| 128 | + if (silent === void 0) { silent = false; } |
| 129 | + return __awaiter(this, void 0, void 0, function () { |
| 130 | + var method, payload, err_1, error; |
| 131 | + return __generator(this, function (_a) { |
| 132 | + switch (_a.label) { |
| 133 | + case 0: |
| 134 | + method = this[fcn]; |
| 135 | + if (!method) { |
| 136 | + if (!silent) { |
| 137 | + this.logger.error("no function of name: " + fcn + " found"); |
| 138 | + return [2 /*return*/, shim.error(serialize(new ChaincodeError_1.ChaincodeError("no function of name: " + fcn + " found", 400)))]; |
| 139 | + } |
| 140 | + else { |
| 141 | + return [2 /*return*/, shim.success()]; |
| 142 | + } |
| 143 | + } |
| 144 | + _a.label = 1; |
| 145 | + case 1: |
| 146 | + _a.trys.push([1, 3, , 4]); |
| 147 | + this.logger.debug("============= START : " + fcn + " ==========="); |
| 148 | + return [4 /*yield*/, method.call(this, new StubHelper_1.StubHelper(stub), params)]; |
| 149 | + case 2: |
| 150 | + payload = _a.sent(); |
| 151 | + if ((payload !== undefined && payload !== null) && !Buffer.isBuffer(payload)) { |
| 152 | + payload = Buffer.from(JSON.stringify(datatransform_1.Transform.normalizePayload(payload))); |
| 153 | + } |
| 154 | + this.logger.debug("============= END : " + fcn + " ==========="); |
| 155 | + return [2 /*return*/, shim.success(payload)]; |
| 156 | + case 3: |
| 157 | + err_1 = _a.sent(); |
| 158 | + error = err_1; |
| 159 | + this.logger.error(err_1); |
| 160 | + if (error.name !== 'ChaincodeError') { |
| 161 | + error = new InternalServerError_1.InternalServerError(error.message); |
| 162 | + } |
| 163 | + delete error.stack; |
| 164 | + return [2 /*return*/, shim.error(Buffer.from(JSON.stringify(serialize(error))))]; |
| 165 | + case 4: return [2 /*return*/]; |
| 166 | + } |
| 167 | + }); |
| 168 | + }); |
| 169 | + }; |
| 170 | + return Chaincode; |
| 171 | +}()); |
| 172 | +exports.Chaincode = Chaincode; |
0 commit comments