Infura

將文件上傳到 IPFS infura 後生成密鑰時出錯

  • January 28, 2019

將文件上傳到 IPFS infura 後生成密鑰時出現錯誤。

下面給出的範常式式碼

var ipfsAPI = require('ipfs-http-client');
const ipfs = ipfsAPI('ipfs.infura.io', '5001', { protocol: 'https' });
var oldpath = files[property].path;
var uploadedFile = fs.readFileSync(oldpath);
const filess = [{
                   path: files[property].name,
                   content: ipfs.types.Buffer.from(uploadedFile)
            }];
ipfs.add(filess, function (err, files) {

                 if (err || !files) {
                       return reject('upload failed');
                   };
var obj;
for (i = 0; i < data.length; i++) {
   obj = { "id": i, "hash": data[i], "EID": formFields.EID };
   tt.push(obj);
}
ipfs.dag.put(tt, {}, (err, cid) => {
   console.log(cid.toBaseEncodedString());
   const adr = cid.toBaseEncodedString();
   var dTimeStamp = new Date().getTime();
ipfs.key.gen(dTimeStamp, {
type: 'rsa',
size: 2048
}, (err, key) => {
   if (err != null) {
       console.log(err)
       }

   console.log("ipfs Key: ", key.id + " key name : " + key.name);

   var keyVall = key.id;


ipfs.name.publish(adr, { key: keyVall }, (err, pVal) => {


       console.log("published hash : " + pVal);

       var ipfsHash = pVal.name;


   return resp.json({ status: 'SUCCESS', message: 'user has been created successfully', digitalID: pVal });
});
});
});

});

生成密鑰時出現以下錯誤

"SyntaxError: Unexpected token < in JSON at position 0
   at JSON.parse (<anonymous>)
   at streamToValue (C:\Service\node_modules\ipfs-http-client\src\utils\stream-to-json-value.js:25:18)
   at concat (C:Service\node_modules\ipfs-http-client\src\utils\stream-to-value.js:12:22)
   at ConcatStream.<anonymous> (C:Service\node_modules\concat-stream\index.js:37:43)
   at ConcatStream.emit (events.js:187:15)
   at finishMaybe (C:Service\node_modules\readable-stream\lib\_stream_writable.js:630:14)
   at afterWrite (C:Service\node_modules\readable-stream\lib\_stream_writable.js:492:3)
   at process._tickCallback (internal/process/next_tick.js:63:19)"

任何線索將不勝感激。

Infura IPFS 服務僅允許通過其網關使用一小組 IPFS API 方法,因此您無法通過其生成密鑰ipfs.key.gen

請參閱 Infura 文件

在此處輸入圖像描述


我建議執行您自己的 IPFS 節點,特別是如果您想生成和儲存密鑰對。

引用自:https://ethereum.stackexchange.com/questions/66241