Skip to content Skip to sidebar Skip to footer

Hyperledger Sawtooth Javascript Sdk:submitted Batches Are Invalid

I am trying to implement hyperledger sawtooth transaction through javascript SDK following this https://sawtooth.hyperledger.org/docs/core/releases/1.0/_autogen/sdk_submit_tutorial

Solution 1:

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

Post a Comment for "Hyperledger Sawtooth Javascript Sdk:submitted Batches Are Invalid"