1

我是区块链新手,想在 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)
})
4

3 回答 3

1

您可以使用intkey-client访问intkey处理器,这是一个 github 存储库:

https://github.com/thegreatsunra/intkey-client-js

于 2018-06-27T05:40:48.580 回答
0

试试这个 请进行必要的更改。

const transactionHeaderBytes = protobuf.TransactionHeader.encode({
    familyName: FAMILY_NAME,
    familyVersion: '1.0',
    inputs: [createHash('sha512').update(FAMILY_NAME).digest('hex').toLowerCase().slice(0, 6)],
    outputs: [createHash('sha512').update(FAMILY_NAME).digest('hex').toLowerCase().slice(0, 6)],
    signerPublicKey: signer.getPublicKey().asHex(),
    batcherPublicKey: signer.getPublicKey().asHex(),
    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 signature1 = signer.sign(batchHeaderBytes)

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


const batchListBytes = protobuf.BatchList.encode({
    batches: [batch]
}).finish();
于 2018-11-28T06:45:23.980 回答
0

最有可能的是,您没有将事务放入批处理或批处理列表中的批处理以发布到 REST API。这是必需的,即使对于单个事务也是如此。

这是一个简单的独立演示 Sawtooth 应用程序(Simple Wallet),用 JavaScript 和其他语言编写,可以作为示例: https ://github.com/askmish/sawtooth-simplewallet

编辑:我又看了一遍。您应该按照上面链接中的 int 键示例进行操作。例如,我注意到您没有设置“nonce”,这是一个一次性数字(使用随机数)。将您的代码与以下内容进行比较:

https://github.com/askmish/sawtooth-simplewallet/blob/master/jsclient/routes/SimpleWalletClient.jshttps://github.com/thegreatsunra/intkey-client-js/blob/develop/sawtooth-client。 js

于 2018-08-17T18:02:28.813 回答