Hyperledger锯齿JavaScript SDK:提交的批次无效 [英] Hyperledger sawtooth JavaScript SDK:submitted batches are invalid

查看:89
本文介绍了Hyperledger锯齿JavaScript SDK:提交的批次无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照提交事务时,我从REST API中收到以下错误消息

While submitting the transaction I am getting the following error from 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"
  }
}

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

Found the following issue similar to my problem

锯齿无效的批处理或签名

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

But its implemented in java the solution not work for my case

推荐答案

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

This should work, try this:

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

这篇关于Hyperledger锯齿JavaScript SDK:提交的批次无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆