IndySdk 在尝试导入时出现错误 具有此名称的钱包已存在 原因:钱包数据库文件已存在

IndySdk when trying to import cause error Wallet with this name already exists Caused by: Wallet database file already exists

提问人:JrealFun 提问时间:11/13/2023 最后编辑:Remy LebeauJrealFun 更新时间:11/15/2023 访问量:16

问:

type Props = StackScreenProps<WalletStackParamList, "SelfCredential">

const RecoverWallet = ({ navigation }: Props) => {
  useLayoutEffect(() => {
    navigation.setOptions({
      title: "Recover Wallet",
    })
  }, [navigation])

  const [importedFileName, setImportedFileName] = useState<string | null>(null)

  const pickDocument = async () => {
    try {
      const response = await DocumentPicker.pick({
        type: [DocumentPicker.types.allFiles],
        copyTo: "documentDirectory", // Save the selected file to the app's document directory
      })

      const selectedFile = response[0] // Assume the user picked only one file
      setImportedFileName(selectedFile.name)
      console.log(`${selectedFile.name} file picked`)
      handleImport(selectedFile)
    } catch (err) {
      if (!DocumentPicker.isCancel(err)) {
        console.error("Error picking document:", err)
        Alert.alert("Error", "An error occurred while picking the document.")
      }
    }
  }

  const handleImport = async (selectedFile: DocumentPickerResponse | null) => {
    try {
      if (!selectedFile) {
        console.log("No file selected")
        return
      }

      const uniqueIdentifier = `${Date.now()}_${Math.floor(
        Math.random() * 1000
      )}`
      const walletName = `imported_wallet_${uniqueIdentifier}`
      const localFilePath = `${RNFS.DocumentDirectoryPath}/${selectedFile.name}`

      await RNFS.moveFile(selectedFile.uri, localFilePath)

      const walletConfig: WalletConfig = config.walletConfig || {}
      const walletCredentials = { key: walletConfig.key || "" }

      // Check if the wallet already exists
      const existingWalletPath = `${RNFS.DocumentDirectoryPath}/.indy_client/wallet/${walletName}`
      const walletExists = await RNFS.exists(existingWalletPath)

      // Delete existing wallet if it exists
      if (walletExists) {
        await RNFS.unlink(existingWalletPath)
        console.log("Deleted existing wallet:", walletName)
      }

      // Import the wallet
      await IndySdk.importWallet(walletConfig, walletCredentials, {
        path: localFilePath,
        key: walletName,
      })

      console.log("Imported Wallet Successfully")
      Alert.alert("Success", "Wallet imported successfully!")
    } catch (error) {
      console.error("Error during import:", error)
      Alert.alert("Error", "An error occurred during wallet import.")
    }
  }

导入过程中出错:{“indyBacktrace”: “”, “indyCode”: 203, “indyMessage”: “错误:具有此名称的钱包已存在 原因:钱包数据库文件已存在:“/storage/emulated/0/Android/data/com.anonymous.xpoafj/files/.indy_client/wallet/sainopal-wallet/sqlite.db” “, ”indyName“: ”WalletAlreadyExistsError“, ”message“: ”WalletAlreadyExistsError“, ”name“: ”IndyError“}

导出为工作,文件被选中进行导入。它提到不能使用钱包,但从我测试的 lissi 应用程序导入备份文件使用相同的钱包并添加备份数据。为什么我无法使用我的钱包“sainopal-wallet”并导入 indy 导出的文件?

Android React-原生 移动开发 Hyperledger-Indy

评论


答:

0赞 JrealFun 11/15/2023 #1
 const configNew: InitConfig = {
    label: "SainoPal Mobile Wallet",
    walletConfig: {
      id: "wa",
      key: "testkey0090000000000000000000001",
    },
    logger: new ConsoleLogger(LogLevel.trace),
  }

  const walletConfig: WalletConfig = configNew.walletConfig || {}
  const walletCredentials = { key: walletConfig.key || "" }

  // Check if the wallet already exists
  const existingWalletPath = `${RNFS.DocumentDirectoryPath}/.indy_client/wallet/${walletName}`
  const walletExists = await RNFS.exists(existingWalletPath)

  console.log("localFilePath  ", localFilePath)

  // Delete existing wallet if it exists
  if (walletExists) {
    await RNFS.unlink(existingWalletPath)
    console.log("Deleted existing wallet:", walletName)
  }

  await IndySdk.importWallet(walletConfig, walletCredentials, {
    path: localFilePath,
    key: "123456",
  })

issus:我用自己的钱包来导入。

解决方法:创建另一个具有不同 ID “wa” 的导入。然后登录该不同的id“wa”帐户仅起作用。