@dotrino/identity 0.137.1 → 0.138.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/identity",
3
- "version": "0.137.1",
3
+ "version": "0.138.0",
4
4
  "description": "Identidad y rating de usuarios compartidos entre apps de Dotrino (vault iframe + postMessage)",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/types/avatar.d.ts CHANGED
@@ -11,6 +11,28 @@
11
11
  export function avatarSvg(seed: unknown, { size }?: {
12
12
  size?: number;
13
13
  }): string;
14
+ /**
15
+ * El mismo identicon, pintado en un canvas 2D (`CanvasRenderingContext2D` u
16
+ * `OffscreenCanvasRenderingContext2D`). Existe para donde no se puede rasterizar un SVG:
17
+ * un service worker no decodifica `image/svg+xml`, y el icono de una extensión
18
+ * (`chrome.action.setIcon`) pide píxeles. Pinta en `[0, size]` desde el origen.
19
+ *
20
+ * @param {{ fillStyle: unknown, beginPath (): void, arc (x: number, y: number, r: number, a0: number, a1: number): void, fill (): void, fillRect (x: number, y: number, w: number, h: number): void, createLinearGradient (x0: number, y0: number, x1: number, y1: number): { addColorStop (o: number, c: string): void } }} ctx
21
+ * @param {unknown} seed
22
+ * @param {{ size?: number }} [opts]
23
+ */
24
+ export function avatarPaint(ctx: {
25
+ fillStyle: unknown;
26
+ beginPath(): void;
27
+ arc(x: number, y: number, r: number, a0: number, a1: number): void;
28
+ fill(): void;
29
+ fillRect(x: number, y: number, w: number, h: number): void;
30
+ createLinearGradient(x0: number, y0: number, x1: number, y1: number): {
31
+ addColorStop(o: number, c: string): void;
32
+ };
33
+ }, seed: unknown, { size }?: {
34
+ size?: number;
35
+ }): void;
14
36
  /**
15
37
  * El avatar como data-URI listo para `<img src>` o `background-image`.
16
38
  * @param {unknown} seed
package/vault/avatar.js CHANGED
@@ -30,6 +30,29 @@ function _hashSeed (s) {
30
30
  return { h: h >>> 0, bytes }
31
31
  }
32
32
 
33
+ /**
34
+ * La GEOMETRÍA del identicon: colores y celdas de la rejilla 5×5. Es la única fuente: el SVG
35
+ * y el canvas (`avatarPaint`) se dibujan de aquí, así que no pueden salir distintos.
36
+ *
37
+ * @param {unknown} seed
38
+ * @returns {{ h: number, fg: string, bg1: string, bg2: string, cells: Array<[number, number]> }}
39
+ * `cells`: pares `[columna, fila]` encendidos, en una rejilla de 5×5.
40
+ */
41
+ function _avatarModel (seed) {
42
+ const { h, bytes } = _hashSeed(String(seed || 'dotrino'))
43
+ const hue = h % 360
44
+ const hue2 = (hue + 40) % 360
45
+ /** @type {Array<[number, number]>} */
46
+ const cells = []
47
+ for (let col = 0; col < 3; col++) {
48
+ for (let row = 0; row < 5; row++) {
49
+ if (!(bytes[col * 5 + row] & 1)) continue
50
+ for (const c of (col === 2 ? [2] : [col, 4 - col])) cells.push([c, row])
51
+ }
52
+ }
53
+ return { h, fg: `hsl(${hue} 62% 46%)`, bg1: `hsl(${hue} 48% 95%)`, bg2: `hsl(${hue2} 48% 90%)`, cells }
54
+ }
55
+
33
56
  /**
34
57
  * Identicon determinista de una semilla (normalmente el pubkey): así cada perfil
35
58
  * nace con imagen sin que el usuario tenga que subir nada. Síncrono → usable
@@ -41,22 +64,11 @@ function _hashSeed (s) {
41
64
  * @returns {string}
42
65
  */
43
66
  export function avatarSvg (seed, { size = 80 } = {}) {
44
- const { h, bytes } = _hashSeed(String(seed || 'dotrino'))
45
- const hue = h % 360
46
- const hue2 = (hue + 40) % 360
47
- const fg = `hsl(${hue} 62% 46%)`
48
- const bg1 = `hsl(${hue} 48% 95%)`
49
- const bg2 = `hsl(${hue2} 48% 90%)`
50
- const cells = 5
51
- const unit = size / cells
67
+ const { h, fg, bg1, bg2, cells } = _avatarModel(seed)
68
+ const unit = size / 5
52
69
  let rects = ''
53
- for (let col = 0; col < 3; col++) {
54
- for (let row = 0; row < cells; row++) {
55
- if (!(bytes[col * cells + row] & 1)) continue
56
- for (const c of (col === 2 ? [2] : [col, cells - 1 - col])) {
57
- rects += `<rect x="${(c * unit).toFixed(2)}" y="${(row * unit).toFixed(2)}" width="${unit.toFixed(2)}" height="${unit.toFixed(2)}"/>`
58
- }
59
- }
70
+ for (const [c, row] of cells) {
71
+ rects += `<rect x="${(c * unit).toFixed(2)}" y="${(row * unit).toFixed(2)}" width="${unit.toFixed(2)}" height="${unit.toFixed(2)}"/>`
60
72
  }
61
73
  const id = 'g' + (h % 100000)
62
74
  return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}">` +
@@ -65,6 +77,31 @@ export function avatarSvg (seed, { size = 80 } = {}) {
65
77
  `<g fill="${fg}" transform="translate(${(size * 0.12).toFixed(2)} ${(size * 0.12).toFixed(2)}) scale(0.76)">${rects}</g></svg>`
66
78
  }
67
79
 
80
+ /**
81
+ * El mismo identicon, pintado en un canvas 2D (`CanvasRenderingContext2D` u
82
+ * `OffscreenCanvasRenderingContext2D`). Existe para donde no se puede rasterizar un SVG:
83
+ * un service worker no decodifica `image/svg+xml`, y el icono de una extensión
84
+ * (`chrome.action.setIcon`) pide píxeles. Pinta en `[0, size]` desde el origen.
85
+ *
86
+ * @param {{ fillStyle: unknown, beginPath (): void, arc (x: number, y: number, r: number, a0: number, a1: number): void, fill (): void, fillRect (x: number, y: number, w: number, h: number): void, createLinearGradient (x0: number, y0: number, x1: number, y1: number): { addColorStop (o: number, c: string): void } }} ctx
87
+ * @param {unknown} seed
88
+ * @param {{ size?: number }} [opts]
89
+ */
90
+ export function avatarPaint (ctx, seed, { size = 80 } = {}) {
91
+ const { fg, bg1, bg2, cells } = _avatarModel(seed)
92
+ const grad = ctx.createLinearGradient(0, 0, size, size)
93
+ grad.addColorStop(0, bg1)
94
+ grad.addColorStop(1, bg2)
95
+ ctx.fillStyle = grad
96
+ ctx.beginPath()
97
+ ctx.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2)
98
+ ctx.fill()
99
+ ctx.fillStyle = fg
100
+ const unit = (size / 5) * 0.76
101
+ const off = size * 0.12
102
+ for (const [c, row] of cells) ctx.fillRect(off + c * unit, off + row * unit, unit, unit)
103
+ }
104
+
68
105
  /**
69
106
  * El avatar como data-URI listo para `<img src>` o `background-image`.
70
107
  * @param {unknown} seed
@@ -22,7 +22,7 @@ export const MANIFESTS = Object.freeze({
22
22
  "env": {
23
23
  "repo": "imdotrino/dotrino-vault",
24
24
  "npm": "@dotrino/env",
25
- "current": "0.121.1",
25
+ "current": "0.121.2",
26
26
  "protocol": 1,
27
27
  "requires": {
28
28
  "vault": ">=0.118.0"
@@ -31,7 +31,7 @@ export const MANIFESTS = Object.freeze({
31
31
  "identity": {
32
32
  "repo": "imdotrino/dotrino-identity",
33
33
  "npm": "@dotrino/identity",
34
- "current": "0.137.0",
34
+ "current": "0.137.1",
35
35
  "protocol": 1,
36
36
  "requires": {
37
37
  "proxy-client": ">=0.20.0"
@@ -273,7 +273,7 @@ export const MANIFESTS = Object.freeze({
273
273
  "vault": {
274
274
  "repo": "imdotrino/dotrino-vault",
275
275
  "npm": "@dotrino/vault",
276
- "current": "0.121.1",
276
+ "current": "0.121.2",
277
277
  "protocol": 1,
278
278
  "requires": {
279
279
  "vaultd": ">=0.105.0",
@@ -289,7 +289,7 @@ export const MANIFESTS = Object.freeze({
289
289
  "vaultd": {
290
290
  "repo": "imdotrino/dotrino-vault",
291
291
  "npm": "@dotrino/vaultd",
292
- "current": "0.193.1",
292
+ "current": "0.193.2",
293
293
  "protocol": 1,
294
294
  "requires": {
295
295
  "identity": ">=0.136.0",
@@ -354,7 +354,7 @@ export const MANIFESTS = Object.freeze({
354
354
  },
355
355
  "app": {
356
356
  "repo": "imdotrino/dotrino-app",
357
- "current": "0.4.30",
357
+ "current": "0.4.33",
358
358
  "protocol": 1,
359
359
  "requires": {
360
360
  "proxy": "1.6.0+",
@@ -364,7 +364,7 @@ export const MANIFESTS = Object.freeze({
364
364
  },
365
365
  "app-ios": {
366
366
  "repo": "imdotrino/dotrino-app",
367
- "current": "0.4.19",
367
+ "current": "0.4.33",
368
368
  "protocol": 1,
369
369
  "requires": {
370
370
  "proxy": "1.6.0+",