Skip to content

_deleteTKeyStoreItem cause error #182

Description

@minhnd410

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();
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions