Skip to content

Commit 2da5fcc

Browse files
authored
feat: add a Gno social wallet creation function (#13)
1 parent 385da57 commit 2da5fcc

File tree

3 files changed

+131
-23
lines changed

3 files changed

+131
-23
lines changed

packages/sdk/src/core/sdk/adena-sdk.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Wallet } from '@gnolang/tm2-js-client';
2-
import { AdenaWalletProvider, GnoSocialWalletProvider, GnoWalletProvider } from '../../providers';
2+
import { AdenaWalletProvider, GnoWalletProvider } from '../../providers';
33
import { ConnectionManager, ConnectionState } from '../connection';
44
import {
55
addEstablish,
@@ -18,7 +18,7 @@ import {
1818
switchNetwork,
1919
} from '../methods';
2020
import { WalletProvider } from '../providers';
21-
import { SDKConfigure, SocialConfigure } from '../types';
21+
import { SDKConfigure } from '../types';
2222
import {
2323
AddEstablishOptions,
2424
AddEstablishResponse,
@@ -200,8 +200,4 @@ export class AdenaSDK {
200200
public static createGnoWallet(wallet: Wallet, config?: SDKConfigure): AdenaSDK {
201201
return new AdenaSDK(new GnoWalletProvider(wallet), config);
202202
}
203-
204-
public static createGnoSocialWallet(config: SocialConfigure & SDKConfigure): AdenaSDK {
205-
return new AdenaSDK(GnoSocialWalletProvider.create(config), config);
206-
}
207203
}

packages/sdk/src/core/types/config.types.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,33 @@ export interface SDKConnectionConfigure {
66
isSession?: boolean;
77
}
88

9-
export interface SocialConfigure {
9+
interface SocialBaseConfigure {
1010
chainId: string;
11+
name: string;
1112
rpcTarget: string;
1213
network: 'mainnet' | 'testnet';
1314
clientId: string;
14-
auth: {
15-
googleName: string;
16-
googleVerifier: string;
17-
googleClientId: string;
18-
};
15+
}
16+
17+
export interface SocialGoogleConfigure extends SocialBaseConfigure {
18+
authClientId: string;
19+
verifier: string;
20+
}
21+
22+
export interface SocialTwitterConfigure extends SocialBaseConfigure {
23+
authClientId: string;
24+
verifier: string;
25+
domain: string;
26+
}
27+
28+
export interface SocialCustomConfigure extends SocialBaseConfigure {
29+
authClientId: string;
30+
verifier: string;
31+
domain: string;
32+
}
33+
34+
export enum SocialType {
35+
GOOGLE = 'GOOGLE',
36+
TWITTER = 'TWITTER',
37+
EMAIL = 'EMAIL',
1938
}

packages/sdk/src/providers/gno-wallet/gno-social-wallet.ts

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import { CommonPrivateKeyProvider } from '@web3auth/base-provider';
44
import { Web3AuthNoModal } from '@web3auth/no-modal';
55
import { OpenloginAdapter } from '@web3auth/openlogin-adapter';
66

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

1111
export class GnoSocialWalletProvider extends GnoWalletProvider {
1212
private web3auth: Web3AuthNoModal;
13+
private socialType: SocialType;
1314

14-
constructor(web3auth: Web3AuthNoModal) {
15+
constructor(web3auth: Web3AuthNoModal, socialType: SocialType) {
1516
super();
1617
this.web3auth = web3auth;
18+
this.socialType = socialType;
1719
}
1820

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

5355
private async connectWeb3Auth(): Promise<boolean> {
5456
return this.web3auth
55-
.connectTo(WALLET_ADAPTERS.OPENLOGIN, { loginProvider: 'google' })
57+
.connectTo(WALLET_ADAPTERS.OPENLOGIN, {
58+
loginProvider: this.socialType,
59+
})
5660
.then(() => true)
5761
.catch((error) => {
5862
if (error?.name === 'WalletLoginError') {
@@ -73,14 +77,99 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
7377
return `${privateKey}`;
7478
}
7579

76-
public static create(config: SocialConfigure) {
80+
public static createGoogle(config: SocialGoogleConfigure) {
81+
const socialType = SocialType.GOOGLE;
7782
const chainConfig: CustomChainConfig = {
83+
displayName: 'Gno.land',
84+
tickerName: 'Gno.land',
85+
ticker: 'ugnot',
7886
chainNamespace: 'other',
7987
chainId: config.chainId,
8088
rpcTarget: config.rpcTarget,
89+
};
90+
91+
const web3auth = new Web3AuthNoModal({
92+
clientId: config.clientId,
93+
web3AuthNetwork: config.network,
94+
chainConfig,
95+
});
96+
97+
const privateKeyProvider = new CommonPrivateKeyProvider({
98+
config: { chainConfig },
99+
});
100+
101+
const openloginAdapter = new OpenloginAdapter({
102+
privateKeyProvider: privateKeyProvider,
103+
adapterSettings: {
104+
clientId: config.clientId,
105+
uxMode: 'popup',
106+
loginConfig: {
107+
[socialType]: {
108+
typeOfLogin: 'google',
109+
name: config.name,
110+
clientId: config.authClientId,
111+
verifier: config.verifier,
112+
},
113+
},
114+
},
115+
});
116+
web3auth.configureAdapter(openloginAdapter);
117+
return new GnoSocialWalletProvider(web3auth, socialType);
118+
}
119+
120+
public static createTwitter(config: SocialTwitterConfigure) {
121+
const socialType = SocialType.TWITTER;
122+
const chainConfig: CustomChainConfig = {
81123
displayName: 'Gno.land',
124+
tickerName: 'Gno.land',
82125
ticker: 'ugnot',
126+
chainNamespace: 'other',
127+
chainId: config.chainId,
128+
rpcTarget: config.rpcTarget,
129+
};
130+
131+
const web3auth = new Web3AuthNoModal({
132+
clientId: config.clientId,
133+
web3AuthNetwork: config.network,
134+
chainConfig,
135+
});
136+
137+
const privateKeyProvider = new CommonPrivateKeyProvider({
138+
config: { chainConfig },
139+
});
140+
141+
const openloginAdapter = new OpenloginAdapter({
142+
privateKeyProvider: privateKeyProvider,
143+
adapterSettings: {
144+
uxMode: 'popup',
145+
loginConfig: {
146+
[socialType]: {
147+
typeOfLogin: 'twitter',
148+
name: config.name,
149+
verifier: config.verifier,
150+
clientId: config.authClientId,
151+
jwtParameters: {
152+
connection: 'twitter',
153+
verifierIdField: 'sub',
154+
domain: config.domain,
155+
},
156+
},
157+
},
158+
},
159+
});
160+
web3auth.configureAdapter(openloginAdapter);
161+
return new GnoSocialWalletProvider(web3auth, socialType);
162+
}
163+
164+
public static createEmail(config: SocialCustomConfigure) {
165+
const socialType = SocialType.EMAIL;
166+
const chainConfig: CustomChainConfig = {
167+
displayName: 'Gno.land',
83168
tickerName: 'Gno.land',
169+
ticker: 'ugnot',
170+
chainNamespace: 'other',
171+
chainId: config.chainId,
172+
rpcTarget: config.rpcTarget,
84173
};
85174

86175
const web3auth = new Web3AuthNoModal({
@@ -96,19 +185,23 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
96185
const openloginAdapter = new OpenloginAdapter({
97186
privateKeyProvider: privateKeyProvider,
98187
adapterSettings: {
99-
clientId: config.clientId,
100188
uxMode: 'popup',
101189
loginConfig: {
102-
google: {
103-
typeOfLogin: 'google',
104-
name: config.auth.googleName,
105-
verifier: config.auth.googleVerifier,
106-
clientId: config.auth.googleClientId,
190+
[socialType]: {
191+
typeOfLogin: 'jwt',
192+
name: config.name,
193+
verifier: config.verifier,
194+
clientId: config.authClientId,
195+
jwtParameters: {
196+
verifierIdField: 'email',
197+
domain: config.domain,
198+
login_hint: '',
199+
},
107200
},
108201
},
109202
},
110203
});
111204
web3auth.configureAdapter(openloginAdapter);
112-
return new GnoSocialWalletProvider(web3auth);
205+
return new GnoSocialWalletProvider(web3auth, socialType);
113206
}
114207
}

0 commit comments

Comments
 (0)