mirror of
https://github.com/luxfi/eth-walletconnect-keyring.git
synced 2026-10-30 16:58:01 +00:00
72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
import KeyValueStorage from '@walletconnect/keyvaluestorage';
|
|
|
|
export interface Data {
|
|
address: string;
|
|
brandName: string;
|
|
chainId?: number;
|
|
silent?: boolean;
|
|
status?: number;
|
|
sessionStatus?: string;
|
|
networkDelay?: number;
|
|
namespace?: string;
|
|
deepLink: string;
|
|
}
|
|
|
|
const storage = new KeyValueStorage();
|
|
|
|
export class Cached {
|
|
topics: Map<string, Data> = new Map();
|
|
|
|
constructor() {
|
|
storage.getItem('wc_topics').then((topics) => {
|
|
if (topics) {
|
|
const topicsArr =
|
|
typeof topics === 'object' ? topics : JSON.parse(topics);
|
|
this.topics = new Map(topicsArr);
|
|
}
|
|
});
|
|
}
|
|
|
|
getTopic(topic: string) {
|
|
return this.topics.get(topic);
|
|
}
|
|
|
|
setTopic(topic: string, data: Data) {
|
|
this.topics.set(topic, data);
|
|
storage.setItem('wc_topics', JSON.stringify(Array.from(this.topics)));
|
|
}
|
|
|
|
deleteTopic(topic: string) {
|
|
this.topics.delete(topic);
|
|
storage.setItem('wc_topics', JSON.stringify(Array.from(this.topics)));
|
|
}
|
|
|
|
updateTopic(topic: string, data: Partial<Data>) {
|
|
if (this.topics.has(topic)) {
|
|
this.setTopic(topic, {
|
|
...this.getTopic(topic),
|
|
...data
|
|
} as Data);
|
|
}
|
|
}
|
|
|
|
findTopic(data: Partial<Data>): string | undefined {
|
|
const { address, brandName, chainId } = data;
|
|
|
|
const keys = this.topics.keys();
|
|
for (const key of keys) {
|
|
const value = this.getTopic(key);
|
|
if (
|
|
value?.address.toLowerCase() === address?.toLowerCase() &&
|
|
value?.brandName.toLowerCase() === brandName?.toLowerCase()
|
|
) {
|
|
return key;
|
|
}
|
|
}
|
|
}
|
|
|
|
getAllTopics() {
|
|
return [...this.topics.keys()];
|
|
}
|
|
}
|