diff --git a/packages/neuron-ui/src/components/NetworkEditor/index.tsx b/packages/neuron-ui/src/components/NetworkEditor/index.tsx index 62d22e76e3..4105007a81 100644 --- a/packages/neuron-ui/src/components/NetworkEditor/index.tsx +++ b/packages/neuron-ui/src/components/NetworkEditor/index.tsx @@ -105,7 +105,7 @@ const NetworkEditor = () => { onChange={onChange} label={t('settings.network.edit-network.rpc-url')} error={editor.urlError} - placeholder="http://localhost:8114" + placeholder="http://127.0.0.1:8114" required autoFocus /> diff --git a/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx b/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx index 75f24be4db..9dcafdbf23 100644 --- a/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx +++ b/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx @@ -10,7 +10,7 @@ const states: { [title: string]: State.Network[] } = { { id: 'Mainnet', name: 'Mainnet', - remote: 'http://localhost:8114', + remote: 'http://127.0.0.1:8114', chain: 'ckb', type: 0, genesisHash: '0x92b197aa1fba0f63633922c61c92375c9c074a93e85963554f5499fe1450d0e5', @@ -18,7 +18,7 @@ const states: { [title: string]: State.Network[] } = { { id: 'Testnet', name: 'Testnet', - remote: 'http://localhost:8114', + remote: 'http://127.0.0.1:8114', chain: 'ckb_testnet', type: 1, genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606', @@ -26,7 +26,7 @@ const states: { [title: string]: State.Network[] } = { { id: 'Local', name: 'Local', - remote: 'http://localhost:8114', + remote: 'http://127.0.0.1:8114', chain: 'ckb_devnet', type: 1, genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606', diff --git a/packages/neuron-ui/src/stories/NetworkStatus.stories.tsx b/packages/neuron-ui/src/stories/NetworkStatus.stories.tsx index 78655db6e2..40c32cf5bb 100644 --- a/packages/neuron-ui/src/stories/NetworkStatus.stories.tsx +++ b/packages/neuron-ui/src/stories/NetworkStatus.stories.tsx @@ -6,7 +6,7 @@ import NetworkStatus, { NetworkStatusProps } from 'components/NetworkStatus' const defaultProps: Omit = { network: { name: 'network', - remote: 'http://localhost:3000', + remote: 'http://127.0.0.1:3000', type: 0, id: 'd', chain: 'ckb', @@ -68,7 +68,7 @@ stories.add('With knobs', () => { const props = { network: { name: text('Network name', 'network name'), - remote: text('Remote', 'http://localhost:3000'), + remote: text('Remote', 'http://127.0.0.1:3000'), type: select('Type', [0, 1], 0) as any, id: text('id', 'd'), chain: select('Chain', ['ckb', 'ckb_testnet', 'ckb_dev'], 'ckb'), diff --git a/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts b/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts index d9fbc65adf..56dc338194 100644 --- a/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts +++ b/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts @@ -22,7 +22,7 @@ const fixtures = { chain: 'ckb', type: 0 as 0 | 1, name: 'Mainnet', - remote: 'http://localhost:8114', + remote: 'http://127.0.0.1:8114', genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606', }, ], @@ -38,7 +38,7 @@ const fixtures = { chain: 'ckb_testnet', type: 0 as 0 | 1, name: 'Mainnet', - remote: 'http://localhost:8114', + remote: 'http://127.0.0.1:8114', genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606', }, ], @@ -54,7 +54,7 @@ const fixtures = { chain: 'ckb', type: 0 as 0 | 1, name: 'Mainnet', - remote: 'http://localhost:8114', + remote: 'http://127.0.0.1:8114', genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606', }, ], diff --git a/packages/neuron-wallet/src/services/networks.ts b/packages/neuron-wallet/src/services/networks.ts index e185118094..5b9320679a 100644 --- a/packages/neuron-wallet/src/services/networks.ts +++ b/packages/neuron-wallet/src/services/networks.ts @@ -7,7 +7,7 @@ import { Validate, Required } from 'utils/validators' import { UsedName, NetworkNotFound, InvalidFormat } from 'exceptions' import { MAINNET_GENESIS_HASH, EMPTY_GENESIS_HASH, NetworkType, Network, TESTNET_GENESIS_HASH } from 'models/network' import CommonUtils from 'utils/common' -import { BUNDLED_LIGHT_CKB_URL, LIGHT_CLIENT_TESTNET } from 'utils/const' +import { BUNDLED_CKB_URL, BUNDLED_LIGHT_CKB_URL, LIGHT_CLIENT_TESTNET } from 'utils/const' import { generateRPC } from 'utils/ckb-rpc' const presetNetworks: { selected: string; networks: Network[] } = { @@ -16,7 +16,7 @@ const presetNetworks: { selected: string; networks: Network[] } = { { id: 'mainnet', name: 'Default', - remote: 'http://localhost:8114', + remote: BUNDLED_CKB_URL, genesisHash: MAINNET_GENESIS_HASH, type: NetworkType.Default, chain: 'ckb' @@ -66,6 +66,12 @@ export default class NetworksService extends Store { public getAll = () => { const networks = this.readSync(NetworksKey.List) || presetNetworks.networks + networks.forEach(network => { + // Currently, the RPC interface of the CKB node is bound to IPv4 by default. + // Starting from node17, its DNS resolution is no longer `ipv4first`. + // Therefore, to ensure normal connection to the ckb node, manual resolution needs to be done here. + network.remote = applyLocalhostIPv4Resolve(network.remote) + }) const defaultNetwork = networks[0] const isOldDefaultName = ['Default', 'Mainnet'].includes(networks[0].name) defaultNetwork.name = isOldDefaultName ? 'default node' : defaultNetwork.name @@ -195,3 +201,17 @@ export default class NetworksService extends Store { return network } } + +function applyLocalhostIPv4Resolve(url: string): string { + let urlObj + try { + urlObj = new URL(url) + } catch (err) { + return url + } + + if (urlObj.hostname !== 'localhost') return url + + urlObj.hostname = '127.0.0.1' + return urlObj.href +} diff --git a/packages/neuron-wallet/src/utils/const.ts b/packages/neuron-wallet/src/utils/const.ts index 97401fb976..c4b6244f92 100644 --- a/packages/neuron-wallet/src/utils/const.ts +++ b/packages/neuron-wallet/src/utils/const.ts @@ -1,6 +1,6 @@ export const MIN_PASSWORD_LENGTH = 8 export const MAX_PASSWORD_LENGTH = 50 -export const BUNDLED_CKB_URL = 'http://localhost:8114' +export const BUNDLED_CKB_URL = 'http://127.0.0.1:8114' export const BUNDLED_LIGHT_CKB_URL = 'http://127.0.0.1:9000' export const LIGHT_CLIENT_TESTNET = 'light_client_testnet' export const SETTINGS_WINDOW_TITLE = process.platform === 'darwin' ? 'settings.title.mac' : 'settings.title.normal' diff --git a/packages/neuron-wallet/tests/controllers/export-debug.test.ts b/packages/neuron-wallet/tests/controllers/export-debug.test.ts index 585a347659..e6676be488 100644 --- a/packages/neuron-wallet/tests/controllers/export-debug.test.ts +++ b/packages/neuron-wallet/tests/controllers/export-debug.test.ts @@ -55,7 +55,7 @@ jest.mock('../../src/services/networks', () => { return { getCurrent() { return { - remote: 'http://localhost:8114' + remote: 'http://127.0.0.1:8114' } } } diff --git a/packages/neuron-wallet/tests/services/networks.test.ts b/packages/neuron-wallet/tests/services/networks.test.ts index c0783f7d6e..204694e208 100644 --- a/packages/neuron-wallet/tests/services/networks.test.ts +++ b/packages/neuron-wallet/tests/services/networks.test.ts @@ -11,7 +11,7 @@ const ERROR_MESSAGE = { describe(`Unit tests of networks service`, () => { const newNetwork: Network = { name: `new network`, - remote: `http://localhost:8114`, + remote: `http://127.0.0.1:8114`, type: 0, genesisHash: '0x', id: '', @@ -20,7 +20,7 @@ describe(`Unit tests of networks service`, () => { const newNetworkWithDefaultTypeOf1 = { name: `new network with the default type of 1`, - remote: `http://localhost:8114`, + remote: `http://127.0.0.1:8114`, id: '', } @@ -82,12 +82,21 @@ describe(`Unit tests of networks service`, () => { it(`update the network' address`, async () => { const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote) - const address = `http://localhost:8115` + const address = `http://127.0.0.1:8115` await service.update(network.id, { remote: address }) const updated = service.get(network.id) expect(updated && updated.remote).toBe(address) }) + it(`use ipv4 to resolve the localhost in network' remote`, async () => { + const network = await service.create(newNetworkWithDefaultTypeOf1.name, 'http://localhost:8114/') + const created = service.get(network.id) + expect(created && created.remote).toBe('http://127.0.0.1:8114/') + await service.update(network.id, { remote: 'http://localhost:8114/' }) + const updated = service.get(network.id) + expect(updated && updated.remote).toBe('http://127.0.0.1:8114/') + }) + it(`set the network to be the current one`, async () => { const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote) await service.activate(network.id) @@ -151,11 +160,11 @@ describe(`Unit tests of networks service`, () => { describe(`validation on network existence`, () => { beforeEach(async () => { - await service.create('Default', 'http://localhost:8114') + await service.create('Default', 'http://127.0.0.1:8114') }); it(`create network with existing name of Default`, () => { - expect(service.create('Default', 'http://localhost:8114')).rejects.toThrowError(t(ERROR_MESSAGE.NAME_USED)) + expect(service.create('Default', 'http://127.0.0.1:8114')).rejects.toThrowError(t(ERROR_MESSAGE.NAME_USED)) }) it(`update network which is not existing`, () => { diff --git a/packages/neuron-wallet/tests/services/node.test.ts b/packages/neuron-wallet/tests/services/node.test.ts index 4c80e11d9d..5adf57f0e4 100644 --- a/packages/neuron-wallet/tests/services/node.test.ts +++ b/packages/neuron-wallet/tests/services/node.test.ts @@ -329,7 +329,7 @@ describe('NodeService', () => { expect(stubbedTipNumberSubjectCallback).toHaveBeenCalledWith('0') }) describe('targets to bundled node', () => { - const bundledNodeUrl = 'http://localhost:8114' + const bundledNodeUrl = 'http://127.0.0.1:8114' beforeEach(async () => { stubbedCKBSetNode.mockImplementation(() => { nodeService.ckb.node.url = bundledNodeUrl