Hyperledger锯齿JavaScript SDK:提交的批次无效

时间:2018-06-23 15:56:57

标签: javascript node.js express hyperledger-sawtooth

我正在按照https://sawtooth.hyperledger.org/docs/core/releases/1.0/_autogen/sdk_submit_tutorial_js.html#encoding-your-payload的要求通过javascript SDK实现超级账本锯齿交易。

/*
*Create the transaction header
*/
const createTransactionHeader = function createTransactionHeader(payloadBytes) {

    return  protobuf.TransactionHeader.encode({
        familyName: 'intkey',
        familyVersion: '1.0',
        inputs: [],
        outputs: [],
        signerPublicKey: '02cb65a26f7af4286d5f8118400262f7790e20018f2d01e1a9ffc25de1aafabdda',

        batcherPublicKey: '02cb65a26f7af4286d5f8118400262f7790e20018f2d01e1a9ffc25de1aafabdda',
        dependencies: [],
        payloadSha512: createHash('sha512').update(payloadBytes).digest('hex')
    }).finish();


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

    const signature = signer.sign(transactionHeaderBytes)

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

提交事务时,我从REST API中收到以下错误消息

{
  "error": {
    "code": 30,
    "message": "The submitted BatchList was rejected by the validator. It was poorly formed, or has an invalid signature.",
    "title": "Submitted Batches Invalid"
  }
}

发现了与我的问题类似的以下问题

Sawtooth Invalid Batch or Signature

但是在Java中实现的解决方案不适用于我的情况

1 个答案:

答案 0 :(得分:0)

这应该可行,请尝试以下操作:

const cbor = require('cbor');
const {createContext, CryptoFactory} = require('sawtooth-sdk/signing');
const {createHash} = require('crypto');
const {protobuf} = require('sawtooth-sdk');
const request = require('request');
const crypto = require('crypto');

const context = createContext('secp256k1');
const privateKey = context.newRandomPrivateKey();
const signer = CryptoFactory(context).newSigner(privateKey);


// Here's how you can generate the input output address
const FAMILY_NAMESPACE = crypto.createHash('sha512').update('intkey').digest('hex').toLowerCase().substr(0, 6);
const address = FAMILY_NAMESPACE + crypto.createHash('sha512').update('foo').digest('hex').toLowerCase().substr(0, 64);

const payload = {
  Verb: 'set',
  Name: 'foo',
  Value: 42
};

const payloadBytes = cbor.encode(payload);

const transactionHeaderBytes = protobuf.TransactionHeader.encode({
  familyName: 'intkey',
  familyVersion: '1.0',
  inputs: [address],
  outputs: [address],
  signerPublicKey: signer.getPublicKey().asHex(),
  batcherPublicKey: signer.getPublicKey().asHex(),
  dependencies: [],
  payloadSha512: createHash('sha512').update(payloadBytes).digest('hex')
}).finish();

const transactionHeaderSignature = signer.sign(transactionHeaderBytes);

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

const transactions = [transaction]

const batchHeaderBytes = protobuf.BatchHeader.encode({
  signerPublicKey: signer.getPublicKey().asHex(),
  transactionIds: transactions.map((txn) => txn.headerSignature),
}).finish();

const batchHeaderSignature = signer.sign(batchHeaderBytes)

const batch = protobuf.Batch.create({
  header: batchHeaderBytes,
  headerSignature: batchHeaderSignature,
  transactions: transactions
};

const batchListBytes = protobuf.BatchList.encode({
  batches: [batch]
}).finish();

request.post({
  url: 'http://rest.api.domain/batches',
  body: batchListBytes,
  headers: {'Content-Type': 'application/octet-stream'}
}, (err, response) => {
  if(err) {
    return console.log(err);
  }

  console.log(response.body);
});
相关问题