Skip to content

Commit

Permalink
feat: Set the provider's network.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfrg committed Dec 31, 2024
1 parent 5f8f429 commit b9939d3
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/core/constants/chains.constant.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const DEFAULT_RPC_URL = 'https://rpc.gno.land:443';
export const GNO_ADDRESS_PREFIX = 'g';
2 changes: 1 addition & 1 deletion packages/sdk/src/core/providers/tm2-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Wallet as TM2Wallet } from '@gnolang/tm2-js-client';
import { WalletProvider } from './wallet';

export interface TM2WalletProvider extends WalletProvider {
connect(rpcUrl?: string): Promise<boolean>;
connect(): Promise<boolean>;

disconnect(): Promise<boolean>;

Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/src/core/types/wallet.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export enum WalletResponseFailureType {
UNADDED_NETWORK = 'UNADDED_NETWORK',
UNSUPPORTED_TYPE = 'UNSUPPORTED_TYPE',
UNEXPECTED_ERROR = 'UNEXPECTED_ERROR',
NOT_INITIALIZED_NETWORK = 'NOT_INITIALIZED_NETWORK',
}

export enum WalletResponseRejectType {
Expand Down Expand Up @@ -203,6 +204,12 @@ const WalletFailureMessageInfo: Record<
type: WalletResponseFailureType.UNADDED_NETWORK,
message: 'The network has not been added on Adena.',
},
NOT_INITIALIZED_NETWORK: {
code: 4001,
status: WalletResponseStatus.FAILURE,
type: WalletResponseFailureType.NOT_INITIALIZED_NETWORK,
message: 'The network has not been initialized on Wallet.',
},
UNSUPPORTED_TYPE: {
code: 4005,
status: WalletResponseStatus.FAILURE,
Expand Down
34 changes: 24 additions & 10 deletions packages/sdk/src/providers/gno-wallet/gno-social-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@ import { CommonPrivateKeyProvider } from '@web3auth/base-provider';
import { Web3AuthNoModal } from '@web3auth/no-modal';
import { OpenloginAdapter } from '@web3auth/openlogin-adapter';

import { SocialCustomConfigure, SocialGoogleConfigure, SocialTwitterConfigure, SocialType } from '../../core';
import {
NetworkInfo,
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, socialType: SocialType) {
super();
constructor(web3auth: Web3AuthNoModal, socialType: SocialType, networks?: NetworkInfo[]) {
super(undefined, networks);
this.web3auth = web3auth;
this.socialType = socialType;
}

public async connect(): Promise<boolean> {
if (!this.currentNetwork) {
return false;
}

const initialized = await this.initWeb3Auth();
if (!initialized) {
return false;
Expand All @@ -32,13 +42,17 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
const privateKey = await this.requestPrivateKey();
const privateKeyBytes = hexToUint8Array(privateKey);

const wallet = await GnoWallet.fromPrivateKey(privateKeyBytes);
const wallet = await GnoWallet.fromPrivateKey(privateKeyBytes, {
addressPrefix: this.currentNetwork.addressPrefix,
});
this.wallet = wallet;

return this.connectProvider();
}

public disconnect = (): Promise<boolean> => {
this.disconnectProvider();

return this.web3auth
.logout({ cleanup: true })
.then(() => true)
Expand Down Expand Up @@ -77,7 +91,7 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
return `${privateKey}`;
}

public static createGoogle(config: SocialGoogleConfigure) {
public static createGoogle(config: SocialGoogleConfigure, networks?: NetworkInfo[]) {
const socialType = SocialType.GOOGLE;
const chainConfig: CustomChainConfig = {
displayName: 'Gno.land',
Expand Down Expand Up @@ -114,10 +128,10 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
},
});
web3auth.configureAdapter(openloginAdapter);
return new GnoSocialWalletProvider(web3auth, socialType);
return new GnoSocialWalletProvider(web3auth, socialType, networks);
}

public static createTwitter(config: SocialTwitterConfigure) {
public static createTwitter(config: SocialTwitterConfigure, networks?: NetworkInfo[]) {
const socialType = SocialType.TWITTER;
const chainConfig: CustomChainConfig = {
displayName: 'Gno.land',
Expand Down Expand Up @@ -158,10 +172,10 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
},
});
web3auth.configureAdapter(openloginAdapter);
return new GnoSocialWalletProvider(web3auth, socialType);
return new GnoSocialWalletProvider(web3auth, socialType, networks);
}

public static createEmail(config: SocialCustomConfigure) {
public static createEmail(config: SocialCustomConfigure, networks?: NetworkInfo[]) {
const socialType = SocialType.EMAIL;
const chainConfig: CustomChainConfig = {
displayName: 'Gno.land',
Expand Down Expand Up @@ -202,6 +216,6 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
},
});
web3auth.configureAdapter(openloginAdapter);
return new GnoSocialWalletProvider(web3auth, socialType);
return new GnoSocialWalletProvider(web3auth, socialType, networks);
}
}
117 changes: 100 additions & 17 deletions packages/sdk/src/providers/gno-wallet/gno-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,53 @@ import {
uint8ArrayToBase64,
} from '@gnolang/tm2-js-client';

import { BroadcastType, WalletResponseFailureType, WalletResponseSuccessType } from '../../core';
import { BroadcastType, NetworkInfo, WalletResponseFailureType, WalletResponseSuccessType } from '../../core';
import { TM2WalletProvider } from '../../core/providers/tm2-wallet';
import {
AddEstablishResponse,
AddNetworkOptions,
AddNetworkResponse,
BroadcastTransactionOptions,
BroadcastTransactionResponse,
GetAccountResponse,
GetNetworkResponse,
IsConnectedResponse,
OnChangeAccountOptions,
OnChangeAccountResponse,
OnChangeNetworkOptions,
OnChangeNetworkResponse,
SignTransactionOptions,
SignTransactionResponse,
SwitchNetworkOptions,
SwitchNetworkResponse,
} from '../../core/types/methods';
import { encodeTransaction } from '../../core/utils/encode.utils';
import { makeResponseMessage } from '../../core/utils/message.utils';
import { DEFAULT_RPC_URL } from '../../core/constants/chains.constant';
import { DEFAULT_RPC_URL, GNO_ADDRESS_PREFIX } from '../../core/constants/chains.constant';

export class GnoWalletProvider implements TM2WalletProvider {
protected wallet: TM2Wallet | null;
protected rpcUrl: string | null;
protected networks: NetworkInfo[];
protected currentNetwork: NetworkInfo | null;
protected networkCallback: ((chainId: string) => void) | null;

constructor(wallet?: TM2Wallet) {
constructor(wallet?: TM2Wallet, networks?: NetworkInfo[]) {
this.wallet = wallet || null;
this.rpcUrl = null;
this.networks = networks || [];
this.currentNetwork = null;
}

public getWallet(): TM2Wallet | null {
return this.wallet;
}

async connect(rpcUrl?: string): Promise<boolean> {
return this.connectProvider(rpcUrl);
async connect(): Promise<boolean> {
return this.connectProvider();
}

async disconnect(): Promise<boolean> {
return true;
return this.disconnectProvider();
}

async isConnected(): Promise<IsConnectedResponse> {
Expand Down Expand Up @@ -93,16 +101,66 @@ export class GnoWalletProvider implements TM2WalletProvider {
}
}

setNetworks(networks: NetworkInfo[]): void {
this.networks = networks;
}

async getNetwork(): Promise<GetNetworkResponse> {
throw new Error('not supported');
const connected = this.wallet !== null;
if (!connected) {
return makeResponseMessage(WalletResponseFailureType.NOT_CONNECTED);
}

if (!this.currentNetwork) {
return makeResponseMessage(WalletResponseFailureType.NOT_INITIALIZED_NETWORK);
}

return makeResponseMessage(WalletResponseSuccessType.GET_NETWORK_SUCCESS, this.currentNetwork);
}

async switchNetwork(): Promise<SwitchNetworkResponse> {
throw new Error('not supported');
async switchNetwork(options: SwitchNetworkOptions): Promise<SwitchNetworkResponse> {
const chainId = options.chainId;
if (!chainId) {
return makeResponseMessage(WalletResponseFailureType.INVALID_FORMAT);
}

const network = this.networks.find((network) => network.chainId === options.chainId);
if (!network) {
return makeResponseMessage(WalletResponseFailureType.UNADDED_NETWORK);
}

this.setNetwork(network);

const connected = this.connectProvider();
if (!connected) {
return makeResponseMessage(WalletResponseFailureType.NOT_CONNECTED);
}

return makeResponseMessage(WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS);
}

async addNetwork(): Promise<AddNetworkResponse> {
throw new Error('not supported');
async addNetwork(options: AddNetworkOptions): Promise<AddNetworkResponse> {
const { chainId, chainName, rpcUrl } = options;

if (!chainId || !chainName || !rpcUrl) {
return makeResponseMessage(WalletResponseFailureType.INVALID_FORMAT);
}

if (rpcUrl.match(/\s/g)) {
return makeResponseMessage(WalletResponseFailureType.INVALID_FORMAT);
}

const network: NetworkInfo = {
chainId,
networkName: chainName,
rpcUrl: rpcUrl.replace(/\/$/, ''),
addressPrefix: GNO_ADDRESS_PREFIX,
indexerUrl: null,
};

this.networks = [...this.networks, network];

return makeResponseMessage(WalletResponseSuccessType.ADD_NETWORK_SUCCESS);
}

async signTransaction(options: SignTransactionOptions): Promise<SignTransactionResponse> {
Expand Down Expand Up @@ -131,21 +189,46 @@ export class GnoWalletProvider implements TM2WalletProvider {
return makeResponseMessage(WalletResponseSuccessType.TRANSACTION_SUCCESS, transactionResult);
}

onChangeAccount(): OnChangeAccountResponse {
onChangeAccount(options: OnChangeAccountOptions): OnChangeAccountResponse {
throw new Error('not supported');
}

onChangeNetwork(): OnChangeNetworkResponse {
throw new Error('not supported');
onChangeNetwork(options: OnChangeNetworkOptions): OnChangeNetworkResponse {
this.networkCallback = options.callback;
}

protected triggerNetworkCallback(chainId: string): void {
if (!this.networkCallback) {
return;
}

this.networkCallback(chainId);
}

protected connectProvider(rpcUrl?: string): boolean {
protected connectProvider(): boolean {
if (!this.wallet) {
return false;
}

const provider = new JSONRPCProvider(rpcUrl || DEFAULT_RPC_URL);
const rpcUrl = this.currentNetwork?.rpcUrl || DEFAULT_RPC_URL;
const provider = new JSONRPCProvider(rpcUrl);
this.wallet.connect(provider);
return true;
}

protected disconnectProvider(): boolean {
this.networkCallback = null;
this.networks = [];
this.currentNetwork = null;
this.wallet = null;

return true;
}

private setNetwork(network: NetworkInfo): void {
this.currentNetwork = network;

// Trigger network change callback
this.triggerNetworkCallback(this.currentNetwork.chainId);
}
}

0 comments on commit b9939d3

Please sign in to comment.