无法使用节点 sdk 从私人数据收集中获取人类可读格式的数据
Unable to get data in human readable format from private data collection using node sdk
所以我正在尝试探索私人数据收集,我已经在 javascript 中编写了链代码。我在实例化链代码和调用链代码时传递了正确的集合文件,我在请求有效负载中添加了瞬态数据,如下所示:
const proposal_request = {
chaincodeId: chaincodeId,
fcn: functionName,
args: args,
chainId: channelCode,
txId: tx_id,
transientMap : { "private" : "test data"}
};
我的链代码文件如下所示:
const shim = require('fabric-shim');
const util = require('util');
var PrivateDataChaincode = class {
// Initialize the chaincode
async Init(stub) {
console.log('<================> ::Private Data Chaincode Instantion:: <=================>');
return shim.success();
}
async Invoke(stub) {
console.log('<================> ::Private Data Chaincode Invocation:: <=================>');
let ret = stub.getFunctionAndParameters();
console.info(ret);
let method = this[ret.fcn];
console.info('methodName');
console.info(method);
if (!method) {
console.error('No method of name:' + ret.fcn + ' found');
return shim.error('No method of name:' + ret.fcn + ' found. \n UNKNOWN FUNCTION INVOCATION: ' + ret.fcn);
}
console.info('\nCalling method : ' + ret.fcn);
try {
let payload = await method(stub, ret.params);
return shim.success(payload);
} catch (err) {
console.log(err);
return shim.error(err);
}
}
async createPrivateDataInCollection(stub, args) {
if (args.length != 2) {
throw new Error('Incorrect number of arguments. Expecting 2');
}
// arg[0] has the collection name
// arg[1] has the key
console.info("============= START : Create Private data ===========");
// get the transient map
let transientData = stub.getTransient();
await stub.putPrivateData(args[0], args[1], transientData.map.private.value);
console.info('============= END : Create private data ===========');
}
async queryPrivateDataInCollection(stub, args) {
console.info('============= START : Query private Data ===========');
// arg[0] has the collection name
// arg[1] has the key
// query private data with argument
let allResults = await stub.getPrivateData(args[0], args[1]);
if (!allResults) {
throw shim.error("Can't get data from state");
}
console.log(allResults);
return allResults;
}
};
shim.start(new PrivateDataChaincode());
我正在使用 createPrivateDataInCollection 方法存储数据并使用 queryPrivateDataInCollection 获取数据。显然,数据在插入之前隐式地转换为某个 Buffer ,而我在读取时无法将此数据转换回可读格式。知道如何将数据转换为可读格式吗?
当我尝试打印我存储的 transientData.map.private.value 的值时,我得到了这个:
ByteBuffer {
buffer: <Buffer 0a e7 07 0a 97 01 08 03 10 01 1a 0b 08 8b f8 d3 f6 05 10 c0 d3 b4 16 22 0d 74 72 61 64 65 2d 63 68 61 6e 6e 65 6c 2a 40 39 36 35 34 61 36 31 39 63 30 ... >,
offset: 1126,
markedOffset: -1,
limit: 1147,
littleEndian: true,
noAssert: false }
当我在 queryPrivateDataInCollection 中打印结果时,我得到了这个:
<Buffer b5 eb 2d b6 18 ac 8a ca 6b 8a f6 ad 79 d6 ad 6a d7 ac b7 68 62>
使用 toString 方法将缓冲区数据转换为字符串。
transientData.map.private.value.buffer.toString();
所以我正在尝试探索私人数据收集,我已经在 javascript 中编写了链代码。我在实例化链代码和调用链代码时传递了正确的集合文件,我在请求有效负载中添加了瞬态数据,如下所示:
const proposal_request = {
chaincodeId: chaincodeId,
fcn: functionName,
args: args,
chainId: channelCode,
txId: tx_id,
transientMap : { "private" : "test data"}
};
我的链代码文件如下所示:
const shim = require('fabric-shim');
const util = require('util');
var PrivateDataChaincode = class {
// Initialize the chaincode
async Init(stub) {
console.log('<================> ::Private Data Chaincode Instantion:: <=================>');
return shim.success();
}
async Invoke(stub) {
console.log('<================> ::Private Data Chaincode Invocation:: <=================>');
let ret = stub.getFunctionAndParameters();
console.info(ret);
let method = this[ret.fcn];
console.info('methodName');
console.info(method);
if (!method) {
console.error('No method of name:' + ret.fcn + ' found');
return shim.error('No method of name:' + ret.fcn + ' found. \n UNKNOWN FUNCTION INVOCATION: ' + ret.fcn);
}
console.info('\nCalling method : ' + ret.fcn);
try {
let payload = await method(stub, ret.params);
return shim.success(payload);
} catch (err) {
console.log(err);
return shim.error(err);
}
}
async createPrivateDataInCollection(stub, args) {
if (args.length != 2) {
throw new Error('Incorrect number of arguments. Expecting 2');
}
// arg[0] has the collection name
// arg[1] has the key
console.info("============= START : Create Private data ===========");
// get the transient map
let transientData = stub.getTransient();
await stub.putPrivateData(args[0], args[1], transientData.map.private.value);
console.info('============= END : Create private data ===========');
}
async queryPrivateDataInCollection(stub, args) {
console.info('============= START : Query private Data ===========');
// arg[0] has the collection name
// arg[1] has the key
// query private data with argument
let allResults = await stub.getPrivateData(args[0], args[1]);
if (!allResults) {
throw shim.error("Can't get data from state");
}
console.log(allResults);
return allResults;
}
};
shim.start(new PrivateDataChaincode());
我正在使用 createPrivateDataInCollection 方法存储数据并使用 queryPrivateDataInCollection 获取数据。显然,数据在插入之前隐式地转换为某个 Buffer ,而我在读取时无法将此数据转换回可读格式。知道如何将数据转换为可读格式吗?
当我尝试打印我存储的 transientData.map.private.value 的值时,我得到了这个:
ByteBuffer {
buffer: <Buffer 0a e7 07 0a 97 01 08 03 10 01 1a 0b 08 8b f8 d3 f6 05 10 c0 d3 b4 16 22 0d 74 72 61 64 65 2d 63 68 61 6e 6e 65 6c 2a 40 39 36 35 34 61 36 31 39 63 30 ... >,
offset: 1126,
markedOffset: -1,
limit: 1147,
littleEndian: true,
noAssert: false }
当我在 queryPrivateDataInCollection 中打印结果时,我得到了这个:
<Buffer b5 eb 2d b6 18 ac 8a ca 6b 8a f6 ad 79 d6 ad 6a d7 ac b7 68 62>
使用 toString 方法将缓冲区数据转换为字符串。
transientData.map.private.value.buffer.toString();