@thirdweb-dev/wallets
Version:
<p align="center"> <br /> <a href="https://thirdweb.com"><img src="https://github.com/thirdweb-dev/js/blob/main/legacy_packages/sdk/logo.svg?raw=true" width="200" alt=""/></a> <br /> </p> <h1 align="center">thirdweb Wallet SDK</h1> <p align="center"> <a h
188 lines (180 loc) • 6.47 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var evm_wallets_localWallet_dist_thirdwebDevWalletsEvmWalletsLocalWallet = require('../../local-wallet/dist/thirdweb-dev-wallets-evm-wallets-local-wallet.cjs.prod.js');
require('../../../../dist/defineProperty-b749763b.cjs.prod.js');
require('../../../../dist/walletIds-6ed32bf4.cjs.prod.js');
require('../../../../dist/base-5085b4d0.cjs.prod.js');
require('@thirdweb-dev/chains');
require('../../abstract/dist/thirdweb-dev-wallets-evm-wallets-abstract.cjs.prod.js');
require('ethers');
require('eventemitter3');
require('@thirdweb-dev/sdk');
require('../../../../dist/headers-140df57f.cjs.prod.js');
require('@thirdweb-dev/crypto');
/* eslint-disable @typescript-eslint/no-var-requires */
class AsyncJsonFileStorage {
constructor(filePath) {
this.filePath = require("node:path").resolve(filePath);
}
async getItem(key) {
const content = await require("node:fs/promises").readFile(this.filePath, {
encoding: "utf-8"
});
if (!content) {
return null;
}
const data = JSON.parse(content);
return data[key];
}
async setItem(key, value) {
// if the file doesn't exist, create it
try {
const content = await require("node:fs/promises").readFile(this.filePath, {
encoding: "utf-8"
});
const data = content ? JSON.parse(content) : {};
data[key] = value;
await require("node:fs/promises").writeFile(this.filePath, JSON.stringify(data));
} catch {
await require("node:fs/promises").writeFile(this.filePath, JSON.stringify({
[key]: value
}));
}
}
async removeItem(key) {
const content = await require("node:fs/promises").readFile(this.filePath, {
encoding: "utf-8"
});
const data = JSON.parse(content);
delete data[key];
await require("node:fs/promises").writeFile(this.filePath, JSON.stringify(data));
}
}
/**
* Generate [Local Wallets](https://portal.thirdweb.com/glossary/local-wallet) in Node.js backends, CLIs and scripts.
*
* This is very useful for situations where you need a wallet for your backend services, scripts or CLI.
*
* After generating wallets this way, you can persist it in different ways. For example, you can save it to a file, or store it in a database.
*
* @example
* ```javascript
* import { ThirdwebSDK } from "@thirdweb-dev/sdk";
* import { LocalWalletNode } from "@thirdweb-dev/wallets/evm/wallets/local-wallet-node";
*
* // wallet data will be saved in 'wallet.json' by default
* // you can also pass a different file path by specifying 'storageJsonFile' in the constructor
* const wallet = new LocalWalletNode();
*
* // generate a random wallet
* await wallet.generate();
*
* // connect wallet
* await wallet.connect();
*
* // at any point, you can save the wallet to persistent storage
* await wallet.save(config);
* // and load it back up
* await wallet.load(config);
*
* // you can also export the wallet out to send somewhere else
* const exportedWallet = await wallet.export(config);
* // and import it back in
* await wallet.import(exportedWallet, config);
*
* // You can then use this wallet to perform transactions via the SDK
* const sdk = await ThirdwebSDK.fromWallet(wallet, "goerli");
* ```
*
* ## Local Wallet Backup
*
* Local wallets can be persisted in a file, database, or backed up to another cloud provider. Currently 3 formats are supported:
*
* - `encryptedJSON` - a standard format for encrypted wallets, this requires a password to encrypt/decrypt the wallet. Recommended for safe backups.
* - `privateKey` - the raw private key. This can be stored encrypted or un-encrypted. If not encrypted, make sure you store it somewhere safe.
* - `mnemonic` - the raw seed phrase. This can be stored encrypted or un-encrypted. If not encrypted, make sure you store it somewhere safe.
*
* We provide encryption capabilities out of the box, but you can customize the type of encryption, and also turn it off completely.
*
* The type of storage can also be overridden, allowing you to store wallets anywhere you want, including remote locations.
*
* By default, the local wallet will be saved in a `wallet.json` file at the root of your project. The path to the file can be customized by passing a `storageJsonFile` option to the constructor.
*
* Customizing where the wallet data is persisted only requires implementing a simple interface. Here's an example of a Local Wallet with custom storage:
*
* ```typescript
* class MyStorage implements AsyncStorage {
* getItem(key: string): Promise<string | null> { ... }
* setItem(key: string, value: string): Promise<void> { ... }
* removeItem(key: string): Promise<void> { ... }
* }
*
* const wallet = new LocalWalletNode({
* storage: new MyStorage(),
* })
* ```
*
* You can implement this any way you like, file persistance, remote cloud storage, etc.
*
* ## Encryption examples
*
* Here's some examples of different encryption configurations:
*
* 1. Save an encrypted privateKey with default encryption a password as the encryption key:
*
* ```javascript
* localWallet.save({
* strategy: "privateKey",
* encryption: {
* password: "your-encryption-password", // uses default encryption
* },
* });
* ```
*
* 2. Import a raw private key or mnemonic (seed phrase) with no encryption:
*
* ```javascript
* // privateKey
* localWallet.import({
* privateKey: "your-raw-privateKey",
* encryption: false, // no encryption
* });
*
* // mnemonic
* localWallet.import({
* mnemonic: "your-raw-mnemonic",
* encryption: false, // no encryption
* });
* ```
*
* 3. Save an encrypted mnemonic with a custom encryption:
*
* ```javascript
* // privateKey
* localWallet.save({
* strategy: "mnemonic",
* encryption: {
* encrypt: (message: string, password: string) => {
* return yourCustomEncryption(message, password);
* },
* password: "your-encryption-password",
* },
* });
* ```
*
* @wallet
*/
class LocalWalletNode extends evm_wallets_localWallet_dist_thirdwebDevWalletsEvmWalletsLocalWallet.LocalWallet {
/**
*
* @param options - Wallet options to initialize the LocalWalletNode
*/
constructor(options) {
const storage = new AsyncJsonFileStorage(options?.storageJsonFile || "./wallet.json");
super({
storage,
...options
});
}
}
exports.LocalWalletNode = LocalWalletNode;