ipfs.cat在React-native中返回 "未定义 "对象。

问题描述 投票:0回答:1

在React-native上,我使用ipfs.add向IPFS添加缓冲字符串(即Buffer(str))时,缓冲字符串被成功添加,IPFS返回哈希值。当我使用ipfs.add将一个缓冲字符串(即Buffer(str) )添加到IPFS时,缓冲字符串被成功添加,IPFS返回哈希值。

当我试图通过ipfs.cat和hash来检索缓冲字符串时,ipfs.cat返回 "undefined"。

在Node.js和ReactJs上。我没有这个问题。ipfs.add和ipfs.cat都可以工作。

问题是否与IPFS中的pining有关? 或者改变packag.json中的ipfs-api版本会有帮助吗?

任何帮助都将是非常感激的。

下面是React-Native使用的app.js代码。

import React, {useState, useRef} from 'react';
import {StyleSheet, Text, View, StatusBar, Button} from 'react-native';

const CryptoJS = require('crypto-js');
const ipfsAPI = require('ipfs-api');
// Connceting to the ipfs network via infura gateway
const ipfs = ipfsAPI('ipfs.infura.io', '5001', {protocol: 'https'});

export default function App() {
  const [number, setNumber] = useState(0);
  const [hash, setHash] = useState(' ');
  console.log('printing: ', number);
  //console.log('testing:', ipfs);


  const handleCaseAdd = () => {
    setNumber(1);

    // Encrypt
    const ciphertext = CryptoJS.AES.encrypt(
      JSON.stringify('my message'),
      'secret key 1234',
    ).toString();
    console.log(' Ciphertext: ', ciphertext); // 'ciphertext
    console.log('Buffered ciphertext: ', Buffer(ciphertext));

    // Adding the encrpyted file to IPFS
    ipfs.add(Buffer(ciphertext), {pin: true}, (error, result) => {
      if (error) {
        console.log(error);
        return;
      }
      setHash(result[0].hash);
      console.log('File added succesfully');
      console.log('IPFS result: ', result);

    });
  }; // end of the function

  const handleCaseGet = fileHash => {
    //const fileHash = hash;
    console.log('fileHash (before) :', fileHash);

    ipfs.files.cat(fileHash, function(err, bufferedCiphertext) {
      console.log('fileHash (after) :', fileHash);
      console.log('Getting Buffered ciphertext: ', bufferedCiphertext);


    });
  }; // end of the function

  //let confirmed;
  //confirmed = true;
  return (
    <View style={styles.container}>
      <View style={styles.first}>
        <View style={styles.box1} />
        <View style={styles.box2} />
      </View>
      <View>
        <Button title="Add" onPress={handleCaseAdd} />
      </View>

      <Text> Number: {number} </Text>
      <Text> Hash: {hash} </Text>
      <View>
        <Button title="Get" onPress={handleCaseGet.bind(this, hash)} />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    //alignItems: "center",
    //justifyContent: "center",
    paddingTop: StatusBar.currentHeight,
  },
  first: {
    backgroundColor: 'green',
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'flex-start',
  },

  box1: {
    backgroundColor: 'dodgerblue',
    width: 50,
    height: 50,
    //flex: 1,
  },
  box2: {
    backgroundColor: 'gold',
    width: 50,
    height: 50,

    //alignSelf: "flex-start",
  },
});
buffer react-native-android undefined cat ipfs
1个回答
0
投票

ipfs-api 已经被废弃了两年,取而代之的是...。ipfs-http-client - 能否请你改用该模块试试?

https:/www.npmjs.compackageipfs-http-client

© www.soinside.com 2019 - 2024. All rights reserved.