Refactor wallet storage and subscription handling
- Updated WalletStore to manage localStorage and handle storage events more effectively. - Introduced mockAccountsSubscribe in constants for better account subscription handling. - Removed unnecessary storage subscription from InjectionConfig. - Enhanced Wallet class to utilize new account and metadata management methods.
This commit is contained in:
1 parent
1966988769
commit
38574daace
6 files changed
+78
-44
No files matched your search
+60
-21
@@ -1,10 +1,11 @@
|
||||
import type { ConnectRequest, SignTransactionRequest, WebApp } from "./types";
|
||||
import type { InjectionConfig } from "./types";
|
||||
import { WalletStore } from "./store";
|
||||
import type { InjectedAccount, InjectedExtension, InjectedWindowProvider } from "@polkadot/extension-inject/types";
|
||||
import type { Injected, InjectedAccount, InjectedExtension, InjectedMetadataKnown, InjectedWindowProvider } from "@polkadot/extension-inject/types";
|
||||
import { encodeRequest } from "./encryption";
|
||||
import { SignerPayloadRaw, SignerResult } from "@polkadot/types/types";
|
||||
import { generateRequestId, parseSignedTxFromStartParam, parseWalletsFromStartParam } from "./helpers";
|
||||
import { mockAccountsSubscribe } from "./constants";
|
||||
|
||||
export class Wallet implements InjectedWindowProvider {
|
||||
private readonly varanWalletUrl!: string;
|
||||
@@ -15,10 +16,10 @@ export class Wallet implements InjectedWindowProvider {
|
||||
|
||||
constructor(
|
||||
private readonly webApp: WebApp,
|
||||
{ callbackUrl, isDev, connectTimeout, storage }: Required<InjectionConfig>,
|
||||
{ callbackUrl, isDev, connectTimeout }: Required<InjectionConfig>,
|
||||
) {
|
||||
this.varanWalletUrl = isDev ? 'https://t.me/devReptileBot?startapp=' : 'https://t.me/varanwalletbot?startapp=';
|
||||
this.store = new WalletStore(storage);
|
||||
this.store = new WalletStore();
|
||||
this.callbackUrl = callbackUrl;
|
||||
this.connectTimeout = connectTimeout;
|
||||
void this._startParamsCheck();
|
||||
@@ -28,6 +29,12 @@ export class Wallet implements InjectedWindowProvider {
|
||||
return '1.0.0';
|
||||
}
|
||||
|
||||
private get signer(): InjectedExtension['signer'] {
|
||||
return {
|
||||
signRaw: (raw) => this.signRawTransaction(raw),
|
||||
};
|
||||
}
|
||||
|
||||
private async _startParamsCheck(): Promise<void> {
|
||||
let actions = 0;
|
||||
const newWalletAddresses = parseWalletsFromStartParam(this.webApp);
|
||||
@@ -66,7 +73,7 @@ export class Wallet implements InjectedWindowProvider {
|
||||
|
||||
|
||||
if (actions) {
|
||||
this.webApp.close();
|
||||
// this.webApp.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +89,29 @@ export class Wallet implements InjectedWindowProvider {
|
||||
return Promise.resolve(accounts.map(this.formatAccount));
|
||||
}
|
||||
|
||||
private get extensionAccounts(): InjectedExtension['accounts'] {
|
||||
return {
|
||||
get: () => this.accounts,
|
||||
subscribe: mockAccountsSubscribe
|
||||
};
|
||||
}
|
||||
|
||||
private get extensionMetadata(): InjectedExtension['metadata'] {
|
||||
const metadata = this.store.get('injectedMetadataKnown');
|
||||
const setMetadata = async (metadata: InjectedMetadataKnown) => {
|
||||
await this.store.set('injectedMetadataKnown', metadata);
|
||||
return true;
|
||||
};
|
||||
if (!metadata) return {
|
||||
get: () => Promise.resolve([]),
|
||||
provide: setMetadata,
|
||||
};
|
||||
return {
|
||||
get: () => Promise.resolve([metadata]),
|
||||
provide: setMetadata,
|
||||
};
|
||||
}
|
||||
|
||||
private async signRawTransaction(raw: SignerPayloadRaw): Promise<SignerResult> {
|
||||
const accounts = await this.accounts;
|
||||
if (!accounts.length) throw new Error('No accounts found');
|
||||
@@ -143,27 +173,36 @@ export class Wallet implements InjectedWindowProvider {
|
||||
}
|
||||
|
||||
async connect(): Promise<InjectedExtension> {
|
||||
await Promise.race([
|
||||
this._connect(),
|
||||
new Promise<void>((_, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(new Error('Connect timeout'));
|
||||
}, this.connectTimeout);
|
||||
})
|
||||
]);
|
||||
const accounts = await this.accounts;
|
||||
if (!accounts.length) {
|
||||
await Promise.race([
|
||||
this._connect(),
|
||||
new Promise<void>((_, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(new Error('Connect timeout'));
|
||||
}, this.connectTimeout);
|
||||
})
|
||||
]);
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
name: this.name,
|
||||
version: this.version,
|
||||
accounts: {
|
||||
get: () => this.accounts,
|
||||
subscribe: () => {
|
||||
return () => {};
|
||||
},
|
||||
},
|
||||
signer: {
|
||||
signRaw: (raw) => this.signRawTransaction(raw),
|
||||
},
|
||||
accounts: this.extensionAccounts,
|
||||
signer: this.signer,
|
||||
metadata: this.extensionMetadata,
|
||||
});
|
||||
}
|
||||
|
||||
async enable(): Promise<Injected> {
|
||||
const accounts = await this.accounts;
|
||||
if (!accounts.length) {
|
||||
throw new Error('Please connect first');
|
||||
}
|
||||
return {
|
||||
accounts: this.extensionAccounts,
|
||||
signer: this.signer,
|
||||
metadata: this.extensionMetadata,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user