Skip to content

Commit bcd22ab

Browse files
committed
feat: add method (getSocialUserProfile)
1 parent 7f6fc1e commit bcd22ab

File tree

9 files changed

+179
-2
lines changed

9 files changed

+179
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { GnoSocialWalletProvider } from '../../providers';
2+
import { WalletResponseSuccessType } from '../types';
3+
import { GetSocialUserProfileResponse } from '../types/methods/get-social-user-profile.types';
4+
import { makeResponseMessage } from '../utils';
5+
6+
export const defineMockSocialWalletProvider = () => {
7+
jest.mock('../../providers', () => ({
8+
GnoSOcialWalletProvider: jest.fn(() => mockSocialWalletProvider),
9+
}));
10+
};
11+
12+
// Mock social user profile response
13+
export const getSocialUserProfileSuccessMock: GetSocialUserProfileResponse = {
14+
15+
name: 'Test User',
16+
profileImage: 'https://example.com/profile.jpg',
17+
verifier: 'test-verifier',
18+
verifierId: 'test-verifier-id',
19+
typeOfLogin: 'google',
20+
aggregateVerifier: 'test-aggregate-verifier',
21+
idToken: 'test-id-token',
22+
oAuthIdToken: 'test-oauth-id-token',
23+
oAuthAccessToken: 'test-oauth-access-token',
24+
};
25+
26+
// Create mock instance
27+
const mockSocialWalletProvider = Object.create(GnoSocialWalletProvider.prototype);
28+
29+
// Add mock implementations
30+
Object.assign(mockSocialWalletProvider, {
31+
isConnected: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.CONNECTION_SUCCESS)),
32+
addEstablish: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.CONNECTION_SUCCESS)),
33+
getAccount: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.GET_ACCOUNT_SUCCESS)),
34+
getNetwork: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.GET_NETWORK_SUCCESS)),
35+
switchNetwork: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS)),
36+
addNetwork: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.ADD_NETWORK_SUCCESS)),
37+
signTransaction: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.SIGN_SUCCESS)),
38+
broadcastTransaction: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.TRANSACTION_SUCCESS)),
39+
onChangeAccount: jest.fn().mockImplementation(() => {
40+
return;
41+
}),
42+
onChangeNetwork: jest.fn().mockImplementation(() => {
43+
return;
44+
}),
45+
connect: jest.fn().mockResolvedValue(true),
46+
disconnect: jest.fn().mockResolvedValue(true),
47+
getWallet: jest.fn().mockReturnValue(null),
48+
setNetworks: jest.fn(),
49+
getSocialUserProfile: jest.fn().mockResolvedValue(getSocialUserProfileSuccessMock),
50+
});
51+
52+
export { mockSocialWalletProvider };
53+
54+
export const getSocialUserProfileFailureMock = new Error('Failed to get social user profile');
55+
export const getSocialUserProfileNotInitializedMock = new Error('Not initialized web3 provider.');
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { getSocialUserProfileSuccessMock, mockSocialWalletProvider } from '../../__mocks__/mock-social-wallet-provider';
2+
import { mockWalletProvider } from '../../__mocks__/mock-wallet-provider';
3+
import { getSocialUserProfile } from '../../methods/get-social-user-profile';
4+
import { GetSocialUserProfileResponse } from '../../types/methods/get-social-user-profile.types';
5+
6+
describe('getSocialUserProfile', () => {
7+
afterEach(() => {
8+
jest.clearAllMocks();
9+
});
10+
11+
it('should throw error if wallet provider is not GnoSocialWalletProvider', async () => {
12+
await expect(getSocialUserProfile(mockWalletProvider)).rejects.toThrow(
13+
'Wallet provider is not a GnoSocialWalletProvider'
14+
);
15+
});
16+
17+
it('should call getSocialUserProfile and return the response', async () => {
18+
const mockResponse: GetSocialUserProfileResponse = getSocialUserProfileSuccessMock;
19+
20+
mockSocialWalletProvider.getSocialUserProfile.mockResolvedValue(mockResponse);
21+
22+
const response = await getSocialUserProfile(mockSocialWalletProvider);
23+
24+
expect(mockSocialWalletProvider.getSocialUserProfile).toHaveBeenCalled();
25+
expect(response).toEqual(mockResponse);
26+
});
27+
28+
it('should throw an error if getSocialUserProfile fails', async () => {
29+
mockSocialWalletProvider.getSocialUserProfile.mockRejectedValue(new Error('Failed to get social user profile'));
30+
31+
await expect(getSocialUserProfile(mockSocialWalletProvider)).rejects.toThrow('Failed to get social user profile');
32+
});
33+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { WalletProvider } from '../providers';
2+
import { GetSocialUserProfileResponse } from '../types/methods/get-social-user-profile.types';
3+
import { isGnoSocialWalletProvider } from '../utils/provider.utils';
4+
5+
export const getSocialUserProfile = async (walletProvider: WalletProvider): Promise<GetSocialUserProfileResponse> => {
6+
if (!isGnoSocialWalletProvider(walletProvider)) {
7+
throw new Error('Wallet provider is not a GnoSocialWalletProvider');
8+
}
9+
10+
return await walletProvider.getSocialUserProfile();
11+
};

packages/sdk/src/core/providers/tm2-wallet.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Wallet as TM2Wallet } from '@gnolang/tm2-js-client';
22
import { WalletProvider } from './wallet';
33
import { GetNetworkResponse } from '../types/methods';
4+
import { GetSocialUserProfileResponse } from '../types/methods/get-social-user-profile.types';
45

56
export interface TM2WalletProvider extends WalletProvider {
67
connect(): Promise<boolean>;
@@ -10,4 +11,6 @@ export interface TM2WalletProvider extends WalletProvider {
1011
getWallet(): TM2Wallet | null;
1112

1213
getNetwork(): Promise<GetNetworkResponse>;
14+
15+
getSocialUserProfile(): Promise<GetSocialUserProfileResponse>;
1316
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
SwitchNetworkOptions,
4242
SwitchNetworkResponse,
4343
} from '../types/methods';
44+
import { getSocialUserProfile } from '../methods/get-social-user-profile';
4445

4546
const DEFAULT_ADENA_URL = 'https://www.adena.app';
4647

@@ -193,6 +194,14 @@ export class AdenaSDK {
193194
return onChangeNetwork(this.walletProvider, options);
194195
}
195196

197+
/**
198+
* For social accounts, get the profile information for the social account.
199+
* @returns A promise that resolves to the profile information for the social account.
200+
*/
201+
getSocialUserProfile() {
202+
return getSocialUserProfile(this.walletProvider);
203+
}
204+
196205
public static createAdenaWallet(config?: SDKConfigure): AdenaSDK {
197206
return new AdenaSDK(new AdenaWalletProvider(), config);
198207
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export declare const LOGIN_PROVIDER: {
2+
readonly GOOGLE: 'google';
3+
readonly FACEBOOK: 'facebook';
4+
readonly REDDIT: 'reddit';
5+
readonly DISCORD: 'discord';
6+
readonly TWITCH: 'twitch';
7+
readonly APPLE: 'apple';
8+
readonly LINE: 'line';
9+
readonly GITHUB: 'github';
10+
readonly KAKAO: 'kakao';
11+
readonly LINKEDIN: 'linkedin';
12+
readonly TWITTER: 'twitter';
13+
readonly WEIBO: 'weibo';
14+
readonly WECHAT: 'wechat';
15+
readonly FARCASTER: 'farcaster';
16+
readonly EMAIL_PASSWORDLESS: 'email_passwordless';
17+
readonly SMS_PASSWORDLESS: 'sms_passwordless';
18+
readonly WEBAUTHN: 'webauthn';
19+
readonly JWT: 'jwt';
20+
};
21+
22+
export type LOGIN_PROVIDER_TYPE = (typeof LOGIN_PROVIDER)[keyof typeof LOGIN_PROVIDER];
23+
24+
export type CUSTOM_LOGIN_PROVIDER_TYPE = string & {
25+
toString?: (radix?: number) => string;
26+
};
27+
28+
export interface SocialUserInfo {
29+
email?: string;
30+
name?: string;
31+
profileImage?: string;
32+
aggregateVerifier?: string;
33+
verifier?: string;
34+
verifierId?: string;
35+
typeOfLogin?: LOGIN_PROVIDER_TYPE | CUSTOM_LOGIN_PROVIDER_TYPE;
36+
dappShare?: string;
37+
/**
38+
* Token issued by Web3Auth.
39+
*/
40+
idToken?: string;
41+
/**
42+
* Token issued by OAuth provider. Will be available only if you are using
43+
* custom verifiers.
44+
*/
45+
oAuthIdToken?: string;
46+
/**
47+
* Access Token issued by OAuth provider. Will be available only if you are using
48+
* custom verifiers.
49+
*/
50+
oAuthAccessToken?: string;
51+
appState?: string;
52+
touchIDPreference?: string;
53+
isMfaEnabled?: boolean;
54+
}
55+
56+
export type GetSocialUserProfileResponse = SocialUserInfo;

packages/sdk/src/core/utils/provider.utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ import { TM2WalletProvider, WalletProvider } from '../providers';
44
export function isTM2WalletProvider(wallet: WalletProvider): wallet is TM2WalletProvider {
55
return wallet instanceof GnoWalletProvider || wallet instanceof GnoSocialWalletProvider;
66
}
7+
8+
export function isGnoSocialWalletProvider(wallet: WalletProvider): wallet is GnoSocialWalletProvider {
9+
return wallet instanceof GnoSocialWalletProvider;
10+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GnoWallet } from '@gnolang/gno-js-client';
2-
import { CustomChainConfig, UserInfo, WALLET_ADAPTERS } from '@web3auth/base';
2+
import { CustomChainConfig, WALLET_ADAPTERS } from '@web3auth/base';
33
import { CommonPrivateKeyProvider } from '@web3auth/base-provider';
44
import { Web3AuthNoModal } from '@web3auth/no-modal';
55
import { OpenloginAdapter } from '@web3auth/openlogin-adapter';
@@ -14,6 +14,7 @@ import {
1414
import { GNO_ADDRESS_PREFIX } from '../../core/constants/chains.constant';
1515
import { hexToUint8Array } from '../../core/utils/encode.utils';
1616
import { GnoWalletProvider } from './gno-wallet';
17+
import { GetSocialUserProfileResponse } from '../../core/types/methods/get-social-user-profile.types';
1718

1819
export class GnoSocialWalletProvider extends GnoWalletProvider {
1920
private web3auth: Web3AuthNoModal;
@@ -247,7 +248,7 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
247248
return new GnoSocialWalletProvider(web3auth, socialType, [networkConfig]);
248249
}
249250

250-
public async getSocialUserProfile(): Promise<Partial<UserInfo>> {
251+
public async getSocialUserProfile(): Promise<GetSocialUserProfileResponse> {
251252
if (!this.web3auth) {
252253
throw new Error('Not initialized web3 provider.');
253254
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { encodeTransaction } from '../../core/utils/encode.utils';
3030
import { makeResponseMessage } from '../../core/utils/message.utils';
3131
import { DEFAULT_RPC_URL, GNO_ADDRESS_PREFIX } from '../../core/constants/chains.constant';
3232
import { normalizeRpcUrl, validateNetworkInput } from '../../core/utils/network.utils';
33+
import { GetSocialUserProfileResponse } from '../../core/types/methods/get-social-user-profile.types';
3334

3435
export class GnoWalletProvider implements TM2WalletProvider {
3536
protected wallet: TM2Wallet | null;
@@ -252,4 +253,8 @@ export class GnoWalletProvider implements TM2WalletProvider {
252253
(network) => network.chainId === chainId || normalizeRpcUrl(network.rpcUrl) === normalizedRpcUrl
253254
);
254255
}
256+
257+
async getSocialUserProfile(): Promise<GetSocialUserProfileResponse> {
258+
throw new Error('Social user profile is not supported in GnoWalletProvider. Use GnoSocialWalletProvider instead.');
259+
}
255260
}

0 commit comments

Comments
 (0)