@talismn/orb 0.3.1 → 0.3.3

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.
Files changed (31) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/declarations/src/components/TalismanOrb.d.ts +1 -19
  3. package/dist/declarations/src/components/TalismanOrbLogo.d.ts +5 -0
  4. package/dist/declarations/src/components/TalismanOrbRectangle.d.ts +3 -0
  5. package/dist/declarations/src/components/index.d.ts +3 -0
  6. package/dist/declarations/src/components/types.d.ts +6 -0
  7. package/dist/declarations/src/components/useTalismanOrb.d.ts +10 -0
  8. package/dist/declarations/src/index.d.ts +1 -0
  9. package/dist/declarations/src/util/getTalismanOrbDataUrl.d.ts +4 -0
  10. package/dist/declarations/src/util/index.d.ts +1 -0
  11. package/dist/talismn-orb.cjs.dev.js +62 -105
  12. package/dist/talismn-orb.cjs.prod.js +62 -105
  13. package/dist/talismn-orb.esm.js +61 -106
  14. package/package.json +3 -4
  15. package/src/components/TalismanOrb.tsx +6 -167
  16. package/src/components/TalismanOrbLogo.tsx +60 -0
  17. package/src/components/TalismanOrbRectangle.tsx +40 -0
  18. package/src/components/index.ts +3 -0
  19. package/src/components/types.ts +1 -0
  20. package/src/components/useTalismanOrb.ts +70 -0
  21. package/src/index.ts +1 -0
  22. package/src/util/getTalismanOrbDataUrl.tsx +14 -0
  23. package/src/util/index.ts +1 -0
  24. package/dist/declarations/src/util/lib/ethAddress.d.ts +0 -2
  25. package/dist/declarations/src/util/lib/index.d.ts +0 -2
  26. package/dist/declarations/src/util/lib/subAddress.d.ts +0 -1
  27. package/dist/declarations/src/util/normalizeAddress.d.ts +0 -1
  28. package/src/util/lib/ethAddress.ts +0 -29
  29. package/src/util/lib/index.ts +0 -2
  30. package/src/util/lib/subAddress.ts +0 -39
  31. package/src/util/normalizeAddress.ts +0 -5
@@ -1,68 +1,9 @@
1
+ import { getAccountPlatformFromAddress, normalizeAddress } from '@talismn/crypto';
1
2
  import md5 from 'blueimp-md5';
2
3
  import Color from 'color';
3
4
  import { useId, useMemo } from 'react';
4
- import { keccak_256 } from '@noble/hashes/sha3';
5
- import { blake2b } from '@noble/hashes/blake2b';
6
- import { base58 } from '@scure/base';
7
- import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
8
-
9
- // inspired from https://github.com/wevm/viem/blob/main/src/utils/address/getAddress.ts
10
-
11
- const TEXT_ENCODER = new TextEncoder();
12
- const isEthAddress = address => /^0x[a-fA-F0-9]{40}$/.test(address);
13
- const normalizeEthAddress = address => {
14
- if (!isEthAddress(address)) throw new Error(`Invalid Ethereum address ${address}`);
15
- const rawAddress = address.toLowerCase().substring(2);
16
- const bytes = TEXT_ENCODER.encode(rawAddress);
17
- const hash = keccak_256(bytes);
18
-
19
- // apply checksum
20
- const csAddress = rawAddress.split("");
21
- for (let i = 0; i < 40; i += 2) {
22
- if (hash[i >> 1] >> 4 >= 8 && address[i]) {
23
- csAddress[i] = csAddress[i].toUpperCase();
24
- }
25
- if ((hash[i >> 1] & 0x0f) >= 8 && address[i + 1]) {
26
- csAddress[i + 1] = csAddress[i + 1].toUpperCase();
27
- }
28
- }
29
- return `0x${csAddress.join("")}`;
30
- };
31
-
32
- // inspired from https://github.com/polkadot-api/polkadot-api/blob/main/packages/substrate-bindings/src/codecs/scale/AccountId.ts
33
-
34
- const SS58_PREFIX = new TextEncoder().encode("SS58PRE");
35
- const SS58_FORMAT = 42;
36
- const CHECKSUM_LENGTH = 2;
37
- const VALID_BYTES_LENGTH = [32, 33];
38
- const encode = publicKey => {
39
- const prefixBytes = Uint8Array.of(SS58_FORMAT);
40
- const checksum = blake2b(Uint8Array.of(...SS58_PREFIX, ...prefixBytes, ...publicKey), {
41
- dkLen: 64
42
- }).subarray(0, CHECKSUM_LENGTH);
43
- return base58.encode(Uint8Array.of(...prefixBytes, ...publicKey, ...checksum));
44
- };
45
- const decode = address => {
46
- const decoded = base58.decode(address);
47
- const prefixBytes = decoded.subarray(0, decoded[0] & 0b0100_0000 ? 2 : 1);
48
- const publicKey = decoded.subarray(prefixBytes.length, decoded.length - CHECKSUM_LENGTH);
49
- if (!VALID_BYTES_LENGTH.includes(publicKey.length)) throw new Error("Invalid public key length");
50
- const checksum = decoded.subarray(prefixBytes.length + publicKey.length);
51
- const expectedChecksum = blake2b(Uint8Array.of(...SS58_PREFIX, ...prefixBytes, ...publicKey), {
52
- dkLen: 64
53
- }).subarray(0, CHECKSUM_LENGTH);
54
- if (checksum[0] !== expectedChecksum[0] || checksum[1] !== expectedChecksum[1]) throw new Error("Invalid checksum");
55
- return publicKey.slice();
56
- };
57
- const normalizeSubAddress = address => {
58
- // source address might be encoded with a different prefix than 42
59
- // decode then reencode with prefix 42
60
- return encode(decode(address));
61
- };
62
-
63
- const normalizeAddress = address => {
64
- return isEthAddress(address) ? normalizeEthAddress(address) : normalizeSubAddress(address);
65
- };
5
+ import { jsx, jsxs } from 'react/jsx-runtime';
6
+ import ReactDOMServer from 'react-dom/server';
66
7
 
67
8
  const djb2 = str => {
68
9
  let hash = 5381;
@@ -80,12 +21,17 @@ const rotateText = (text, nbChars = 0) => text.slice(nbChars) + text.slice(0, nb
80
21
  const useTalismanOrb = seed => {
81
22
  const id = useId();
82
23
  return useMemo(() => {
83
- const iconType = seed?.startsWith("0x") ? "ethereum" : "substrate";
84
24
  try {
25
+ // those break if seed is empty or an invalid address
26
+
27
+ // eslint-disable-next-line no-var
28
+ var platform = getAccountPlatformFromAddress(seed);
29
+
85
30
  // seed may be specific to a ss58 prefix, get the base address
86
31
  // eslint-disable-next-line no-var
87
32
  var address = normalizeAddress(seed);
88
33
  } catch (err) {
34
+ platform = "polkadot";
89
35
  address = seed;
90
36
  }
91
37
 
@@ -113,15 +59,15 @@ const useTalismanOrb = seed => {
113
59
  transform: `rotate(${rotation} 32 32)`,
114
60
  cx: dotX,
115
61
  cy: dotY,
116
- iconType
62
+ platform
117
63
  };
118
64
  }, [id, seed]);
119
65
  };
66
+
120
67
  const TalismanOrbLogo = ({
121
- id,
122
- iconType
68
+ platform
123
69
  }) => {
124
- switch (iconType) {
70
+ switch (platform) {
125
71
  case "ethereum":
126
72
  return /*#__PURE__*/jsxs("g", {
127
73
  opacity: "0.75",
@@ -135,47 +81,46 @@ const TalismanOrbLogo = ({
135
81
  fill: "white"
136
82
  })]
137
83
  });
138
- case "substrate":
139
- return /*#__PURE__*/jsxs(Fragment, {
140
- children: [/*#__PURE__*/jsxs("g", {
141
- clipPath: `url(#${id}_1751_2030)`,
142
- opacity: "0.75",
143
- transform: "scale(2.2) translate(4.5 3.9)",
144
- className: "orb-type",
145
- children: [/*#__PURE__*/jsx("path", {
146
- d: "M9.99937 4.4612C12.1176 4.4612 13.8347 3.46253 13.8347 2.2306C13.8347 0.998674 12.1176 0 9.99937 0C7.88119 0 6.16406 0.998674 6.16406 2.2306C6.16406 3.46253 7.88119 4.4612 9.99937 4.4612Z",
147
- fill: "white"
148
- }), /*#__PURE__*/jsx("path", {
149
- d: "M9.99937 21.2683C12.1176 21.2683 13.8347 20.2697 13.8347 19.0377C13.8347 17.8058 12.1176 16.8071 9.99937 16.8071C7.88119 16.8071 6.16406 17.8058 6.16406 19.0377C6.16406 20.2697 7.88119 21.2683 9.99937 21.2683Z",
150
- fill: "white"
151
- }), /*#__PURE__*/jsx("path", {
152
- d: "M4.65427 7.54892C5.71336 5.71457 5.70649 3.72787 4.63892 3.11149C3.57135 2.49511 1.84735 3.48246 0.788259 5.31681C-0.270832 7.15115 -0.263958 9.13786 0.803612 9.75424C1.87118 10.3706 3.59518 9.38326 4.65427 7.54892Z",
153
- fill: "white"
154
- }), /*#__PURE__*/jsx("path", {
155
- d: "M19.2083 15.9515C20.2674 14.1171 20.2611 12.1307 19.1943 11.5148C18.1274 10.8988 16.404 11.8865 15.3449 13.7209C14.2858 15.5552 14.2921 17.5416 15.3589 18.1575C16.4258 18.7735 18.1492 17.7858 19.2083 15.9515Z",
156
- fill: "white"
157
- }), /*#__PURE__*/jsx("path", {
158
- d: "M4.6399 18.1571C5.70747 17.5407 5.71434 15.554 4.65525 13.7196C3.59616 11.8853 1.87216 10.8979 0.804589 11.5143C-0.262981 12.1307 -0.269855 14.1174 0.789235 15.9517C1.84833 17.7861 3.57233 18.7734 4.6399 18.1571Z",
159
- fill: "white"
160
- }), /*#__PURE__*/jsx("path", {
161
- d: "M19.1952 9.75475C20.2621 9.13878 20.2684 7.15241 19.2093 5.31807C18.1502 3.48372 16.4268 2.49603 15.3599 3.11199C14.2931 3.72796 14.2868 5.71433 15.3459 7.54867C16.405 9.38302 18.1284 10.3707 19.1952 9.75475Z",
162
- fill: "white"
163
- })]
164
- }), /*#__PURE__*/jsx("defs", {
165
- children: /*#__PURE__*/jsx("clipPath", {
166
- id: `${id}_1751_2030`,
167
- children: /*#__PURE__*/jsx("rect", {
168
- width: "20",
169
- height: "21.2699",
170
- fill: "white"
171
- })
172
- })
84
+ case "polkadot":
85
+ return /*#__PURE__*/jsxs("g", {
86
+ opacity: "0.75",
87
+ transform: "scale(2.2) translate(4.5 3.9)",
88
+ className: "orb-type",
89
+ children: [/*#__PURE__*/jsx("path", {
90
+ d: "M9.99937 4.4612C12.1176 4.4612 13.8347 3.46253 13.8347 2.2306C13.8347 0.998674 12.1176 0 9.99937 0C7.88119 0 6.16406 0.998674 6.16406 2.2306C6.16406 3.46253 7.88119 4.4612 9.99937 4.4612Z",
91
+ fill: "white"
92
+ }), /*#__PURE__*/jsx("path", {
93
+ d: "M9.99937 21.2683C12.1176 21.2683 13.8347 20.2697 13.8347 19.0377C13.8347 17.8058 12.1176 16.8071 9.99937 16.8071C7.88119 16.8071 6.16406 17.8058 6.16406 19.0377C6.16406 20.2697 7.88119 21.2683 9.99937 21.2683Z",
94
+ fill: "white"
95
+ }), /*#__PURE__*/jsx("path", {
96
+ d: "M4.65427 7.54892C5.71336 5.71457 5.70649 3.72787 4.63892 3.11149C3.57135 2.49511 1.84735 3.48246 0.788259 5.31681C-0.270832 7.15115 -0.263958 9.13786 0.803612 9.75424C1.87118 10.3706 3.59518 9.38326 4.65427 7.54892Z",
97
+ fill: "white"
98
+ }), /*#__PURE__*/jsx("path", {
99
+ d: "M19.2083 15.9515C20.2674 14.1171 20.2611 12.1307 19.1943 11.5148C18.1274 10.8988 16.404 11.8865 15.3449 13.7209C14.2858 15.5552 14.2921 17.5416 15.3589 18.1575C16.4258 18.7735 18.1492 17.7858 19.2083 15.9515Z",
100
+ fill: "white"
101
+ }), /*#__PURE__*/jsx("path", {
102
+ d: "M4.6399 18.1571C5.70747 17.5407 5.71434 15.554 4.65525 13.7196C3.59616 11.8853 1.87216 10.8979 0.804589 11.5143C-0.262981 12.1307 -0.269855 14.1174 0.789235 15.9517C1.84833 17.7861 3.57233 18.7734 4.6399 18.1571Z",
103
+ fill: "white"
104
+ }), /*#__PURE__*/jsx("path", {
105
+ d: "M19.1952 9.75475C20.2621 9.13878 20.2684 7.15241 19.2093 5.31807C18.1502 3.48372 16.4268 2.49603 15.3599 3.11199C14.2931 3.72796 14.2868 5.71433 15.3459 7.54867C16.405 9.38302 18.1284 10.3707 19.1952 9.75475Z",
106
+ fill: "white"
173
107
  })]
174
108
  });
109
+ case "solana":
110
+ return /*#__PURE__*/jsx("g", {
111
+ opacity: "0.75",
112
+ className: "orb-type",
113
+ transform: "scale(0.45) translate(37.5, 37.5)",
114
+ children: /*#__PURE__*/jsx("path", {
115
+ d: "M70.6648 50.1769L58.9393 62.775C58.6844 63.0486 58.376 63.2668 58.0332 63.4159C57.6905 63.5651 57.3208 63.6419 56.9472 63.6417H1.36128C1.09605 63.6417 0.836598 63.5641 0.614804 63.4184C0.393011 63.2727 0.218536 63.0652 0.112817 62.8215C0.00709765 62.5779 -0.0252603 62.3085 0.0197186 62.0467C0.0646974 61.7848 0.185054 61.5418 0.366 61.3476L12.1006 48.7496C12.3548 48.4766 12.6623 48.2589 13.0039 48.1098C13.3455 47.9607 13.714 47.8834 14.0866 47.8828H69.6695C69.9348 47.8828 70.1942 47.9604 70.416 48.1062C70.6378 48.2519 70.8123 48.4593 70.918 48.703C71.0237 48.9467 71.0561 49.216 71.0111 49.4778C70.9661 49.7397 70.8458 49.9827 70.6648 50.1769V50.1769ZM58.9393 24.8081C58.6844 24.5345 58.376 24.3163 58.0332 24.1672C57.6905 24.0181 57.3208 23.9412 56.9472 23.9414H1.36128C1.09605 23.9414 0.836598 24.019 0.614804 24.1647C0.393011 24.3105 0.218536 24.5179 0.112817 24.7616C0.00709765 25.0053 -0.0252603 25.2746 0.0197186 25.5364C0.0646974 25.7983 0.185054 26.0413 0.366 26.2355L12.1006 38.8336C12.3548 39.1065 12.6623 39.3242 13.0039 39.4733C13.3455 39.6224 13.714 39.6997 14.0866 39.7003H69.6695C69.9348 39.7003 70.1942 39.6227 70.416 39.477C70.6378 39.3313 70.8123 39.1238 70.918 38.8801C71.0237 38.6365 71.0561 38.3671 71.0111 38.1053C70.9661 37.8434 70.8458 37.6004 70.6648 37.4062L58.9393 24.8081ZM1.36128 15.7589H56.9472C57.3208 15.7591 57.6905 15.6822 58.0332 15.5331C58.376 15.384 58.6844 15.1658 58.9393 14.8922L70.6648 2.29413C70.8458 2.09986 70.9661 1.85688 71.0111 1.59502C71.0561 1.33317 71.0237 1.06385 70.918 0.820169C70.8123 0.576485 70.6378 0.369047 70.416 0.223341C70.1942 0.0776352 69.9348 8.97308e-06 69.6695 0L14.0866 0C13.714 0.000635258 13.3455 0.077888 13.0039 0.226975C12.6623 0.376063 12.3548 0.593811 12.1006 0.866739L0.369025 13.4648C0.188254 13.6588 0.0679503 13.9016 0.0228694 14.1631C-0.0222115 14.4247 0.00988974 14.6938 0.115236 14.9373C0.220583 15.1809 0.394595 15.3884 0.615931 15.5343C0.837268 15.6802 1.09631 15.7583 1.36128 15.7589V15.7589Z",
116
+ fill: "white"
117
+ })
118
+ });
175
119
  default:
176
120
  return null;
177
121
  }
178
122
  };
123
+
179
124
  const TalismanOrb = ({
180
125
  seed,
181
126
  width = "1em",
@@ -190,7 +135,7 @@ const TalismanOrb = ({
190
135
  glowColor,
191
136
  cx,
192
137
  cy,
193
- iconType
138
+ platform
194
139
  } = useTalismanOrb(seed);
195
140
  return /*#__PURE__*/jsxs("svg", {
196
141
  width: width,
@@ -244,12 +189,12 @@ const TalismanOrb = ({
244
189
  opacity: 0.7
245
190
  })]
246
191
  }), /*#__PURE__*/jsx(TalismanOrbLogo, {
247
- id: id,
248
- iconType: iconType
192
+ platform: platform
249
193
  })]
250
194
  })]
251
195
  });
252
196
  };
197
+
253
198
  const TalismanOrbRectangle = ({
254
199
  width,
255
200
  height,
@@ -322,4 +267,14 @@ const TalismanOrbRectangle = ({
322
267
  });
323
268
  };
324
269
 
325
- export { TalismanOrb, TalismanOrbRectangle, useTalismanOrb };
270
+ const getTalismanOrbDataUrl = address => {
271
+ // render the TalismanOrb component and output the SVG as text
272
+ const svg = ReactDOMServer.renderToStaticMarkup(/*#__PURE__*/jsx(TalismanOrb, {
273
+ seed: address
274
+ }));
275
+
276
+ // convert to data url
277
+ return `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`;
278
+ };
279
+
280
+ export { TalismanOrb, TalismanOrbRectangle, getTalismanOrbDataUrl, useTalismanOrb };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/orb",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "author": "Talisman",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "publishConfig": {
@@ -16,10 +16,9 @@
16
16
  "main": "dist/talismn-orb.cjs.js",
17
17
  "module": "dist/talismn-orb.esm.js",
18
18
  "dependencies": {
19
- "@noble/hashes": "1.8.0",
20
- "@scure/base": "1.2.6",
21
19
  "blueimp-md5": "2.19.0",
22
- "color": "4.2.3"
20
+ "color": "4.2.3",
21
+ "@talismn/crypto": "0.2.2"
23
22
  },
24
23
  "devDependencies": {
25
24
  "@types/react": "18.3.12",
@@ -1,133 +1,8 @@
1
- import md5 from "blueimp-md5"
2
- import Color from "color"
3
- import { FC, useId, useMemo } from "react"
1
+ import { FC } from "react"
4
2
 
5
- import { normalizeAddress } from "../util/normalizeAddress"
6
-
7
- const djb2 = (str: string) => {
8
- let hash = 5381
9
- for (let i = 0; i < str.length; i++) hash = (hash << 5) + hash + str.charCodeAt(i)
10
- return hash
11
- }
12
-
13
- const valueFromHash = (hash: string, max: number) => {
14
- return (max + djb2(hash)) % max
15
- }
16
-
17
- const colorFromHash = (hash: string) => {
18
- const hue = valueFromHash(hash, 360)
19
- return Color.hsv(hue, 100, 100)
20
- }
21
-
22
- const rotateText = (text: string, nbChars = 0) => text.slice(nbChars) + text.slice(0, nbChars)
23
-
24
- type TalismanOrbProps = { seed: string; width?: number; height?: number; className?: string }
25
- type LogoIconType = "ethereum" | "substrate"
26
-
27
- export const useTalismanOrb = (seed: string) => {
28
- const id = useId()
29
-
30
- return useMemo(() => {
31
- const iconType: LogoIconType = seed?.startsWith("0x") ? "ethereum" : "substrate"
32
- try {
33
- // seed may be specific to a ss58 prefix, get the base address
34
- // eslint-disable-next-line no-var
35
- var address = normalizeAddress(seed)
36
- } catch (err) {
37
- address = seed
38
- }
39
-
40
- // derive 3 hashs from the seed, used to generate the 3 colors
41
- const hash1 = md5(address)
42
- const hash2 = rotateText(hash1, 1)
43
- const hash3 = rotateText(hash1, 2)
44
-
45
- // the 2 darkest ones will be used as gradient BG
46
- // the lightest one will be used as gradient circle, to mimic a 3D lighting effect
47
- const colors = [colorFromHash(hash1), colorFromHash(hash2), colorFromHash(hash3)].sort(
48
- (c1, c2) => c1.lightness() - c2.lightness(),
49
- )
50
-
51
- // random location in top left corner, avoid beeing to close from the center
52
- const dotX = 10 + valueFromHash(hash1, 10)
53
- const dotY = 10 + valueFromHash(hash2, 10)
54
-
55
- // global rotation
56
- const rotation = valueFromHash(hash1, 360)
57
-
58
- return {
59
- id, //multiple avatars should cohabit on the same page
60
- bgColor1: colors[0].hex(),
61
- bgColor2: colors[1].hex(),
62
- glowColor: colors[2].hex(),
63
- transform: `rotate(${rotation} 32 32)`,
64
- cx: dotX,
65
- cy: dotY,
66
- iconType,
67
- }
68
- }, [id, seed])
69
- }
70
-
71
- const TalismanOrbLogo = ({ id, iconType }: { id: string; iconType?: LogoIconType }) => {
72
- switch (iconType) {
73
- case "ethereum":
74
- return (
75
- <g opacity="0.75" transform="scale(0.7) translate(14 14)" className="orb-type">
76
- <path
77
- d="M12.8101 32.76L32.0001 44.62L51.1901 32.76L32.0001 -0.0699997L12.8101 32.76Z"
78
- fill="white"
79
- />
80
- <path
81
- d="M12.8101 36.48L32.0001 48.43L51.1901 36.48L32.0001 63.93L12.8101 36.48Z"
82
- fill="white"
83
- />
84
- </g>
85
- )
86
- case "substrate":
87
- return (
88
- <>
89
- <g
90
- clipPath={`url(#${id}_1751_2030)`}
91
- opacity="0.75"
92
- transform="scale(2.2) translate(4.5 3.9)"
93
- className="orb-type"
94
- >
95
- <path
96
- d="M9.99937 4.4612C12.1176 4.4612 13.8347 3.46253 13.8347 2.2306C13.8347 0.998674 12.1176 0 9.99937 0C7.88119 0 6.16406 0.998674 6.16406 2.2306C6.16406 3.46253 7.88119 4.4612 9.99937 4.4612Z"
97
- fill="white"
98
- />
99
- <path
100
- d="M9.99937 21.2683C12.1176 21.2683 13.8347 20.2697 13.8347 19.0377C13.8347 17.8058 12.1176 16.8071 9.99937 16.8071C7.88119 16.8071 6.16406 17.8058 6.16406 19.0377C6.16406 20.2697 7.88119 21.2683 9.99937 21.2683Z"
101
- fill="white"
102
- />
103
- <path
104
- d="M4.65427 7.54892C5.71336 5.71457 5.70649 3.72787 4.63892 3.11149C3.57135 2.49511 1.84735 3.48246 0.788259 5.31681C-0.270832 7.15115 -0.263958 9.13786 0.803612 9.75424C1.87118 10.3706 3.59518 9.38326 4.65427 7.54892Z"
105
- fill="white"
106
- />
107
- <path
108
- d="M19.2083 15.9515C20.2674 14.1171 20.2611 12.1307 19.1943 11.5148C18.1274 10.8988 16.404 11.8865 15.3449 13.7209C14.2858 15.5552 14.2921 17.5416 15.3589 18.1575C16.4258 18.7735 18.1492 17.7858 19.2083 15.9515Z"
109
- fill="white"
110
- />
111
- <path
112
- d="M4.6399 18.1571C5.70747 17.5407 5.71434 15.554 4.65525 13.7196C3.59616 11.8853 1.87216 10.8979 0.804589 11.5143C-0.262981 12.1307 -0.269855 14.1174 0.789235 15.9517C1.84833 17.7861 3.57233 18.7734 4.6399 18.1571Z"
113
- fill="white"
114
- />
115
- <path
116
- d="M19.1952 9.75475C20.2621 9.13878 20.2684 7.15241 19.2093 5.31807C18.1502 3.48372 16.4268 2.49603 15.3599 3.11199C14.2931 3.72796 14.2868 5.71433 15.3459 7.54867C16.405 9.38302 18.1284 10.3707 19.1952 9.75475Z"
117
- fill="white"
118
- />
119
- </g>
120
- <defs>
121
- <clipPath id={`${id}_1751_2030`}>
122
- <rect width="20" height="21.2699" fill="white" />
123
- </clipPath>
124
- </defs>
125
- </>
126
- )
127
- default:
128
- return null
129
- }
130
- }
3
+ import { TalismanOrbLogo } from "./TalismanOrbLogo"
4
+ import { TalismanOrbProps } from "./types"
5
+ import { useTalismanOrb } from "./useTalismanOrb"
131
6
 
132
7
  export const TalismanOrb: FC<TalismanOrbProps> = ({
133
8
  seed,
@@ -135,7 +10,7 @@ export const TalismanOrb: FC<TalismanOrbProps> = ({
135
10
  height = "1em",
136
11
  className,
137
12
  }) => {
138
- const { id, bgColor1, bgColor2, transform, glowColor, cx, cy, iconType } = useTalismanOrb(seed)
13
+ const { id, bgColor1, bgColor2, transform, glowColor, cx, cy, platform } = useTalismanOrb(seed)
139
14
 
140
15
  return (
141
16
  <svg
@@ -164,43 +39,7 @@ export const TalismanOrb: FC<TalismanOrbProps> = ({
164
39
  <rect fill={`url(#${id}-bg)`} x={0} y={0} width={64} height={64} />
165
40
  <circle fill={`url(#${id}-circle)`} cx={cx} cy={cy} r={45} opacity={0.7} />
166
41
  </g>
167
- <TalismanOrbLogo id={id} iconType={iconType} />
168
- </g>
169
- </svg>
170
- )
171
- }
172
-
173
- export const TalismanOrbRectangle: FC<TalismanOrbProps> = ({ width, height, seed, className }) => {
174
- const { id, bgColor1, bgColor2, transform, glowColor, cx, cy } = useTalismanOrb(seed)
175
-
176
- return (
177
- <svg
178
- width={width}
179
- height={height}
180
- viewBox={`0 0 64 64`}
181
- preserveAspectRatio="none"
182
- className={className}
183
- version="1.1"
184
- xmlns="http://www.w3.org/2000/svg"
185
- >
186
- <defs>
187
- <linearGradient id={`${id}-bg`}>
188
- <stop offset="20%" stopColor={bgColor1} />
189
- <stop offset="100%" stopColor={bgColor2} />
190
- </linearGradient>
191
- <radialGradient id={`${id}-circle`}>
192
- <stop offset="10%" stopColor={glowColor} />
193
- <stop offset="100%" stopColor="transparent" />
194
- </radialGradient>
195
- <clipPath id={`${id}-clip`}>
196
- <circle cx="32" cy="32" r="48" />
197
- </clipPath>
198
- </defs>
199
- <g clipPath={`url(#${id}-clip)`}>
200
- <g transform={transform}>
201
- <rect fill={`url(#${id}-bg)`} x={-16} y={-16} width={96} height={96} />
202
- <circle fill={`url(#${id}-circle)`} cx={cx} cy={cy} r={45} opacity={0.7} />
203
- </g>
42
+ <TalismanOrbLogo platform={platform} />
204
43
  </g>
205
44
  </svg>
206
45
  )
@@ -0,0 +1,60 @@
1
+ import { AccountPlatform } from "@talismn/crypto"
2
+ import { FC } from "react"
3
+
4
+ export const TalismanOrbLogo: FC<{ platform: AccountPlatform }> = ({ platform }) => {
5
+ switch (platform) {
6
+ case "ethereum":
7
+ return (
8
+ <g opacity="0.75" transform="scale(0.7) translate(14 14)" className="orb-type">
9
+ <path
10
+ d="M12.8101 32.76L32.0001 44.62L51.1901 32.76L32.0001 -0.0699997L12.8101 32.76Z"
11
+ fill="white"
12
+ />
13
+ <path
14
+ d="M12.8101 36.48L32.0001 48.43L51.1901 36.48L32.0001 63.93L12.8101 36.48Z"
15
+ fill="white"
16
+ />
17
+ </g>
18
+ )
19
+ case "polkadot":
20
+ return (
21
+ <g opacity="0.75" transform="scale(2.2) translate(4.5 3.9)" className="orb-type">
22
+ <path
23
+ d="M9.99937 4.4612C12.1176 4.4612 13.8347 3.46253 13.8347 2.2306C13.8347 0.998674 12.1176 0 9.99937 0C7.88119 0 6.16406 0.998674 6.16406 2.2306C6.16406 3.46253 7.88119 4.4612 9.99937 4.4612Z"
24
+ fill="white"
25
+ />
26
+ <path
27
+ d="M9.99937 21.2683C12.1176 21.2683 13.8347 20.2697 13.8347 19.0377C13.8347 17.8058 12.1176 16.8071 9.99937 16.8071C7.88119 16.8071 6.16406 17.8058 6.16406 19.0377C6.16406 20.2697 7.88119 21.2683 9.99937 21.2683Z"
28
+ fill="white"
29
+ />
30
+ <path
31
+ d="M4.65427 7.54892C5.71336 5.71457 5.70649 3.72787 4.63892 3.11149C3.57135 2.49511 1.84735 3.48246 0.788259 5.31681C-0.270832 7.15115 -0.263958 9.13786 0.803612 9.75424C1.87118 10.3706 3.59518 9.38326 4.65427 7.54892Z"
32
+ fill="white"
33
+ />
34
+ <path
35
+ d="M19.2083 15.9515C20.2674 14.1171 20.2611 12.1307 19.1943 11.5148C18.1274 10.8988 16.404 11.8865 15.3449 13.7209C14.2858 15.5552 14.2921 17.5416 15.3589 18.1575C16.4258 18.7735 18.1492 17.7858 19.2083 15.9515Z"
36
+ fill="white"
37
+ />
38
+ <path
39
+ d="M4.6399 18.1571C5.70747 17.5407 5.71434 15.554 4.65525 13.7196C3.59616 11.8853 1.87216 10.8979 0.804589 11.5143C-0.262981 12.1307 -0.269855 14.1174 0.789235 15.9517C1.84833 17.7861 3.57233 18.7734 4.6399 18.1571Z"
40
+ fill="white"
41
+ />
42
+ <path
43
+ d="M19.1952 9.75475C20.2621 9.13878 20.2684 7.15241 19.2093 5.31807C18.1502 3.48372 16.4268 2.49603 15.3599 3.11199C14.2931 3.72796 14.2868 5.71433 15.3459 7.54867C16.405 9.38302 18.1284 10.3707 19.1952 9.75475Z"
44
+ fill="white"
45
+ />
46
+ </g>
47
+ )
48
+ case "solana":
49
+ return (
50
+ <g opacity="0.75" className="orb-type" transform="scale(0.45) translate(37.5, 37.5)">
51
+ <path
52
+ d="M70.6648 50.1769L58.9393 62.775C58.6844 63.0486 58.376 63.2668 58.0332 63.4159C57.6905 63.5651 57.3208 63.6419 56.9472 63.6417H1.36128C1.09605 63.6417 0.836598 63.5641 0.614804 63.4184C0.393011 63.2727 0.218536 63.0652 0.112817 62.8215C0.00709765 62.5779 -0.0252603 62.3085 0.0197186 62.0467C0.0646974 61.7848 0.185054 61.5418 0.366 61.3476L12.1006 48.7496C12.3548 48.4766 12.6623 48.2589 13.0039 48.1098C13.3455 47.9607 13.714 47.8834 14.0866 47.8828H69.6695C69.9348 47.8828 70.1942 47.9604 70.416 48.1062C70.6378 48.2519 70.8123 48.4593 70.918 48.703C71.0237 48.9467 71.0561 49.216 71.0111 49.4778C70.9661 49.7397 70.8458 49.9827 70.6648 50.1769V50.1769ZM58.9393 24.8081C58.6844 24.5345 58.376 24.3163 58.0332 24.1672C57.6905 24.0181 57.3208 23.9412 56.9472 23.9414H1.36128C1.09605 23.9414 0.836598 24.019 0.614804 24.1647C0.393011 24.3105 0.218536 24.5179 0.112817 24.7616C0.00709765 25.0053 -0.0252603 25.2746 0.0197186 25.5364C0.0646974 25.7983 0.185054 26.0413 0.366 26.2355L12.1006 38.8336C12.3548 39.1065 12.6623 39.3242 13.0039 39.4733C13.3455 39.6224 13.714 39.6997 14.0866 39.7003H69.6695C69.9348 39.7003 70.1942 39.6227 70.416 39.477C70.6378 39.3313 70.8123 39.1238 70.918 38.8801C71.0237 38.6365 71.0561 38.3671 71.0111 38.1053C70.9661 37.8434 70.8458 37.6004 70.6648 37.4062L58.9393 24.8081ZM1.36128 15.7589H56.9472C57.3208 15.7591 57.6905 15.6822 58.0332 15.5331C58.376 15.384 58.6844 15.1658 58.9393 14.8922L70.6648 2.29413C70.8458 2.09986 70.9661 1.85688 71.0111 1.59502C71.0561 1.33317 71.0237 1.06385 70.918 0.820169C70.8123 0.576485 70.6378 0.369047 70.416 0.223341C70.1942 0.0776352 69.9348 8.97308e-06 69.6695 0L14.0866 0C13.714 0.000635258 13.3455 0.077888 13.0039 0.226975C12.6623 0.376063 12.3548 0.593811 12.1006 0.866739L0.369025 13.4648C0.188254 13.6588 0.0679503 13.9016 0.0228694 14.1631C-0.0222115 14.4247 0.00988974 14.6938 0.115236 14.9373C0.220583 15.1809 0.394595 15.3884 0.615931 15.5343C0.837268 15.6802 1.09631 15.7583 1.36128 15.7589V15.7589Z"
53
+ fill="white"
54
+ />
55
+ </g>
56
+ )
57
+ default:
58
+ return null
59
+ }
60
+ }
@@ -0,0 +1,40 @@
1
+ import { FC } from "react"
2
+
3
+ import { TalismanOrbProps } from "./types"
4
+ import { useTalismanOrb } from "./useTalismanOrb"
5
+
6
+ export const TalismanOrbRectangle: FC<TalismanOrbProps> = ({ width, height, seed, className }) => {
7
+ const { id, bgColor1, bgColor2, transform, glowColor, cx, cy } = useTalismanOrb(seed)
8
+
9
+ return (
10
+ <svg
11
+ width={width}
12
+ height={height}
13
+ viewBox={`0 0 64 64`}
14
+ preserveAspectRatio="none"
15
+ className={className}
16
+ version="1.1"
17
+ xmlns="http://www.w3.org/2000/svg"
18
+ >
19
+ <defs>
20
+ <linearGradient id={`${id}-bg`}>
21
+ <stop offset="20%" stopColor={bgColor1} />
22
+ <stop offset="100%" stopColor={bgColor2} />
23
+ </linearGradient>
24
+ <radialGradient id={`${id}-circle`}>
25
+ <stop offset="10%" stopColor={glowColor} />
26
+ <stop offset="100%" stopColor="transparent" />
27
+ </radialGradient>
28
+ <clipPath id={`${id}-clip`}>
29
+ <circle cx="32" cy="32" r="48" />
30
+ </clipPath>
31
+ </defs>
32
+ <g clipPath={`url(#${id}-clip)`}>
33
+ <g transform={transform}>
34
+ <rect fill={`url(#${id}-bg)`} x={-16} y={-16} width={96} height={96} />
35
+ <circle fill={`url(#${id}-circle)`} cx={cx} cy={cy} r={45} opacity={0.7} />
36
+ </g>
37
+ </g>
38
+ </svg>
39
+ )
40
+ }
@@ -1 +1,4 @@
1
+ export * from "./types"
2
+ export * from "./useTalismanOrb"
1
3
  export * from "./TalismanOrb"
4
+ export * from "./TalismanOrbRectangle"
@@ -0,0 +1 @@
1
+ export type TalismanOrbProps = { seed: string; width?: number; height?: number; className?: string }
@@ -0,0 +1,70 @@
1
+ import { getAccountPlatformFromAddress, normalizeAddress } from "@talismn/crypto"
2
+ import md5 from "blueimp-md5"
3
+ import Color from "color"
4
+ import { useId, useMemo } from "react"
5
+
6
+ const djb2 = (str: string) => {
7
+ let hash = 5381
8
+ for (let i = 0; i < str.length; i++) hash = (hash << 5) + hash + str.charCodeAt(i)
9
+ return hash
10
+ }
11
+
12
+ const valueFromHash = (hash: string, max: number) => {
13
+ return (max + djb2(hash)) % max
14
+ }
15
+
16
+ const colorFromHash = (hash: string) => {
17
+ const hue = valueFromHash(hash, 360)
18
+ return Color.hsv(hue, 100, 100)
19
+ }
20
+
21
+ const rotateText = (text: string, nbChars = 0) => text.slice(nbChars) + text.slice(0, nbChars)
22
+
23
+ export const useTalismanOrb = (seed: string) => {
24
+ const id = useId()
25
+
26
+ return useMemo(() => {
27
+ try {
28
+ // those break if seed is empty or an invalid address
29
+
30
+ // eslint-disable-next-line no-var
31
+ var platform = getAccountPlatformFromAddress(seed)
32
+
33
+ // seed may be specific to a ss58 prefix, get the base address
34
+ // eslint-disable-next-line no-var
35
+ var address = normalizeAddress(seed)
36
+ } catch (err) {
37
+ platform = "polkadot"
38
+ address = seed
39
+ }
40
+
41
+ // derive 3 hashs from the seed, used to generate the 3 colors
42
+ const hash1 = md5(address)
43
+ const hash2 = rotateText(hash1, 1)
44
+ const hash3 = rotateText(hash1, 2)
45
+
46
+ // the 2 darkest ones will be used as gradient BG
47
+ // the lightest one will be used as gradient circle, to mimic a 3D lighting effect
48
+ const colors = [colorFromHash(hash1), colorFromHash(hash2), colorFromHash(hash3)].sort(
49
+ (c1, c2) => c1.lightness() - c2.lightness(),
50
+ )
51
+
52
+ // random location in top left corner, avoid beeing to close from the center
53
+ const dotX = 10 + valueFromHash(hash1, 10)
54
+ const dotY = 10 + valueFromHash(hash2, 10)
55
+
56
+ // global rotation
57
+ const rotation = valueFromHash(hash1, 360)
58
+
59
+ return {
60
+ id, //multiple avatars should cohabit on the same page
61
+ bgColor1: colors[0].hex(),
62
+ bgColor2: colors[1].hex(),
63
+ glowColor: colors[2].hex(),
64
+ transform: `rotate(${rotation} 32 32)`,
65
+ cx: dotX,
66
+ cy: dotY,
67
+ platform,
68
+ }
69
+ }, [id, seed])
70
+ }
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./components"
2
+ export * from "./util"
@@ -0,0 +1,14 @@
1
+ import ReactDOMServer from "react-dom/server"
2
+
3
+ import { TalismanOrb } from "../components"
4
+
5
+ /**
6
+ * Returns a base64 encoded data url for the Talisman Orb svg
7
+ */
8
+ export const getTalismanOrbDataUrl = (address: string): `data:image/svg+xml;base64,${string}` => {
9
+ // render the TalismanOrb component and output the SVG as text
10
+ const svg = ReactDOMServer.renderToStaticMarkup(<TalismanOrb seed={address} />)
11
+
12
+ // convert to data url
13
+ return `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`
14
+ }
@@ -0,0 +1 @@
1
+ export * from "./getTalismanOrbDataUrl"
@@ -1,2 +0,0 @@
1
- export declare const isEthAddress: (address: string) => address is `0x${string}`;
2
- export declare const normalizeEthAddress: (address: `0x${string}`) => `0x${string}`;
@@ -1,2 +0,0 @@
1
- export * from "./ethAddress";
2
- export * from "./subAddress";
@@ -1 +0,0 @@
1
- export declare const normalizeSubAddress: (address: string) => string;