Skip to content

Commit

Permalink
fix: burner wallet storage incompatibility with Node.js env (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron authored Jul 12, 2024
1 parent fccb12d commit 481b5fe
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .changeset/thick-squids-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@fuel-connectors/burner-wallet-connector": patch
"@fuels/connectors": patch
---

- [Add support for burner wallet config on default connectors](https://github.com/FuelLabs/fuel-connectors/commit/fb845c0cfdfccfda60637af8d405964e9efbdecb)
- [Burner wallet storage not working on nodejs](https://github.com/FuelLabs/fuel-connectors/commit/bbf19a2f8b6faeafc5c99055fe2ce2beb2988400)
- [Create in memory storage on burner wallet](https://github.com/FuelLabs/fuel-connectors/commit/242b24f84f4ac35dba9c1332cb46c80bac5f4766)

Observation: Burner Wallet's Storage will not persist information between executions on Vercel or Node.js env.
11 changes: 6 additions & 5 deletions packages/burner-wallet-connector/src/BurnerWalletConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Wallet,
type WalletUnlocked,
} from 'fuels';
import { InMemoryStorage } from './InMemoryStorage';
import {
BURNER_WALLET_ICON,
BURNER_WALLET_PRIVATE_KEY,
Expand Down Expand Up @@ -88,13 +89,13 @@ export class BurnerWalletConnector extends FuelConnector {
}

private getStorage(storage?: StorageAbstract) {
const _storage =
storage ?? (WINDOW.localStorage as unknown as StorageAbstract);
if (!_storage) {
throw new Error('No storage provided');
if (storage) {
return storage;
}

return _storage;
return typeof window !== 'undefined' && window.localStorage
? (window.localStorage as unknown as StorageAbstract)
: new InMemoryStorage();
}

/**
Expand Down
26 changes: 26 additions & 0 deletions packages/burner-wallet-connector/src/InMemoryStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { StorageAbstract } from 'fuels';

export class InMemoryStorage implements StorageAbstract {
private storage: Map<string, string>;

constructor() {
this.storage = new Map<string, string>();
}

async setItem(key: string, value: string): Promise<void> {
this.storage.set(key, value);
}

async getItem(key: string): Promise<string | null> {
const value = this.storage.get(key);
return value !== undefined ? value : null;
}

async removeItem(key: string): Promise<void> {
this.storage.delete(key);
}

async clear(): Promise<void> {
this.storage.clear();
}
}
5 changes: 4 additions & 1 deletion packages/connectors/src/defaultConnectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import { FuelWalletConnector } from '@fuel-connectors/fuel-wallet';
import { FueletWalletConnector } from '@fuel-connectors/fuelet-wallet';
import { WalletConnectConnector } from '@fuel-connectors/walletconnect-connector';
import type { FuelConnector } from 'fuels';
import type { BurnerWalletConfig } from '../../burner-wallet-connector/src/types';

type DefaultConnectors = {
devMode?: boolean;
burnerWalletConfig?: BurnerWalletConfig;
};

export function defaultConnectors({
devMode,
burnerWalletConfig,
}: DefaultConnectors = {}): Array<FuelConnector> {
const connectors = [
new FuelWalletConnector(),
new BakoSafeConnector(),
new FueletWalletConnector(),
new WalletConnectConnector(),
new BurnerWalletConnector(),
new BurnerWalletConnector(burnerWalletConfig),
];

if (devMode) {
Expand Down

0 comments on commit 481b5fe

Please sign in to comment.