@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
277 lines (272 loc) • 8.25 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var defineProperty = require('../../../../dist/defineProperty-9051a5d3.cjs.dev.js');
var ethers = require('ethers');
var normalizeChainId = require('../../../../dist/normalizeChainId-5da85f42.cjs.dev.js');
var walletIds = require('../../../../dist/walletIds-a0be5020.cjs.dev.js');
var connector = require('../../../../dist/connector-a63dd9e7.cjs.dev.js');
var embeddedWallet = require('../../../../dist/embedded-wallet-5fe773c1.cjs.dev.js');
require('eventemitter3');
require('@paperxyz/sdk-common-utilities');
require('ethers/lib/utils');
require('@thirdweb-dev/sdk');
class EmbeddedWalletConnector extends connector.Connector {
constructor(options) {
super();
defineProperty._defineProperty(this, "id", walletIds.walletIds.paper);
defineProperty._defineProperty(this, "name", "Embedded Wallet");
defineProperty._defineProperty(this, "ready", true);
defineProperty._defineProperty(this, "user", null);
defineProperty._defineProperty(this, "onAccountsChanged", async accounts => {
if (accounts.length === 0) {
await this.onDisconnect();
} else {
this.emit("change", {
account: ethers.utils.getAddress(accounts[0])
});
}
});
defineProperty._defineProperty(this, "onChainChanged", chainId => {
const id = normalizeChainId.normalizeChainId(chainId);
const unsupported = this.options.chains.findIndex(c => c.chainId === id) === -1;
this.emit("change", {
chain: {
id,
unsupported
}
});
});
defineProperty._defineProperty(this, "onDisconnect", async () => {
this.emit("disconnect");
});
this.options = options;
}
getEmbeddedWalletSDK() {
if (!this._embeddedWalletSdk) {
this._embeddedWalletSdk = new embeddedWallet.EmbeddedWalletSdk({
clientId: this.options.clientId,
chain: "Ethereum",
onAuthSuccess: this.options.onAuthSuccess
});
}
return this._embeddedWalletSdk;
}
async connect(args) {
// backwards compatibility - options should really be required here
if (!args) {
// default to iframe flow
const result = await this.authenticate({
strategy: "iframe"
});
if (!result.user) {
throw new Error("Error connecting User");
}
this.user = result.user;
} else {
if (!args.authResult) {
throw new Error("Missing authData - call authenticate() first with your authentication strategy");
}
if (!args.authResult.user) {
throw new Error("Missing authData.user - call authenticate() first with your authentication strategy");
}
this.user = args.authResult.user;
}
if (args?.chainId) {
this.switchChain(args.chainId);
}
return this.getAddress();
}
async disconnect() {
const paper = this._embeddedWalletSdk;
await paper?.auth.logout();
this._signer = undefined;
this._embeddedWalletSdk = undefined;
this.user = null;
}
async getAddress() {
if (!this.user) {
throw new Error("Embedded Wallet is not connected");
}
return await this.getSigner().then(signer => signer.getAddress());
}
async isConnected() {
try {
const addr = await this.getAddress();
return !!addr;
} catch (e) {
return false;
}
}
async getProvider() {
const signer = await this.getSigner();
if (!signer.provider) {
throw new Error("Provider not found");
}
return signer.provider;
}
async getSigner() {
if (this._signer) {
return this._signer;
}
const user = await this.getUser();
const signer = await user.wallet.getEthersJsSigner({
rpcEndpoint: this.options.chain.rpc[0] || "" // TODO: handle chain.rpc being empty array
});
if (!signer) {
throw new Error("Signer not found");
}
this._signer = signer;
return signer;
}
async isAuthorized() {
return false;
}
async switchChain(chainId) {
const chain = this.options.chains.find(c => c.chainId === chainId);
if (!chain) {
throw new Error("Chain not configured");
}
try {
// update chain in wallet
await this.user?.wallet.setChain({
chain: "Ethereum"
}); // just pass Ethereum no matter what chain we are going to connect
// update signer
this._signer = await this.user?.wallet.getEthersJsSigner({
rpcEndpoint: chain.rpc[0] || ""
});
this.emit("change", {
chain: {
id: chainId,
unsupported: false
}
});
} catch (e) {
console.warn("Failed to switch chain", e);
}
}
async setupListeners() {
return Promise.resolve();
}
updateChains(chains) {
this.options.chains = chains;
}
async getUser() {
if (!this.user || !this.user.wallet || !this.user.wallet.getEthersJsSigner // when serializing, functions are lost, need to rehydrate
) {
const embeddedWalletSdk = this.getEmbeddedWalletSDK();
const user = await embeddedWalletSdk.getUser();
switch (user.status) {
case embeddedWallet.UserWalletStatus.LOGGED_IN_WALLET_INITIALIZED:
{
this.user = user;
break;
}
default:
{
// if logged out or unitialized, we can't get a signer, so throw an error
throw new Error("Embedded Wallet is not authenticated, please authenticate first");
}
}
}
return this.user;
}
async getEmail() {
const user = await this.getUser();
return user.authDetails.email;
}
async getPhoneNumber() {
const user = await this.getUser();
return user.authDetails.phoneNumber;
}
async getRecoveryInformation() {
const user = await this.getUser();
return user.authDetails;
}
async sendVerificationEmail(_ref) {
let {
email
} = _ref;
const ewSDK = this.getEmbeddedWalletSDK();
return ewSDK.auth.sendEmailLoginOtp({
email
});
}
async sendVerificationSms(_ref2) {
let {
phoneNumber
} = _ref2;
const ewSDK = this.getEmbeddedWalletSDK();
return ewSDK.auth.sendSmsLoginOtp({
phoneNumber
});
}
async authenticate(params) {
const ewSDK = this.getEmbeddedWalletSDK();
const strategy = params.strategy;
switch (strategy) {
case "email_verification":
{
return await ewSDK.auth.verifyEmailLoginOtp({
email: params.email,
otp: params.verificationCode,
recoveryCode: params.recoveryCode
});
}
case "phone_number_verification":
{
return await ewSDK.auth.verifySmsLoginOtp({
phoneNumber: params.phoneNumber,
otp: params.verificationCode,
recoveryCode: params.recoveryCode
});
}
case "apple":
case "facebook":
case "google":
{
const oauthProvider = oauthStrategyToAuthProvider[strategy];
return ewSDK.auth.loginWithOauth({
oauthProvider,
closeOpenedWindow: params.closeOpenedWindow,
openedWindow: params.openedWindow
});
}
case "jwt":
{
return ewSDK.auth.loginWithCustomJwt({
jwt: params.jwt,
encryptionKey: params.encryptionKey
});
}
case "auth_endpoint":
{
return ewSDK.auth.loginWithCustomAuthEndpoint({
payload: params.payload,
encryptionKey: params.encryptionKey
});
}
case "iframe_email_verification":
{
return ewSDK.auth.loginWithEmailOtp({
email: params.email
});
}
case "iframe":
{
return ewSDK.auth.loginWithModal();
}
default:
assertUnreachable(strategy);
}
}
}
function assertUnreachable(x) {
throw new Error("Invalid param: " + x);
}
const oauthStrategyToAuthProvider = {
google: embeddedWallet.AuthProvider.GOOGLE,
facebook: embeddedWallet.AuthProvider.FACEBOOK,
apple: embeddedWallet.AuthProvider.APPLE
};
exports.EmbeddedWalletConnector = EmbeddedWalletConnector;