mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-03 05:55:03 +01:00
feat(utils): add parseElectrumUrl util
This commit is contained in:
@@ -22,6 +22,7 @@ export * from './isNotUndefined';
|
||||
export * from './isUrl';
|
||||
export * from './mergeDeepObject';
|
||||
export * from './objectPartition';
|
||||
export * from './parseElectrumUrl';
|
||||
export * from './parseHostname';
|
||||
export * from './promiseAllSequence';
|
||||
export * from './redactUserPath';
|
||||
|
||||
12
packages/utils/src/parseElectrumUrl.ts
Normal file
12
packages/utils/src/parseElectrumUrl.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
// URL is in format host:port:[t|s] (t for tcp, s for ssl)
|
||||
const ELECTRUM_URL_REGEX = /^(?:([a-zA-Z0-9.-]+)|\[([a-f0-9:]+)\]):([0-9]{1,5}):([ts])$/;
|
||||
|
||||
export const parseElectrumUrl = (url: string) => {
|
||||
const match = url.match(ELECTRUM_URL_REGEX);
|
||||
if (!match) return undefined;
|
||||
return {
|
||||
host: match[1] ?? match[2],
|
||||
port: Number.parseInt(match[3], 10),
|
||||
protocol: match[4] as 't' | 's',
|
||||
};
|
||||
};
|
||||
29
packages/utils/tests/parseElectrumUrl.test.ts
Normal file
29
packages/utils/tests/parseElectrumUrl.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { parseElectrumUrl } from '../src/parseElectrumUrl';
|
||||
|
||||
const FIXTURE = [
|
||||
['electrum.example.com:50001:t', 'electrum.example.com', 50001, 't'],
|
||||
['electrum.example.com:50001:s', 'electrum.example.com', 50001, 's'],
|
||||
['electrum.example.onion:50001:t', 'electrum.example.onion', 50001, 't'],
|
||||
['electrum.example.com:50001:x'],
|
||||
['127.0.0.1:50001:t', '127.0.0.1', 50001, 't'],
|
||||
['2001:0db8:85a3:0000:0000:8a2e:0370:7334:50001:t'],
|
||||
[
|
||||
'[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:50001:t',
|
||||
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
|
||||
50001,
|
||||
't',
|
||||
],
|
||||
['[::1]:50001:t', '::1', 50001, 't'],
|
||||
['[example.com]:50001:t'],
|
||||
['wss://blockfrost.io'],
|
||||
['https://google.com'],
|
||||
[''],
|
||||
] as const;
|
||||
|
||||
describe('parseElectrumUrl', () => {
|
||||
FIXTURE.forEach(([url, host, port, protocol]) =>
|
||||
it(url, () => {
|
||||
expect(parseElectrumUrl(url)).toStrictEqual(host && { host, port, protocol });
|
||||
}),
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user