on master help sign in
feat: implement maximum point length
9e64822Matt Condon (shrugs)
src/components/Inputs.js+6−1
@@ -15,6 +15,7 @@ import {
15 validateLength,
15 validateLength,
16 validateHexString,
16 validateHexString,
17 validateOneOf,
17 validateOneOf,
18+ validateMaximumPatpByteLength,
18} from 'lib/validators';
19} from 'lib/validators';
19import { prependSig } from 'lib/transformers';
20import { prependSig } from 'lib/transformers';
20
21
@@ -167,7 +168,11 @@ export function useTicketInput({ validators = [], deriving = false, ...rest }) {
167 );
168 );
168}
169}
169
170
170−const kPointValidators = [validatePoint, validateNotEmpty];
171+const kPointValidators = [
172+ validatePoint,
173+ validateMaximumPatpByteLength(4),
174+ validateNotEmpty,
175+];
171const kPointTransformers = [prependSig];
176const kPointTransformers = [prependSig];
172export function usePointInput(rest) {
177export function usePointInput(rest) {
173 const [lastValidPoint, setLastValidPoint] = useState('');
178 const [lastValidPoint, setLastValidPoint] = useState('');
src/lib/lib.js+6
@@ -34,6 +34,11 @@ const randomPatq = len => {
34 return ob.hex2patq(hex);
34 return ob.hex2patq(hex);
35};
35};
36
36
37+// TODO: is there a function for this calculation in urbit-ob or similar?
38+// 3 characters per byte, plus a tilde and hypen prefix per pair of bytes
39+const patpStringLength = byteLength =>
40+ byteLength * 3 + Math.ceil(byteLength / 2);
41+
37export {
42export {
38 compose,
43 compose,
39 allFalse,
44 allFalse,
@@ -44,4 +49,5 @@ export {
44 fill,
49 fill,
45 isValidGalaxy,
50 isValidGalaxy,
46 randomPatq,
51 randomPatq,
52+ patpStringLength,
47};
53};
src/lib/validators.js+35−13
@@ -4,6 +4,7 @@ import { identity, includes } from 'lodash';
4
4
5import { isValidAddress } from './wallet';
5import { isValidAddress } from './wallet';
6import patp2dec from './patp2dec';
6import patp2dec from './patp2dec';
7+import { patpStringLength } from './lib';
7
8
8// NOTE: do not use the /g modifier for these regexes
9// NOTE: do not use the /g modifier for these regexes
9// https://stackoverflow.com/a/21373261
10// https://stackoverflow.com/a/21373261
@@ -136,19 +137,6 @@ export const validateShard = m =>
136 error: 'This is not a valid shard',
137 error: 'This is not a valid shard',
137 });
138 });
138
139
139−export const validateLength = l => m =>
140− simpleValidatorWrapper({
141− prevMessage: m,
142− validator: d => {
143− try {
144− return d.length === l;
145− } catch {
146− return false;
147− }
148− },
149− error: `Must be exactly ${l} characters`,
150− });
151−
152export const validateOneOf = (options = []) => m =>
140export const validateOneOf = (options = []) => m =>
153 simpleValidatorWrapper({
141 simpleValidatorWrapper({
154 prevMessage: m,
142 prevMessage: m,
@@ -215,3 +203,37 @@ export const validateExactly = (value, error) => m =>
215 validator: d => d === value,
203 validator: d => d === value,
216 error,
204 error,
217 });
205 });
206+
207+export const validateLength = l => m =>
208+ simpleValidatorWrapper({
209+ prevMessage: m,
210+ validator: d => {
211+ try {
212+ return d.length === l;
213+ } catch {
214+ return false;
215+ }
216+ },
217+ error: `Must be exactly ${l} characters`,
218+ });
219+
220+export const validateMaximumLength = l => m =>
221+ simpleValidatorWrapper({
222+ prevMessage: m,
223+ validator: d => {
224+ try {
225+ return d.length <= l;
226+ } catch {
227+ return false;
228+ }
229+ },
230+ error: `Must be ${l} characters or fewer.`,
231+ });
232+
233+export const validatePatpByteLength = byteLength => {
234+ return validateLength(patpStringLength(byteLength));
235+};
236+
237+export const validateMaximumPatpByteLength = byteLength => {
238+ return validateMaximumLength(patpStringLength(byteLength));
239+};