@originals/auth 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -0
- package/dist/client/index.d.ts +22 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +22 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/turnkey-client.d.ts +53 -0
- package/dist/client/turnkey-client.d.ts.map +1 -0
- package/dist/client/turnkey-client.js +268 -0
- package/dist/client/turnkey-client.js.map +1 -0
- package/dist/client/turnkey-did-signer.d.ts +54 -0
- package/dist/client/turnkey-did-signer.d.ts.map +1 -0
- package/dist/client/turnkey-did-signer.js +125 -0
- package/dist/client/turnkey-did-signer.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/server/email-auth.d.ts +42 -0
- package/dist/server/email-auth.d.ts.map +1 -0
- package/dist/server/email-auth.js +187 -0
- package/dist/server/email-auth.js.map +1 -0
- package/dist/server/index.d.ts +22 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +22 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/jwt.d.ts +49 -0
- package/dist/server/jwt.d.ts.map +1 -0
- package/dist/server/jwt.js +113 -0
- package/dist/server/jwt.js.map +1 -0
- package/dist/server/middleware.d.ts +39 -0
- package/dist/server/middleware.d.ts.map +1 -0
- package/dist/server/middleware.js +110 -0
- package/dist/server/middleware.js.map +1 -0
- package/dist/server/turnkey-client.d.ts +24 -0
- package/dist/server/turnkey-client.d.ts.map +1 -0
- package/dist/server/turnkey-client.js +118 -0
- package/dist/server/turnkey-client.js.map +1 -0
- package/dist/server/turnkey-signer.d.ts +40 -0
- package/dist/server/turnkey-signer.d.ts.map +1 -0
- package/dist/server/turnkey-signer.js +121 -0
- package/dist/server/turnkey-signer.js.map +1 -0
- package/dist/types.d.ts +155 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +79 -0
- package/src/client/index.ts +37 -0
- package/src/client/turnkey-client.ts +340 -0
- package/src/client/turnkey-did-signer.ts +189 -0
- package/src/index.ts +32 -0
- package/src/server/email-auth.ts +258 -0
- package/src/server/index.ts +38 -0
- package/src/server/jwt.ts +154 -0
- package/src/server/middleware.ts +136 -0
- package/src/server/turnkey-client.ts +152 -0
- package/src/server/turnkey-signer.ts +170 -0
- package/src/types.ts +168 -0
- package/tests/index.test.ts +25 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side Turnkey utilities
|
|
3
|
+
* Handles Turnkey client initialization and authentication flow in the browser
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { TurnkeyClient, OtpType, WalletAccount } from '@turnkey/core';
|
|
7
|
+
import type { TurnkeyWallet } from '../types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Session expired error for handling token expiration
|
|
11
|
+
*/
|
|
12
|
+
export class TurnkeySessionExpiredError extends Error {
|
|
13
|
+
constructor(message: string = 'Your Turnkey session has expired. Please log in again.') {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = 'TurnkeySessionExpiredError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Wrapper to handle token expiration errors
|
|
21
|
+
*/
|
|
22
|
+
export async function withTokenExpiration<T>(
|
|
23
|
+
fn: () => Promise<T>,
|
|
24
|
+
onExpired?: () => void
|
|
25
|
+
): Promise<T> {
|
|
26
|
+
try {
|
|
27
|
+
return await fn();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const errorStr = JSON.stringify(error);
|
|
30
|
+
if (
|
|
31
|
+
errorStr.toLowerCase().includes('api_key_expired') ||
|
|
32
|
+
errorStr.toLowerCase().includes('expired api key') ||
|
|
33
|
+
errorStr.toLowerCase().includes('"code":16')
|
|
34
|
+
) {
|
|
35
|
+
console.warn('Detected expired API key, calling onExpired');
|
|
36
|
+
if (onExpired) {
|
|
37
|
+
onExpired();
|
|
38
|
+
}
|
|
39
|
+
throw new TurnkeySessionExpiredError();
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Initialize Turnkey client with auth proxy configuration
|
|
47
|
+
*/
|
|
48
|
+
export function initializeTurnkeyClient(): TurnkeyClient {
|
|
49
|
+
// Access Vite environment variables
|
|
50
|
+
const env = (import.meta as { env?: Record<string, string | undefined> }).env;
|
|
51
|
+
const authProxyConfigId = env?.VITE_TURNKEY_AUTH_PROXY_CONFIG_ID;
|
|
52
|
+
const organizationId = env?.VITE_TURNKEY_ORGANIZATION_ID ?? '';
|
|
53
|
+
|
|
54
|
+
if (!authProxyConfigId) {
|
|
55
|
+
throw new Error('VITE_TURNKEY_AUTH_PROXY_CONFIG_ID environment variable not set');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return new TurnkeyClient({
|
|
59
|
+
authProxyConfigId,
|
|
60
|
+
organizationId,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Send OTP code to email
|
|
66
|
+
*/
|
|
67
|
+
export async function initOtp(turnkeyClient: TurnkeyClient, email: string): Promise<string> {
|
|
68
|
+
try {
|
|
69
|
+
const response = await turnkeyClient.initOtp({
|
|
70
|
+
otpType: OtpType.Email,
|
|
71
|
+
contact: email,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (!response || typeof response !== 'string') {
|
|
75
|
+
throw new Error('No OTP ID returned from Turnkey');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return response;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error('Error initializing OTP:', error);
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Failed to send OTP: ${error instanceof Error ? error.message : String(error)}`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Complete OTP authentication flow (verifies OTP and logs in/signs up)
|
|
89
|
+
*/
|
|
90
|
+
export async function completeOtp(
|
|
91
|
+
turnkeyClient: TurnkeyClient,
|
|
92
|
+
otpId: string,
|
|
93
|
+
otpCode: string,
|
|
94
|
+
email: string
|
|
95
|
+
): Promise<{ sessionToken: string; userId: string; action: 'login' | 'signup' }> {
|
|
96
|
+
try {
|
|
97
|
+
const response = await turnkeyClient.completeOtp({
|
|
98
|
+
otpId,
|
|
99
|
+
otpCode,
|
|
100
|
+
contact: email,
|
|
101
|
+
otpType: OtpType.Email,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (!response.sessionToken) {
|
|
105
|
+
throw new Error('No session token returned from completeOtp');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Fetch user info to get stable identifiers
|
|
109
|
+
const userInfo = await turnkeyClient.fetchUser();
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
sessionToken: response.sessionToken,
|
|
113
|
+
userId: userInfo.userId,
|
|
114
|
+
action: response.action === 'LOGIN' ? 'login' : 'signup',
|
|
115
|
+
};
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error('Error completing OTP:', error);
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Failed to complete OTP: ${error instanceof Error ? error.message : String(error)}`
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Fetch user information
|
|
126
|
+
*/
|
|
127
|
+
export async function fetchUser(
|
|
128
|
+
turnkeyClient: TurnkeyClient,
|
|
129
|
+
onExpired?: () => void
|
|
130
|
+
): Promise<unknown> {
|
|
131
|
+
return withTokenExpiration(async () => {
|
|
132
|
+
try {
|
|
133
|
+
const response = await turnkeyClient.fetchUser();
|
|
134
|
+
return response;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error('Error fetching user:', error);
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Failed to fetch user: ${error instanceof Error ? error.message : String(error)}`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}, onExpired);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Fetch user's wallets
|
|
146
|
+
*/
|
|
147
|
+
export async function fetchWallets(
|
|
148
|
+
turnkeyClient: TurnkeyClient,
|
|
149
|
+
onExpired?: () => void
|
|
150
|
+
): Promise<TurnkeyWallet[]> {
|
|
151
|
+
return withTokenExpiration(async () => {
|
|
152
|
+
try {
|
|
153
|
+
const response = await turnkeyClient.fetchWallets();
|
|
154
|
+
|
|
155
|
+
const wallets: TurnkeyWallet[] = [];
|
|
156
|
+
|
|
157
|
+
for (const wallet of response || []) {
|
|
158
|
+
const accountsResponse = await turnkeyClient.fetchWalletAccounts({
|
|
159
|
+
wallet: wallet,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
wallets.push({
|
|
163
|
+
walletId: wallet.walletId,
|
|
164
|
+
walletName: wallet.walletName,
|
|
165
|
+
accounts: accountsResponse.map((acc: WalletAccount) => ({
|
|
166
|
+
address: acc.address,
|
|
167
|
+
curve: acc.curve as 'CURVE_SECP256K1' | 'CURVE_ED25519',
|
|
168
|
+
path: acc.path,
|
|
169
|
+
addressFormat: acc.addressFormat,
|
|
170
|
+
})),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return wallets;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.error('Error fetching wallets:', error);
|
|
177
|
+
throw new Error(
|
|
178
|
+
`Failed to fetch wallets: ${error instanceof Error ? error.message : String(error)}`
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
}, onExpired);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Get key by curve type
|
|
186
|
+
*/
|
|
187
|
+
export function getKeyByCurve(
|
|
188
|
+
wallets: TurnkeyWallet[],
|
|
189
|
+
curve: 'CURVE_SECP256K1' | 'CURVE_ED25519'
|
|
190
|
+
): WalletAccount | null {
|
|
191
|
+
for (const wallet of wallets) {
|
|
192
|
+
for (const account of wallet.accounts) {
|
|
193
|
+
if (account.curve === curve) {
|
|
194
|
+
return account as unknown as WalletAccount;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Create a wallet with the required accounts for DID creation
|
|
203
|
+
*/
|
|
204
|
+
export async function createWalletWithAccounts(
|
|
205
|
+
turnkeyClient: TurnkeyClient,
|
|
206
|
+
onExpired?: () => void
|
|
207
|
+
): Promise<TurnkeyWallet> {
|
|
208
|
+
return withTokenExpiration(async () => {
|
|
209
|
+
try {
|
|
210
|
+
const response = await turnkeyClient.createWallet({
|
|
211
|
+
walletName: 'default-wallet',
|
|
212
|
+
accounts: [
|
|
213
|
+
{
|
|
214
|
+
curve: 'CURVE_SECP256K1',
|
|
215
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
216
|
+
path: "m/44'/0'/0'/0/0",
|
|
217
|
+
addressFormat: 'ADDRESS_FORMAT_BITCOIN_MAINNET_P2TR',
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
curve: 'CURVE_ED25519',
|
|
221
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
222
|
+
path: "m/44'/501'/0'/0'",
|
|
223
|
+
addressFormat: 'ADDRESS_FORMAT_SOLANA',
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
curve: 'CURVE_ED25519',
|
|
227
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
228
|
+
path: "m/44'/501'/1'/0'",
|
|
229
|
+
addressFormat: 'ADDRESS_FORMAT_SOLANA',
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
let walletId: string;
|
|
235
|
+
if (typeof response === 'string') {
|
|
236
|
+
walletId = response;
|
|
237
|
+
} else {
|
|
238
|
+
walletId = (response as { walletId?: string })?.walletId ?? '';
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (!walletId) {
|
|
242
|
+
throw new Error('No wallet ID returned from createWallet');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Wait for wallet to be created, then fetch it
|
|
246
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
247
|
+
|
|
248
|
+
const wallets = await fetchWallets(turnkeyClient, onExpired);
|
|
249
|
+
const createdWallet = wallets.find((w) => w.walletId === walletId);
|
|
250
|
+
|
|
251
|
+
if (!createdWallet) {
|
|
252
|
+
throw new Error('Failed to fetch created wallet');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return createdWallet;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
console.error('Error creating wallet:', error);
|
|
258
|
+
throw new Error(
|
|
259
|
+
`Failed to create wallet: ${error instanceof Error ? error.message : String(error)}`
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
}, onExpired);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Ensure user has a wallet with the required accounts for DID creation
|
|
267
|
+
*/
|
|
268
|
+
export async function ensureWalletWithAccounts(
|
|
269
|
+
turnkeyClient: TurnkeyClient,
|
|
270
|
+
onExpired?: () => void
|
|
271
|
+
): Promise<TurnkeyWallet[]> {
|
|
272
|
+
return withTokenExpiration(async () => {
|
|
273
|
+
try {
|
|
274
|
+
let wallets = await fetchWallets(turnkeyClient, onExpired);
|
|
275
|
+
|
|
276
|
+
if (wallets.length === 0) {
|
|
277
|
+
console.log('No wallets found, creating new wallet with accounts...');
|
|
278
|
+
const newWallet = await createWalletWithAccounts(turnkeyClient, onExpired);
|
|
279
|
+
wallets = [newWallet];
|
|
280
|
+
return wallets;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const defaultWallet = wallets[0];
|
|
284
|
+
const allAccounts = defaultWallet.accounts;
|
|
285
|
+
const secp256k1Accounts = allAccounts.filter((acc) => acc.curve === 'CURVE_SECP256K1');
|
|
286
|
+
const ed25519Accounts = allAccounts.filter((acc) => acc.curve === 'CURVE_ED25519');
|
|
287
|
+
|
|
288
|
+
// Check if we need more accounts
|
|
289
|
+
if (secp256k1Accounts.length >= 1 && ed25519Accounts.length >= 2) {
|
|
290
|
+
return wallets;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Need to create additional accounts
|
|
294
|
+
const accountsToCreate: Array<{
|
|
295
|
+
curve: 'CURVE_SECP256K1' | 'CURVE_ED25519';
|
|
296
|
+
pathFormat: 'PATH_FORMAT_BIP32';
|
|
297
|
+
path: string;
|
|
298
|
+
addressFormat: 'ADDRESS_FORMAT_BITCOIN_MAINNET_P2TR' | 'ADDRESS_FORMAT_SOLANA';
|
|
299
|
+
}> = [];
|
|
300
|
+
|
|
301
|
+
if (secp256k1Accounts.length === 0) {
|
|
302
|
+
accountsToCreate.push({
|
|
303
|
+
curve: 'CURVE_SECP256K1',
|
|
304
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
305
|
+
path: "m/44'/0'/0'/0/0",
|
|
306
|
+
addressFormat: 'ADDRESS_FORMAT_BITCOIN_MAINNET_P2TR',
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const ed25519Needed = 2 - ed25519Accounts.length;
|
|
311
|
+
for (let i = 0; i < ed25519Needed; i++) {
|
|
312
|
+
const pathIndex = ed25519Accounts.length + i;
|
|
313
|
+
accountsToCreate.push({
|
|
314
|
+
curve: 'CURVE_ED25519',
|
|
315
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
316
|
+
path: pathIndex === 0 ? "m/44'/501'/0'/0'" : "m/44'/501'/1'/0'",
|
|
317
|
+
addressFormat: 'ADDRESS_FORMAT_SOLANA',
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (accountsToCreate.length > 0) {
|
|
322
|
+
console.log(`Creating ${accountsToCreate.length} missing account(s)...`);
|
|
323
|
+
await turnkeyClient.createWalletAccounts({
|
|
324
|
+
walletId: defaultWallet.walletId,
|
|
325
|
+
accounts: accountsToCreate,
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
wallets = await fetchWallets(turnkeyClient, onExpired);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return wallets;
|
|
332
|
+
} catch (error) {
|
|
333
|
+
console.error('Error ensuring wallet with accounts:', error);
|
|
334
|
+
throw new Error(
|
|
335
|
+
`Failed to ensure wallet with accounts: ${error instanceof Error ? error.message : String(error)}`
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}, onExpired);
|
|
339
|
+
}
|
|
340
|
+
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turnkey DID Signer Adapter
|
|
3
|
+
* Adapts Turnkey signing to work with didwebvh-ts signer interface
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { TurnkeyClient, WalletAccount } from '@turnkey/core';
|
|
7
|
+
import { OriginalsSDK, encoding } from '@originals/sdk';
|
|
8
|
+
import { TurnkeySessionExpiredError, withTokenExpiration } from './turnkey-client';
|
|
9
|
+
|
|
10
|
+
interface SigningInput {
|
|
11
|
+
document: Record<string, unknown>;
|
|
12
|
+
proof: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface SigningOutput {
|
|
16
|
+
proofValue: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Signer that uses Turnkey for signing DID documents
|
|
21
|
+
* Compatible with didwebvh-ts signer interface
|
|
22
|
+
*/
|
|
23
|
+
export class TurnkeyDIDSigner {
|
|
24
|
+
private turnkeyClient: TurnkeyClient;
|
|
25
|
+
private walletAccount: WalletAccount;
|
|
26
|
+
private publicKeyMultibase: string;
|
|
27
|
+
private onExpired?: () => void;
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
turnkeyClient: TurnkeyClient,
|
|
31
|
+
walletAccount: WalletAccount,
|
|
32
|
+
publicKeyMultibase: string,
|
|
33
|
+
onExpired?: () => void
|
|
34
|
+
) {
|
|
35
|
+
this.turnkeyClient = turnkeyClient;
|
|
36
|
+
this.walletAccount = walletAccount;
|
|
37
|
+
this.publicKeyMultibase = publicKeyMultibase;
|
|
38
|
+
this.onExpired = onExpired;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Sign the document and proof using Turnkey
|
|
43
|
+
*/
|
|
44
|
+
async sign(input: SigningInput): Promise<SigningOutput> {
|
|
45
|
+
return withTokenExpiration(async () => {
|
|
46
|
+
try {
|
|
47
|
+
// Use SDK's prepareDIDDataForSigning
|
|
48
|
+
const dataToSign = await OriginalsSDK.prepareDIDDataForSigning(input.document, input.proof);
|
|
49
|
+
|
|
50
|
+
// Sign with Turnkey
|
|
51
|
+
const response = await this.turnkeyClient.httpClient.signRawPayload({
|
|
52
|
+
signWith: this.walletAccount.address,
|
|
53
|
+
payload: Buffer.from(dataToSign).toString('hex'),
|
|
54
|
+
encoding: 'PAYLOAD_ENCODING_HEXADECIMAL',
|
|
55
|
+
hashFunction: 'HASH_FUNCTION_NOT_APPLICABLE',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (!response.r || !response.s) {
|
|
59
|
+
throw new Error('Invalid signature response from Turnkey');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// For Ed25519, combine r+s only (64 bytes total)
|
|
63
|
+
const cleanR = response.r.startsWith('0x') ? response.r.slice(2) : response.r;
|
|
64
|
+
const cleanS = response.s.startsWith('0x') ? response.s.slice(2) : response.s;
|
|
65
|
+
const combinedHex = cleanR + cleanS;
|
|
66
|
+
|
|
67
|
+
const signatureBytes = Buffer.from(combinedHex, 'hex');
|
|
68
|
+
|
|
69
|
+
if (signatureBytes.length !== 64) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Invalid Ed25519 signature length: ${signatureBytes.length} (expected 64 bytes)`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const proofValue = encoding.multibase.encode(signatureBytes, 'base58btc');
|
|
76
|
+
|
|
77
|
+
return { proofValue };
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error('[TurnkeyDIDSigner] Error signing with Turnkey:', error);
|
|
80
|
+
|
|
81
|
+
const errorStr = JSON.stringify(error);
|
|
82
|
+
if (
|
|
83
|
+
errorStr.toLowerCase().includes('api_key_expired') ||
|
|
84
|
+
errorStr.toLowerCase().includes('expired api key') ||
|
|
85
|
+
errorStr.toLowerCase().includes('"code":16')
|
|
86
|
+
) {
|
|
87
|
+
console.warn('Detected expired API key in sign method, calling onExpired');
|
|
88
|
+
if (this.onExpired) {
|
|
89
|
+
this.onExpired();
|
|
90
|
+
}
|
|
91
|
+
throw new TurnkeySessionExpiredError();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}, this.onExpired);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get the verification method ID for this signer
|
|
101
|
+
*/
|
|
102
|
+
getVerificationMethodId(): string {
|
|
103
|
+
return `did:key:${this.publicKeyMultibase}`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Verify a signature
|
|
108
|
+
*/
|
|
109
|
+
async verify(
|
|
110
|
+
signature: Uint8Array,
|
|
111
|
+
message: Uint8Array,
|
|
112
|
+
publicKey: Uint8Array
|
|
113
|
+
): Promise<boolean> {
|
|
114
|
+
try {
|
|
115
|
+
return await OriginalsSDK.verifyDIDSignature(signature, message, publicKey);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error('[TurnkeyDIDSigner] Error verifying signature:', error);
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Create a DID:WebVH using OriginalsSDK.createDIDOriginal() with Turnkey signing
|
|
125
|
+
*/
|
|
126
|
+
export async function createDIDWithTurnkey(params: {
|
|
127
|
+
turnkeyClient: TurnkeyClient;
|
|
128
|
+
updateKeyAccount: WalletAccount;
|
|
129
|
+
authKeyPublic: string;
|
|
130
|
+
assertionKeyPublic: string;
|
|
131
|
+
updateKeyPublic: string;
|
|
132
|
+
domain: string;
|
|
133
|
+
slug: string;
|
|
134
|
+
onExpired?: () => void;
|
|
135
|
+
}): Promise<{
|
|
136
|
+
did: string;
|
|
137
|
+
didDocument: unknown;
|
|
138
|
+
didLog: unknown;
|
|
139
|
+
}> {
|
|
140
|
+
const {
|
|
141
|
+
turnkeyClient,
|
|
142
|
+
updateKeyAccount,
|
|
143
|
+
authKeyPublic,
|
|
144
|
+
assertionKeyPublic,
|
|
145
|
+
updateKeyPublic,
|
|
146
|
+
domain,
|
|
147
|
+
slug,
|
|
148
|
+
onExpired,
|
|
149
|
+
} = params;
|
|
150
|
+
|
|
151
|
+
// Create Turnkey signer for the update key
|
|
152
|
+
const signer = new TurnkeyDIDSigner(turnkeyClient, updateKeyAccount, updateKeyPublic, onExpired);
|
|
153
|
+
|
|
154
|
+
// Use SDK's createDIDOriginal
|
|
155
|
+
const result = await OriginalsSDK.createDIDOriginal({
|
|
156
|
+
type: 'did',
|
|
157
|
+
domain,
|
|
158
|
+
signer,
|
|
159
|
+
verifier: signer,
|
|
160
|
+
updateKeys: [signer.getVerificationMethodId()],
|
|
161
|
+
verificationMethods: [
|
|
162
|
+
{
|
|
163
|
+
id: '#key-0',
|
|
164
|
+
type: 'Multikey',
|
|
165
|
+
controller: '',
|
|
166
|
+
publicKeyMultibase: authKeyPublic,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
id: '#key-1',
|
|
170
|
+
type: 'Multikey',
|
|
171
|
+
controller: '',
|
|
172
|
+
publicKeyMultibase: assertionKeyPublic,
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
paths: [slug],
|
|
176
|
+
portable: false,
|
|
177
|
+
authentication: ['#key-0'],
|
|
178
|
+
assertionMethod: ['#key-1'],
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
did: result.did,
|
|
183
|
+
didDocument: result.doc,
|
|
184
|
+
didLog: result.log,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @originals/auth - Turnkey-based authentication for the Originals Protocol
|
|
3
|
+
*
|
|
4
|
+
* This package provides authentication utilities for both server and client applications.
|
|
5
|
+
*
|
|
6
|
+
* Server-side:
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { createAuthMiddleware, initiateEmailAuth, verifyEmailAuth } from '@originals/auth/server';
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Client-side (pure functions, no React):
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { initializeTurnkeyClient, initOtp, completeOtp, fetchWallets } from '@originals/auth/client';
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Types:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import type { AuthUser, TokenPayload, TurnkeyWallet } from '@originals/auth/types';
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
// Re-export types
|
|
23
|
+
export * from './types';
|
|
24
|
+
|
|
25
|
+
// Re-export server utilities (for convenience, though subpath is preferred)
|
|
26
|
+
export * from './server';
|
|
27
|
+
|
|
28
|
+
// Note: Client utilities should be imported from '@originals/auth/client'
|
|
29
|
+
// to avoid bundling React in server environments
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|