代码之家  ›  专栏  ›  技术社区  ›  shamon shamsudeen

编码为十六进制字符串:参数必须是字符串node.js

  •  0
  • shamon shamsudeen  · 技术社区  · 7 年前

    我试图在hyper ledger sawtooth中实现一个简单的事务流,为了创建事务,它必须通过一些步骤

    /*
    * 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
        });
    }
    

    我需要编码 headerSignature 一个十六进制字符串,但我得到以下错误

    Argument must be a string
    

    但是 console.log(signature); 给出以下结果 a51d254f0c27f15abb016030eeb9e38b5ee06ee13d28d88ac5f5cc13a2520b42088090a1d1d19d321098996dc980b3f94cfc84ba0399a73ba7cd9ddc9b2a453d

    更新

    错误日志

    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)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Marcos Casagrande    7 年前

    错误不在 Buffer.from 但是在 protobuf.Transaction.create

    headerSignature 需要成为 string ,而你通过了 Buffer

    根据 documentation 应该是这样的:

    const signature = signer.sign(transactionHeaderBytes)
    
    const transaction = protobuf.Transaction.create({
        header: transactionHeaderBytes,
        headerSignature: signature,
        payload: payloadBytes
    })
    
    推荐文章