@thirdweb-dev/wallets
Version:
<p align="center"> <br /> <a href="https://thirdweb.com"><img src="https://github.com/thirdweb-dev/js/blob/main/legacy_packages/sdk/logo.svg?raw=true" width="200" alt=""/></a> <br /> </p> <h1 align="center">thirdweb Wallet SDK</h1> <p align="center"> <a h
134 lines (128 loc) • 4.01 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var defineProperty = require('../../../../dist/defineProperty-9051a5d3.cjs.dev.js');
var normalizeChainId = require('../../../../dist/normalizeChainId-5da85f42.cjs.dev.js');
var connector = require('../../../../dist/connector-a63dd9e7.cjs.dev.js');
var sdk = require('@thirdweb-dev/sdk');
var ethers = require('ethers');
var utils = require('ethers/lib/utils');
require('eventemitter3');
class WrappedSigner extends ethers.Signer {
constructor(signer) {
super();
this.signer = signer;
utils.defineReadOnly(this, "provider", signer.provider);
}
async getAddress() {
return await this.signer.getAddress();
}
async signMessage(message) {
return await this.signer.signMessage(message);
}
async signTransaction(transaction) {
return await this.signer.signTransaction(transaction);
}
connect(provider) {
return new WrappedSigner(this.signer.connect(provider));
}
_signTypedData(domain, types, value) {
return this.signer._signTypedData(domain, types, value);
}
async sendTransaction(transaction) {
if (!this.provider) {
throw new Error("Provider not found");
}
const gas = await sdk.getDefaultGasOverrides(this.provider);
const txWithGas = {
...gas,
...transaction
};
return await this.signer.sendTransaction(txWithGas);
}
}
class LocalWalletConnector extends connector.Connector {
constructor(options) {
super();
defineProperty._defineProperty(this, "id", "local_wallet");
defineProperty._defineProperty(this, "name", "Local Wallet");
defineProperty._defineProperty(this, "shimDisconnectKey", "localWallet.shimDisconnect");
defineProperty._defineProperty(this, "onChainChanged", chainId => {
const id = normalizeChainId.normalizeChainId(chainId);
const unsupported = !this.options.chains.find(c => c.chainId === id);
this.emit("change", {
chain: {
id,
unsupported
}
});
});
this.options = options;
}
async connect(args) {
if (args.chainId) {
this.switchChain(args.chainId);
}
const signer = await this.getSigner();
const address = await signer.getAddress();
return address;
}
async disconnect() {
this._provider = undefined;
this._signer = undefined;
}
async getAddress() {
const signer = await this.getSigner();
if (!signer) {
throw new Error("No signer found");
}
return await signer.getAddress();
}
async isConnected() {
try {
const addr = await this.getAddress();
return !!addr;
} catch {
return false;
}
}
async getProvider() {
if (!this._provider) {
this._provider = sdk.getChainProvider(this.options.chain, {
clientId: this.options.clientId,
secretKey: this.options.secretKey
});
}
return this._provider;
}
async getSigner() {
if (!this._signer) {
const provider = await this.getProvider();
this._signer = getSignerFromEthersWallet(this.options.ethersWallet, provider);
}
return this._signer;
}
async switchChain(chainId) {
const chain = this.options.chains.find(c => c.chainId === chainId);
if (!chain) {
throw new Error(`Chain not found for chainId ${chainId}, please add it to the chains property when creating this wallet`);
}
this._provider = sdk.getChainProvider(chain, {
clientId: this.options.clientId,
secretKey: this.options.secretKey
});
this._signer = getSignerFromEthersWallet(this.options.ethersWallet, this._provider);
this.onChainChanged(chainId);
}
async setupListeners() {}
updateChains(chains) {
this.options.chains = chains;
}
}
function getSignerFromEthersWallet(ethersWallet, provider) {
let signer = ethersWallet;
if (provider) {
signer = ethersWallet.connect(provider);
}
return new WrappedSigner(signer);
}
exports.LocalWalletConnector = LocalWalletConnector;