8000 _deleteTKeyStoreItem cause error · Issue #182 · tkey/tkey · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

_deleteTKeyStoreItem cause error #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
minhnd410 opened this issue Dec 16, 2022 · 0 comments
Open

_deleteTKeyStoreItem cause error #182

minhnd410 opened this issue Dec 16, 2022 · 0 comments

Comments

@minhnd410
Copy link
minhnd410 commented Dec 16, 2022

Step to reproduce

  1. assumed login and reconstruct key successfully
  2. create item: _setTKeyStoreItem(module: "A", {id: 1, data: ...})
  3. create other item: _setTKeyStoreItem(module: "A", {id: 2, data: ...})
  4. try delete one of them: _deleteTKeyStoreItem(module: "A", id: 2})
  5. execute getTKeyStore("A") cause error

Error

The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type undefined

Investigate

The following method cause error

  async _deleteTKeyStoreItem(moduleName: string, id: string): Promise<void> {
    if (!this.metadata) {
      throw CoreError.metadataUndefined();
    }
    const rawTkeyStoreItems = (this.metadata.getTkeyStoreDomain(moduleName) as EncryptedMessage[]) || [];
    const decryptedItems = await Promise.all(
      rawTkeyStoreItems.map(async (x) => {
        const decryptedItem = await this.decrypt(x);
        return JSON.parse(decryptedItem.toString()) as TkeyStoreItemType;
      })
    );
    const finalItems = decryptedItems.filter((x) => x.id !== id);
    this.metadata.setTkeyStoreDomain(moduleName, finalItems);
    await this._syncShareMetadata();
  }

Suggested Correction

  async _deleteTKeyStoreItem(moduleName: string, id: string): Promise<void> {
            if (!this.tKey.metadata) {
                throw CoreError.metadataUndefined();
            }
            const rawTkeyStoreItems = (this.tKey.metadata.getTkeyStoreDomain(moduleName) as EncryptedMessage[]) || [];
            const decryptedItems = await Promise.all(
            rawTkeyStoreItems.map(async (x) => {
                  const decryptedItem = await this.tKey.decrypt(x);
                  return JSON.parse(decryptedItem.toString()) as TkeyStoreItemType;
                })
            );
            const finalItems = decryptedItems.filter((x) => x.id !== id);

            const finalRawTkeyStoreItems: EncryptedMessage[] = []
            for (let i = 0; i < finalItems.length; i++) {
                const encryptedData = await this.tKey.encrypt(Buffer.from(stringify(finalItems[i])));
                finalRawTkeyStoreItems.push(encryptedData)
            }
    
            this.tKey.metadata.setTkeyStoreDomain(moduleName, finalRawTkeyStoreItems);
            await this.tKey._syncShareMetadata();
  }
@minhnd410 minhnd410 changed the title _deleteTKeyStoreItem cause error for TkeyStoreDomain _deleteTKeyStoreItem cause error Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0