burner-connector
Version:
1. Install the dependencies:
37 lines • 1.43 kB
JavaScript
import { generatePrivateKey } from "viem/accounts";
const burnerStorageKey = "burnerWallet.pk";
export const burnerWalletId = "burnerWallet";
export const burnerWalletName = "Burner Wallet";
/**
* Checks if the private key is valid
*/
const isValidPK = (pk) => {
return pk?.length === 64 || pk?.length === 66;
};
/**
* Save the current burner private key to storage
*/
const saveBurnerPK = ({ privateKey, useSessionStorage = false, }) => {
if (typeof window !== "undefined" && window != null) {
const storage = useSessionStorage ? window.sessionStorage : window.localStorage;
storage?.setItem(burnerStorageKey, privateKey);
}
};
/**
* Gets the current burner private key from local/session storage
*/
export const loadBurnerPK = ({ useSessionStorage = false } = {}) => {
let currentSk = "0x";
if (typeof window !== "undefined" && window != null) {
const storage = useSessionStorage ? window.sessionStorage : window.localStorage;
currentSk = (storage?.getItem?.(burnerStorageKey)?.replaceAll('"', "") ?? "0x");
}
if (!!currentSk && isValidPK(currentSk)) {
return currentSk;
}
// If no burner is found in storage, we will generate a random private key
const newDefaultPrivateKey = generatePrivateKey();
saveBurnerPK({ privateKey: newDefaultPrivateKey, useSessionStorage });
return newDefaultPrivateKey;
};
//# sourceMappingURL=index.js.map