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

Fix/existed sdk handle window not defined #372

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions .changeset/long-windows-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@blocto/aptos-wallet-adapter-plugin': patch
'@blocto/connectkit-connector': patch
'@blocto/rainbowkit-connector': patch
'@blocto/web3-react-connector': patch
'@blocto/web3modal-connector': patch
'@blocto/wagmi-connector': patch
'@blocto/sdk': patch
'@blocto/dappauth': patch
---

fix: handle existedSDK handle window not defined
22 changes: 11 additions & 11 deletions packages/blocto-sdk/src/providers/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class AptosProvider
sessionKey: KEY_SESSION;

private get existedSDK() {
if (typeof window === 'undefined') return undefined;
return (window as any).bloctoAptos;
}

Expand Down Expand Up @@ -147,7 +148,6 @@ export default class AptosProvider
transaction: AptosTypes.TransactionPayload,
txOptions: TxOptions = {}
): Promise<{ hash: AptosTypes.HexEncodedBytes }> {

if (this.existedSDK) {
return this.existedSDK.signAndSubmitTransaction(transaction, txOptions);
}
Expand Down Expand Up @@ -217,7 +217,6 @@ export default class AptosProvider
}

async signMessage(payload: SignMessagePayload): Promise<SignMessageResponse> {

const formattedPayload = checkMessagePayloadFormat(payload);

if (this.existedSDK) {
Expand Down Expand Up @@ -291,7 +290,10 @@ export default class AptosProvider
if (this.existedSDK) {
return new Promise((resolve, reject) =>
// add a small delay to make sure the network has been switched
setTimeout(() => this.existedSDK.connect().then(resolve).catch(reject), 10)
setTimeout(
() => this.existedSDK.connect().then(resolve).catch(reject),
10
)
);
}

Expand Down Expand Up @@ -406,18 +408,16 @@ export default class AptosProvider
}

override on(event: string, listener: (arg: any) => void): void {
if (this.existedSDK)
this.existedSDK.on(event, listener);

if (this.existedSDK) this.existedSDK.on(event, listener);

super.on(event, listener);
}

override removeListener(event: string, listener: (arg: any) => void): void {
if (this.existedSDK)
this.existedSDK.off(event, listener);

if (this.existedSDK) this.existedSDK.off(event, listener);

super.removeListener(event, listener);
}

off = this.removeListener;
}
8 changes: 8 additions & 0 deletions packages/blocto-sdk/src/providers/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default class EthereumProvider
};

private get existedSDK() {
if (typeof window === 'undefined') return undefined;
return (window as any).ethereum;
}

Expand Down Expand Up @@ -647,6 +648,13 @@ export default class EthereumProvider
// eip-1102 alias
// DEPRECATED API: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1102.md
async enable(email?: string): Promise<ProviderAccounts> {
if (typeof window === 'undefined') {
throw ethErrors.provider.custom({
code: 1001,
message: 'Blocto SDK only works in browser environment',
});
}

const { walletServer, blockchainName, sessionKeyEnv } =
await this.#getBloctoProperties();

Expand Down
Loading