简体   繁体   中英

Encode to hex string: argument must be a string node.js

I am trying to implement a simple transaction flow in hyper ledger sawtooth, for creating transaction it must through some steps

/*
* Create the transactions
*/
const createTransaction = function createTransaction(transactionHeaderBytes, payloadBytes) {

    const signature = signer.sign(transactionHeaderBytes)

    console.log(signature);

    return transaction = protobuf.Transaction.create({
        header: transactionHeaderBytes,
        headerSignature:Buffer.from(signature, "hex"),
        payload: payloadBytes
    });
}

I need to encode headerSignature to a hex string ,but i am getting the following error

Argument must be a string

But the console.log(signature); gives the following result a51d254f0c27f15abb016030eeb9e38b5ee06ee13d28d88ac5f5cc13a2520b42088090a1d1d19d321098996dc980b3f94cfc84ba0399a73ba7cd9ddc9b2a453d

UPDATE

Error log

TypeError: Argument must be a string
    at Op.writeStringBuffer [as fn] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/protobufjs/src/writer_buffer.js:61:13)
    at BufferWriter.finish (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/protobufjs/src/writer.js:449:14)
    at Object.createBatchHeader (/var/accubits-workspace/hypeerledger-sawtooth/tuts/helpers/private-key.js:82:8)
    at app.get (/var/accubits-workspace/hypeerledger-sawtooth/tuts/index.js:24:32)
    at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
    at /var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/index.js:335:12)

The error is not in Buffer.from but in protobuf.Transaction.create

headerSignature needs to be a string , and you're passing a Buffer

According to the documentation it should be like this:

const signature = signer.sign(transactionHeaderBytes)

const transaction = protobuf.Transaction.create({
    header: transactionHeaderBytes,
    headerSignature: signature,
    payload: payloadBytes
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM