Sawtooth Javascript SDK

时间:2018-06-14 09:20:41

标签: javascript node.js blockchain hyperledger-sawtooth

我是区块链的新手,想在Node Js中使用锯齿的Javascript Sdk做一些特定的任务,但无法成功运行它。请找到下面的代码,我收到如下错误

我是这个锯齿代码的新手。有人可以指导我如何使用条形码验证等自定义应用程序吗?

// Errror

{
  "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)
})

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

最有可能您没有将事务放入批处理中,也未将其放入批处理列表中以发布到REST API。即使是单笔交易,这也是必需的。

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

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

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

答案 2 :(得分: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();