From 03714b74878582fc8550adeaaa7e888c45fed31b Mon Sep 17 00:00:00 2001 From: mtyszczak <mateusz.tyszczak@gmail.com> Date: Fri, 21 Mar 2025 14:56:11 +0100 Subject: [PATCH] Do not create a new chain for each beekeeper provider --- examples/ts/signature-extension/package.json | 3 --- ts/packages/signers-beekeeper/README.md | 2 +- ts/packages/signers-beekeeper/src/index.ts | 20 +++++++++----------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/examples/ts/signature-extension/package.json b/examples/ts/signature-extension/package.json index eb75f7a0b..75df6e5cd 100644 --- a/examples/ts/signature-extension/package.json +++ b/examples/ts/signature-extension/package.json @@ -7,9 +7,6 @@ "test": "../../../ts/npm-common-config/scripts/bash/npm-helpers/npm_download_browser_extensions.sh \"${HOME}/preinstalled_browser_extensions\" && ln -sf \"${HOME}/preinstalled_browser_extensions\" ./__tests__/extensions && playwright install chromium && playwright test --max-failures 1", "test:manual": "rm -rf ./.parcel-cache && parcel test/index.html" }, - "alias": { - "@hiveio/wax": "@hiveio/wax" - }, "dependencies": { "@hiveio/beekeeper": "1.27.10-stable.250305202831", "@hiveio/wax": "file:../../../ts", diff --git a/ts/packages/signers-beekeeper/README.md b/ts/packages/signers-beekeeper/README.md index 57fb97f10..680380f03 100644 --- a/ts/packages/signers-beekeeper/README.md +++ b/ts/packages/signers-beekeeper/README.md @@ -10,7 +10,7 @@ import BeekeeperProvider from "@hiveio/wax-signers-beekeeper"; const chain = await createHiveChain(); -const provider = BeekeeperProvider.for(myWallet, "myaccount", "active"); +const provider = BeekeeperProvider.for(myWallet, "myaccount", "active", chain); // Create a transaction using the Wax Hive chain instance const tx = await chain.createTransaction(); diff --git a/ts/packages/signers-beekeeper/src/index.ts b/ts/packages/signers-beekeeper/src/index.ts index 486c68f79..8d5235f79 100644 --- a/ts/packages/signers-beekeeper/src/index.ts +++ b/ts/packages/signers-beekeeper/src/index.ts @@ -1,4 +1,4 @@ -import { createHiveChain, type IOnlineSignatureProvider, type ITransaction, type TAccountName, type TRole } from "@hiveio/wax"; +import type { IHiveChainInterface, IOnlineSignatureProvider, ITransaction, TAccountName, TRole } from "@hiveio/wax"; import type { IBeekeeperUnlockedWallet, TPublicKey } from "@hiveio/beekeeper"; @@ -10,7 +10,7 @@ export class WaxBeekeeperProviderError extends Error {} * * @example * ``` - * const provider = BeekeeperProvider.for(myWallet, "myaccount", "active"); + * const provider = BeekeeperProvider.for(myWallet, "myaccount", "active", chain); * * // Create a transaction using the Wax Hive chain instance * const tx = await chain.createTransaction(); @@ -30,19 +30,17 @@ class BeekeeperProvider implements IOnlineSignatureProvider { private readonly publicKey: TPublicKey ) {} - public static for(wallet: IBeekeeperUnlockedWallet, publicKeyOrAccount: TPublicKey | TAccountName, role?: TRole): BeekeeperProvider | Promise<BeekeeperProvider> { + public static for(wallet: IBeekeeperUnlockedWallet, publicKeyOrAccount: TPublicKey | TAccountName, role?: TRole, chain?: IHiveChainInterface): BeekeeperProvider | Promise<BeekeeperProvider> { if (role === undefined) return new BeekeeperProvider(wallet, publicKeyOrAccount); - return createHiveChain().then(chain => { - return chain.api.database_api.find_accounts({ accounts: [publicKeyOrAccount], delayed_votes_active: false }).then(({ accounts: [ account ] }) => { - if (account === undefined) - return Promise.reject(new WaxBeekeeperProviderError(`Account ${publicKeyOrAccount} not found`)); + return chain!.api.database_api.find_accounts({ accounts: [publicKeyOrAccount], delayed_votes_active: false }).then(({ accounts: [ account ] }) => { + if (account === undefined) + return Promise.reject(new WaxBeekeeperProviderError(`Account ${publicKeyOrAccount} not found`)); - const actualRole = role === "memo" ? "memo_key" : role; + const actualRole = role === "memo" ? "memo_key" : role; - return account[actualRole] ? new BeekeeperProvider(wallet, role === "memo" ? account.memo_key : account[role].key_auths[0][0]) : Promise.reject(new WaxBeekeeperProviderError(`Account ${publicKeyOrAccount} does not have ${role} key`)); - }); + return account[actualRole] ? new BeekeeperProvider(wallet, role === "memo" ? account.memo_key : account[role].key_auths[0][0]) : Promise.reject(new WaxBeekeeperProviderError(`Account ${publicKeyOrAccount} does not have ${role} key`)); }); } @@ -53,7 +51,7 @@ class BeekeeperProvider implements IOnlineSignatureProvider { export interface WaxBeekeeperProviderCreator { for(wallet: IBeekeeperUnlockedWallet, publicKey: TPublicKey): BeekeeperProvider; - for(wallet: IBeekeeperUnlockedWallet, account: TAccountName, role: TRole): Promise<BeekeeperProvider>; + for(wallet: IBeekeeperUnlockedWallet, account: TAccountName, role: TRole, chain: IHiveChainInterface): Promise<BeekeeperProvider>; } export default BeekeeperProvider as WaxBeekeeperProviderCreator; \ No newline at end of file -- GitLab