diff --git a/src/api.ts b/src/api.ts index 3d4db725..e32b461d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -17,6 +17,8 @@ import { FunctionCall, TokensObject, DelayedMessage, + Network, + AddNetwork, } from './models'; import { UniqueArray, Address } from './utils'; @@ -1632,6 +1634,45 @@ export type ProviderApi = { message: DelayedMessage; }; }; + + /** + * Request user to add a new network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + addNetwork: { + input: { + /** + * Network info + */ + network: AddNetwork; + /** + * Whether to switch to the added network (false by default) + */ + switchNetwork?: boolean; + }; + output: { + network: Network | null + }; + }; + + /** + * Request user to change selected network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + changeNetwork: { + input: { + networkId: number; + }; + output: { + network: Network | null; + }; + }; }; /** diff --git a/src/index.ts b/src/index.ts index 491982d3..8661a4ba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,7 @@ import { parsePartialTokensObject, parseTransaction, serializeTokensObject, + Network, } from './models'; import { Address, DelayedTransactions, getUniqueId } from './utils'; import * as subscriber from './stream'; @@ -1040,6 +1041,32 @@ export class ProviderRpcClient { }; } + /** + * Request user to add a new network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + public async addNetwork(args: ProviderApiRequestParams<'addNetwork'>): Promise> { + await this.ensureInitialized(); + return await this._api.addNetwork(args); + } + + /** + * Request user to change selected network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + public async changeNetwork( + args: ProviderApiRequestParams<'changeNetwork'>, + ): Promise> { + await this.ensureInitialized(); + return await this._api.changeNetwork(args); + } + private _registerEventHandlers(provider: Provider) { const knownEvents: { [K in ProviderEvent]: (data: RawProviderEventData) => ProviderEventData } = { connected: data => data, diff --git a/src/models.ts b/src/models.ts index 0fa3cb50..a32df03d 100644 --- a/src/models.ts +++ b/src/models.ts @@ -312,6 +312,87 @@ export type EncryptedData = { nonce: string; }; +/* Network stuff */ + +/** + * @category Models + */ +export type NetworkDescription = { + globalId: number; + capabilities: string; + signatureId: number | undefined; +}; + +/** + * @category Models + */ +export type NetworkConfig = { + symbol?: string; + explorerBaseUrl?: string; + tokensManifestUrl?: string; +}; + +/** + * @category Models + */ +export type GqlSocketParams = { + /** + * Path to graphql api endpoints + */ + endpoints: string[] + /** + * Frequency of sync latency detection + */ + latencyDetectionInterval: number + /** + * Maximum value for the endpoint's blockchain data sync latency + */ + maxLatency: number + /** + * Gql node type + */ + local: boolean +}; + +/** + * @category Models + */ +export type JrpcSocketParams = { + /** + * Path to jrpc api endpoint + */ + endpoint: string +}; + +/** + * @category Models + */ +export type ProtoSocketParams = JrpcSocketParams & {}; + +export type GqlConnection = { type: 'graphql', data: GqlSocketParams }; +export type JrpcConnection = { type: 'jrpc', data: JrpcSocketParams }; +export type ProtoConnection = { type: 'proto', data: ProtoSocketParams }; + +/** + * @category Models + */ +export type Network = { + name: string; + description: NetworkDescription; + connection: GqlConnection | JrpcConnection | ProtoConnection | any; + config: NetworkConfig; +}; + +/** + * @category Models + */ +export type AddNetwork = { + name: string; + networkId: number; + connection: GqlConnection | JrpcConnection | ProtoConnection | any; + config?: NetworkConfig; +}; + /* ABI stuff */ /**