Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add web3modal extension #742

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
4df5a5a
add web3modal extension
hcote Apr 22, 2024
0b7d9c7
remove unused vars
hcote Apr 22, 2024
a5864bb
rename
hcote Apr 22, 2024
55cc255
move override methods to thirdpartywallet module
hcote Apr 23, 2024
a746b06
remove provider dependency
hcote Apr 23, 2024
ffbb08d
typing updates
hcote Apr 24, 2024
a92e507
fix logout on disconnect
hcote Apr 24, 2024
a29c027
add returns to fns routing to tpw methods
hcote Apr 24, 2024
c3b302e
Merge branch 'master' into hcote-web3modal
hcote Apr 24, 2024
26604e3
move all logic to thirdpartywallet module
hcote Apr 25, 2024
42a4280
dont route nftCheckout to 3pw
hcote Apr 25, 2024
5a56d1a
update iframe.allow for google login
hcote Apr 30, 2024
3732179
fix capitalization
hcote May 4, 2024
56bb93c
add connectWIthUI options param
hcote May 7, 2024
f262429
make param optional
hcote May 12, 2024
b9b9caa
add event 1193 event listeners after connecting
hcote May 13, 2024
56fde71
update isConnected initialization
hcote May 14, 2024
3390b6f
remove old tests
hcote May 15, 2024
ad6d291
update tests
hcote May 18, 2024
dac3493
fix deepsource lint errors
hcote May 19, 2024
7953979
Merge branch 'master' into hcote-web3modal
hcote May 19, 2024
3a83159
update ls key names
hcote May 20, 2024
d328947
temp: remove reference to ThirdPartyWallets
hcote May 21, 2024
b44497f
update import
hcote May 21, 2024
53124ba
pluralize thirdpartywallets & update to web3modal canary version for …
hcote May 29, 2024
afa46ff
fix tests
hcote May 29, 2024
0386f43
implement nft.checkout with 3rd-party wallets (#744)
octave08 May 31, 2024
e8f9583
nft checkout update
hcote May 31, 2024
d862d23
add intermediary events for nft.checkout
hcote Jun 6, 2024
baa4f36
Merge branch 'master' into hcote-web3modal
hcote Jun 6, 2024
7ebccbf
Merge branch 'master' into hcote-web3modal
hcote Jun 7, 2024
218e104
yarn.lock
hcote Jun 7, 2024
c714eea
update web3modal package
hcote Jun 12, 2024
c05cd91
Merge branch 'master' into hcote-web3modal
hcote Jun 12, 2024
884053f
yarn.lock
hcote Jun 12, 2024
39d6e46
update web3modal package
hcote Jun 14, 2024
ff13a5f
yarn.lock
hcote Jun 14, 2024
17937db
update web3modal sdk
hcote Jun 20, 2024
3fe30b8
Merge branch 'master' into hcote-web3modal
hcote Jun 20, 2024
fb7b0c8
remove unsubscribeFromModalEvents
hcote Jun 21, 2024
911b23e
remove comments
hcote Jun 21, 2024
adddb32
add web3modal polyfill
hcote Jun 22, 2024
f0a0b3a
pass configOptions directly
hcote Jul 2, 2024
bca2cfc
Merge branch 'master' into hcote-web3modal
hcote Jul 2, 2024
9499a53
remove polyfills
hcote Jul 2, 2024
3194efc
update eslint config
hcote Jul 2, 2024
95261f5
update jest coverage
hcote Jul 2, 2024
2b79c30
Merge branch 'master' into hcote-web3modal
hcote Jul 13, 2024
e3ecd35
yarn
hcote Jul 13, 2024
561e4f0
yarn.lock
hcote Jul 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update ls key names
  • Loading branch information
hcote committed May 20, 2024
commit 3a831593e178e1c71ede058360b3967c1014cb09
2 changes: 1 addition & 1 deletion packages/@magic-ext/web3modal-ethers5/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config: Config.InitialOptions = {
'^.+\\.(js|jsx)$': 'babel-jest',
'\\.(ts|tsx)$': 'ts-jest',
},
coveragePathIgnorePatterns: ['index.ts, index.cdn.ts', 'index.native.ts'],
coveragePathIgnorePatterns: ['index.cdn.ts', 'index.native.ts'],
};

export default config;
18 changes: 9 additions & 9 deletions packages/@magic-ext/web3modal-ethers5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export class Web3ModalExtension extends Extension.Internal<'web3modal'> {
}

public setIsConnected() {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('3pw_address', this.modal.getAddress() as string);
localStorage.setItem('3pw_chainId', (this.modal.getChainId() as number).toString());
localStorage.setItem('magic_3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_address', this.modal.getAddress() as string);
localStorage.setItem('magic_3pw_chainId', (this.modal.getChainId() as number).toString());
this.sdk.thirdPartyWallet.isConnected = true;
}

public initialize() {
this.sdk.thirdPartyWallet.enabledWallets.web3modal = true;
this.sdk.thirdPartyWallet.isConnected = Boolean(localStorage.getItem('3pw_address'));
this.sdk.thirdPartyWallet.isConnected = Boolean(localStorage.getItem('magic_3pw_address'));
this.sdk.thirdPartyWallet.eventListeners.push({
event: ThirdPartyWalletEvents.Web3ModalSelected,
callback: async (payloadId) => {
Expand All @@ -50,16 +50,16 @@ export class Web3ModalExtension extends Extension.Internal<'web3modal'> {

this.modal.subscribeProvider(({ address, chainId }) => {
// If user disconnected all accounts from wallet
if (!address && localStorage.getItem('3pw_address')) {
if (!address && localStorage.getItem('magic_3pw_address')) {
this.sdk.thirdPartyWallet.resetState();
return this.sdk.rpcProvider.emit('accountsChanged', []);
}
if (address && address !== localStorage.getItem('3pw_address')) {
localStorage.setItem('3pw_address', address);
if (address && address !== localStorage.getItem('magic_3pw_address')) {
localStorage.setItem('magic_3pw_address', address);
return this.sdk.rpcProvider.emit('accountsChanged', [address]);
}
if (chainId && chainId !== Number(localStorage.getItem('3pw_chainId'))) {
localStorage.setItem('3pw_chainId', chainId.toString());
if (chainId && chainId !== Number(localStorage.getItem('magic_3pw_chainId'))) {
localStorage.setItem('magic_3pw_chainId', chainId.toString());
return this.sdk.rpcProvider.emit('chainChanged', chainId);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const web3modalParams = {

test('setEip1193EventListeners emits chainChanged', () => {
const magic = createMagicSDKWithExtension({}, [new Web3ModalExtension(web3modalParams)]);
localStorage.setItem('3pw_address', '0x123');
localStorage.setItem('magic_3pw_address', '0x123');
magic.web3modal.modal.subscribeProvider = jest.fn(
(callback: (provider: { address: string; chainId: number }) => void) => {
callback({ address: '0x123', chainId: 1 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const web3modalParams = {
test('setIsConnected sets localStorage values', () => {
const magic = createMagicSDKWithExtension({}, [new Web3ModalExtension(web3modalParams)]);
magic.web3modal.setIsConnected();
expect(localStorage.getItem('3pw_provider')).toEqual('web3modal');
expect(localStorage.getItem('3pw_address')).toEqual('0x123');
expect(localStorage.getItem('3pw_chainId')).toEqual('1');
expect(localStorage.getItem('magic_3pw_provider')).toEqual('web3modal');
expect(localStorage.getItem('magic_3pw_address')).toEqual('0x123');
expect(localStorage.getItem('magic_3pw_chainId')).toEqual('1');
expect(magic.thirdPartyWallet.isConnected).toBeTruthy();
});
14 changes: 7 additions & 7 deletions packages/@magic-sdk/provider/src/modules/third-party-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export class ThirdPartyWalletModule extends BaseModule {
public isConnected = false;

public resetState() {
localStorage.removeItem('3pw_provider');
localStorage.removeItem('3pw_address');
localStorage.removeItem('3pw_chainId');
localStorage.removeItem('magic_3pw_provider');
localStorage.removeItem('magic_3pw_address');
localStorage.removeItem('magic_3pw_chainId');
this.isConnected = false;
}

Expand All @@ -30,7 +30,7 @@ export class ThirdPartyWalletModule extends BaseModule {
return this.logout(payload);
}
// Route all other requests to 3pw provider
switch (localStorage.getItem('3pw_provider')) {
switch (localStorage.getItem('magic_3pw_provider')) {
case 'web3modal':
return this.web3modalRequest(payload);
// Fallback to default request
Expand All @@ -43,7 +43,7 @@ export class ThirdPartyWalletModule extends BaseModule {
/* Core Method Overrides */

private isLoggedIn(payload: Partial<JsonRpcRequestPayload>): PromiEvent<boolean> {
switch (localStorage.getItem('3pw_provider')) {
switch (localStorage.getItem('magic_3pw_provider')) {
case 'web3modal':
return this.web3modalIsLoggedIn();
default:
Expand All @@ -53,7 +53,7 @@ export class ThirdPartyWalletModule extends BaseModule {
}

private getInfo(payload: Partial<JsonRpcRequestPayload>): PromiEvent<MagicUserMetadata> {
switch (localStorage.getItem('3pw_provider')) {
switch (localStorage.getItem('magic_3pw_provider')) {
case 'web3modal':
return this.web3modalGetInfo();
default:
Expand All @@ -63,7 +63,7 @@ export class ThirdPartyWalletModule extends BaseModule {
}

private logout(payload: Partial<JsonRpcRequestPayload>): PromiEvent<boolean> {
const provider = localStorage.getItem('3pw_provider');
const provider = localStorage.getItem('magic_3pw_provider');
this.resetState();
switch (provider) {
case 'web3modal': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ beforeEach(() => {

describe('third party wallet getInfo', () => {
it('should call web3modalGetInfo if provider is web3modal', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const payload = { method: 'getInfo' };
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalGetInfo').mockImplementation(() => Promise.resolve({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ beforeEach(() => {

describe('third party wallet isLoggedIn', () => {
it('should call web3modalIsLoggedIn if provider is web3modal', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const payload = { method: 'isLoggedIn' };
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalIsLoggedIn').mockImplementation(() => Promise.resolve({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ beforeEach(() => {

describe('third party wallet logout', () => {
it('should call web3modal.logout if provider is web3modal', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const payload = { method: 'logout' };
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalLogout').mockImplementation(() => Promise.resolve({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('third party wallet requestOverride', () => {
});

it('should call web3modalRequest if provider is web3modal', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const payload = { method: 'someMethod' };
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalRequest').mockImplementation(() => Promise.resolve({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ beforeEach(() => {
describe('third party wallet resetState', () => {
it('should clear relevant localStorage items and set isConnected to false', () => {
const magic = createMagicSDK();
localStorage.setItem('3pw_provider', 'some_provider');
localStorage.setItem('3pw_address', 'some_address');
localStorage.setItem('3pw_chainId', 'some_chainId');
localStorage.setItem('magic_3pw_provider', 'some_provider');
localStorage.setItem('magic_3pw_address', 'some_address');
localStorage.setItem('magic_3pw_chainId', 'some_chainId');
magic.thirdPartyWallet.isConnected = true;

magic.thirdPartyWallet.resetState();

expect(localStorage.getItem('3pw_provider')).toBeUndefined();
expect(localStorage.getItem('3pw_address')).toBeUndefined();
expect(localStorage.getItem('3pw_chainId')).toBeUndefined();
expect(localStorage.getItem('magic_3pw_provider')).toBeUndefined();
expect(localStorage.getItem('magic_3pw_address')).toBeUndefined();
expect(localStorage.getItem('magic_3pw_chainId')).toBeUndefined();
expect(magic.thirdPartyWallet.isConnected).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ beforeEach(() => {

describe('third party wallet web3modalGetInfo', () => {
it('should call getIsConnected', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const payload = { method: 'getInfo' };
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalGetInfo').mockImplementation(() => Promise.resolve({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ beforeEach(() => {

describe('third party wallet web3modalIsLoggedIn', () => {
it('should call getIsConnected', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalIsLoggedIn').mockImplementation(() => Promise.resolve({}));
magic.thirdPartyWallet.isLoggedIn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ beforeEach(() => {

describe('third party wallet web3modalLogout', () => {
it('should call getIsConnected', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const payload = { method: 'logout' };
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalLogout').mockImplementation(() => Promise.resolve({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ beforeEach(() => {

describe('third party wallet web3modalRequest', () => {
it('should call getIsConnected', () => {
localStorage.setItem('3pw_provider', 'web3modal');
localStorage.setItem('magic_3pw_provider', 'web3modal');
const payload = { method: 'request' };
const magic = createMagicSDK();
const spy = jest.spyOn(magic.thirdPartyWallet, 'web3modalRequest').mockImplementation(() => Promise.resolve({}));
Expand Down
Loading