Skip to content

Instantly share code, notes, and snippets.

@sasaken555
Created March 21, 2020 08:56
Show Gist options
  • Save sasaken555/ae75d4b9ab5e641f7a79f281507d4e85 to your computer and use it in GitHub Desktop.
Save sasaken555/ae75d4b9ab5e641f7a79f281507d4e85 to your computer and use it in GitHub Desktop.
HLF - Chaincode for Developers tutorial with Node.js
"use strict";
const { Contract, Context } = require("fabric-contract-api");
module.exports = class SampleContract extends Contract {
constructor() {
super("BasicContract");
}
/**
* 取得処理
* @param {Context} ctx コンテキスト
* @param {string} key 保存するキー
* @param {Object} value 保存する値
*/
async setValueTransaction(ctx, key, value) {
if (args.length !== 2) {
const msg = Buffer.from("Incorrect arguments. Expected key and value.");
throw new Error(msg);
}
try {
await ctx.stub.putState(key, Buffer.from(value));
} catch (error) {
console.log("error", error);
throw new Error(`failed to save asset: ${key}`);
}
}
/**
* 取得処理
* @param {Context} ctx コンテキスト
* @param {string} key 取得するキー
*/
async getValueTransaction(ctx, key) {
if (!key) {
const msg = Buffer.from("Incorrect arguments. Expected key.");
return shim.error(msg);
}
try {
const result = await ctx.stub.getState(key);
if (!result) {
throw new Error(`asset not found: ${key}`);
}
return result;
} catch (error) {
console.log("error", error);
throw new Error(`failed to get asset: ${key}`);
}
}
};
"use strict";
const shim = require("fabric-shim");
class ChainCode {
/**
* チェーンコードの初期化処理
* @param {ChaincodeStub} stub HLFのインターフェイス
*/
async init(stub) {
const params = stub.getArgs();
if (params.length !== 2) {
const msg = Buffer.from("Incorrect arguments. Expected key and value.");
return shim.error(msg);
}
try {
await stub.putState(params[0], Buffer.from(params[1]));
return shim.success();
} catch (error) {
console.log("error", error);
return shim.error(Buffer.from("initialization failed"));
}
}
/**
* チェーンコードの実行
* @param {ChaincodeStub} stub HLFのインターフェイス
*/
async invoke(stub) {
const { params, fcn } = stub.getFunctionAndParameters();
let functionToCall;
let result;
switch (fcn) {
case "set":
functionToCall = this.set;
break;
case "get":
functionToCall = this.get;
break;
default:
return shim.error(Buffer.from(`unsupported fcn: ${fcn}`));
}
try {
result = await functionToCall(stub, params);
return shim.success(Buffer.from(result));
} catch (error) {
console.log("error", error);
return shim.error(Buffer.from("invoke failed"));
}
}
/**
* 保存処理
* @param {ChaincodeStub} stub HLFのインターフェイス
* @param {string[]} args 実行引数
*/
async set(stub, args) {
if (args.length !== 2) {
const msg = Buffer.from("Incorrect arguments. Expected key and value.");
return shim.error(msg);
}
try {
await stub.putState(args[0], Buffer.from(args[1]));
} catch (error) {
console.log("error", error);
return shim.error(Buffer.from(`failed to save asset: ${args[0]}`));
}
}
/**
* 取得処理
* @param {ChaincodeStub} stub HLFのインターフェイス
* @param {string[]} args 実行引数
*/
async get(stub, args) {
if (args.length !== 1) {
const msg = Buffer.from("Incorrect arguments. Expected key.");
return shim.error(msg);
}
const key = args[0];
try {
const result = await stub.getState(key);
if (!result) {
return shim.error(Buffer.from(`asset not found: ${key}`));
}
return shim.success(Buffer.from(result));
} catch (error) {
console.log("error", error);
return shim.error(Buffer.from(`failed to get asset: ${key}`));
}
}
}
shim.start(new ChainCode());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment