@0001-labs/idv1 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # IDv1
2
2
 
3
- Short codes whose shape says what they name. Every code is six characters, and one look at it tells an account from a list or an item, so a single address, `example.com/<code>`, can open any of them and never two at once.
3
+ Short codes whose shape says what they name. Documentation at [idv1.0001.dev](https://idv1.0001.dev). Every code is six characters, and one look at it tells an account from a list or an item, so a single address, `example.com/<code>`, can open any of them and never two at once.
4
4
 
5
5
  | What | Shape | Example | How many |
6
6
  |---|---|---|---|
@@ -15,6 +15,7 @@ Short codes whose shape says what they name. Every code is six characters, and o
15
15
  - **New lists draw four capitals.** Older lists may hold small letters, and every check reads both.
16
16
  - **An item never ends in two digits**, or it would be an account.
17
17
  - **A code does not change by itself.** A product may give an account a chosen code, as when it should match the code its owner has elsewhere.
18
+ - **Some codes are kept for a person or a role.** `RESERVED_ACCOUNT_CODES` names them, `RESERVED_ACCOUNT_EMAILS` says whose they are, and no product draws them for anyone else: Joachim is `Jo4712` (jaa90@icloud.com) and `Jo0001` (joachim@0001.dev), the onboarding test account is `On0001` (joachim+onboarding@0001.dev, `On4713` before) and the test account is `Te0001` (joachim+test@0001.dev). A product gives the code `reservedAccountCode(email)` returns before drawing one, so the same person is the same code in every product and easy to leave out of the figures.
18
19
 
19
20
  ## Which shape for what you build
20
21
 
@@ -39,6 +40,31 @@ isCodeOf('list', '47KQWE') // true
39
40
  codeKind('Ab12Cd') // 'item'
40
41
  ```
41
42
 
43
+ ## Advanced: a code for every event
44
+
45
+ The four shapes hold about 2.5 billion codes, enough for every person, list and item, but not for every event a product records or every visit to it. The expanded version, `@0001-labs/idv1/advanced`, keeps six characters of the same 62 letters and digits and takes every code the four shapes leave free: any order at all, so long as it is none of them.
46
+
47
+ | Version | What | Shape | How many |
48
+ |---|---|---|---|
49
+ | Standard | account, product, list, item | the four shapes above | 2.5 billion, read as they are read |
50
+ | Advanced | event | any six letters or digits that make none of the four shapes, such as `k3Rw9Q` | 54.3 billion |
51
+ | Both | every code | six of 62 | 56.8 billion (62⁶) |
52
+
53
+ - **An event is still told apart on sight**, since no account, product, list or item can be one: `advancedCodeKind` reads all five kinds, and `codeKind` in the standard version is unchanged and reads an event as no code.
54
+ - **It is drawn uniformly from cryptographic random numbers** (`secureRandom`), again while it makes a shape or spells a blocked word. Nobody types it and it never opens anything when scanned, though the scannable code can carry it.
55
+ - **Whether one is free is still the product's to check.** With 54 billion to draw from, two events rarely meet, but a product that keeps them by code looks before it keeps one.
56
+
57
+ ```ts
58
+ import { ADVANCED_SPACE, advancedCodeKind, eventCodeCandidate, isEventCode } from '@0001-labs/idv1/advanced'
59
+
60
+ eventCodeCandidate() // k3Rw9Q
61
+ isEventCode('k3Rw9Q') // true
62
+ advancedCodeKind('Ab12Cd') // 'item'
63
+ ADVANCED_SPACE.events // 54_298_494_784
64
+ ```
65
+
66
+ A product copies it alone with Sync 1's `advanced` export (`advanced.ts` with the `shapes.ts` and `words.ts` it reads against), or whole with `ids`.
67
+
42
68
  ## The scannable code
43
69
 
44
70
  `scanCode.ts` turns any six-character code into a strip of 16 × 4 cells (64 bits: a format byte, the code in five bytes, a two-byte check) and reads it back from a camera frame. Any single damaged cell fails the check rather than reading wrong. `scanCodePath` draws the strip as SVG; `readRgbaScanFrame`, `locateRgbaScanFrame` and `StableScan` read it from camera pixels, also when it is written on paper beside other marks.
@@ -48,8 +74,9 @@ codeKind('Ab12Cd') // 'item'
48
74
  - `src/shapes.ts` — the patterns (`ACCOUNT_CODE`, `PRODUCT_CODE`, `LIST_CODE`, `ITEM_CODE`, and `CODE_PATTERN` by kind), `codeKind`, `isCodeOf`, the candidates each shape is drawn from (`codeCandidate` by kind), and `nameLetters`
49
75
  - `src/space.ts` — `ID_SPACE`, how many codes of each kind there can be, and `idUsage`
50
76
  - `src/words.ts` — the words and digits a product's code must never spell
77
+ - `src/advanced.ts` — the expanded version: an event's code, `eventCodeCandidate`, `advancedCodeKind` and `ADVANCED_SPACE`
51
78
  - `src/scanCode.ts`, `src/scanCodeDrawing.ts` — the scannable code
52
- - `tests/` — the shapes, the counts and the scannable code
79
+ - `tests/` — the shapes, the counts, the expanded version and the scannable code
53
80
 
54
81
  ## Using it
55
82
 
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
1
  {
2
2
  "name": "@0001-labs/idv1",
3
- "version": "0.2.0",
4
- "description": "Short codes whose shape says what they name: accounts, products, lists and items, how many there can be, and the scannable code that carries them.",
3
+ "version": "0.3.1",
4
+ "description": "Short codes whose shape says what they name: accounts, products, lists and items, how many there can be, and the scannable code that carries them; and, under advanced, a code for every event.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/0001-labs/IDv1.git"
9
9
  },
10
- "type": "module",
11
- "exports": {
12
- ".": "./src/index.ts"
13
- },
14
10
  "files": [
15
11
  "src/",
16
12
  "README.md",
17
13
  "LICENSE"
18
14
  ],
15
+ "type": "module",
16
+ "exports": {
17
+ ".": "./src/index.ts",
18
+ "./advanced": "./src/advanced.ts"
19
+ },
19
20
  "devDependencies": {
20
21
  "@types/node": "^22.13.1",
21
22
  "typescript": "^6.0.3",
22
- "vite": "npm:@voidzero-dev/vite-plus-core@0.2.8",
23
- "vite-plus": "0.2.8",
24
- "vitest": "4.1.10"
23
+ "vite": "npm:@voidzero-dev/vite-plus-core@1.3.0",
24
+ "vite-plus": "1.3.0"
25
25
  },
26
26
  "devEngines": {
27
27
  "packageManager": {
@@ -0,0 +1,93 @@
1
+ /**
2
+ * IDv1 Advanced: the codes the four shapes leave free, for things there are
3
+ * far more of than accounts, lists or items, such as every event a product
4
+ * records or every visit to it. Still six characters of the same 62 letters
5
+ * and digits, capitals mattering, but in any order, so long as the code is
6
+ * none of the four shapes: `k3Rw9Q`, `7xP2mB`, `Q8z1aT`. One look still
7
+ * tells it apart from an account, a product, a list or an item, since none of
8
+ * them can be an event's code.
9
+ *
10
+ * - Six characters of 62 make 62⁶, 56.8 billion codes.
11
+ * - The four shapes, read as they are read (lists with small letters too),
12
+ * take 2.5 billion of them.
13
+ * - That leaves 54.3 billion for events, before the blocked words: 54 times
14
+ * as many as there are items, and enough for a product to give each event
15
+ * its own code for a long while.
16
+ *
17
+ * Nobody types an event's code and it is never scanned to open anything, so
18
+ * it is drawn uniformly, from the browser's or the server's cryptographic
19
+ * random numbers unless a test passes its own. Codes that spell a blocked word
20
+ * anywhere in them, capitals aside, are drawn again (`blockedCodeLetters` in
21
+ * words.ts), since an event's code may still be shown to someone.
22
+ */
23
+
24
+ import { CODE_PATTERN, codeKind, type CodeKind } from './shapes'
25
+ import { blockedCodeLetters } from './words'
26
+
27
+ /** Every kind, the four shapes and an event. */
28
+ export type AdvancedCodeKind = CodeKind | 'event'
29
+
30
+ /** Any six of IDv1's characters, the outline every code has. */
31
+ export const ANY_CODE = /^[A-Za-z0-9]{6}$/
32
+
33
+ const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
34
+
35
+ /** An event's code: six letters or digits that make none of the four shapes. */
36
+ export const isEventCode = (value: string): boolean => ANY_CODE.test(value) && codeKind(value) === null
37
+
38
+ /** What a code names by its shape alone, events included, or null when it is no IDv1 code. */
39
+ export function advancedCodeKind(value: string): AdvancedCodeKind | null {
40
+ return codeKind(value) ?? (ANY_CODE.test(value) ? 'event' : null)
41
+ }
42
+
43
+ /** Whether a code has the shape of the kind a product expects, events included: `isAdvancedCodeOf('event', 'k3Rw9Q')`. */
44
+ export const isAdvancedCodeOf = (kind: AdvancedCodeKind, value: string): boolean =>
45
+ kind === 'event' ? isEventCode(value) : CODE_PATTERN[kind].test(value)
46
+
47
+ type Random = () => number
48
+
49
+ /** A number in [0, 1) from the platform's cryptographic random numbers, Math.random where there are none. */
50
+ export function secureRandom(): number {
51
+ const crypto = globalThis.crypto
52
+ if (!crypto?.getRandomValues) return Math.random()
53
+ const value = new Uint32Array(1)
54
+ crypto.getRandomValues(value)
55
+ return value[0] / 2 ** 32
56
+ }
57
+
58
+ /**
59
+ * An event's code to try: six characters drawn uniformly, again while they
60
+ * make one of the four shapes or spell a blocked word. Whether one is free is
61
+ * the product's to check, as with every other kind; with 54 billion to draw
62
+ * from, two events a product keeps will rarely meet.
63
+ */
64
+ export function eventCodeCandidate(random: Random = secureRandom): string {
65
+ for (let attempt = 0; attempt < 1000; attempt++) {
66
+ let code = ''
67
+ for (let index = 0; index < 6; index++) code += ALPHABET[Math.floor(random() * ALPHABET.length)]
68
+ if (isEventCode(code) && !blockedCodeLetters(code)) return code
69
+ }
70
+ throw new Error('No event code could be drawn.')
71
+ }
72
+
73
+ /**
74
+ * How many codes each version of IDv1 has room for. The standard version is
75
+ * `ID_SPACE` in space.ts, by kind, as its candidates draw them; the expanded
76
+ * version counts every six-character code, and events are what the four
77
+ * shapes leave, read as they are read (a list's small letters included).
78
+ *
79
+ * - All six-character codes: 62⁶, 56,800,235,584.
80
+ * - The four shapes as read: 52²·10⁴ + 52⁴·10² + 10²·52⁴ + 52²·10²·(62² − 10²), 2,501,740,800.
81
+ * - Events: the rest, 54,298,494,784, before the blocked words.
82
+ */
83
+ export const ADVANCED_SPACE = (() => {
84
+ const letters = 52
85
+ const digits = 10
86
+ const all = (letters + digits) ** 6
87
+ const shapes =
88
+ letters ** 2 * digits ** 4 +
89
+ letters ** 4 * digits ** 2 +
90
+ digits ** 2 * letters ** 4 +
91
+ letters ** 2 * digits ** 2 * ((letters + digits) ** 2 - digits ** 2)
92
+ return { all, shapes, events: all - shapes } as const
93
+ })()
package/src/shapes.ts CHANGED
@@ -87,7 +87,48 @@ export function nameLetters(name: string, email = ''): string | null {
87
87
  /** An account's code: the name's letters (or two at random) and four digits. */
88
88
  export function accountCodeCandidate(letters: string | null, random: Random = Math.random): string {
89
89
  const start = letters ?? pick(LETTERS, random) + pick(LETTERS, random)
90
- return start + String(Math.floor(random() * 10000)).padStart(4, '0')
90
+ for (;;) {
91
+ const code = start + String(Math.floor(random() * 10000)).padStart(4, '0')
92
+ if (!isReservedAccountCode(code)) return code
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Account codes kept for one person or role in every product, never drawn for
98
+ * anyone else, so the same account is the same code everywhere and easy to
99
+ * leave out of a product's figures: Joachim is Jo4712 (jaa90@icloud.com) and
100
+ * Jo0001 (joachim@0001.dev), the onboarding test account, which walks through
101
+ * a product as a newcomer would, is On0001 (joachim+onboarding@0001.dev), and
102
+ * the test account is Te0001 (joachim+test@0001.dev). A product gives the code
103
+ * to the account whose email `reservedAccountCode` names.
104
+ */
105
+ export const RESERVED_ACCOUNT_CODES = { owner: 'Jo4712', studio: 'Jo0001', onboarding: 'On0001', test: 'Te0001' } as const satisfies Record<string, string>
106
+
107
+ export type ReservedRole = keyof typeof RESERVED_ACCOUNT_CODES
108
+
109
+ /** The email each reserved code belongs to, lower case. */
110
+ export const RESERVED_ACCOUNT_EMAILS = {
111
+ 'jaa90@icloud.com': 'owner',
112
+ 'joachim@0001.dev': 'studio',
113
+ 'joachim+onboarding@0001.dev': 'onboarding',
114
+ 'joachim+test@0001.dev': 'test',
115
+ } as const satisfies Record<string, ReservedRole>
116
+
117
+ /** Codes a role once had, still never drawn for anyone else: the onboarding test account was On4713 before On0001. */
118
+ export const FORMER_RESERVED_ACCOUNT_CODES = ['On4713'] as const
119
+
120
+ // Built on first use rather than when the module loads, so a page that only reads codes leaves the
121
+ // reserved ones out of its bundle: a bundler keeps a top-level `new Set(…)` in case it has side effects.
122
+ let reserved: Set<string> | undefined
123
+
124
+ /** Whether a code, in any of its capitals, is kept for a person or role, now or before. */
125
+ export const isReservedAccountCode = (code: string): boolean =>
126
+ (reserved ??= new Set([...Object.values(RESERVED_ACCOUNT_CODES), ...FORMER_RESERVED_ACCOUNT_CODES].map(kept => kept.toLowerCase()))).has(code.toLowerCase())
127
+
128
+ /** The code kept for an email, else null: a product gives it before drawing one. */
129
+ export function reservedAccountCode(email: string | null | undefined): string | null {
130
+ const key = email?.trim().toLowerCase() as keyof typeof RESERVED_ACCOUNT_EMAILS | undefined
131
+ return key && key in RESERVED_ACCOUNT_EMAILS ? RESERVED_ACCOUNT_CODES[RESERVED_ACCOUNT_EMAILS[key]] : null
91
132
  }
92
133
 
93
134
  /**