Skip to content

Commit

Permalink
Initialize ether wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Nov 16, 2024
1 parent fc923c7 commit d1b828c
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 280 deletions.
84 changes: 42 additions & 42 deletions src/clients/evm/OffChain/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainType, OffChainClientOptions, SignType } from '../../../types';
import { OffChainClientBase } from '../../../interface/OffChainClientBase';
import { ChainType, OffChainClientOptions, SignType } from "../../../types"
import { OffChainClientBase } from "../../../interface/OffChainClientBase"
import {
PrivateKeyAccount,
PublicClient,
Expand All @@ -8,36 +8,36 @@ import {
createPublicClient,
createWalletClient,
custom,
} from 'viem';
import { mainnet } from 'viem/chains';
import { OffChainRpc } from '../../../types/offChain';
} from "viem"
import { mainnet } from "viem/chains"
import { OffChainRpc } from "../../../types/offChain"

export class OffChainClient extends OffChainClientBase {
rpc: OffChainRpc | string;
public walletClient!: WalletClient;
public publicClient!: PublicClient;
public privateKeyAccount?: PrivateKeyAccount;
public chain: any;
public account!: { address: `0x${string}` };
rpc: OffChainRpc | string
public walletClient!: WalletClient
public publicClient!: PublicClient
public privateKeyAccount?: PrivateKeyAccount
public chain: any
public account!: { address: `0x${string}` }
constructor({
signType,
rpcUrl: rpc,
account: privateKeyAccount,
walletClient,
}: {
signType: SignType;
rpcUrl?: OffChainRpc | string;
account?: PrivateKeyAccount;
walletClient?: WalletClient;
signType: SignType
rpcUrl?: OffChainRpc | string
account?: PrivateKeyAccount
walletClient?: WalletClient
}) {
super(ChainType.evm, signType, rpc || OffChainRpc.mainnet);
this.rpc = rpc || OffChainRpc.mainnet;
const chain = mainnet;
this.chain = chain;
super(ChainType.evm, signType, rpc || OffChainRpc.mainnet)
this.rpc = rpc || OffChainRpc.mainnet
const chain = mainnet
this.chain = chain
this.publicClient = createPublicClient({
chain,
transport: http(),
});
})
this.walletClient =
walletClient ||
createWalletClient({
Expand All @@ -47,67 +47,67 @@ export class OffChainClient extends OffChainClientBase {
: window.ethereum
? custom(window.ethereum)
: http(),
});
this.privateKeyAccount = privateKeyAccount;
})
this.privateKeyAccount = privateKeyAccount
}

public async getAccount() {
let account;
let account
if (this.privateKeyAccount) {
account = this.privateKeyAccount;
account = this.privateKeyAccount
} else {
const accounts = await this.walletClient.getAddresses();
account = { address: accounts[0] } as PrivateKeyAccount;
const accounts = await this.walletClient.getAddresses()
account = { address: accounts[0] } as PrivateKeyAccount
}
return account;
return account
}

async signTypedData({
message,
types,
primaryType,
}: {
message: { [key: string]: any };
types: { [key: string]: { name: string; type: string }[] };
primaryType: string;
message: { [key: string]: any }
types: { [key: string]: { name: string; type: string }[] }
primaryType: string
}): Promise<{ message: any; signature: string }> {
const data: any = {
domain: {
name: 'sign.global',
version: '1',
name: "sign.global",
version: "1",
} as const,
message: message,
primaryType,
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: "name", type: "string" },
{ name: "version", type: "string" },
],
...types,
} as const,
};
const account = await this.getAccount();
}
const account = await this.getAccount()
const signTypedData: any = this.privateKeyAccount
? account.signTypedData
: this.walletClient.signTypedData;
: this.walletClient.signTypedData
const signature = await signTypedData({
account: account.address,
...data,
});
})
return {
message: data,
signature,
};
}
}

async signMessage(message: string): Promise<string> {
const account = await this.getAccount();
const account = await this.getAccount()
const signMessage = this.privateKeyAccount
? account.signMessage
: this.walletClient.signMessage;
: this.walletClient.signMessage
return await signMessage({
account: account.address,
message,
});
})
}
}
Loading

0 comments on commit d1b828c

Please sign in to comment.