Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit 59d5d65

Browse files
committed
custom fabric-shim-crypto
1 parent 080c124 commit 59d5d65

23 files changed

+1104
-48
lines changed

fabric-shim-crypto-custom.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
declare module 'fabric-shim-crypto-custom' {
2+
3+
namespace ShimCrypto {
4+
/*~ Write your module's methods and properties in this class */
5+
6+
/*~ If you want to expose types from your module as well, you can
7+
*~ place them in this block.
8+
*/
9+
export class ShimCryptoClass {
10+
constructor(stub: Object);
11+
12+
ENCRYPT_KEY: string;
13+
SIGN_KEY: string;
14+
INIT_VECTOR: string;
15+
16+
encrypt(plaintext: string): Buffer;
17+
18+
decrypt(ciphertext: string): Buffer;
19+
20+
sign(message: string): Buffer;
21+
22+
verify(signature: Buffer, message: string): ShimCrypto.VerifyResponse;
23+
24+
}
25+
26+
export interface VerifyResponse {
27+
ok: boolean;
28+
error?: Error;
29+
}
30+
}
31+
32+
export = ShimCrypto;
33+
}

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
},
2828
"homepage": "https://github.com/wearetheledger/fabric-chaincode-utils#readme",
2929
"devDependencies": {
30-
"@theledger/fabric-mock-stub": "^4.0.0",
31-
"@theledger/fabric-shim-crypto-types": "^1.0.5",
32-
"@types/chai": "^4.0.4",
30+
"@types/chai": "^4.2.7",
3331
"@types/mocha": "^5.2.1",
3432
"@types/node": "^10.3.0",
3533
"@types/winston": "^2.3.8",
@@ -49,10 +47,10 @@
4947
},
5048
"dependencies": {
5149
"@theledger/couchdb-query-engine": "^2.4.4",
52-
"@types/jest": "^24.0.25",
50+
"@theledger/fabric-mock-stub": "^5.0.0",
5351
"aguid": "^2.0.0",
5452
"fabric-shim": "^1.3.0",
55-
"fabric-shim-crypto": "^1.3.0",
53+
"fabric-shim-crypto": "^1.4.4",
5654
"serialize-error": "^3.0.0",
5755
"ts-node": "^8.6.2",
5856
"typescript": "^3.7.4",

src/Chaincode.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)