@@ -1115,7 +1115,9 @@ export class NetworkController extends BaseController<
11151115 * @returns The ID for the added or updated network configuration.
11161116 */
11171117 async upsertNetworkConfiguration (
1118- networkConfiguration : NetworkConfiguration ,
1118+ networkConfiguration : NetworkConfiguration & {
1119+ id ?: NetworkConfigurationId ;
1120+ } ,
11191121 {
11201122 referrer,
11211123 source,
@@ -1126,11 +1128,17 @@ export class NetworkController extends BaseController<
11261128 setActive ?: boolean ;
11271129 } ,
11281130 ) : Promise < string > {
1129- const sanitizedNetworkConfiguration : NetworkConfiguration = pick (
1130- networkConfiguration ,
1131- [ 'rpcUrl' , 'chainId' , 'ticker' , 'nickname' , 'rpcPrefs' ] ,
1132- ) ;
1133- const { rpcUrl, chainId, ticker } = sanitizedNetworkConfiguration ;
1131+ const sanitizedNetworkConfiguration : NetworkConfiguration & {
1132+ id ?: NetworkConfigurationId ;
1133+ } = pick ( networkConfiguration , [
1134+ 'rpcUrl' ,
1135+ 'chainId' ,
1136+ 'ticker' ,
1137+ 'nickname' ,
1138+ 'rpcPrefs' ,
1139+ 'id' ,
1140+ ] ) ;
1141+ const { rpcUrl, chainId, ticker, id } = sanitizedNetworkConfiguration ;
11341142
11351143 assertIsStrictHexString ( chainId ) ;
11361144 if ( ! isSafeChainId ( chainId ) ) {
@@ -1166,12 +1174,33 @@ export class NetworkController extends BaseController<
11661174 const autoManagedNetworkClientRegistry =
11671175 this . #ensureAutoManagedNetworkClientRegistryPopulated( ) ;
11681176
1169- const existingNetworkConfiguration = Object . values (
1177+ const existingNetworkConfigurationWithId = Object . values (
1178+ this . state . networkConfigurations ,
1179+ ) . find ( ( networkConfig ) => networkConfig . id === id ) ;
1180+ if ( id && ! existingNetworkConfigurationWithId ) {
1181+ throw new Error ( 'No network configuration matches the provided id' ) ;
1182+ }
1183+
1184+ const existingNetworkConfigurationWithRpcUrl = Object . values (
11701185 this . state . networkConfigurations ,
11711186 ) . find (
11721187 ( networkConfig ) =>
11731188 networkConfig . rpcUrl . toLowerCase ( ) === rpcUrl . toLowerCase ( ) ,
11741189 ) ;
1190+ if (
1191+ id &&
1192+ existingNetworkConfigurationWithRpcUrl &&
1193+ existingNetworkConfigurationWithRpcUrl . id !== id
1194+ ) {
1195+ throw new Error (
1196+ 'A different network configuration already exists with the provided rpcUrl' ,
1197+ ) ;
1198+ }
1199+
1200+ const existingNetworkConfiguration =
1201+ existingNetworkConfigurationWithId ??
1202+ existingNetworkConfigurationWithRpcUrl ;
1203+
11751204 const upsertedNetworkConfigurationId = existingNetworkConfiguration
11761205 ? existingNetworkConfiguration . id
11771206 : random ( ) ;
@@ -1202,8 +1231,8 @@ export class NetworkController extends BaseController<
12021231
12031232 this . update ( ( state ) => {
12041233 state . networkConfigurations [ upsertedNetworkConfigurationId ] = {
1205- id : upsertedNetworkConfigurationId ,
12061234 ...sanitizedNetworkConfiguration ,
1235+ id : upsertedNetworkConfigurationId ,
12071236 } ;
12081237 } ) ;
12091238
0 commit comments