UNPKG

@near-js/keystores-browser

Version:

KeyStore implementation for working with keys in browser LocalStorage

108 lines (107 loc) 3.46 kB
import { KeyPair } from "@near-js/crypto"; import { KeyStore } from "@near-js/keystores"; const LOCAL_STORAGE_KEY_PREFIX = "near-api-js:keystore:"; class BrowserLocalStorageKeyStore extends KeyStore { /** @hidden */ localStorage; /** @hidden */ prefix; /** * @param localStorage defaults to window.localStorage * @param prefix defaults to `near-api-js:keystore:` */ constructor(localStorage = window.localStorage, prefix = LOCAL_STORAGE_KEY_PREFIX) { super(); this.localStorage = localStorage; this.prefix = prefix; } /** * Stores a {@link KeyPair} in local storage. * @param networkId The targeted network. (ex. default, betanet, etc…) * @param accountId The NEAR account tied to the key pair * @param keyPair The key pair to store in local storage */ async setKey(networkId, accountId, keyPair) { this.localStorage.setItem(this.storageKeyForSecretKey(networkId, accountId), keyPair.toString()); } /** * Gets a {@link KeyPair} from local storage * @param networkId The targeted network. (ex. default, betanet, etc…) * @param accountId The NEAR account tied to the key pair * @returns {Promise<KeyPair>} */ async getKey(networkId, accountId) { const value = this.localStorage.getItem(this.storageKeyForSecretKey(networkId, accountId)); if (!value) { return null; } return KeyPair.fromString(value); } /** * Removes a {@link KeyPair} from local storage * @param networkId The targeted network. (ex. default, betanet, etc…) * @param accountId The NEAR account tied to the key pair */ async removeKey(networkId, accountId) { this.localStorage.removeItem(this.storageKeyForSecretKey(networkId, accountId)); } /** * Removes all items that start with `prefix` from local storage */ async clear() { for (const key of this.storageKeys()) { if (key.startsWith(this.prefix)) { this.localStorage.removeItem(key); } } } /** * Get the network(s) from local storage * @returns {Promise<string[]>} */ async getNetworks() { const result = /* @__PURE__ */ new Set(); for (const key of this.storageKeys()) { if (key.startsWith(this.prefix)) { const parts = key.substring(this.prefix.length).split(":"); result.add(parts[1]); } } return Array.from(result.values()); } /** * Gets the account(s) from local storage * @param networkId The targeted network. (ex. default, betanet, etc…) */ async getAccounts(networkId) { const result = new Array(); for (const key of this.storageKeys()) { if (key.startsWith(this.prefix)) { const parts = key.substring(this.prefix.length).split(":"); if (parts[1] === networkId) { result.push(parts[0]); } } } return result; } /** * @hidden * Helper function to retrieve a local storage key * @param networkId The targeted network. (ex. default, betanet, etc…) * @param accountId The NEAR account tied to the storage key that's sought * @returns {string} An example might be: `near-api-js:keystore:near-friend:default` */ storageKeyForSecretKey(networkId, accountId) { return `${this.prefix}${accountId}:${networkId}`; } /** @hidden */ *storageKeys() { for (let i = 0; i < this.localStorage.length; i++) { yield this.localStorage.key(i); } } } export { BrowserLocalStorageKeyStore };