@universal-everything/sdk-core-privy 1.0.0-dev.cb4e80f
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/LICENSE +201 -0
- package/README.md +59 -0
- package/dist/index.d.ts +87 -0
- package/dist/index.js +303 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023 LUKSO Blockchain GmbH
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @universal-everything/sdk-core-privy
|
|
2
|
+
|
|
3
|
+
Privy implementations of the `IdentityProvider` and
|
|
4
|
+
`KeyCustodyProvider` interfaces from
|
|
5
|
+
[`@universal-everything/sdk-core`](../sdk-core/). This is what the
|
|
6
|
+
relayer uses in production; your app doesn't normally import this
|
|
7
|
+
package directly — you just hand the relayer a Privy access token and
|
|
8
|
+
the server does the rest.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @universal-everything/sdk-core-privy @universal-everything/sdk-core
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Server-side use (inside the relayer)
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import {
|
|
20
|
+
PrivyIdentityProvider,
|
|
21
|
+
PrivyKeyCustodyProvider,
|
|
22
|
+
} from "@universal-everything/sdk-core-privy";
|
|
23
|
+
|
|
24
|
+
const identity = new PrivyIdentityProvider({
|
|
25
|
+
appId: process.env.PRIVY_APP_ID!,
|
|
26
|
+
issuer: "privy.io",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const custody = new PrivyKeyCustodyProvider({
|
|
30
|
+
appId: process.env.PRIVY_APP_ID!,
|
|
31
|
+
appSecret: process.env.PRIVY_APP_SECRET!,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// `identity.verify(jwt)` → `{ subject: "did:privy:…", isAdmin }`
|
|
35
|
+
// `custody.signTransaction({ walletId, tx, opContext })` → serialized tx
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## What each provider does
|
|
39
|
+
|
|
40
|
+
- **`PrivyIdentityProvider`** — verifies ES256 JWTs via Privy's JWKS,
|
|
41
|
+
checks `iss`/`aud` claims, returns a subject identity. No secrets
|
|
42
|
+
needed (public-key verification only).
|
|
43
|
+
- **`PrivyKeyCustodyProvider`** — calls Privy's REST API
|
|
44
|
+
(`api.privy.io`) to create / import / sign with user-owned wallets.
|
|
45
|
+
Requires the Privy app secret. Forwards
|
|
46
|
+
`privy-authorization-signature` + `privy-request-expiry` headers
|
|
47
|
+
from the caller's `opContext` so user-owned wallets are signable
|
|
48
|
+
end-to-end.
|
|
49
|
+
|
|
50
|
+
## Client-side (browser / React Native)
|
|
51
|
+
|
|
52
|
+
If you need the per-op authorization signature on the client, use the
|
|
53
|
+
official Privy SDK directly — `@privy-io/react-auth`,
|
|
54
|
+
`@privy-io/expo`, or the platform-native SDK. Feed the result into
|
|
55
|
+
your `WalletKitClient` calls via the `opContext` option.
|
|
56
|
+
|
|
57
|
+
This package does not re-export client SDKs — the whole point of the
|
|
58
|
+
architecture is that the relayer is the only component that holds the
|
|
59
|
+
Privy app secret.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { IdentityProvider, IdentityError, KeyCustodyProvider, KeyCustodyOpContext, WalletInfo, Hex, ExportedKey, SignTxRequest } from '@universal-everything/sdk-core';
|
|
2
|
+
|
|
3
|
+
type PrivyIdentityProviderConfig = {
|
|
4
|
+
/** Privy app id (e.g. `cluvxxx...`). Used as the JWT audience and to derive the JWKS URL. */
|
|
5
|
+
appId: string;
|
|
6
|
+
/** Override the issuer; defaults to `privy.io`. */
|
|
7
|
+
issuer?: string;
|
|
8
|
+
/** Override the JWKS URL; defaults to `https://auth.privy.io/api/v1/apps/${appId}/jwks.json`. */
|
|
9
|
+
jwksUrl?: string;
|
|
10
|
+
/** Clock skew tolerance in seconds. Defaults to 5. */
|
|
11
|
+
clockToleranceSeconds?: number;
|
|
12
|
+
};
|
|
13
|
+
declare function createPrivyIdentityProvider(config: PrivyIdentityProviderConfig): IdentityProvider;
|
|
14
|
+
/**
|
|
15
|
+
* Map a jose verification failure to an `IdentityError`, distinguishing
|
|
16
|
+
* **client** token problems (401 — bad/expired/forged token) from **provider**
|
|
17
|
+
* outages (503 — Privy/JWKS unreachable or broken). Exported for unit testing.
|
|
18
|
+
*
|
|
19
|
+
* The provider bucket is what a Privy-health alert keys on (DEV-16308): a JWKS
|
|
20
|
+
* fetch that timed out, failed at the transport layer (a non-jose error is only
|
|
21
|
+
* thrown here by the JWKS fetch), or returned a malformed document is a
|
|
22
|
+
* dependency outage — it must never be reported to the caller as a 401 that
|
|
23
|
+
* blames their token.
|
|
24
|
+
*/
|
|
25
|
+
declare function mapJoseError(err: unknown): IdentityError;
|
|
26
|
+
|
|
27
|
+
type PrivyKeyCustodyProviderConfig = {
|
|
28
|
+
/** Privy app id; same value as the identity provider config. */
|
|
29
|
+
appId: string;
|
|
30
|
+
/** Privy app secret. Required — used to authenticate REST calls. */
|
|
31
|
+
appSecret: string;
|
|
32
|
+
/** Privy REST base URL. Defaults to production. */
|
|
33
|
+
baseUrl?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Fetch impl override. Defaults to the global `fetch`. Tests inject a
|
|
36
|
+
* mock here to assert request shape without hitting the network.
|
|
37
|
+
*/
|
|
38
|
+
fetch?: typeof fetch;
|
|
39
|
+
/**
|
|
40
|
+
* User id to bind to newly-created wallets. When set, wallets created
|
|
41
|
+
* through this provider are **user-owned** (the owner's passkey
|
|
42
|
+
* produces the per-op authorization signature). Leave undefined to
|
|
43
|
+
* create app-owned wallets (the app secret alone authorizes
|
|
44
|
+
* operations — only use for internal / non-custodial slots).
|
|
45
|
+
*/
|
|
46
|
+
ownerUserId?: string;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Privy implementation of KeyCustodyProvider. Talks to the Privy REST
|
|
50
|
+
* API directly via `fetch`.
|
|
51
|
+
*
|
|
52
|
+
* Endpoints used:
|
|
53
|
+
* - `POST /v1/wallets` — create a (user-owned if `ownerUserId` set) wallet.
|
|
54
|
+
* - `POST /v1/wallets/{id}/rpc` — signing RPCs (`secp256k1_sign`,
|
|
55
|
+
* `personal_sign`, `eth_signTransaction`).
|
|
56
|
+
*
|
|
57
|
+
* Auth:
|
|
58
|
+
* - `Authorization: Basic base64(appId:appSecret)` + `privy-app-id: {appId}`.
|
|
59
|
+
* - When the caller supplies `opContext.authorizationSignature`, it's
|
|
60
|
+
* forwarded as `privy-authorization-signature`. Required for
|
|
61
|
+
* user-owned wallet operations; Privy rejects unsigned requests
|
|
62
|
+
* against those wallets.
|
|
63
|
+
*
|
|
64
|
+
* Not implemented yet (out of scope for this slice):
|
|
65
|
+
* - `importWallet` — needs Privy's HPKE `/v1/wallets/import/init` flow
|
|
66
|
+
* (fetch encryption pubkey, encrypt plaintext client-side under it,
|
|
67
|
+
* submit in follow-up call). Tracked under Milestone 10's cryptobox.
|
|
68
|
+
* - `exportWallet` — Privy's REST surface does not expose plaintext
|
|
69
|
+
* export. We surface a clear error so callers don't silently block.
|
|
70
|
+
*/
|
|
71
|
+
declare class PrivyKeyCustodyProvider implements KeyCustodyProvider {
|
|
72
|
+
#private;
|
|
73
|
+
constructor(config: PrivyKeyCustodyProviderConfig);
|
|
74
|
+
createWallet(op?: KeyCustodyOpContext): Promise<WalletInfo>;
|
|
75
|
+
importWallet(_privateKey: Hex, _op?: KeyCustodyOpContext): Promise<WalletInfo>;
|
|
76
|
+
exportWallet(_walletId: string, _op?: KeyCustodyOpContext): Promise<ExportedKey>;
|
|
77
|
+
signDigest(walletId: string, digest32: Uint8Array | Hex, op?: KeyCustodyOpContext): Promise<Hex>;
|
|
78
|
+
personalSign(walletId: string, message: string | Uint8Array, op?: KeyCustodyOpContext): Promise<Hex>;
|
|
79
|
+
signTransaction(walletId: string, tx: SignTxRequest, op?: KeyCustodyOpContext): Promise<Hex>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Factory. We keep it separate so consumers don't have to reach for the
|
|
83
|
+
* class directly — mirrors the identity-provider factory pattern.
|
|
84
|
+
*/
|
|
85
|
+
declare function createPrivyKeyCustodyProvider(config: PrivyKeyCustodyProviderConfig): KeyCustodyProvider;
|
|
86
|
+
|
|
87
|
+
export { type PrivyIdentityProviderConfig, PrivyKeyCustodyProvider, type PrivyKeyCustodyProviderConfig, createPrivyIdentityProvider, createPrivyKeyCustodyProvider, mapJoseError };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// src/identity.ts
|
|
2
|
+
import {
|
|
3
|
+
IdentityError
|
|
4
|
+
} from "@universal-everything/sdk-core";
|
|
5
|
+
import {
|
|
6
|
+
createRemoteJWKSet,
|
|
7
|
+
errors as joseErrors,
|
|
8
|
+
jwtVerify
|
|
9
|
+
} from "jose";
|
|
10
|
+
var DEFAULT_ISSUER = "privy.io";
|
|
11
|
+
function createPrivyIdentityProvider(config) {
|
|
12
|
+
const issuer = config.issuer ?? DEFAULT_ISSUER;
|
|
13
|
+
const jwksUrl = new URL(
|
|
14
|
+
config.jwksUrl ?? `https://auth.privy.io/api/v1/apps/${config.appId}/jwks.json`
|
|
15
|
+
);
|
|
16
|
+
const jwks = createRemoteJWKSet(jwksUrl);
|
|
17
|
+
const tolerance = config.clockToleranceSeconds ?? 5;
|
|
18
|
+
return {
|
|
19
|
+
async verifyToken(token, opts) {
|
|
20
|
+
const verifyOpts = {
|
|
21
|
+
issuer,
|
|
22
|
+
// Privy bakes the appId into `aud`; respect a caller override if present.
|
|
23
|
+
audience: opts?.audience ?? config.appId,
|
|
24
|
+
clockTolerance: tolerance
|
|
25
|
+
};
|
|
26
|
+
let payload;
|
|
27
|
+
try {
|
|
28
|
+
({ payload } = await jwtVerify(token, jwks, verifyOpts));
|
|
29
|
+
} catch (err) {
|
|
30
|
+
throw mapJoseError(err);
|
|
31
|
+
}
|
|
32
|
+
const subject = payload.sub;
|
|
33
|
+
if (!subject || typeof subject !== "string") {
|
|
34
|
+
throw new IdentityError("missing_subject", "token has no `sub` claim");
|
|
35
|
+
}
|
|
36
|
+
if (opts?.authorizedParties && opts.authorizedParties.length > 0) {
|
|
37
|
+
const azp = payload.azp;
|
|
38
|
+
if (typeof azp !== "string" || !opts.authorizedParties.includes(azp)) {
|
|
39
|
+
throw new IdentityError(
|
|
40
|
+
"azp_mismatch",
|
|
41
|
+
`token azp ${JSON.stringify(azp)} not in authorized parties`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { subject, claims: payload };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function mapJoseError(err) {
|
|
50
|
+
if (err instanceof joseErrors.JWTExpired) {
|
|
51
|
+
return new IdentityError("expired_token", err.message);
|
|
52
|
+
}
|
|
53
|
+
if (err instanceof joseErrors.JWTClaimValidationFailed) {
|
|
54
|
+
if (err.claim === "iss")
|
|
55
|
+
return new IdentityError("issuer_mismatch", err.message);
|
|
56
|
+
if (err.claim === "aud")
|
|
57
|
+
return new IdentityError("audience_mismatch", err.message);
|
|
58
|
+
return new IdentityError("invalid_token", err.message);
|
|
59
|
+
}
|
|
60
|
+
if (err instanceof joseErrors.JWKSNoMatchingKey) {
|
|
61
|
+
return new IdentityError("jwks_unavailable", err.message);
|
|
62
|
+
}
|
|
63
|
+
if (err instanceof joseErrors.JWKSTimeout || err instanceof joseErrors.JWKSInvalid || !(err instanceof joseErrors.JOSEError)) {
|
|
64
|
+
return new IdentityError(
|
|
65
|
+
"provider_unavailable",
|
|
66
|
+
err instanceof Error ? err.message : String(err)
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return new IdentityError("invalid_token", err.message);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/key-custody.ts
|
|
73
|
+
import {
|
|
74
|
+
KeyCustodyError
|
|
75
|
+
} from "@universal-everything/sdk-core";
|
|
76
|
+
var DEFAULT_BASE_URL = "https://api.privy.io";
|
|
77
|
+
var PrivyKeyCustodyProvider = class {
|
|
78
|
+
#appId;
|
|
79
|
+
#authHeader;
|
|
80
|
+
#baseUrl;
|
|
81
|
+
#fetch;
|
|
82
|
+
#ownerUserId;
|
|
83
|
+
constructor(config) {
|
|
84
|
+
if (!config.appId)
|
|
85
|
+
throw new Error("PrivyKeyCustodyProvider: appId required");
|
|
86
|
+
if (!config.appSecret)
|
|
87
|
+
throw new Error("PrivyKeyCustodyProvider: appSecret required");
|
|
88
|
+
this.#appId = config.appId;
|
|
89
|
+
this.#authHeader = `Basic ${Buffer.from(`${config.appId}:${config.appSecret}`).toString("base64")}`;
|
|
90
|
+
this.#baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
91
|
+
this.#fetch = config.fetch ?? fetch;
|
|
92
|
+
this.#ownerUserId = config.ownerUserId;
|
|
93
|
+
}
|
|
94
|
+
async createWallet(op) {
|
|
95
|
+
const body = { chain_type: "ethereum" };
|
|
96
|
+
if (this.#ownerUserId) {
|
|
97
|
+
body.owner = { user_id: this.#ownerUserId };
|
|
98
|
+
}
|
|
99
|
+
const res = await this.#request("POST", "/v1/wallets", body, op);
|
|
100
|
+
const parsed = ensureObject(res, "create_wallet");
|
|
101
|
+
const walletId = ensureString(parsed.id, "id", "create_wallet");
|
|
102
|
+
const address = ensureHexAddress(parsed.address, "create_wallet");
|
|
103
|
+
return { walletId, address };
|
|
104
|
+
}
|
|
105
|
+
async importWallet(_privateKey, _op) {
|
|
106
|
+
throw new KeyCustodyError(
|
|
107
|
+
"provider_unavailable",
|
|
108
|
+
"PrivyKeyCustodyProvider.importWallet is not yet implemented \u2014 Privy requires an HPKE-encrypted two-step flow via /v1/wallets/import/init. Tracked under Milestone 10."
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
async exportWallet(_walletId, _op) {
|
|
112
|
+
throw new KeyCustodyError(
|
|
113
|
+
"provider_unavailable",
|
|
114
|
+
"PrivyKeyCustodyProvider.exportWallet is not supported \u2014 Privy's REST surface does not expose plaintext export by design. See Milestone 10 for the cryptobox export design that ships plaintext sealed to a client-ephemeral pubkey."
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
async signDigest(walletId, digest32, op) {
|
|
118
|
+
const hash = typeof digest32 === "string" ? digest32 : bytesToHex(digest32);
|
|
119
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(hash)) {
|
|
120
|
+
throw new KeyCustodyError(
|
|
121
|
+
"invalid_digest",
|
|
122
|
+
"digest must be exactly 32 bytes"
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const data = await this.#rpc(
|
|
126
|
+
walletId,
|
|
127
|
+
{ method: "secp256k1_sign", params: { hash } },
|
|
128
|
+
op
|
|
129
|
+
);
|
|
130
|
+
return ensureHexSignature(data.signature, "secp256k1_sign");
|
|
131
|
+
}
|
|
132
|
+
async personalSign(walletId, message, op) {
|
|
133
|
+
const payload = typeof message === "string" ? { message, encoding: "utf-8" } : { message: bytesToHex(message), encoding: "hex" };
|
|
134
|
+
const data = await this.#rpc(
|
|
135
|
+
walletId,
|
|
136
|
+
{ method: "personal_sign", params: payload },
|
|
137
|
+
op
|
|
138
|
+
);
|
|
139
|
+
return ensureHexSignature(data.signature, "personal_sign");
|
|
140
|
+
}
|
|
141
|
+
async signTransaction(walletId, tx, op) {
|
|
142
|
+
const transaction = {
|
|
143
|
+
chain_id: tx.chainId,
|
|
144
|
+
type: txTypeToNumber(tx.type ?? "eip1559")
|
|
145
|
+
};
|
|
146
|
+
if (tx.to !== void 0) transaction.to = tx.to;
|
|
147
|
+
if (tx.data !== void 0) transaction.data = tx.data;
|
|
148
|
+
if (tx.value !== void 0) transaction.value = bigintToQuantity(tx.value);
|
|
149
|
+
if (tx.gas !== void 0) transaction.gas_limit = bigintToQuantity(tx.gas);
|
|
150
|
+
if (tx.maxFeePerGas !== void 0) {
|
|
151
|
+
transaction.max_fee_per_gas = bigintToQuantity(tx.maxFeePerGas);
|
|
152
|
+
}
|
|
153
|
+
if (tx.maxPriorityFeePerGas !== void 0) {
|
|
154
|
+
transaction.max_priority_fee_per_gas = bigintToQuantity(
|
|
155
|
+
tx.maxPriorityFeePerGas
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
if (tx.nonce !== void 0) transaction.nonce = tx.nonce;
|
|
159
|
+
const data = await this.#rpc(
|
|
160
|
+
walletId,
|
|
161
|
+
{ method: "eth_signTransaction", params: { transaction } },
|
|
162
|
+
op
|
|
163
|
+
);
|
|
164
|
+
return ensureHexSignature(data.signed_transaction, "eth_signTransaction");
|
|
165
|
+
}
|
|
166
|
+
async #rpc(walletId, body, op) {
|
|
167
|
+
const res = await this.#request(
|
|
168
|
+
"POST",
|
|
169
|
+
`/v1/wallets/${encodeURIComponent(walletId)}/rpc`,
|
|
170
|
+
body,
|
|
171
|
+
op
|
|
172
|
+
);
|
|
173
|
+
const parsed = ensureObject(res, body.method);
|
|
174
|
+
const data = parsed.data;
|
|
175
|
+
if (typeof data !== "object" || data === null) {
|
|
176
|
+
throw new KeyCustodyError(
|
|
177
|
+
"provider_unavailable",
|
|
178
|
+
`Privy ${body.method} response missing data object`
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
return data;
|
|
182
|
+
}
|
|
183
|
+
async #request(method, path, body, op) {
|
|
184
|
+
const headers = {
|
|
185
|
+
Authorization: this.#authHeader,
|
|
186
|
+
"Content-Type": "application/json",
|
|
187
|
+
"privy-app-id": this.#appId
|
|
188
|
+
};
|
|
189
|
+
if (op?.authorizationSignature) {
|
|
190
|
+
headers["privy-authorization-signature"] = op.authorizationSignature;
|
|
191
|
+
}
|
|
192
|
+
if (op?.requestExpiry !== void 0) {
|
|
193
|
+
headers["privy-request-expiry"] = String(op.requestExpiry);
|
|
194
|
+
}
|
|
195
|
+
let response;
|
|
196
|
+
try {
|
|
197
|
+
response = await this.#fetch(`${this.#baseUrl}${path}`, {
|
|
198
|
+
method,
|
|
199
|
+
headers,
|
|
200
|
+
body: JSON.stringify(body)
|
|
201
|
+
});
|
|
202
|
+
} catch (err) {
|
|
203
|
+
throw new KeyCustodyError(
|
|
204
|
+
"provider_unavailable",
|
|
205
|
+
`Privy ${method} ${path} failed at transport layer: ${err instanceof Error ? err.message : String(err)}`
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if (response.ok) {
|
|
209
|
+
return await response.json();
|
|
210
|
+
}
|
|
211
|
+
const raw = await response.text();
|
|
212
|
+
let parsed = {};
|
|
213
|
+
try {
|
|
214
|
+
parsed = raw ? JSON.parse(raw) : {};
|
|
215
|
+
} catch {
|
|
216
|
+
}
|
|
217
|
+
throw mapPrivyError(response.status, parsed, raw);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
function createPrivyKeyCustodyProvider(config) {
|
|
221
|
+
return new PrivyKeyCustodyProvider(config);
|
|
222
|
+
}
|
|
223
|
+
function mapPrivyError(status, body, raw) {
|
|
224
|
+
const messageParts = [
|
|
225
|
+
`Privy returned ${status}`,
|
|
226
|
+
body.error_code ? `code=${body.error_code}` : void 0,
|
|
227
|
+
body.error ?? body.message ?? (raw || void 0),
|
|
228
|
+
body.cause ? `cause=${body.cause}` : void 0
|
|
229
|
+
].filter(Boolean);
|
|
230
|
+
const message = messageParts.join(" ");
|
|
231
|
+
if (status === 404) return new KeyCustodyError("wallet_not_found", message);
|
|
232
|
+
if (status === 401 || status === 403) {
|
|
233
|
+
return new KeyCustodyError("invalid_signature", message);
|
|
234
|
+
}
|
|
235
|
+
if (status === 429) return new KeyCustodyError("rate_limited", message);
|
|
236
|
+
if (status === 400) {
|
|
237
|
+
return new KeyCustodyError(
|
|
238
|
+
body.error_code === "invalid_hash" || body.error_code === "invalid_private_key" ? "invalid_digest" : "invalid_signature",
|
|
239
|
+
message
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return new KeyCustodyError("provider_unavailable", message);
|
|
243
|
+
}
|
|
244
|
+
function ensureObject(value, op) {
|
|
245
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
246
|
+
throw new KeyCustodyError(
|
|
247
|
+
"provider_unavailable",
|
|
248
|
+
`Privy ${op} response was not a JSON object`
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
function ensureString(value, field, op) {
|
|
254
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
255
|
+
throw new KeyCustodyError(
|
|
256
|
+
"provider_unavailable",
|
|
257
|
+
`Privy ${op} response missing string field ${field}`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return value;
|
|
261
|
+
}
|
|
262
|
+
function ensureHexAddress(value, op) {
|
|
263
|
+
if (typeof value !== "string" || !/^0x[0-9a-fA-F]{40}$/.test(value)) {
|
|
264
|
+
throw new KeyCustodyError(
|
|
265
|
+
"provider_unavailable",
|
|
266
|
+
`Privy ${op} response missing valid 20-byte hex address`
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return value;
|
|
270
|
+
}
|
|
271
|
+
function ensureHexSignature(value, op) {
|
|
272
|
+
if (typeof value !== "string" || !/^0x[0-9a-fA-F]+$/.test(value)) {
|
|
273
|
+
throw new KeyCustodyError(
|
|
274
|
+
"provider_unavailable",
|
|
275
|
+
`Privy ${op} response missing valid hex signature`
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
return value;
|
|
279
|
+
}
|
|
280
|
+
function bytesToHex(bytes) {
|
|
281
|
+
let s = "0x";
|
|
282
|
+
for (const b of bytes) s += b.toString(16).padStart(2, "0");
|
|
283
|
+
return s;
|
|
284
|
+
}
|
|
285
|
+
function bigintToQuantity(n) {
|
|
286
|
+
return `0x${n.toString(16)}`;
|
|
287
|
+
}
|
|
288
|
+
function txTypeToNumber(type) {
|
|
289
|
+
switch (type) {
|
|
290
|
+
case "legacy":
|
|
291
|
+
return 0;
|
|
292
|
+
case "eip2930":
|
|
293
|
+
return 1;
|
|
294
|
+
case "eip1559":
|
|
295
|
+
return 2;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
export {
|
|
299
|
+
PrivyKeyCustodyProvider,
|
|
300
|
+
createPrivyIdentityProvider,
|
|
301
|
+
createPrivyKeyCustodyProvider,
|
|
302
|
+
mapJoseError
|
|
303
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@universal-everything/sdk-core-privy",
|
|
3
|
+
"version": "1.0.0-dev.cb4e80f",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/universal-everything/universal-relayer.git",
|
|
7
|
+
"directory": "packages/sdk-core-privy"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"jose": "^6.2.2",
|
|
26
|
+
"@universal-everything/sdk-core": "1.0.0-dev.cb4e80f"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^26.2.0",
|
|
30
|
+
"typescript": "^5.7.0",
|
|
31
|
+
"vitest": "^4.1.10",
|
|
32
|
+
"tsup": "^8.5.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest",
|
|
38
|
+
"build": "tsup"
|
|
39
|
+
}
|
|
40
|
+
}
|