【问题标题】:ipfs.cat returns "undefined" object in React-nativeipfs.cat 在 React-native 中返回“未定义”对象
【发布时间】:2020-09-26 22:55:15
【问题描述】:

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

当我尝试通过 ipfs.cat 和哈希检索缓冲字符串时,ipfs.cat 返回“未定义”。

在 Node.jS 和 ReactJs 上: 我没有这个问题。 ipfs.add 和 ipfs.cat 都有效。

问题是否与 IPFS 中的 pinning 有关?或者更改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】:

    ipfs-api 已被弃用两年,替换为 ipfs-http-client - 您可以尝试使用该模块吗?

    https://www.npmjs.com/package/ipfs-http-client

    【讨论】:

    • 我尝试在我的代码中导入和使用 ipfs-http-client。但是,我收到以下错误:“错误:未实现” 我正在使用“ipfs-http-client”:“^42.0.0”、“node-libs-react-native”:“^1.2.0”、“反应”:“16.11.0”,“反应原生”:“0.62.2”,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多