我是区块链新手,想在 Node Js 中使用锯齿的 Javascript Sdk 完成一些特定任务,但无法成功运行。请在下面找到代码,我收到此错误如下
我是这个锯齿代码的新手。谁能指导我如何使用自定义应用程序,如条形码验证?
//错误
{
"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"
}
}
//代码
const {createContext, CryptoFactory} = require('sawtooth-sdk/signing')
const context = createContext('secp256k1')
const privateKey = context.newRandomPrivateKey();
const signer = new CryptoFactory(context).newSigner(privateKey)
const cbor = require('cbor')
const payload = {
Verb: 'set',
Name: 'foo',
Value: 42
}
const payloadBytes = cbor.encode(payload)
const {createHash} = require('crypto')
const {protobuf} = require('sawtooth-sdk')
const transactionHeaderBytes = protobuf.TransactionHeader.encode({
familyName: 'intkey',
familyVersion: '1.0',
inputs: [createHash('sha512').update("intkey").digest('hex')],
outputs: [createHash('sha512').update("intkey").digest('hex')],
signerPublicKey: signer.getPublicKey().asHex(),
// In this example, we're signing the batch with the same private key,
// but the batch can be signed by another party, in which case, the
// public key will need to be associated with that key.
batcherPublicKey: signer.getPublicKey().asHex(),
// In this example, there are no dependencies. This list should include
// an previous transaction header signatures that must be applied for
// this transaction to successfully commit.
// For example,
// dependencies: ['540a6803971d1880ec73a96cb97815a95d374cbad5d865925e5aa0432fcf1931539afe10310c122c5eaae15df61236079abbf4f258889359c4d175516934484a'],
dependencies: [],
payloadSha512: createHash('sha512').update(payloadBytes).digest('hex')
}).finish()
const signature = signer.sign(transactionHeaderBytes);
const transaction = protobuf.Transaction.create({
header: transactionHeaderBytes,
headerSignature: signature,
payload: payloadBytes
})
const transactions = [transaction]
const batchHeaderBytes = protobuf.BatchHeader.encode({
signerPublicKey: signer.getPublicKey().asHex(),
transactionIds: transactions.map((txn) => txn.headerSignature),
}).finish()
const batch = protobuf.Batch.create({
header: batchHeaderBytes,
headerSignature: signature,
transactions: transactions
});
const batchListBytes = protobuf.BatchList.encode({
batches: [batch]
}).finish()
const request = require('request')
request.post({
url: 'http://192.168.99.100:8008/batches',
body: batchListBytes,
headers: {'Content-Type': 'application/octet-stream'}
}, (err, response) => {
if (err) return console.log(err)
console.log(response.body)
})