identitiesUtils.ts 12.2 KB
Newer Older
1
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
Hanwen Cheng's avatar
Hanwen Cheng committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

17
import { pathsRegex } from './regex';
18
19
import { decryptData, substrateAddress } from './native';
import { constructSURI, parseSURI } from './suri';
20
import { generateAccountId } from './account';
Hanwen Cheng's avatar
Hanwen Cheng committed
21

22
import { TryCreateFunc } from 'utils/seedRefHooks';
Hanwen Cheng's avatar
Hanwen Cheng committed
23
24
import {
	NETWORK_LIST,
25
	PATH_IDS_LIST,
26
	SUBSTRATE_NETWORK_LIST,
27
	SubstrateNetworkKeys,
28
29
	UnknownNetworkKeys,
	unknownNetworkPathId
30
31
32
} from 'constants/networkSpecs';
import {
	Account,
33
	AccountMeta,
34
35
36
37
38
39
40
	FoundAccount,
	FoundLegacyAccount,
	Identity,
	PathGroup,
	SerializedIdentity,
	UnlockedAccount
} from 'types/identityTypes';
41
import {
42
43
	centrifugeAmberMetadata,
	centrifugeMetadata,
44
	defaultMetaData,
45
46
	edgewareMetadata,
	kulupuMetadata,
47
	kusamaMetadata,
48
	polkadotMetaData,
49
50
51
	substrateDevMetadata,
	westendMetadata
} from 'constants/networkMetadata';
Hanwen Cheng's avatar
Hanwen Cheng committed
52
53

//walk around to fix the regular expression support for positive look behind;
54
export const removeSlash = (str: string): string => str.replace(/\//g, '');
Hanwen Cheng's avatar
Hanwen Cheng committed
55

56
57
58
59
60
61
export function isLegacyFoundAccount(
	foundAccount: FoundAccount
): foundAccount is FoundLegacyAccount {
	return foundAccount.isLegacy;
}

62
export const extractPathId = (path: string): string => {
Hanwen Cheng's avatar
Hanwen Cheng committed
63
	const matchNetworkPath = path.match(pathsRegex.networkPath);
64
65
66
67
68
69
70
	if (matchNetworkPath && matchNetworkPath[0]) {
		const targetPathId = removeSlash(matchNetworkPath[0]);
		if (PATH_IDS_LIST.includes(targetPathId)) {
			return targetPathId;
		}
	}
	return unknownNetworkPathId;
Hanwen Cheng's avatar
Hanwen Cheng committed
71
72
};

73
export const extractSubPathName = (path: string): string => {
Hanwen Cheng's avatar
Hanwen Cheng committed
74
	const pathFragments = path.match(pathsRegex.allPath);
75
76
	if (!pathFragments || pathFragments.length === 0) return '';
	if (pathFragments.length === 1) return removeSlash(pathFragments[0]);
Hanwen Cheng's avatar
Hanwen Cheng committed
77
78
79
	return removeSlash(pathFragments.slice(1).join(''));
};

80
export const isSubstratePath = (path: string): boolean =>
81
	path.match(pathsRegex.allPath) !== null || path === '';
Hanwen Cheng's avatar
Hanwen Cheng committed
82

83
84
export const isEthereumAccountId = (v: string): boolean =>
	v.indexOf('ethereum:') === 0;
85

86
87
88
89
90
91
92
93
94
export const isSubstrateHardDerivedPath = (path: string): boolean => {
	if (!isSubstratePath(path)) return false;
	const pathFragments = path.match(pathsRegex.allPath);
	if (!pathFragments || pathFragments.length === 0) return false;
	return pathFragments.every((pathFragment: string) => {
		return pathFragment.substring(0, 2) === '//';
	});
};

95
export const extractAddressFromAccountId = (id: string): string => {
96
97
98
99
100
101
102
103
	const withoutNetwork = id.split(':')[1];
	const address = withoutNetwork.split('@')[0];
	if (address.indexOf('0x') !== -1) {
		return address.slice(2);
	}
	return address;
};

104
105
106
107
108
109
export const getAddressKeyByPath = (
	path: string,
	pathMeta: AccountMeta
): string => {
	const address = pathMeta.address;
	return isSubstratePath(path)
110
		? address
111
112
113
114
115
		: generateAccountId({
				address,
				networkKey: getNetworkKeyByPath(path, pathMeta)
		  });
};
116

117
export function emptyIdentity(): Identity {
Hanwen Cheng's avatar
Hanwen Cheng committed
118
	return {
119
		addresses: new Map(),
Hanwen Cheng's avatar
Hanwen Cheng committed
120
		derivationPassword: '',
121
		encryptedSeed: '',
Hanwen Cheng's avatar
Hanwen Cheng committed
122
123
124
125
126
		meta: new Map(),
		name: ''
	};
}

127
128
129
export const serializeIdentity = (identity: Identity): SerializedIdentity =>
	Object.entries(identity).reduce((newIdentity: any, entry: [string, any]) => {
		const [key, value] = entry;
Hanwen Cheng's avatar
Hanwen Cheng committed
130
131
132
133
134
135
136
137
		if (value instanceof Map) {
			newIdentity[key] = Array.from(value.entries());
		} else {
			newIdentity[key] = value;
		}
		return newIdentity;
	}, {});

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
export const deserializeIdentity = (
	identityJSON: SerializedIdentity
): Identity =>
	Object.entries(identityJSON).reduce(
		(newIdentity: any, entry: [string, any]) => {
			const [key, value] = entry;
			if (value instanceof Array) {
				newIdentity[key] = new Map(value);
			} else {
				newIdentity[key] = value;
			}
			return newIdentity;
		},
		{}
	);
Hanwen Cheng's avatar
Hanwen Cheng committed
153

154
export const serializeIdentities = (identities: Identity[]): string => {
Hanwen Cheng's avatar
Hanwen Cheng committed
155
156
157
158
	const identitiesWithObject = identities.map(serializeIdentity);
	return JSON.stringify(identitiesWithObject);
};

159
export const deserializeIdentities = (identitiesJSON: string): Identity[] => {
Hanwen Cheng's avatar
Hanwen Cheng committed
160
161
162
163
	const identitiesWithObject = JSON.parse(identitiesJSON);
	return identitiesWithObject.map(deserializeIdentity);
};

164
export const deepCopyIdentities = (identities: Identity[]): Identity[] =>
Hanwen Cheng's avatar
Hanwen Cheng committed
165
	deserializeIdentities(serializeIdentities(identities));
166
167

export const deepCopyIdentity = (identity: Identity): Identity =>
Hanwen Cheng's avatar
Hanwen Cheng committed
168
169
	deserializeIdentity(serializeIdentity(identity));

170
export const getPathsWithSubstrateNetworkKey = (
171
	identity: Identity,
172
173
	networkKey: string
): string[] => {
174
	const pathEntries = Array.from(identity.meta.entries());
175
176
	const targetPathId =
		SUBSTRATE_NETWORK_LIST[networkKey]?.pathId || unknownNetworkPathId;
177
178
179
180
181
182
	const pathReducer = (
		groupedPaths: string[],
		[path, pathMeta]: [string, AccountMeta]
	): string[] => {
		let pathId;
		if (!isSubstratePath(path)) return groupedPaths;
183
		if (pathMeta.networkPathId !== undefined) {
184
185
186
187
188
			pathId = pathMeta.networkPathId;
		} else {
			pathId = extractPathId(path);
		}

189
190
191
		if (pathId === targetPathId) {
			groupedPaths.push(path);
			return groupedPaths;
192
193
194
195
		}
		return groupedPaths;
	};
	return pathEntries.reduce(pathReducer, []);
196
};
Hanwen Cheng's avatar
Hanwen Cheng committed
197

198
export const getNetworkKeyByPathId = (pathId: string): string => {
199
	const networkKeyIndex = Object.values(SUBSTRATE_NETWORK_LIST).findIndex(
200
201
		networkParams => networkParams.pathId === pathId
	);
202
203
	if (networkKeyIndex !== -1)
		return Object.keys(SUBSTRATE_NETWORK_LIST)[networkKeyIndex];
204
205
206
	return UnknownNetworkKeys.UNKNOWN;
};

207
export const getNetworkKey = (path: string, identity: Identity): string => {
208
	if (identity.meta.has(path)) {
209
		return getNetworkKeyByPath(path, identity.meta.get(path)!);
210
	}
211
	return UnknownNetworkKeys.UNKNOWN;
212
213
};

214
215
216
217
export const getNetworkKeyByPath = (
	path: string,
	pathMeta: AccountMeta
): string => {
Hanwen Cheng's avatar
Hanwen Cheng committed
218
	if (!isSubstratePath(path) && NETWORK_LIST.hasOwnProperty(path)) {
219
		//It is a ethereum path
Hanwen Cheng's avatar
Hanwen Cheng committed
220
221
		return path;
	}
222
	const pathId = pathMeta.networkPathId || extractPathId(path);
Hanwen Cheng's avatar
Hanwen Cheng committed
223

224
	return getNetworkKeyByPathId(pathId);
Hanwen Cheng's avatar
Hanwen Cheng committed
225
226
};

227
228
229
230
231
232
233
234
export const parseFoundLegacyAccount = (
	legacyAccount: Account,
	accountId: string
): FoundLegacyAccount => {
	const returnAccount: FoundLegacyAccount = {
		accountId,
		address: legacyAccount.address,
		createdAt: legacyAccount.createdAt,
235
		encryptedSeed: legacyAccount.encryptedSeed,
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		isLegacy: true,
		name: legacyAccount.name,
		networkKey: legacyAccount.networkKey,
		updatedAt: legacyAccount.updatedAt,
		validBip39Seed: legacyAccount.validBip39Seed
	};
	if (legacyAccount.hasOwnProperty('derivationPath')) {
		returnAccount.path = (legacyAccount as UnlockedAccount).derivationPath;
	}
	return returnAccount;
};

export const getIdentityFromSender = (
	sender: FoundAccount,
	identities: Identity[]
): Identity | undefined =>
Hanwen Cheng's avatar
Hanwen Cheng committed
252
253
	identities.find(i => i.encryptedSeed === sender.encryptedSeed);

254
255
export const getAddressWithPath = (
	path: string,
256
	identity: Identity | null
257
): string => {
258
	if (identity == null) return '';
Hanwen Cheng's avatar
Hanwen Cheng committed
259
	const pathMeta = identity.meta.get(path);
260
261
262
263
264
	if (!pathMeta) return '';
	const { address } = pathMeta;
	return isEthereumAccountId(address)
		? extractAddressFromAccountId(address)
		: address;
Hanwen Cheng's avatar
Hanwen Cheng committed
265
266
};

267
export const unlockIdentitySeedWithReturn = async (
268
	pin: string,
269
270
	identity: Identity,
	createSeedRef: TryCreateFunc
271
): Promise<string> => {
Hanwen Cheng's avatar
Hanwen Cheng committed
272
273
	const { encryptedSeed } = identity;
	const seed = await decryptData(encryptedSeed, pin);
274
	await createSeedRef(pin);
Hanwen Cheng's avatar
Hanwen Cheng committed
275
276
277
278
	const { phrase } = parseSURI(seed);
	return phrase;
};

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
export const verifyPassword = async (
	password: string,
	seedPhrase: string,
	identity: Identity,
	path: string
): Promise<boolean> => {
	const suri = constructSURI({
		derivePath: path,
		password: password,
		phrase: seedPhrase
	});
	const networkKey = getNetworkKey(path, identity);
	const networkParams = SUBSTRATE_NETWORK_LIST[networkKey];
	const address = await substrateAddress(suri, networkParams.prefix);
	const accountMeta = identity.meta.get(path);
	return address === accountMeta?.address;
};

297
export const getExistedNetworkKeys = (identity: Identity): string[] => {
298
299
300
301
302
303
304
305
306
307
308
309
310
	const pathEntries = Array.from(identity.meta.entries());
	const networkKeysSet = pathEntries.reduce(
		(networksSet, [path, pathMeta]: [string, AccountMeta]) => {
			let networkKey;
			if (isSubstratePath(path)) {
				networkKey = getNetworkKeyByPath(path, pathMeta);
			} else {
				networkKey = path;
			}
			return { ...networksSet, [networkKey]: true };
		},
		{}
	);
Hanwen Cheng's avatar
Hanwen Cheng committed
311
312
313
	return Object.keys(networkKeysSet);
};

314
export const validateDerivedPath = (derivedPath: string): boolean =>
Hanwen Cheng's avatar
Hanwen Cheng committed
315
316
	pathsRegex.validateDerivedPath.test(derivedPath);

317
318
319
320
export const getIdentityName = (
	identity: Identity,
	identities: Identity[]
): string => {
Hanwen Cheng's avatar
Hanwen Cheng committed
321
322
323
324
325
326
327
	if (identity.name) return identity.name;
	const identityIndex = identities.findIndex(
		i => i.encryptedSeed === identity.encryptedSeed
	);
	return `Identity_${identityIndex}`;
};

328
329
330
331
export const getPathName = (
	path: string,
	lookUpIdentity: Identity | null
): string => {
Hanwen Cheng's avatar
Hanwen Cheng committed
332
333
334
	if (
		lookUpIdentity &&
		lookUpIdentity.meta.has(path) &&
335
		lookUpIdentity.meta.get(path)!.name !== ''
Hanwen Cheng's avatar
Hanwen Cheng committed
336
	) {
337
		return lookUpIdentity.meta.get(path)!.name;
Hanwen Cheng's avatar
Hanwen Cheng committed
338
	}
339
340
	if (!isSubstratePath(path)) return 'No name';
	if (path === '') return 'Identity root';
Hanwen Cheng's avatar
Hanwen Cheng committed
341
342
343
	return extractSubPathName(path);
};

344
345
346
347
348
349
/**
 * This function decides how to group the list of derivation paths in the display based on the following rules.
 * If the network is unknown: group by the first subpath, e.g. '/random' of '/random//derivation/1'
 * If the network is known: group by the second subpath, e.g. '//staking' of '//kusama//staking/0'
 * Please refer to identitiesUtils.spec.js for more examples.
 **/
350
351
352
353
354
355
356
357
export const groupPaths = (paths: string[]): PathGroup[] => {
	const insertPathIntoGroup = (
		matchingPath: string,
		fullPath: string,
		pathGroup: PathGroup[]
	): void => {
		const matchResult = matchingPath.match(pathsRegex.firstPath);
		const groupName = matchResult ? matchResult[0] : '-';
358
359
360
361
362
363
364
365
366
367

		const existedItem = pathGroup.find(p => p.title === groupName);
		if (existedItem) {
			existedItem.paths.push(fullPath);
			existedItem.paths.sort();
		} else {
			pathGroup.push({ paths: [fullPath], title: groupName });
		}
	};

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
	const groupedPaths = paths.reduce(
		(groupedPath: PathGroup[], path: string) => {
			if (path === '') {
				groupedPath.push({ paths: [''], title: 'Identity root' });
				return groupedPath;
			}

			const rootPath = path.match(pathsRegex.firstPath)?.[0];
			if (rootPath === undefined) return groupedPath;

			const networkParams = Object.values(SUBSTRATE_NETWORK_LIST).find(
				v => `//${v.pathId}` === rootPath
			);
			if (networkParams === undefined) {
				insertPathIntoGroup(path, path, groupedPath);
				return groupedPath;
			}

			const isRootPath = path === rootPath;
			if (isRootPath) {
				groupedPath.push({
					paths: [path],
					title: `${networkParams.title} root`
				});
				return groupedPath;
			}

			const subPath = path.slice(rootPath.length);
			insertPathIntoGroup(subPath, path, groupedPath);
397

398
			return groupedPath;
399
400
401
		},
		[]
	);
402
403
404
405
406
407
	return groupedPaths.sort((a, b) => {
		if (a.paths.length === 1 && b.paths.length === 1) {
			return a.paths[0].length - b.paths[0].length;
		}
		return a.paths.length - b.paths.length;
	});
Hanwen Cheng's avatar
Hanwen Cheng committed
408
};
409
410
411

export const getMetadata = (networkKey: string): string => {
	switch (networkKey) {
412
413
414
415
		case SubstrateNetworkKeys.CENTRIFUGE:
			return centrifugeMetadata;
		case SubstrateNetworkKeys.CENTRIFUGE_AMBER:
			return centrifugeAmberMetadata;
416
417
418
419
420
421
422
423
		case SubstrateNetworkKeys.KUSAMA:
		case SubstrateNetworkKeys.KUSAMA_CC2:
		case SubstrateNetworkKeys.KUSAMA_DEV:
			return kusamaMetadata;
		case SubstrateNetworkKeys.WESTEND:
			return westendMetadata;
		case SubstrateNetworkKeys.SUBSTRATE_DEV:
			return substrateDevMetadata;
424
425
426
427
		case SubstrateNetworkKeys.EDGEWARE:
			return edgewareMetadata;
		case SubstrateNetworkKeys.KULUPU:
			return kulupuMetadata;
428
429
		case SubstrateNetworkKeys.POLKADOT:
			return polkadotMetaData;
430
431
432
433
		default:
			return defaultMetaData;
	}
};