diff --git a/sdk/src/VeraxSdk.ts b/sdk/src/VeraxSdk.ts index 76e1fd5b..e78df355 100644 --- a/sdk/src/VeraxSdk.ts +++ b/sdk/src/VeraxSdk.ts @@ -19,7 +19,7 @@ if (typeof window === "undefined") { const dotenv = require("dotenv"); dotenv.config({ path: "./.env" }); account = privateKeyToAccount(process.env.PRIVATE_KEY as Hex); -} else { +} else if (typeof window.ethereum !== "undefined") { window.ethereum.request({ method: "eth_requestAccounts" }).then((result: Address[]) => { account = result[0]; }); @@ -57,7 +57,7 @@ export class VeraxSdk { }; private readonly web3Client: PublicClient; - private readonly walletClient: WalletClient; + private readonly walletClient: WalletClient | undefined; public attestation: AttestationDataMapper; public schema: SchemaDataMapper; @@ -71,18 +71,22 @@ export class VeraxSdk { transport: http(), }); - this.walletClient = - conf.mode === SDKMode.BACKEND - ? createWalletClient({ - chain: conf.chain, - account, - transport: http(), - }) - : createWalletClient({ - chain: conf.chain, - account, - transport: custom(window.ethereum), - }); + if (typeof account === "undefined") { + this.walletClient = undefined; + } else { + this.walletClient = + conf.mode === SDKMode.BACKEND + ? createWalletClient({ + chain: conf.chain, + account, + transport: http(), + }) + : createWalletClient({ + chain: conf.chain, + account, + transport: custom(window.ethereum), + }); + } this.attestation = new AttestationDataMapper(conf, this.web3Client, this.walletClient, this); this.schema = new SchemaDataMapper(conf, this.web3Client, this.walletClient, this); diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index 92bd49d4..8ec1f95b 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -166,6 +166,7 @@ export default class AttestationDataMapper extends BaseDataMapper< } private async simulateContract(functionName: string, args: unknown[]) { + if (!this.walletClient) throw new Error("Account not available"); try { const { request } = await this.web3Client.simulateContract({ address: this.conf.attestationRegistryAddress, diff --git a/sdk/src/dataMapper/BaseDataMapper.ts b/sdk/src/dataMapper/BaseDataMapper.ts index 029131d4..16e85691 100644 --- a/sdk/src/dataMapper/BaseDataMapper.ts +++ b/sdk/src/dataMapper/BaseDataMapper.ts @@ -8,12 +8,12 @@ import axios from "axios"; export default abstract class BaseDataMapper { protected readonly conf: Conf; protected readonly web3Client: PublicClient; - protected readonly walletClient: WalletClient; + protected readonly walletClient: WalletClient | undefined; protected readonly veraxSdk: VeraxSdk; protected abstract typeName: string; protected abstract gqlInterface: string; - constructor(_conf: Conf, _web3Client: PublicClient, _walletClient: WalletClient, _veraxSdk: VeraxSdk) { + constructor(_conf: Conf, _web3Client: PublicClient, _walletClient: WalletClient | undefined, _veraxSdk: VeraxSdk) { this.conf = _conf; this.web3Client = _web3Client; this.walletClient = _walletClient; diff --git a/sdk/src/dataMapper/ModuleDataMapper.ts b/sdk/src/dataMapper/ModuleDataMapper.ts index b1f79354..003d9771 100644 --- a/sdk/src/dataMapper/ModuleDataMapper.ts +++ b/sdk/src/dataMapper/ModuleDataMapper.ts @@ -129,6 +129,7 @@ export default class ModuleDataMapper extends BaseDataMapper { +export async function executeTransaction(walletClient: WalletClient | undefined, request: any): Promise { + if (walletClient === undefined) throw new Error("Account not available"); const hash: Hash = await walletClient.writeContract(request); console.log(`Transaction sent with hash ${hash}`); return hash;