@0001-labs/idv1 0.1.0 → 0.3.0
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 +53 -3
- package/package.json +4 -3
- package/src/advanced.ts +93 -0
- package/src/shapes.ts +59 -1
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,55 @@ 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.
|
|
19
|
+
|
|
20
|
+
## Which shape for what you build
|
|
21
|
+
|
|
22
|
+
Pick the shape by what the thing is, not by what a product calls it, so the same kind of thing has the same shape in every product and a code is easy to place on sight:
|
|
23
|
+
|
|
24
|
+
| What it is | Shape | Called in products | Example |
|
|
25
|
+
|---|---|---|---|
|
|
26
|
+
| A person | account | user, member, owner | `Jo4712` |
|
|
27
|
+
| Something that holds things | list | list, category, board, collection, folder, project | `12ABCD` |
|
|
28
|
+
| One thing kept | item | item, clip, listing, note, a piece of inspiration | `Ab12Cd` |
|
|
29
|
+
| A thing in the world with a code printed on it | product | product, tag, sticker | `AbCd12` |
|
|
30
|
+
|
|
31
|
+
Two products agree without asking each other: a category in one is a list's shape like a list in another, and a piece of inspiration is an item's shape like an item anywhere.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { codeCandidate, codeKind, isCodeOf, nameLetters } from '@0001-labs/idv1'
|
|
35
|
+
|
|
36
|
+
codeCandidate('list') // a category: 47KQWE
|
|
37
|
+
codeCandidate('item') // a piece: Ab12Cd
|
|
38
|
+
codeCandidate('account', { letters: nameLetters('Leo Andersen') }) // a person: Le0125
|
|
39
|
+
isCodeOf('list', '47KQWE') // true
|
|
40
|
+
codeKind('Ab12Cd') // 'item'
|
|
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`.
|
|
18
67
|
|
|
19
68
|
## The scannable code
|
|
20
69
|
|
|
@@ -22,11 +71,12 @@ Short codes whose shape says what they name. Every code is six characters, and o
|
|
|
22
71
|
|
|
23
72
|
## What is in here
|
|
24
73
|
|
|
25
|
-
- `src/shapes.ts` — the patterns (`ACCOUNT_CODE`, `PRODUCT_CODE`, `LIST_CODE`, `ITEM_CODE`), `codeKind`, the candidates each shape is drawn from, and `nameLetters`
|
|
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`
|
|
26
75
|
- `src/space.ts` — `ID_SPACE`, how many codes of each kind there can be, and `idUsage`
|
|
27
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`
|
|
28
78
|
- `src/scanCode.ts`, `src/scanCodeDrawing.ts` — the scannable code
|
|
29
|
-
- `tests/` — the shapes, the counts and the scannable code
|
|
79
|
+
- `tests/` — the shapes, the counts, the expanded version and the scannable code
|
|
30
80
|
|
|
31
81
|
## Using it
|
|
32
82
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0001-labs/idv1",
|
|
3
|
-
"version": "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.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; and, under advanced, a code for every event.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
|
-
".": "./src/index.ts"
|
|
12
|
+
".": "./src/index.ts",
|
|
13
|
+
"./advanced": "./src/advanced.ts"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
16
|
"src/",
|
package/src/advanced.ts
ADDED
|
@@ -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,45 @@ 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
|
-
|
|
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
|
+
const RESERVED = new Set([...Object.values(RESERVED_ACCOUNT_CODES), ...FORMER_RESERVED_ACCOUNT_CODES].map(code => code.toLowerCase()))
|
|
121
|
+
|
|
122
|
+
/** Whether a code, in any of its capitals, is kept for a person or role, now or before. */
|
|
123
|
+
export const isReservedAccountCode = (code: string): boolean => RESERVED.has(code.toLowerCase())
|
|
124
|
+
|
|
125
|
+
/** The code kept for an email, else null: a product gives it before drawing one. */
|
|
126
|
+
export function reservedAccountCode(email: string | null | undefined): string | null {
|
|
127
|
+
const key = email?.trim().toLowerCase() as keyof typeof RESERVED_ACCOUNT_EMAILS | undefined
|
|
128
|
+
return key && key in RESERVED_ACCOUNT_EMAILS ? RESERVED_ACCOUNT_CODES[RESERVED_ACCOUNT_EMAILS[key]] : null
|
|
91
129
|
}
|
|
92
130
|
|
|
93
131
|
/**
|
|
@@ -118,6 +156,26 @@ export function itemCodeCandidate(random: Random = Math.random): string {
|
|
|
118
156
|
return start + (/\d/.test(first) && /\d/.test(second) ? pick(LETTERS, random) : first) + second
|
|
119
157
|
}
|
|
120
158
|
|
|
159
|
+
/** The pattern each kind is read by. */
|
|
160
|
+
export const CODE_PATTERN: Record<CodeKind, RegExp> = { account: ACCOUNT_CODE, product: PRODUCT_CODE, list: LIST_CODE, item: ITEM_CODE }
|
|
161
|
+
|
|
162
|
+
/** Whether a code has the shape of the kind a product expects: `isCodeOf('list', '12ABCD')`. */
|
|
163
|
+
export const isCodeOf = (kind: CodeKind, value: string): boolean => CODE_PATTERN[kind].test(value)
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A code to try for any kind, so a product picks the shape by what it names
|
|
167
|
+
* and never by its pattern: `codeCandidate('list')` for a category,
|
|
168
|
+
* `codeCandidate('item')` for one thing kept. An account takes the name's
|
|
169
|
+
* letters (`nameLetters`), or two at random.
|
|
170
|
+
*/
|
|
171
|
+
export function codeCandidate(kind: CodeKind, options: { letters?: string | null; random?: Random } = {}): string {
|
|
172
|
+
const random = options.random ?? Math.random
|
|
173
|
+
if (kind === 'account') return accountCodeCandidate(options.letters ?? null, random)
|
|
174
|
+
if (kind === 'product') return productCodeCandidate(random)
|
|
175
|
+
if (kind === 'list') return listCodeCandidate(random)
|
|
176
|
+
return itemCodeCandidate(random)
|
|
177
|
+
}
|
|
178
|
+
|
|
121
179
|
/**
|
|
122
180
|
* Every way a name-shaped code's two letters can be written, for a product
|
|
123
181
|
* that must keep a code free in all its capitals (Jo4712, jo4712, JO4712, jO4712).
|