Skip to content

Commit

Permalink
feat: add a Gno social wallet creation function (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss authored Sep 10, 2024
1 parent 385da57 commit 2da5fcc
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 23 deletions.
8 changes: 2 additions & 6 deletions packages/sdk/src/core/sdk/adena-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Wallet } from '@gnolang/tm2-js-client';
import { AdenaWalletProvider, GnoSocialWalletProvider, GnoWalletProvider } from '../../providers';
import { AdenaWalletProvider, GnoWalletProvider } from '../../providers';
import { ConnectionManager, ConnectionState } from '../connection';
import {
addEstablish,
Expand All @@ -18,7 +18,7 @@ import {
switchNetwork,
} from '../methods';
import { WalletProvider } from '../providers';
import { SDKConfigure, SocialConfigure } from '../types';
import { SDKConfigure } from '../types';
import {
AddEstablishOptions,
AddEstablishResponse,
Expand Down Expand Up @@ -200,8 +200,4 @@ export class AdenaSDK {
public static createGnoWallet(wallet: Wallet, config?: SDKConfigure): AdenaSDK {
return new AdenaSDK(new GnoWalletProvider(wallet), config);
}

public static createGnoSocialWallet(config: SocialConfigure & SDKConfigure): AdenaSDK {
return new AdenaSDK(GnoSocialWalletProvider.create(config), config);
}
}
31 changes: 25 additions & 6 deletions packages/sdk/src/core/types/config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@ export interface SDKConnectionConfigure {
isSession?: boolean;
}

export interface SocialConfigure {
interface SocialBaseConfigure {
chainId: string;
name: string;
rpcTarget: string;
network: 'mainnet' | 'testnet';
clientId: string;
auth: {
googleName: string;
googleVerifier: string;
googleClientId: string;
};
}

export interface SocialGoogleConfigure extends SocialBaseConfigure {
authClientId: string;
verifier: string;
}

export interface SocialTwitterConfigure extends SocialBaseConfigure {
authClientId: string;
verifier: string;
domain: string;
}

export interface SocialCustomConfigure extends SocialBaseConfigure {
authClientId: string;
verifier: string;
domain: string;
}

export enum SocialType {
GOOGLE = 'GOOGLE',
TWITTER = 'TWITTER',
EMAIL = 'EMAIL',
}
115 changes: 104 additions & 11 deletions packages/sdk/src/providers/gno-wallet/gno-social-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { CommonPrivateKeyProvider } from '@web3auth/base-provider';
import { Web3AuthNoModal } from '@web3auth/no-modal';
import { OpenloginAdapter } from '@web3auth/openlogin-adapter';

import { SocialConfigure } from '../../core';
import { SocialCustomConfigure, SocialGoogleConfigure, SocialTwitterConfigure, SocialType } from '../../core';
import { hexToUint8Array } from '../../core/utils/encode.utils';
import { GnoWalletProvider } from './gno-wallet';

export class GnoSocialWalletProvider extends GnoWalletProvider {
private web3auth: Web3AuthNoModal;
private socialType: SocialType;

constructor(web3auth: Web3AuthNoModal) {
constructor(web3auth: Web3AuthNoModal, socialType: SocialType) {
super();
this.web3auth = web3auth;
this.socialType = socialType;
}

public async connect(): Promise<boolean> {
Expand Down Expand Up @@ -52,7 +54,9 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {

private async connectWeb3Auth(): Promise<boolean> {
return this.web3auth
.connectTo(WALLET_ADAPTERS.OPENLOGIN, { loginProvider: 'google' })
.connectTo(WALLET_ADAPTERS.OPENLOGIN, {
loginProvider: this.socialType,
})
.then(() => true)
.catch((error) => {
if (error?.name === 'WalletLoginError') {
Expand All @@ -73,14 +77,99 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
return `${privateKey}`;
}

public static create(config: SocialConfigure) {
public static createGoogle(config: SocialGoogleConfigure) {
const socialType = SocialType.GOOGLE;
const chainConfig: CustomChainConfig = {
displayName: 'Gno.land',
tickerName: 'Gno.land',
ticker: 'ugnot',
chainNamespace: 'other',
chainId: config.chainId,
rpcTarget: config.rpcTarget,
};

const web3auth = new Web3AuthNoModal({
clientId: config.clientId,
web3AuthNetwork: config.network,
chainConfig,
});

const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig },
});

const openloginAdapter = new OpenloginAdapter({
privateKeyProvider: privateKeyProvider,
adapterSettings: {
clientId: config.clientId,
uxMode: 'popup',
loginConfig: {
[socialType]: {
typeOfLogin: 'google',
name: config.name,
clientId: config.authClientId,
verifier: config.verifier,
},
},
},
});
web3auth.configureAdapter(openloginAdapter);
return new GnoSocialWalletProvider(web3auth, socialType);
}

public static createTwitter(config: SocialTwitterConfigure) {
const socialType = SocialType.TWITTER;
const chainConfig: CustomChainConfig = {
displayName: 'Gno.land',
tickerName: 'Gno.land',
ticker: 'ugnot',
chainNamespace: 'other',
chainId: config.chainId,
rpcTarget: config.rpcTarget,
};

const web3auth = new Web3AuthNoModal({
clientId: config.clientId,
web3AuthNetwork: config.network,
chainConfig,
});

const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig },
});

const openloginAdapter = new OpenloginAdapter({
privateKeyProvider: privateKeyProvider,
adapterSettings: {
uxMode: 'popup',
loginConfig: {
[socialType]: {
typeOfLogin: 'twitter',
name: config.name,
verifier: config.verifier,
clientId: config.authClientId,
jwtParameters: {
connection: 'twitter',
verifierIdField: 'sub',
domain: config.domain,
},
},
},
},
});
web3auth.configureAdapter(openloginAdapter);
return new GnoSocialWalletProvider(web3auth, socialType);
}

public static createEmail(config: SocialCustomConfigure) {
const socialType = SocialType.EMAIL;
const chainConfig: CustomChainConfig = {
displayName: 'Gno.land',
tickerName: 'Gno.land',
ticker: 'ugnot',
chainNamespace: 'other',
chainId: config.chainId,
rpcTarget: config.rpcTarget,
};

const web3auth = new Web3AuthNoModal({
Expand All @@ -96,19 +185,23 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
const openloginAdapter = new OpenloginAdapter({
privateKeyProvider: privateKeyProvider,
adapterSettings: {
clientId: config.clientId,
uxMode: 'popup',
loginConfig: {
google: {
typeOfLogin: 'google',
name: config.auth.googleName,
verifier: config.auth.googleVerifier,
clientId: config.auth.googleClientId,
[socialType]: {
typeOfLogin: 'jwt',
name: config.name,
verifier: config.verifier,
clientId: config.authClientId,
jwtParameters: {
verifierIdField: 'email',
domain: config.domain,
login_hint: '',
},
},
},
},
});
web3auth.configureAdapter(openloginAdapter);
return new GnoSocialWalletProvider(web3auth);
return new GnoSocialWalletProvider(web3auth, socialType);
}
}

0 comments on commit 2da5fcc

Please sign in to comment.