This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
/
util.ts
100 lines (81 loc) · 3.07 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2018-2019 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Keyring } from '@polkadot/ui-keyring';
import { mnemonicToSeed, naclKeypairFromSeed } from '@polkadot/util-crypto';
import { Either, left, right } from 'fp-ts/lib/Either';
import { UserInput, UserInputError, PhrasePartialRewrite, PhrasePartialRewriteError } from './types';
/**
* Fischer Yates shuffle numbers between 0 and @max
* @param max highest number the random number should be
*/
export function getRandomInts (max: number): number[] {
const scratch = [];
// populate with the range of possible numbers
for (let i = 1; i <= max; ++i) {
scratch.push(i);
}
let temp;
let randIndex;
for (let i = max - 1; i >= 0; i -= 1) {
randIndex = Math.floor(Math.random() * Math.floor((i)));
temp = scratch[i];
scratch[i] = scratch[randIndex];
scratch[randIndex] = temp;
}
return scratch;
}
/**
* Derive public address from mnemonic key
*/
export function generateAddressFromMnemonic (keyring: Keyring, mnemonic: string): string {
const keypair = naclKeypairFromSeed(mnemonicToSeed(mnemonic));
return keyring.encodeAddress(
keypair.publicKey
);
}
/**
* Validate user inputs
*/
export function validateMeta (values: UserInput, step: string, whichAccount: string): Either<UserInputError, UserInput> {
const errors = {} as UserInputError;
if (whichAccount) {
values.tags = [whichAccount];
}
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
values.tags = values.tags.map(tag => tag.toLowerCase());
if (step === 'meta') {
(['name', 'password'] as (Exclude<keyof UserInput, 'tags'>)[])
.filter((key) => !values[key])
.forEach((key) => {
errors[key] = `Field "${key}" cannot be empty`;
});
}
// Should not tag an account as both a stash and controller
if (values.tags.includes('stash') && values.tags.includes('controller')) {
errors.tags = 'Each account should be either a Stash or a Controller, not both.';
}
return Object.keys(errors).length ? left(errors) : right(values);
}
/**
* Validate phrase rewrite
*/
export function validateRewrite (values: PhrasePartialRewrite, randomFourWords: Array<Array<string>>): Either<PhrasePartialRewriteError, PhrasePartialRewrite> {
const errors = {} as PhrasePartialRewriteError;
const { firstWord, secondWord, thirdWord, fourthWord } = values;
if (!firstWord || !secondWord || !thirdWord || !fourthWord) {
errors.emptyFields = 'All fields must be set to be sure you copied the phrase correctly.';
}
if (
firstWord === randomFourWords[0][1] &&
secondWord === randomFourWords[1][1] &&
thirdWord === randomFourWords[2][1] &&
fourthWord === randomFourWords[3][1]
) {
return right(values);
} else {
errors.incorrectFields = 'It seems you did not copy all the words properly. Please double check your inputs and try again.';
}
return Object.keys(errors).length ? left(errors) : right(values);
}