首页 > 解决方案 > setState 在远程调试器打开时完美运行,但在它关闭时无法正常工作

问题描述

我需要使用私钥解密 json 数据,当远程调试器打开时,该功能可以正常工作,但是当我停止它时,它永远不会得到解决。

我试过用 async/await 来做,但没有任何改变

decryptWithPrivateKey = (privateKey, encrypted) => {
    const buf = new Buffer(encrypted, "hex");
    encrypted = {
      iv: buf.toString("hex", 0, 16),
      ephemPublicKey: buf.toString("hex", 16, 49),
      mac: buf.toString("hex", 49, 81),
      ciphertext: buf.toString("hex", 81, buf.length)
    };
    // decompress publicKey
    encrypted.ephemPublicKey = publicKeyConvert(
      new Buffer(encrypted.ephemPublicKey, "hex"),
      false
    ).toString("hex");
    const twoStripped = privateKey.substring(2);
    const encryptedBuffer = {
      iv: new Buffer(encrypted.iv, "hex"),
      ephemPublicKey: new Buffer(encrypted.ephemPublicKey, "hex"),
      ciphertext: new Buffer(encrypted.ciphertext, "hex"),
      mac: new Buffer(encrypted.mac, "hex")
    };

    return decrypt(new Buffer(twoStripped, "hex"), 
    encryptedBuffer).then(
      decryptedBuffer => decryptedBuffer.toString()
    );
};

像这样称呼它:

<TouchableOpacity
          onPress={() => {
            this.decryptWithPrivateKey(
              this.state.privateKey,
              this.state.ciphered
            ).then(result => {
              this.setState({ deciphered: result }, () => {
                console.log(this.state.deciphered);
                alert(this.state.deciphered);
              });
            });
          }}
        >
          <Text>decrypt</Text>
</TouchableOpacity>

当按下按钮并且远程调试器打开时,它工作得很好。一旦调试器关闭,它既不会设置状态也不会发出警报。

标签: javascriptreact-nativepromise

解决方案


推荐阅读