on master help sign in
fix: expand ensurePatFormat to cover prependSig case
commitd72cd54a999cd27a508884af43506128084e4a44 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 13 Aug 2019, 09:24 (7 years ago) parent7b9591f
4 files changed+16−28
src/form/Inputs.js+2−8 src/form/formatters.js+10−16 src/lib/useImpliedPoint.js+2−2 src/lib/useImpliedTicket.js+2−2
src/form/Inputs.js+2−8
@@ -3,11 +3,10 @@ import { Input, AccessoryIcon } from 'indigo-react';
33import { useField } from 'react-final-form';
44
55import {
6− prependSig,
76 convertToNumber,
87 buildFormatter,
98 downcase,
10− ensurePatQDashes,
9+ ensurePatFormat,
1110} from 'form/formatters';
1211import { DEFAULT_HD_PATH } from 'lib/wallet';
1312import InputSigil from 'components/InputSigil';
@@ -22,12 +21,7 @@ const PLACEHOLDER_PRIVATE_KEY =
2221 '0x12345abcdee6beb2f323fab48b432925c9785808d33a6ca6d7ba00b45e9370c3';
2322const PLACEHOLDER_EMAIL = 'Email Address';
2423
25−const TICKET_MAX_BYTE_LEN = 32; // tickets can be as large as 32 bytes
26−const formatPat = buildFormatter([
27− downcase,
28− prependSig,
29− ensurePatQDashes(TICKET_MAX_BYTE_LEN),
30−]);
24+const formatPat = buildFormatter([downcase, ensurePatFormat]);
3125
3226export function TicketInput({ name, ...rest }) {
3327 const {
src/form/formatters.js+10−16
@@ -1,39 +1,33 @@
11import { compose, strSplice } from 'lib/lib';
22
3+const TICKET_MAX_BYTE_LEN = 32; // tickets can be as large as 32 bytes
4+
35export const buildFormatter = (formatters = []) =>
46 compose(...formatters.reverse());
57
6−export const prependSig = s => {
7− if (!s || s.length === 0) {
8− return s || '';
9− }
10−
11− return s.charAt(0) === '~' ? s : `~${s}`;
12−};
13−
148/**
15− * inserts a - every 7 characters
16− * (must be preceeded by prependSig to work correctly)
17− * @param {number} maxByteLength the maximum number of patq blocks to have in a string
9+ * inserts a ~ or - every 7 characters
1810 */
19−export const ensurePatQDashes = maxByteLength => s => {
11+export const ensurePatFormat = s => {
2012 if (!s || s.length === 0) {
2113 return s || '';
2214 }
2315
2416 // ensure there's a dash every 6 + 1 characters
2517 const dashAt = i => i * (6 + 1);
26− const maxBlocks = maxByteLength / 2;
18+ const maxBlocks = TICKET_MAX_BYTE_LEN / 2;
2719
28− for (let i = 1; i !== maxBlocks; i++) {
20+ for (let i = 0; i !== maxBlocks; i++) {
2921 const dashIndex = dashAt(i);
3022
3123 if (s.length - 1 < dashIndex) {
3224 return s;
3325 }
3426
35− if (s.charAt(dashIndex) !== '-') {
36− s = strSplice(s, dashIndex, '-');
27+ const sep = i === 0 ? '~' : '-';
28+
29+ if (s.charAt(dashIndex) !== sep) {
30+ s = strSplice(s, dashIndex, sep);
3731 }
3832 }
3933
src/lib/useImpliedPoint.js+2−2
@@ -1,13 +1,13 @@
11import * as ob from 'urbit-ob';
22
3−import { prependSig } from 'form/formatters';
3+import { ensurePatFormat } from 'form/formatters';
44
55/**
66 * pull the suggested point from the subdomain
77 */
88export default function useImpliedPoint() {
99 const subdomain = window.location.host.split('.')[0];
10− const patp = prependSig(subdomain);
10+ const patp = ensurePatFormat(subdomain);
1111 const isValid = ob.isValidPatp(patp);
1212
1313 return isValid ? patp : null;
src/lib/useImpliedTicket.js+2−2
@@ -1,13 +1,13 @@
11import * as ob from 'urbit-ob';
22
3−import { prependSig } from 'form/formatters';
3+import { ensurePatFormat } from 'form/formatters';
44
55/**
66 * pull the suggested ticket from the #hash in the url
77 */
88export default function useImpliedTicket() {
99 const hash = window.location.hash.slice(1);
10− const ticket = prependSig(hash);
10+ const ticket = ensurePatFormat(hash);
1111 const isValid = ob.isValidPatq(ticket);
1212
1313 return isValid ? ticket : null;