on master help sign in
bridge/src/lib/useImpliedTicket.ts
40 lines 1.4 kB history raw
1import ob from 'urbit-ob';
2import { ensurePatFormat, ensureSigPrefix } from 'form/formatters';
3import { patp2dec } from './patp2dec';
5/**
6 * pull the implied ticket and point from the #hash in the url
7 */
8// e.g., https://bridge.urbit.org/#widweb-tipfep-sabnux-nispec-sampel-palnet
10// Example tickets:
11// ~widweb-tipfep-sabnux-nispec
13// Example tickets + points (Activation Flow):
14// ~widweb-tipfep-sabnux-nispec-sampel
15// ~widweb-tipfep-sabnux-nispec-sampel-palnet
16const VALID_TICKET_REGEX = /^~(?:[[a-z]{6}-){3}[a-z]{6}/;
17const VALID_TICKET_AND_POINT_REGEX = /^~([a-z]{6}-){4}([a-z-]{6,13})/;
18const CAPTURE_TICKET_REGEX = /^~((?:[[a-z]{6}-){3}[a-z]{6})/;
19const CAPTURE_POINT_REGEX = /^~(?:(?:[[a-z]{6}-){3}[a-z]{6}-{1})([a-z-]{6,13})/;
21export default function useImpliedTicket() {
22 const hash = window.location.hash.slice(1);
23 const pat = ensurePatFormat(hash);
24 const impliedTicket = VALID_TICKET_REGEX.test(pat)
25 ? pat.match(CAPTURE_TICKET_REGEX)![0]
26 : null;
28 const impliedPatp = VALID_TICKET_AND_POINT_REGEX.test(pat)
29 ? ensureSigPrefix(pat.match(CAPTURE_POINT_REGEX)![1])
30 : null;
32 return {
33 impliedAzimuthPoint:
34 impliedPatp && ob.isValidPatq(impliedPatp) ? patp2dec(impliedPatp) : null,
35 impliedPatp:
36 impliedPatp && ob.isValidPatq(impliedPatp) ? impliedPatp : null,
37 impliedTicket:
38 impliedTicket && ob.isValidPatq(impliedTicket) ? impliedTicket : null,
39 };