javascripthyperledger-fabrichyperledger-chaincodehyperledger-fabric-sdk-js

Hyperledger Fabric javascript chaincode read transient data


I am passing a json object from hyperledger fabric node application and converting it to buffer as shown below:

const transientData = (transient && transient.length > 0) ? {data: Buffer.from(JSON.stringify(transient[0]))} : null;

this transientData is then passed to the request object as shown below:

        let request = {
            chaincodeId: smartContractName,
            fcn: funcName,
            args: args ? [JSON.stringify(args)] : [],
            chainId: channelName,
            txId: tx_id,
            collection-config: collections,
            transientMap: transientData
        };

The problem is that when I try to receive this transient map into the chaincode and try to log the converted buffer into string using transientData.get('data').toString(), it shows the following output:

ByteBufferNB(offset=1029,markedOffset=-1,limit=1222,capacity=1222)

However, I was expecting string object. I am not able to convert this ByteBuffer to a string. I also tried to print this buffer and it shows the following output:

ByteBuffer {
  buffer: <Buffer 0a c6 07 0a 91 01 08 03 10 01 1a 0c 08 df ef ca f8 05 10 80 bf df 80 01 22 0c 61 73 73 65 74 63 68 61 6e 6e 65 6c 2a 40 66 61 32 36 33 61 32 30 65 61 ... >,
  offset: 1029,
  markedOffset: -1,
  limit: 1222,
  littleEndian: true,
  noAssert: false }

Any suggestions would be appreciated

Update: When I logged the output of transientData.get('data').buffer.toString(), it displayed the following output:

�
�
������߀"
        assetchannel*@fa263a20eaf15aac5eeac0ad2056a25193acc66f05c3316f1ee8d5062ac75de8:
                                                                                        assetccB �_찣ݹ��,R�Z���眕ӳ8v�pP�
�
+
)       assetcc��
submitTransactionReqLock�
data�{"sonefield":"T08700d-5c63-49a2-914f-535fd1abb045","collection":"xxxx","xxx":"xxx","xxxx":"xxx xx xxxx,"locked":false}

Solution

  • Posting the solution here for anyone who is looking for answers.

    I managed to get this working by converting the ByteBuffer to ArrayBuffer. Using the ArrayBuffer, I created a new buffer object and then used it.

    const data = JSON.parse(new Buffer(transient.get('data').toArrayBuffer()).toString());