1import * as bip39 from 'bip39';
2import * as bitcoin from 'bitcoinjs-lib';
3import ob from 'urbit-ob';
4import { includes } from 'lodash';
6import { isValidAddress } from './utils/address';
7import { patp2dec } from './patp2dec';
8import { patpStringLength } from './lib';
16// NOTE: do not use the /g modifier for these regexes
17// https://stackoverflow.com/a/21373261
18// https://stackoverflow.com/a/1520853
20// via: https://emailregex.com/
21const emailRegExp = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
23// validate only strings that use hex characters
24const isHexString = /[0-9A-Fa-f]/;
26// validate only strings that are prefixed with 0x
27const isHexPrefixed = /^0x/;
29// Validates a bip39 mnemonic
30export const validateMnemonic = (v: string) =>
31 !bip39.validateMnemonic(v) && 'This is not a valid mnemonic.';
33const isHdPath = /^m(\/[0-9]+'?)*$/;
34export const validateHdPath = (v: string) =>
35 !isHdPath.test(v) && 'Invalid HD derivation path.';
37// Checks an empty field
38export const validateNotEmpty = (v?: Array<unknown> | string) =>
39 (v === undefined || v.length === 0) && 'This field is required.';
41// Checks if a patp is a valid galaxy
42export const validateGalaxy = (v: string) => {
44 const point = patp2dec(v);
45 const isValidGalaxy = point >= MIN_GALAXY && point <= MAX_GALAXY;
50 return 'This is not a valid galaxy.';
54export const validatePoint = (v: string) => {
56 if (!ob.isValidPatp(v)) {
60 return 'This is not a valid point.';
64export const validatePatq = (v: string) => {
66 if (!ob.isValidPatq(v)) {
70 return 'This is not a valid ticket.';
74export const validateActivationTicket = (v: string) => {
75 console.log('HERE', v.length)
81 return 'This is not a valid activation ticket.';
85export const validateShard = (v: string) => {
87 if (v !== undefined && v !== '' && !ob.isValidPatq(v)) {
91 return 'This is not a valid shard.';
95export const validateOneOf = (options: unknown[] = []) => (v: unknown) =>
96 !includes(options, v) && 'Is not a valid option.';
98export const validateHexString = (v: string) =>
99 !isHexString.test(v) && 'This is not a valid hex string.';
101export const validateHexPrefix = (v: string) =>
102 !isHexPrefixed.test(v) && 'Must include 0x prefix.';
104export const validateEthereumAddress = (v: string) =>
105 !isValidAddress(v) && 'This is not a valid Ethereum address.';
107export const validateEmail = (v: string) =>
108 !emailRegExp.test(v) && 'This is not a valid email address.';
110export function validateExactly<T>(value: T, error: string) {
111 return (v: T) => v !== value && error;
114export const validateNotAny = (values: unknown[] = []) => (v: unknown) =>
115 values.includes(v) && `Cannot be ${v}.`;
117type Lengthable = string | Array<unknown> | Buffer;
119export const validateLength = (l: number) => (v: Lengthable) =>
120 v.length !== l && `Must be exactly ${l} characters.`;
122export const validateHexLength = (l: number) => (v: Lengthable) =>
123 v.length !== l + 2 && `Must be exactly ${l} hex characters.`;
125export const validateMaximumLength = (l: number) => (v: Lengthable) =>
126 v.length > l && `Must be ${l} characters or fewer.`;
128export const validateMinimumLength = (l: number) => (v: Lengthable) =>
129 v.length < l && `Must be ${l} characters or more.`;
131export const validateGreaterThan = (l: number) => (v: number) =>
132 !(v > l) && `Must be greater than ${l}.`;
134export const validateLessThan = (l: number) => (v: number) =>
135 !(v < l) && `Must be less than ${l}`;
137export const validateInSet = (set: Set<unknown>, error: string) => (
139) => !set.has(v) && error;
141export const validateMaximumPatpByteLength = (byteLength: number) =>
142 validateMaximumLength(patpStringLength(byteLength));
144export const validateMinimumPatpByteLength = (byteLength: number) =>
145 validateMinimumLength(patpStringLength(byteLength));
147export const validateNotNullAddress = validateNotAny([
152export const validateUnique = (arr: unknown[]) => {
154 [...Array.from(new Set(arr))].length !== arr.length && 'Must be unique';
158export const validateChild = (ourShip: string) => (ship: string) =>
159 ourShip !== ob.sein(ship) && `This point is not a child of ${ourShip}.`;
161export const validatePsbt = (base64: string) => {
163 bitcoin.Psbt.fromBase64(base64);
165 return 'Invalid Partially Signed Bitcoin Transaction';
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list