@renown/sdk 6.2.2-dev.6 → 6.2.2-dev.60

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 (75) hide show
  1. package/README.md +162 -1
  2. package/dist/{browser-key-storage-v4O-fjz0.js → browser-key-storage-e3RQsnLS.js} +1 -1
  3. package/dist/{browser-key-storage-v4O-fjz0.js.map → browser-key-storage-e3RQsnLS.js.map} +1 -1
  4. package/dist/constants-BexAR53F.js +82 -0
  5. package/dist/constants-BexAR53F.js.map +1 -0
  6. package/dist/crypto.d.ts +2 -2
  7. package/dist/crypto.js +2 -2
  8. package/dist/index-COkz6hfp.d.ts +131 -0
  9. package/dist/index-COkz6hfp.d.ts.map +1 -0
  10. package/dist/index.d.ts +11 -8
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +21 -9
  13. package/dist/index.js.map +1 -1
  14. package/dist/local-credential-verifier-BcCzjvmd.js +846 -0
  15. package/dist/local-credential-verifier-BcCzjvmd.js.map +1 -0
  16. package/dist/meta-C5aQjD4L.d.ts +19 -0
  17. package/dist/meta-C5aQjD4L.d.ts.map +1 -0
  18. package/dist/meta-CHgd9Qd0.js +31 -0
  19. package/dist/meta-CHgd9Qd0.js.map +1 -0
  20. package/dist/meta-D-5kvwx8.js +27 -0
  21. package/dist/meta-D-5kvwx8.js.map +1 -0
  22. package/dist/meta-DufkALio.d.ts +16 -0
  23. package/dist/meta-DufkALio.d.ts.map +1 -0
  24. package/dist/meta-tst3zTfz.d.ts +19 -0
  25. package/dist/meta-tst3zTfz.d.ts.map +1 -0
  26. package/dist/node.d.ts +11 -11
  27. package/dist/node.d.ts.map +1 -1
  28. package/dist/node.js +104 -12
  29. package/dist/node.js.map +1 -1
  30. package/dist/{signer-CgYhXTV7.js → signer-DrjINLqt.js} +13 -6
  31. package/dist/signer-DrjINLqt.js.map +1 -0
  32. package/dist/{signer-DYKGoYHB.d.ts → types-BlhSzU4B.d.ts} +163 -118
  33. package/dist/types-BlhSzU4B.d.ts.map +1 -0
  34. package/dist/types-BqXZ_2bH.d.ts +59 -0
  35. package/dist/types-BqXZ_2bH.d.ts.map +1 -0
  36. package/dist/types-C_qc_62W.js +20 -0
  37. package/dist/types-C_qc_62W.js.map +1 -0
  38. package/dist/{utils-C9ZgMs_C.d.ts → utils-C8MlqUUa.d.ts} +81 -94
  39. package/dist/utils-C8MlqUUa.d.ts.map +1 -0
  40. package/dist/wallet/index.d.ts +8 -0
  41. package/dist/wallet/index.d.ts.map +1 -0
  42. package/dist/wallet/index.js +30 -0
  43. package/dist/wallet/index.js.map +1 -0
  44. package/dist/wallet/mock/factory.d.ts +9 -0
  45. package/dist/wallet/mock/factory.d.ts.map +1 -0
  46. package/dist/wallet/mock/factory.js +35 -0
  47. package/dist/wallet/mock/factory.js.map +1 -0
  48. package/dist/wallet/mock/index.d.ts +9 -0
  49. package/dist/wallet/mock/index.d.ts.map +1 -0
  50. package/dist/wallet/mock/index.js +16 -0
  51. package/dist/wallet/mock/index.js.map +1 -0
  52. package/dist/wallet/privy/factory.d.ts +58 -0
  53. package/dist/wallet/privy/factory.d.ts.map +1 -0
  54. package/dist/wallet/privy/factory.js +258 -0
  55. package/dist/wallet/privy/factory.js.map +1 -0
  56. package/dist/wallet/privy/index.d.ts +9 -0
  57. package/dist/wallet/privy/index.d.ts.map +1 -0
  58. package/dist/wallet/privy/index.js +16 -0
  59. package/dist/wallet/privy/index.js.map +1 -0
  60. package/dist/wallet/rainbow/factory.d.ts +7 -0
  61. package/dist/wallet/rainbow/factory.d.ts.map +1 -0
  62. package/dist/wallet/rainbow/factory.js +219 -0
  63. package/dist/wallet/rainbow/factory.js.map +1 -0
  64. package/dist/wallet/rainbow/index.d.ts +9 -0
  65. package/dist/wallet/rainbow/index.d.ts.map +1 -0
  66. package/dist/wallet/rainbow/index.js +21 -0
  67. package/dist/wallet/rainbow/index.js.map +1 -0
  68. package/package.json +77 -4
  69. package/dist/browser-key-storage-DSkDBAhU.d.ts +0 -14
  70. package/dist/browser-key-storage-DSkDBAhU.d.ts.map +0 -1
  71. package/dist/renown-builder-Sn_Y2Cb2.js +0 -496
  72. package/dist/renown-builder-Sn_Y2Cb2.js.map +0 -1
  73. package/dist/signer-CgYhXTV7.js.map +0 -1
  74. package/dist/signer-DYKGoYHB.d.ts.map +0 -1
  75. package/dist/utils-C9ZgMs_C.d.ts.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../src/wallet/rainbow/meta.ts","../../../src/wallet/rainbow/index.ts"],"sourcesContent":["import { LoginMethod, type WalletAdapterMeta } from \"../types.js\";\n\n// Any `wagmi/chains` export satisfies this. Declared here so the module stays\n// free of wallet-peer imports, type ones included.\nexport interface PHRenownChain {\n id: number;\n name: string;\n}\n\n// Config slice this adapter consumes; operators pass the same shape from\n// powerhouse.config.json. No `methods`: this adapter only does wallet login.\nexport interface PHRenownRainbowAdapterConfig {\n // Optional, but without it only an installed browser wallet (or a Safe App\n // iframe) can sign in — every other wallet needs it. See buildWagmiConfig.\n walletConnectProjectId?: string;\n infuraProjectId?: string;\n appName?: string;\n // From `wagmi/chains`; defaults to mainnet alone, as each one adds bundle\n // weight. A wallet on a chain outside this list must switch to sign in.\n chains?: readonly [PHRenownChain, ...PHRenownChain[]];\n // Opt-in for server-rendered hosts (e.g. Next.js) so wagmi defers its hydrate\n // reconnect to an effect instead of running it during render.\n ssr?: boolean;\n}\n\nexport const RAINBOW_METHODS: LoginMethod[] = [LoginMethod.WALLET];\n\n// Dependency-free identity (see privy/meta.ts). Injected wallets have no\n// full-page OAuth redirect, so no redirect-return params.\nexport const rainbowAdapterMeta: WalletAdapterMeta = {\n id: \"rainbow\",\n redirectReturnParams: [],\n supportedMethods: RAINBOW_METHODS,\n};\n","import type { WalletAdapterDescriptor } from \"../types.js\";\nimport {\n rainbowAdapterMeta,\n type PHRenownRainbowAdapterConfig,\n} from \"./meta.js\";\n\n/** RainbowKit + wagmi wallet adapter (external / injected wallets) for `RenownWalletProvider`. Importing this module loads no wallet library — `@rainbow-me/rainbowkit`, `wagmi` and `@tanstack/react-query` are fetched by `load()` on the first login click — but it does make them build-time requirements, so install them alongside this import. Pass `ssr: true` on server-rendered hosts. */\nexport function rainbowAdapter(\n config: PHRenownRainbowAdapterConfig,\n): WalletAdapterDescriptor {\n return {\n meta: rainbowAdapterMeta,\n load: () =>\n import(\"./factory.js\").then((m) => m.createRainbowAdapter(config)),\n };\n}\n\nexport { RAINBOW_METHODS, rainbowAdapterMeta } from \"./meta.js\";\nexport type { PHRenownChain, PHRenownRainbowAdapterConfig } from \"./meta.js\";\n"],"mappings":";;AAyBA,MAAa,kBAAiC,CAAC,YAAY,OAAO;AAIlE,MAAa,qBAAwC;CACnD,IAAI;CACJ,sBAAsB,EAAE;CACxB,kBAAkB;CACnB;;;;AC1BD,SAAgB,eACd,QACyB;AACzB,QAAO;EACL,MAAM;EACN,YACE,OAAO,gBAAgB,MAAM,MAAM,EAAE,qBAAqB,OAAO,CAAC;EACrE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@renown/sdk",
3
- "version": "6.2.2-dev.6",
3
+ "version": "6.2.2-dev.60",
4
4
  "description": "Renown SDK — wallet-based authentication and user identity management for Powerhouse React applications.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "private": false,
@@ -31,6 +31,48 @@
31
31
  "types": "./dist/crypto.d.ts",
32
32
  "import": "./dist/crypto.js",
33
33
  "default": "./dist/crypto.js"
34
+ },
35
+ "./wallet": {
36
+ "source": "./src/wallet/index.ts",
37
+ "types": "./dist/wallet/index.d.ts",
38
+ "import": "./dist/wallet/index.js",
39
+ "default": "./dist/wallet/index.js"
40
+ },
41
+ "./wallet/rainbow": {
42
+ "source": "./src/wallet/rainbow/index.ts",
43
+ "types": "./dist/wallet/rainbow/index.d.ts",
44
+ "import": "./dist/wallet/rainbow/index.js",
45
+ "default": "./dist/wallet/rainbow/index.js"
46
+ },
47
+ "./wallet/rainbow/factory": {
48
+ "source": "./src/wallet/rainbow/factory.ts",
49
+ "types": "./dist/wallet/rainbow/factory.d.ts",
50
+ "import": "./dist/wallet/rainbow/factory.js",
51
+ "default": "./dist/wallet/rainbow/factory.js"
52
+ },
53
+ "./wallet/privy": {
54
+ "source": "./src/wallet/privy/index.ts",
55
+ "types": "./dist/wallet/privy/index.d.ts",
56
+ "import": "./dist/wallet/privy/index.js",
57
+ "default": "./dist/wallet/privy/index.js"
58
+ },
59
+ "./wallet/privy/factory": {
60
+ "source": "./src/wallet/privy/factory.ts",
61
+ "types": "./dist/wallet/privy/factory.d.ts",
62
+ "import": "./dist/wallet/privy/factory.js",
63
+ "default": "./dist/wallet/privy/factory.js"
64
+ },
65
+ "./wallet/mock": {
66
+ "source": "./src/wallet/mock/index.ts",
67
+ "types": "./dist/wallet/mock/index.d.ts",
68
+ "import": "./dist/wallet/mock/index.js",
69
+ "default": "./dist/wallet/mock/index.js"
70
+ },
71
+ "./wallet/mock/factory": {
72
+ "source": "./src/wallet/mock/factory.ts",
73
+ "types": "./dist/wallet/mock/factory.d.ts",
74
+ "import": "./dist/wallet/mock/factory.js",
75
+ "default": "./dist/wallet/mock/factory.js"
34
76
  }
35
77
  },
36
78
  "types": "./dist/index.d.ts",
@@ -46,17 +88,47 @@
46
88
  "dist"
47
89
  ],
48
90
  "devDependencies": {
91
+ "@privy-io/react-auth": "3.35.0",
92
+ "@rainbow-me/rainbowkit": "^2.2.11",
93
+ "@tanstack/react-query": "^5.90.2",
94
+ "@types/react": "19.2.14",
95
+ "react": ">=19",
49
96
  "tsdown": "0.21.1",
50
- "vitest": "4.1.1"
97
+ "vitest": "4.1.1",
98
+ "wagmi": "^2.19.5"
51
99
  },
52
100
  "dependencies": {
53
101
  "@didtools/key-webcrypto": "^0.2.0",
102
+ "@powerhousedao/shared": "6.2.2-dev.60",
54
103
  "did-jwt-vc": "^4.0.12",
55
104
  "did-resolver": "^4.1.0",
56
105
  "key-did-resolver": "^4.0.0",
57
106
  "uint8arrays": "^5.1.0",
58
- "viem": "~2.50.4",
59
- "@powerhousedao/shared": "6.2.2-dev.6"
107
+ "viem": "~2.50.4"
108
+ },
109
+ "peerDependencies": {
110
+ "@privy-io/react-auth": "^3.25.0",
111
+ "@rainbow-me/rainbowkit": "^2.2.11",
112
+ "@tanstack/react-query": "^5.90.2",
113
+ "react": ">=19",
114
+ "wagmi": "^2.19.5"
115
+ },
116
+ "peerDependenciesMeta": {
117
+ "@privy-io/react-auth": {
118
+ "optional": true
119
+ },
120
+ "@rainbow-me/rainbowkit": {
121
+ "optional": true
122
+ },
123
+ "@tanstack/react-query": {
124
+ "optional": true
125
+ },
126
+ "react": {
127
+ "optional": true
128
+ },
129
+ "wagmi": {
130
+ "optional": true
131
+ }
60
132
  },
61
133
  "keywords": [
62
134
  "powerhouse",
@@ -75,6 +147,7 @@
75
147
  "homepage": "https://academy.vetra.io/academy/Reference/Authorization/renown-sdk/Overview",
76
148
  "scripts": {
77
149
  "build": "tsdown",
150
+ "test": "vitest --run",
78
151
  "tsc": "tsc",
79
152
  "lint": "eslint"
80
153
  }
@@ -1,14 +0,0 @@
1
- import { m as JwkKeyPair, p as JsonWebKeyPairStorage } from "./signer-DYKGoYHB.js";
2
-
3
- //#region src/crypto/browser-key-storage.d.ts
4
- declare class BrowserKeyStorage implements JsonWebKeyPairStorage {
5
- #private;
6
- constructor(db: IDBDatabase);
7
- static create(dbName?: string): Promise<BrowserKeyStorage>;
8
- saveKeyPair(keyPair: JwkKeyPair): Promise<void>;
9
- loadKeyPair(): Promise<JwkKeyPair | undefined>;
10
- removeKeyPair(): Promise<void>;
11
- }
12
- //#endregion
13
- export { BrowserKeyStorage as t };
14
- //# sourceMappingURL=browser-key-storage-DSkDBAhU.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"browser-key-storage-DSkDBAhU.d.ts","names":[],"sources":["../src/crypto/browser-key-storage.ts"],"mappings":";;;cAMa,iBAAA,YAA6B,qBAAA;EAAA;cAG5B,EAAA,EAAI,WAAA;EAAA,OAIH,MAAA,CACX,MAAA,YACC,OAAA,CAAQ,iBAAA;EA4CL,WAAA,CAAY,OAAA,EAAS,UAAA,GAAU,OAAA;EAS/B,WAAA,CAAA,GAAe,OAAA,CAAQ,UAAA;EAavB,aAAA,CAAA,GAAiB,OAAA;AAAA"}
@@ -1,496 +0,0 @@
1
- import { m as verifyAuthBearerToken, p as parsePkhDid, s as RenownCryptoBuilder, t as RenownCryptoSigner } from "./signer-CgYhXTV7.js";
2
- import { recoverTypedDataAddress } from "viem";
3
- //#region src/constants.ts
4
- const DEFAULT_RENOWN_URL = "https://www.renown.id";
5
- const DEFAULT_RENOWN_NETWORK_ID = "eip155";
6
- const DEFAULT_RENOWN_CHAIN_ID = "1";
7
- const DOMAIN_TYPE = [{
8
- name: "version",
9
- type: "string"
10
- }, {
11
- name: "chainId",
12
- type: "uint256"
13
- }];
14
- const VERIFIABLE_CREDENTIAL_EIP712_TYPE = [
15
- {
16
- name: "@context",
17
- type: "string[]"
18
- },
19
- {
20
- name: "type",
21
- type: "string[]"
22
- },
23
- {
24
- name: "id",
25
- type: "string"
26
- },
27
- {
28
- name: "issuer",
29
- type: "Issuer"
30
- },
31
- {
32
- name: "credentialSubject",
33
- type: "CredentialSubject"
34
- },
35
- {
36
- name: "credentialSchema",
37
- type: "CredentialSchema"
38
- },
39
- {
40
- name: "issuanceDate",
41
- type: "string"
42
- },
43
- {
44
- name: "expirationDate",
45
- type: "string"
46
- }
47
- ];
48
- const CREDENTIAL_SCHEMA_EIP712_TYPE = [{
49
- name: "id",
50
- type: "string"
51
- }, {
52
- name: "type",
53
- type: "string"
54
- }];
55
- const CREDENTIAL_SUBJECT_TYPE = [{
56
- name: "app",
57
- type: "string"
58
- }, {
59
- name: "id",
60
- type: "string"
61
- }];
62
- const ISSUER_TYPE = [{
63
- name: "id",
64
- type: "string"
65
- }, {
66
- name: "ethereumAddress",
67
- type: "string"
68
- }];
69
- const CREDENTIAL_TYPES = {
70
- EIP712Domain: DOMAIN_TYPE,
71
- VerifiableCredential: VERIFIABLE_CREDENTIAL_EIP712_TYPE,
72
- CredentialSchema: CREDENTIAL_SCHEMA_EIP712_TYPE,
73
- CredentialSubject: CREDENTIAL_SUBJECT_TYPE,
74
- Issuer: ISSUER_TYPE
75
- };
76
- //#endregion
77
- //#region src/credential.ts
78
- /** Assemble a Renown delegation VC and sign it with the provided signer. */
79
- async function buildAndSignCredential(params) {
80
- const { signTypedData, address, chainId, app, appId, expiresInDays = 7 } = params;
81
- const issuerId = `did:pkh:${DEFAULT_RENOWN_NETWORK_ID}:${chainId}:${address.toLowerCase()}`;
82
- const credentialId = `urn:uuid:${globalThis.crypto.randomUUID()}`;
83
- const now = /* @__PURE__ */ new Date();
84
- const expirationDate = new Date(now.getTime() + expiresInDays * 24 * 60 * 60 * 1e3);
85
- const message = {
86
- "@context": ["https://www.w3.org/2018/credentials/v1"],
87
- type: ["VerifiableCredential", "RenownCredential"],
88
- id: credentialId,
89
- issuer: {
90
- id: issuerId,
91
- ethereumAddress: address
92
- },
93
- credentialSubject: {
94
- id: appId,
95
- app
96
- },
97
- credentialSchema: {
98
- id: "https://renown.id/schemas/renown-credential/v1",
99
- type: "JsonSchemaValidator2018"
100
- },
101
- issuanceDate: now.toISOString(),
102
- expirationDate: expirationDate.toISOString()
103
- };
104
- const domain = {
105
- version: "1",
106
- chainId
107
- };
108
- const { EIP712Domain, ...signTypes } = CREDENTIAL_TYPES;
109
- const signature = await signTypedData({
110
- domain,
111
- types: signTypes,
112
- primaryType: "VerifiableCredential",
113
- message
114
- });
115
- return {
116
- ...message,
117
- proof: {
118
- type: "EthereumEip712Signature2021",
119
- created: message.issuanceDate,
120
- verificationMethod: issuerId,
121
- proofPurpose: "assertionMethod",
122
- proofValue: signature,
123
- ethereumAddress: address,
124
- eip712: {
125
- domain,
126
- types: CREDENTIAL_TYPES,
127
- primaryType: "VerifiableCredential"
128
- }
129
- }
130
- };
131
- }
132
- /** Recover the address that signed a credential's EIP-712 proof. */
133
- async function recoverCredentialSigner(credential) {
134
- const { proof, ...message } = credential;
135
- const { EIP712Domain, ...types } = CREDENTIAL_TYPES;
136
- return recoverTypedDataAddress({
137
- domain: proof.eip712.domain,
138
- types,
139
- primaryType: "VerifiableCredential",
140
- message,
141
- signature: proof.proofValue
142
- });
143
- }
144
- /** True when the proof was signed by the credential's declared issuer address. */
145
- async function verifyCredentialSignature(credential) {
146
- try {
147
- return (await recoverCredentialSigner(credential)).toLowerCase() === credential.issuer.ethereumAddress.toLowerCase();
148
- } catch {
149
- return false;
150
- }
151
- }
152
- function withEip712Domain(credential, chainId) {
153
- if (credential.proof.eip712?.domain) return credential;
154
- return {
155
- ...credential,
156
- proof: {
157
- ...credential.proof,
158
- eip712: {
159
- domain: {
160
- version: "1",
161
- chainId
162
- },
163
- types: CREDENTIAL_TYPES,
164
- primaryType: "VerifiableCredential"
165
- }
166
- }
167
- };
168
- }
169
- async function fetchDelegationCredential(options) {
170
- const { address, chainId, appDid, baseUrl = DEFAULT_RENOWN_URL, verifySignature = true } = options;
171
- try {
172
- const url = new URL(`/api/auth/credential?address=${encodeURIComponent(address)}&chainId=${encodeURIComponent(chainId)}&connectId=${encodeURIComponent(appDid)}&appId=${encodeURIComponent(appDid)}`, baseUrl);
173
- const response = await fetch(url, { method: "GET" });
174
- if (!response.ok) return void 0;
175
- const { credential } = await response.json();
176
- if (!credential) return void 0;
177
- const [, , , issuerChainId, issuerAddress] = credential.issuer.id.split(":");
178
- if (credential.credentialSubject.id !== appDid || issuerChainId !== String(chainId) || issuerAddress?.toLowerCase() !== address.toLowerCase() || credential.issuer.ethereumAddress.toLowerCase() !== address.toLowerCase()) return;
179
- if (credential.expirationDate) {
180
- const expiresAt = Date.parse(credential.expirationDate);
181
- if (Number.isNaN(expiresAt) || expiresAt <= Date.now()) return;
182
- }
183
- if (verifySignature) {
184
- const withDomain = withEip712Domain(credential, chainId);
185
- if (withDomain.proof.eip712.domain.chainId !== chainId || !await verifyCredentialSignature(withDomain)) return;
186
- return withDomain;
187
- }
188
- return credential;
189
- } catch {
190
- return;
191
- }
192
- }
193
- async function verifyAuthCredential(jwt, options = {}) {
194
- const verified = await verifyAuthBearerToken(jwt, { audience: options.audience });
195
- if (!verified) return void 0;
196
- const { address, chainId, networkId } = verified.verifiableCredential.credentialSubject;
197
- const appDid = verified.issuer;
198
- const credential = await fetchDelegationCredential({
199
- address,
200
- chainId,
201
- appDid,
202
- baseUrl: options.renownUrl,
203
- verifySignature: options.verifySignature
204
- });
205
- if (!credential) return void 0;
206
- return {
207
- address,
208
- chainId,
209
- networkId,
210
- did: credential.issuer.id,
211
- appDid,
212
- credential
213
- };
214
- }
215
- //#endregion
216
- //#region src/storage/common.ts
217
- var BaseStorage = class {};
218
- var MemoryStorage = class extends BaseStorage {
219
- data = /* @__PURE__ */ new Map();
220
- get(key) {
221
- return this.data.get(key);
222
- }
223
- set(key, value) {
224
- if (value === void 0) this.data.delete(key);
225
- else this.data.set(key, value);
226
- }
227
- delete(key) {
228
- this.data.delete(key);
229
- }
230
- };
231
- //#endregion
232
- //#region src/common.ts
233
- var RenownMemoryStorage = class extends MemoryStorage {};
234
- var Renown = class {
235
- #baseUrl;
236
- #store;
237
- #eventEmitter;
238
- #appName;
239
- #crypto;
240
- #signer;
241
- #profileFetcher;
242
- #status = "initial";
243
- constructor(store, eventEmitter, crypto, appName, baseUrl = DEFAULT_RENOWN_URL, profileFetcher) {
244
- this.#store = store;
245
- this.#eventEmitter = eventEmitter;
246
- this.#baseUrl = baseUrl;
247
- this.#crypto = crypto;
248
- this.#appName = appName;
249
- this.#profileFetcher = profileFetcher;
250
- this.#signer = new RenownCryptoSigner(crypto, this.#appName, this.user);
251
- this.on("user", (user) => {
252
- this.#signer.user = user;
253
- });
254
- }
255
- get baseUrl() {
256
- return this.#baseUrl;
257
- }
258
- get user() {
259
- return this.#store.get("user");
260
- }
261
- get status() {
262
- return this.#status;
263
- }
264
- get signer() {
265
- return this.#signer;
266
- }
267
- get crypto() {
268
- return this.#crypto;
269
- }
270
- get did() {
271
- return this.#crypto.did;
272
- }
273
- get profileFetcher() {
274
- return this.#profileFetcher;
275
- }
276
- #updateStatus(status) {
277
- this.#status = status;
278
- this.#eventEmitter.emit("status", status);
279
- }
280
- #updateUser(user) {
281
- if (user) this.#store.set("user", user);
282
- else this.#store.delete("user");
283
- this.#eventEmitter.emit("user", user);
284
- }
285
- async login(userDid) {
286
- this.#updateStatus("checking");
287
- try {
288
- const result = parsePkhDid(userDid);
289
- const credential = await this.#getCredential(result.address, result.chainId, this.#crypto.did);
290
- if (!credential) {
291
- this.#updateUser(void 0);
292
- throw new Error("Credential not found");
293
- }
294
- if (!(credential.issuer.id === userDid && credential.credentialSubject.id === this.did)) throw new Error("Invalid credential");
295
- const verifiableCredential = credential.proof.eip712?.domain ? credential : {
296
- ...credential,
297
- proof: {
298
- ...credential.proof,
299
- eip712: {
300
- domain: {
301
- version: "1",
302
- chainId: result.chainId
303
- },
304
- types: CREDENTIAL_TYPES,
305
- primaryType: "VerifiableCredential"
306
- }
307
- }
308
- };
309
- if (credential.issuer.ethereumAddress.toLowerCase() !== result.address.toLowerCase() || verifiableCredential.proof.eip712.domain.chainId !== result.chainId || !await verifyCredentialSignature(verifiableCredential)) throw new Error("Invalid credential signature");
310
- const user = {
311
- ...result,
312
- address: credential.issuer.ethereumAddress,
313
- did: userDid,
314
- credential: verifiableCredential
315
- };
316
- this.#updateUser(user);
317
- this.#updateStatus("authorized");
318
- if (this.#profileFetcher) this.#profileFetcher(user, this.#baseUrl).then((profile) => {
319
- if (profile && this.user?.address === user.address && this.user.chainId === user.chainId) this.#updateUser({
320
- ...this.user,
321
- profile,
322
- ens: {
323
- name: profile.username ?? void 0,
324
- avatarUrl: profile.userImage ?? void 0
325
- }
326
- });
327
- }).catch(console.error);
328
- return user;
329
- } catch (error) {
330
- this.#updateUser(void 0);
331
- this.#updateStatus("not-authorized");
332
- throw error;
333
- }
334
- }
335
- logout() {
336
- this.#updateUser(void 0);
337
- this.#updateStatus("initial");
338
- return Promise.resolve();
339
- }
340
- on(event, listener) {
341
- return this.#eventEmitter.on(event, listener);
342
- }
343
- async #getCredential(address, chainId, appDid) {
344
- if (!this.#baseUrl) throw new Error("RENOWN_URL is not set");
345
- const url = new URL(`/api/auth/credential?address=${encodeURIComponent(address)}&chainId=${encodeURIComponent(chainId)}&connectId=${encodeURIComponent(appDid)}&appId=${encodeURIComponent(appDid)}`, this.#baseUrl);
346
- const response = await fetch(url, { method: "GET" });
347
- if (response.ok) return (await response.json()).credential;
348
- else throw new Error(`Failed to get credential: ${response.status}`);
349
- }
350
- async verifyBearerToken(token) {
351
- return verifyAuthBearerToken(token);
352
- }
353
- async getBearerToken(options) {
354
- if (!this.user) throw new Error("User not found");
355
- return this.#crypto.getBearerToken(this.user.address, options);
356
- }
357
- };
358
- //#endregion
359
- //#region src/event/memory.ts
360
- var MemoryEventEmitter = class {
361
- #listeners = /* @__PURE__ */ new Map();
362
- on(event, listener) {
363
- if (!this.#listeners.has(event)) this.#listeners.set(event, /* @__PURE__ */ new Set());
364
- this.#listeners.get(event).add(listener);
365
- return () => {
366
- this.#listeners.get(event)?.delete(listener);
367
- };
368
- }
369
- emit(event, data) {
370
- this.#listeners.get(event)?.forEach((listener) => listener(data));
371
- }
372
- };
373
- //#endregion
374
- //#region src/profile.ts
375
- const fetchRenownProfile = async (user, baseUrl) => {
376
- try {
377
- const response = await fetch(`${baseUrl.replace(/\/+$/, "")}/api/profile`, {
378
- method: "POST",
379
- headers: { "Content-Type": "application/json" },
380
- body: JSON.stringify({ ethAddress: user.address })
381
- });
382
- if (!response.ok) return;
383
- return (await response.json()).profile ?? void 0;
384
- } catch {
385
- return;
386
- }
387
- };
388
- //#endregion
389
- //#region src/renown-builder.ts
390
- /**
391
- * Base builder for creating Renown instances.
392
- * Use platform-specific builders (RenownBuilder from init.browser.js or init.node.js)
393
- * for pre-configured defaults.
394
- */
395
- var BaseRenownBuilder = class BaseRenownBuilder {
396
- #appName;
397
- #storage;
398
- #eventEmitter;
399
- #crypto;
400
- #keyPairStorage;
401
- #baseUrl;
402
- #profileFetcher;
403
- /**
404
- * @param appName - Application name used for signing context
405
- */
406
- constructor(appName) {
407
- this.#appName = appName;
408
- }
409
- /**
410
- * Set custom storage for user data persistence.
411
- * Defaults to in-memory storage if not set.
412
- */
413
- withStorage(storage) {
414
- this.#storage = storage;
415
- return this;
416
- }
417
- /**
418
- * Set custom event emitter for user state changes.
419
- * Defaults to in-memory event emitter if not set.
420
- */
421
- withEventEmitter(eventEmitter) {
422
- this.#eventEmitter = eventEmitter;
423
- return this;
424
- }
425
- /**
426
- * Set a pre-built crypto instance.
427
- * Either crypto or keyPairStorage must be provided.
428
- */
429
- withCrypto(crypto) {
430
- this.#crypto = crypto;
431
- return this;
432
- }
433
- /**
434
- * Set key pair storage for cryptographic keys.
435
- * A crypto instance will be built from this storage.
436
- * Either crypto or keyPairStorage must be provided.
437
- */
438
- withKeyPairStorage(keyPairStorage) {
439
- this.#keyPairStorage = keyPairStorage;
440
- return this;
441
- }
442
- /**
443
- * Set the Renown server URL for credential verification.
444
- * Defaults to https://www.renown.id
445
- */
446
- withBaseUrl(baseUrl) {
447
- this.#baseUrl = baseUrl;
448
- return this;
449
- }
450
- /**
451
- * Set a profile fetcher strategy for enriching user data after login.
452
- * The fetcher receives the authenticated user and the base URL,
453
- * and returns a RenownProfile. Called in the background after each login.
454
- * Defaults to fetchRenownProfile which calls the Renown API.
455
- */
456
- withProfileFetcher(profileFetcher) {
457
- this.#profileFetcher = profileFetcher;
458
- return this;
459
- }
460
- /**
461
- * Build and initialize the Renown instance.
462
- * If a user is stored, attempts to re-authenticate them.
463
- * @throws Error if neither crypto nor keyPairStorage is provided
464
- */
465
- async build() {
466
- if (!this.#crypto && !this.#keyPairStorage) throw new Error("Either crypto or keyPairStorage is required. Use withCrypto() or withKeyPairStorage() to set one.");
467
- const crypto = this.#crypto ?? await new RenownCryptoBuilder().withKeyPairStorage(this.#keyPairStorage).build();
468
- const storage = this.#storage ?? new RenownMemoryStorage();
469
- const eventEmitter = this.#eventEmitter ?? new MemoryEventEmitter();
470
- const baseUrl = this.#baseUrl ?? "https://www.renown.id";
471
- const renown = new Renown(storage, eventEmitter, crypto, this.#appName, baseUrl, this.#profileFetcher ?? fetchRenownProfile);
472
- if (renown.user) try {
473
- await renown.login(renown.user.did);
474
- } catch (error) {
475
- console.error("Failed to re-authenticate user:", error);
476
- }
477
- return renown;
478
- }
479
- /**
480
- * Create a BaseRenownBuilder from options object for a more concise API
481
- */
482
- static from(options) {
483
- const builder = new BaseRenownBuilder(options.appName);
484
- if (options.storage) builder.withStorage(options.storage);
485
- if (options.eventEmitter) builder.withEventEmitter(options.eventEmitter);
486
- if (options.crypto) builder.withCrypto(options.crypto);
487
- if (options.keyPairStorage) builder.withKeyPairStorage(options.keyPairStorage);
488
- if (options.baseUrl) builder.withBaseUrl(options.baseUrl);
489
- if (options.profileFetcher) builder.withProfileFetcher(options.profileFetcher);
490
- return builder;
491
- }
492
- };
493
- //#endregion
494
- export { DOMAIN_TYPE as _, BaseStorage as a, recoverCredentialSigner as c, CREDENTIAL_SCHEMA_EIP712_TYPE as d, CREDENTIAL_SUBJECT_TYPE as f, DEFAULT_RENOWN_URL as g, DEFAULT_RENOWN_NETWORK_ID as h, RenownMemoryStorage as i, verifyAuthCredential as l, DEFAULT_RENOWN_CHAIN_ID as m, fetchRenownProfile as n, buildAndSignCredential as o, CREDENTIAL_TYPES as p, Renown as r, fetchDelegationCredential as s, BaseRenownBuilder as t, verifyCredentialSignature as u, ISSUER_TYPE as v, VERIFIABLE_CREDENTIAL_EIP712_TYPE as y };
495
-
496
- //# sourceMappingURL=renown-builder-Sn_Y2Cb2.js.map