@privy-io/node 0.1.0-alpha.2 → 0.1.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.
Files changed (164) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/client.d.mts +3 -3
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +3 -3
  5. package/client.d.ts.map +1 -1
  6. package/client.js +1 -1
  7. package/client.js.map +1 -1
  8. package/client.mjs +1 -1
  9. package/client.mjs.map +1 -1
  10. package/index.d.mts +6 -0
  11. package/index.d.mts.map +1 -1
  12. package/index.d.ts +6 -0
  13. package/index.d.ts.map +1 -1
  14. package/index.js +7 -1
  15. package/index.js.map +1 -1
  16. package/index.mjs +2 -0
  17. package/index.mjs.map +1 -1
  18. package/internal/utils/values.js +3 -3
  19. package/internal/utils/values.js.map +1 -1
  20. package/internal/utils/values.mjs +3 -3
  21. package/internal/utils/values.mjs.map +1 -1
  22. package/lib/auth.d.mts +68 -0
  23. package/lib/auth.d.mts.map +1 -0
  24. package/lib/auth.d.ts +68 -0
  25. package/lib/auth.d.ts.map +1 -0
  26. package/lib/auth.js +130 -0
  27. package/lib/auth.js.map +1 -0
  28. package/lib/auth.mjs +123 -0
  29. package/lib/auth.mjs.map +1 -0
  30. package/lib/identity-token.d.mts +15 -0
  31. package/lib/identity-token.d.mts.map +1 -0
  32. package/lib/identity-token.d.ts +15 -0
  33. package/lib/identity-token.d.ts.map +1 -0
  34. package/lib/identity-token.js +273 -0
  35. package/lib/identity-token.js.map +1 -0
  36. package/lib/identity-token.mjs +268 -0
  37. package/lib/identity-token.mjs.map +1 -0
  38. package/lib/user-utils.d.mts +22 -0
  39. package/lib/user-utils.d.mts.map +1 -0
  40. package/lib/user-utils.d.ts +22 -0
  41. package/lib/user-utils.d.ts.map +1 -0
  42. package/lib/user-utils.js +15 -0
  43. package/lib/user-utils.js.map +1 -0
  44. package/lib/user-utils.mjs +11 -0
  45. package/lib/user-utils.mjs.map +1 -0
  46. package/package.json +19 -1
  47. package/public-api/PrivyClient.d.mts +2 -1
  48. package/public-api/PrivyClient.d.mts.map +1 -1
  49. package/public-api/PrivyClient.d.ts +2 -1
  50. package/public-api/PrivyClient.d.ts.map +1 -1
  51. package/public-api/PrivyClient.js +10 -3
  52. package/public-api/PrivyClient.js.map +1 -1
  53. package/public-api/PrivyClient.mjs +10 -3
  54. package/public-api/PrivyClient.mjs.map +1 -1
  55. package/public-api/services/ethereum.d.mts.map +1 -1
  56. package/public-api/services/ethereum.d.ts.map +1 -1
  57. package/public-api/services/ethereum.js +0 -6
  58. package/public-api/services/ethereum.js.map +1 -1
  59. package/public-api/services/ethereum.mjs +0 -6
  60. package/public-api/services/ethereum.mjs.map +1 -1
  61. package/public-api/services/solana.d.mts.map +1 -1
  62. package/public-api/services/solana.d.ts.map +1 -1
  63. package/public-api/services/solana.js +0 -3
  64. package/public-api/services/solana.js.map +1 -1
  65. package/public-api/services/solana.mjs +0 -3
  66. package/public-api/services/solana.mjs.map +1 -1
  67. package/public-api/services/users.d.mts +23 -1
  68. package/public-api/services/users.d.mts.map +1 -1
  69. package/public-api/services/users.d.ts +23 -1
  70. package/public-api/services/users.d.ts.map +1 -1
  71. package/public-api/services/users.js +24 -0
  72. package/public-api/services/users.js.map +1 -1
  73. package/public-api/services/users.mjs +24 -0
  74. package/public-api/services/users.mjs.map +1 -1
  75. package/public-api/services/utils/auth.d.mts +18 -0
  76. package/public-api/services/utils/auth.d.mts.map +1 -0
  77. package/public-api/services/utils/auth.d.ts +18 -0
  78. package/public-api/services/utils/auth.d.ts.map +1 -0
  79. package/public-api/services/utils/auth.js +33 -0
  80. package/public-api/services/utils/auth.js.map +1 -0
  81. package/public-api/services/utils/auth.mjs +29 -0
  82. package/public-api/services/utils/auth.mjs.map +1 -0
  83. package/public-api/services/utils/request-formatter.d.mts +5 -0
  84. package/public-api/services/utils/request-formatter.d.mts.map +1 -0
  85. package/public-api/services/utils/request-formatter.d.ts +5 -0
  86. package/public-api/services/utils/request-formatter.d.ts.map +1 -0
  87. package/public-api/services/utils/request-formatter.js +12 -0
  88. package/public-api/services/utils/request-formatter.js.map +1 -0
  89. package/public-api/services/utils/request-formatter.mjs +8 -0
  90. package/public-api/services/utils/request-formatter.mjs.map +1 -0
  91. package/public-api/services/utils/request-signer.d.mts +25 -0
  92. package/public-api/services/utils/request-signer.d.mts.map +1 -0
  93. package/public-api/services/utils/request-signer.d.ts +25 -0
  94. package/public-api/services/utils/request-signer.d.ts.map +1 -0
  95. package/public-api/services/utils/request-signer.js +17 -0
  96. package/public-api/services/utils/request-signer.js.map +1 -0
  97. package/public-api/services/utils/request-signer.mjs +13 -0
  98. package/public-api/services/utils/request-signer.mjs.map +1 -0
  99. package/public-api/services/utils.d.mts +12 -22
  100. package/public-api/services/utils.d.mts.map +1 -1
  101. package/public-api/services/utils.d.ts +12 -22
  102. package/public-api/services/utils.d.ts.map +1 -1
  103. package/public-api/services/utils.js +15 -9
  104. package/public-api/services/utils.js.map +1 -1
  105. package/public-api/services/utils.mjs +16 -10
  106. package/public-api/services/utils.mjs.map +1 -1
  107. package/resources/index.d.mts +1 -1
  108. package/resources/index.d.mts.map +1 -1
  109. package/resources/index.d.ts +1 -1
  110. package/resources/index.d.ts.map +1 -1
  111. package/resources/policies.d.mts +2 -1
  112. package/resources/policies.d.mts.map +1 -1
  113. package/resources/policies.d.ts +2 -1
  114. package/resources/policies.d.ts.map +1 -1
  115. package/resources/users.d.mts +35 -24
  116. package/resources/users.d.mts.map +1 -1
  117. package/resources/users.d.ts +35 -24
  118. package/resources/users.d.ts.map +1 -1
  119. package/resources/users.js +16 -16
  120. package/resources/users.js.map +1 -1
  121. package/resources/users.mjs +16 -16
  122. package/resources/users.mjs.map +1 -1
  123. package/resources/wallets/wallets.d.mts +3 -18
  124. package/resources/wallets/wallets.d.mts.map +1 -1
  125. package/resources/wallets/wallets.d.ts +3 -18
  126. package/resources/wallets/wallets.d.ts.map +1 -1
  127. package/resources/wallets/wallets.js.map +1 -1
  128. package/resources/wallets/wallets.mjs.map +1 -1
  129. package/src/client.ts +4 -4
  130. package/src/index.ts +15 -0
  131. package/src/internal/utils/values.ts +3 -3
  132. package/src/lib/auth.ts +210 -0
  133. package/src/lib/identity-token.ts +280 -0
  134. package/src/lib/user-utils.ts +31 -0
  135. package/src/public-api/PrivyClient.ts +12 -2
  136. package/src/public-api/services/ethereum.ts +0 -8
  137. package/src/public-api/services/solana.ts +0 -4
  138. package/src/public-api/services/users.ts +36 -2
  139. package/src/public-api/services/utils/auth.ts +41 -0
  140. package/src/public-api/services/utils/request-formatter.ts +6 -0
  141. package/src/public-api/services/utils/request-signer.ts +35 -0
  142. package/src/public-api/services/utils.ts +21 -31
  143. package/src/resources/index.ts +1 -1
  144. package/src/resources/policies.ts +2 -1
  145. package/src/resources/users.ts +50 -29
  146. package/src/resources/wallets/wallets.ts +6 -30
  147. package/src/version.ts +1 -1
  148. package/src/viem.ts +223 -0
  149. package/version.d.mts +1 -1
  150. package/version.d.mts.map +1 -1
  151. package/version.d.ts +1 -1
  152. package/version.d.ts.map +1 -1
  153. package/version.js +1 -1
  154. package/version.js.map +1 -1
  155. package/version.mjs +1 -1
  156. package/version.mjs.map +1 -1
  157. package/viem.d.mts +22 -0
  158. package/viem.d.mts.map +1 -0
  159. package/viem.d.ts +22 -0
  160. package/viem.d.ts.map +1 -0
  161. package/viem.js +183 -0
  162. package/viem.js.map +1 -0
  163. package/viem.mjs +180 -0
  164. package/viem.mjs.map +1 -0
@@ -0,0 +1,35 @@
1
+ import { generateAuthorizationSignature, generateAuthorizationSignatures } from '../../../lib/authorization';
2
+ import { PrivyClient } from '../../PrivyClient';
3
+
4
+ export class PrivyRequestSigner {
5
+ private privyClient: PrivyClient;
6
+
7
+ constructor(privyClient: PrivyClient) {
8
+ this.privyClient = privyClient;
9
+ }
10
+
11
+ // Expose these as methods here for convenience
12
+ public generateAuthorizationSignature = generateAuthorizationSignature;
13
+ public generateAuthorizationSignatures(...input: PrivyRequestSigner.GenerateAuthorizationSignaturesInput) {
14
+ // Pre-populates the client instance for the function
15
+ return generateAuthorizationSignatures(this.privyClient, ...input);
16
+ }
17
+ }
18
+
19
+ // prettier-ignore
20
+ export namespace PrivyRequestSigner {
21
+ export type GenerateAuthorizationSignaturesInput = ParametersExceptFirst<typeof generateAuthorizationSignatures>;
22
+ }
23
+
24
+ /**
25
+ * Utility type similar to `Parameters` but excluding the first argument.
26
+ * This is used to get the types of the remaining arguments of a function, excluding the first one.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * type MyFunction = (arg0: string, arg1: number, arg2: boolean) => any;
31
+ * type MyFunctionParameters = ParametersExceptFirst<MyFunction>;
32
+ * // type MyFunctionParameters = [number, boolean]
33
+ * ```
34
+ */
35
+ type ParametersExceptFirst<F> = F extends (arg0: any, ...rest: infer R) => any ? R : never;
@@ -1,40 +1,30 @@
1
- import {
2
- formatRequestForAuthorizationSignature,
3
- generateAuthorizationSignature,
4
- generateAuthorizationSignatures,
5
- } from '../../lib/authorization';
1
+ import { PrivyAppJWKS } from '../../lib/auth';
2
+ import { PrivyAPI } from '../../client';
6
3
  import { PrivyClient } from '../PrivyClient';
4
+ import { PrivyAuthUtils } from './utils/auth';
5
+ import { PrivyRequestFormatter } from './utils/request-formatter';
6
+ import { PrivyRequestSigner } from './utils/request-signer';
7
7
 
8
8
  export class PrivyUtils {
9
- private privyClient: PrivyClient;
9
+ private _requestSigner: PrivyRequestSigner;
10
+ private _requestFormatter: PrivyRequestFormatter;
11
+ private _auth: PrivyAuthUtils;
10
12
 
11
- constructor(privyClient: PrivyClient) {
12
- this.privyClient = privyClient;
13
+ constructor(privyApiClient: PrivyAPI, privyClient: PrivyClient, appJwks: PrivyAppJWKS) {
14
+ this._requestSigner = new PrivyRequestSigner(privyClient);
15
+ this._requestFormatter = new PrivyRequestFormatter();
16
+ this._auth = new PrivyAuthUtils(privyApiClient, appJwks);
13
17
  }
14
18
 
15
- // Expose these as methods here for convenience
16
- public formatRequestForAuthorizationSignature = formatRequestForAuthorizationSignature;
17
- public generateAuthorizationSignature = generateAuthorizationSignature;
18
- public generateAuthorizationSignatures(...input: PrivyUtils.GenerateAuthorizationSignaturesInput) {
19
- // Pre-populates the client instance for the function
20
- return generateAuthorizationSignatures(this.privyClient, ...input);
19
+ public requestSigner(): PrivyRequestSigner {
20
+ return this._requestSigner;
21
21
  }
22
- }
23
22
 
24
- // prettier-ignore
25
- export namespace PrivyUtils {
26
- export type GenerateAuthorizationSignaturesInput = ParametersExceptFirst<typeof generateAuthorizationSignatures>;
27
- }
23
+ public requestFormatter(): PrivyRequestFormatter {
24
+ return this._requestFormatter;
25
+ }
28
26
 
29
- /**
30
- * Utility type similar to `Parameters` but excluding the first argument.
31
- * This is used to get the types of the remaining arguments of a function, excluding the first one.
32
- *
33
- * @example
34
- * ```ts
35
- * type MyFunction = (arg0: string, arg1: number, arg2: boolean) => any;
36
- * type MyFunctionParameters = ParametersExceptFirst<MyFunction>;
37
- * // type MyFunctionParameters = [number, boolean]
38
- * ```
39
- */
40
- type ParametersExceptFirst<F> = F extends (arg0: any, ...rest: infer R) => any ? R : never;
27
+ public auth(): PrivyAuthUtils {
28
+ return this._auth;
29
+ }
30
+ }
@@ -30,7 +30,6 @@ export {
30
30
  type User,
31
31
  type UserCreateParams,
32
32
  type UserListParams,
33
- type UserCreateCustomMetadataParams,
34
33
  type UserGetByCustomAuthIDParams,
35
34
  type UserGetByDiscordUsernameParams,
36
35
  type UserGetByEmailAddressParams,
@@ -45,6 +44,7 @@ export {
45
44
  type UserGetByWalletAddressParams,
46
45
  type UserPregenerateWalletsParams,
47
46
  type UserSearchParams,
47
+ type UserSetCustomMetadataParams,
48
48
  type UserUnlinkLinkedAccountParams,
49
49
  type UsersCursor,
50
50
  } from './users';
@@ -290,7 +290,8 @@ export interface Policy {
290
290
 
291
291
  export namespace Policy {
292
292
  /**
293
- * The rules that apply to each method the policy covers.
293
+ * A rule that defines the conditions and action to take if the conditions are
294
+ * true.
294
295
  */
295
296
  export interface Rule {
296
297
  id: string;
@@ -58,34 +58,15 @@ export class Users extends APIResource {
58
58
  });
59
59
  }
60
60
 
61
- /**
62
- * Adds custom metadata to a user by user ID.
63
- *
64
- * @example
65
- * ```ts
66
- * const user = await client.users.createCustomMetadata(
67
- * 'user_id',
68
- * { custom_metadata: { key: 'value' } },
69
- * );
70
- * ```
71
- */
72
- createCustomMetadata(
73
- userID: string,
74
- body: UserCreateCustomMetadataParams,
75
- options?: RequestOptions,
76
- ): APIPromise<User> {
77
- return this._client.post(path`/v1/users/${userID}/custom_metadata`, { body, ...options });
78
- }
79
-
80
61
  /**
81
62
  * Get a user by user ID.
82
63
  *
83
64
  * @example
84
65
  * ```ts
85
- * const user = await client.users.get('user_id');
66
+ * const user = await client.users._get('user_id');
86
67
  * ```
87
68
  */
88
- get(userID: string, options?: RequestOptions): APIPromise<User> {
69
+ _get(userID: string, options?: RequestOptions): APIPromise<User> {
89
70
  return this._client.get(path`/v1/users/${userID}`, options);
90
71
  }
91
72
 
@@ -293,6 +274,25 @@ export class Users extends APIResource {
293
274
  return this._client.post('/v1/users/search', { body, ...options });
294
275
  }
295
276
 
277
+ /**
278
+ * Adds custom metadata to a user by user ID.
279
+ *
280
+ * @example
281
+ * ```ts
282
+ * const user = await client.users.setCustomMetadata(
283
+ * 'user_id',
284
+ * { custom_metadata: { key: 'value' } },
285
+ * );
286
+ * ```
287
+ */
288
+ setCustomMetadata(
289
+ userID: string,
290
+ body: UserSetCustomMetadataParams,
291
+ options?: RequestOptions,
292
+ ): APIPromise<User> {
293
+ return this._client.post(path`/v1/users/${userID}/custom_metadata`, { body, ...options });
294
+ }
295
+
296
296
  /**
297
297
  * Unlinks a user linked account.
298
298
  *
@@ -352,6 +352,7 @@ export interface User {
352
352
  | User.LinkedAccountSmartWallet
353
353
  | User.LinkedAccountPasskey
354
354
  | User.LinkedAccountFarcaster
355
+ | User.LinkedAccountTelegram
355
356
  | User.LinkedAccountEthereum
356
357
  | User.LinkedAccountEthereumEmbeddedWallet
357
358
  | User.LinkedAccountSolana
@@ -685,6 +686,26 @@ export namespace User {
685
686
  username?: string;
686
687
  }
687
688
 
689
+ export interface LinkedAccountTelegram {
690
+ first_verified_at: number | null;
691
+
692
+ latest_verified_at: number | null;
693
+
694
+ telegram_user_id: string;
695
+
696
+ type: 'telegram';
697
+
698
+ verified_at: number;
699
+
700
+ first_name?: string | null;
701
+
702
+ last_name?: string | null;
703
+
704
+ photo_url?: string | null;
705
+
706
+ username?: string | null;
707
+ }
708
+
688
709
  export interface LinkedAccountEthereum {
689
710
  address: string;
690
711
 
@@ -1152,13 +1173,6 @@ export namespace UserCreateParams {
1152
1173
 
1153
1174
  export interface UserListParams extends CursorParams {}
1154
1175
 
1155
- export interface UserCreateCustomMetadataParams {
1156
- /**
1157
- * Custom metadata associated with the user.
1158
- */
1159
- custom_metadata: { [key: string]: string | number | boolean };
1160
- }
1161
-
1162
1176
  export interface UserGetByCustomAuthIDParams {
1163
1177
  custom_user_id: string;
1164
1178
  }
@@ -1258,6 +1272,13 @@ export declare namespace UserSearchParams {
1258
1272
  }
1259
1273
  }
1260
1274
 
1275
+ export interface UserSetCustomMetadataParams {
1276
+ /**
1277
+ * Custom metadata associated with the user.
1278
+ */
1279
+ custom_metadata: { [key: string]: string | number | boolean };
1280
+ }
1281
+
1261
1282
  export interface UserUnlinkLinkedAccountParams {
1262
1283
  handle: string;
1263
1284
 
@@ -1292,7 +1313,6 @@ export declare namespace Users {
1292
1313
  type UsersCursor as UsersCursor,
1293
1314
  type UserCreateParams as UserCreateParams,
1294
1315
  type UserListParams as UserListParams,
1295
- type UserCreateCustomMetadataParams as UserCreateCustomMetadataParams,
1296
1316
  type UserGetByCustomAuthIDParams as UserGetByCustomAuthIDParams,
1297
1317
  type UserGetByDiscordUsernameParams as UserGetByDiscordUsernameParams,
1298
1318
  type UserGetByEmailAddressParams as UserGetByEmailAddressParams,
@@ -1307,6 +1327,7 @@ export declare namespace Users {
1307
1327
  type UserGetByWalletAddressParams as UserGetByWalletAddressParams,
1308
1328
  type UserPregenerateWalletsParams as UserPregenerateWalletsParams,
1309
1329
  type UserSearchParams as UserSearchParams,
1330
+ type UserSetCustomMetadataParams as UserSetCustomMetadataParams,
1310
1331
  type UserUnlinkLinkedAccountParams as UserUnlinkLinkedAccountParams,
1311
1332
  };
1312
1333
  }
@@ -410,11 +410,9 @@ export namespace WalletRpcResponse {
410
410
  }
411
411
 
412
412
  export interface SolanaSignAndSendTransactionRpcResponse {
413
- method: 'signAndSendTransaction';
414
-
415
- data?: SolanaSignAndSendTransactionRpcResponse.Data;
413
+ data: SolanaSignAndSendTransactionRpcResponse.Data;
416
414
 
417
- error?: SolanaSignAndSendTransactionRpcResponse.Error;
415
+ method: 'signAndSendTransaction';
418
416
  }
419
417
 
420
418
  export namespace SolanaSignAndSendTransactionRpcResponse {
@@ -425,12 +423,6 @@ export namespace WalletRpcResponse {
425
423
 
426
424
  transaction_id?: string;
427
425
  }
428
-
429
- export interface Error {
430
- code: string;
431
-
432
- message: string;
433
- }
434
426
  }
435
427
 
436
428
  export interface SolanaSignMessageRpcResponse {
@@ -462,11 +454,9 @@ export namespace WalletRpcResponse {
462
454
  }
463
455
 
464
456
  export interface EthereumSendTransactionRpcResponse {
465
- method: 'eth_sendTransaction';
466
-
467
- data?: EthereumSendTransactionRpcResponse.Data;
457
+ data: EthereumSendTransactionRpcResponse.Data;
468
458
 
469
- error?: EthereumSendTransactionRpcResponse.Error;
459
+ method: 'eth_sendTransaction';
470
460
  }
471
461
 
472
462
  export namespace EthereumSendTransactionRpcResponse {
@@ -505,12 +495,6 @@ export namespace WalletRpcResponse {
505
495
  value?: string | number;
506
496
  }
507
497
  }
508
-
509
- export interface Error {
510
- code: string;
511
-
512
- message: string;
513
- }
514
498
  }
515
499
 
516
500
  export interface EthereumPersonalSignRpcResponse {
@@ -542,11 +526,9 @@ export namespace WalletRpcResponse {
542
526
  }
543
527
 
544
528
  export interface EthereumSign7702AuthorizationRpcResponse {
545
- method: 'eth_sign7702Authorization';
546
-
547
- data?: EthereumSign7702AuthorizationRpcResponse.Data;
529
+ data: EthereumSign7702AuthorizationRpcResponse.Data;
548
530
 
549
- error?: EthereumSign7702AuthorizationRpcResponse.Error;
531
+ method: 'eth_sign7702Authorization';
550
532
  }
551
533
 
552
534
  export namespace EthereumSign7702AuthorizationRpcResponse {
@@ -569,12 +551,6 @@ export namespace WalletRpcResponse {
569
551
  y_parity: number;
570
552
  }
571
553
  }
572
-
573
- export interface Error {
574
- code: string;
575
-
576
- message: string;
577
- }
578
554
  }
579
555
 
580
556
  export interface EthereumSecp256k1SignRpcResponse {
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.1.0-alpha.2'; // x-release-please-version
1
+ export const VERSION = '0.1.0'; // x-release-please-version
package/src/viem.ts ADDED
@@ -0,0 +1,223 @@
1
+ import type { Hex } from 'viem';
2
+ import {
3
+ type LocalAccount,
4
+ type SignMessageParameters,
5
+ type SignTransactionParameters,
6
+ toAccount,
7
+ } from 'viem/accounts';
8
+ import { toHex } from 'viem/utils';
9
+ import { PrivyAPIError } from './core/error';
10
+ import type { AuthorizationContext } from './lib/authorization';
11
+ import type { PrivyClient } from './public-api/PrivyClient';
12
+ import type { WalletRpcParams } from './resources';
13
+
14
+ export interface CreateViemAccountInput {
15
+ /** ID for the wallet. */
16
+ walletId: string;
17
+ /** Ethereum address for the wallet. */
18
+ address: Hex;
19
+ /** Authorization context for the wallet. */
20
+ authorizationContext?: AuthorizationContext;
21
+ }
22
+
23
+ /**
24
+ * Creates a viem `Account` instance for a Privy wallet given its ID, which can be used to sign
25
+ * messages and send transactions with the wallet.
26
+ *
27
+ * @param client instance of the Privy client.
28
+ * @param input an object specifying the details of the wallet to create the viem account for.
29
+ * @returns viem `Account` instance for the wallet.
30
+ */
31
+ export function createViemAccount(
32
+ client: PrivyClient,
33
+ { walletId, address, authorizationContext }: CreateViemAccountInput,
34
+ ): LocalAccount {
35
+ return toAccount({
36
+ address: address as Hex,
37
+ sign: async ({ hash }) => {
38
+ const response = await client
39
+ .wallets()
40
+ .ethereum()
41
+ .signSecp256k1(walletId, {
42
+ params: { hash },
43
+ ...(authorizationContext ? { authorization_context: authorizationContext } : {}),
44
+ });
45
+ return response.signature as `0x${string}`;
46
+ },
47
+ signMessage: async ({ message }) => {
48
+ const response = await client
49
+ .wallets()
50
+ .ethereum()
51
+ .signMessage(walletId, {
52
+ message: formatViemPersonalSignMessage(message),
53
+ ...(authorizationContext ? { authorization_context: authorizationContext } : {}),
54
+ });
55
+ return response.signature as `0x${string}`;
56
+ },
57
+ signTypedData: async (typedData) => {
58
+ const { message, domain, types, primaryType } = replaceBigInts(typedData, toHex);
59
+ // Viem accepts undefined `domain`, `message`, and `types` in this method
60
+ // though those are required per the EIP712 spec.
61
+ if (!domain) throw new PrivyAPIError('typedData.domain must be defined');
62
+ if (!message) throw new PrivyAPIError('typedData.message must be defined');
63
+ if (!types) throw new PrivyAPIError('typedData.message must be defined');
64
+ const { signature } = await client
65
+ .wallets()
66
+ .ethereum()
67
+ .signTypedData(walletId, {
68
+ params: {
69
+ typed_data: { domain, message, primary_type: primaryType as string, types: types as any },
70
+ },
71
+ ...(authorizationContext ? { authorization_context: authorizationContext } : {}),
72
+ });
73
+ return signature as Hex;
74
+ },
75
+ signTransaction: async (transaction) => {
76
+ const { signed_transaction: signedTransaction } = await client
77
+ .wallets()
78
+ .ethereum()
79
+ .signTransaction(walletId, {
80
+ params: { transaction: formatViemTransaction(transaction) },
81
+ ...(authorizationContext ? { authorization_context: authorizationContext } : {}),
82
+ });
83
+
84
+ return signedTransaction as Hex;
85
+ },
86
+ signAuthorization: async (parameters) => {
87
+ const { authorization } = await client
88
+ .wallets()
89
+ .ethereum()
90
+ .sign7702Authorization(walletId, {
91
+ params: {
92
+ contract: (parameters.contractAddress ?? parameters.address) as Hex,
93
+ chain_id: parameters.chainId,
94
+ nonce: parameters.nonce,
95
+ },
96
+ ...(authorizationContext ? { authorization_context: authorizationContext } : {}),
97
+ });
98
+ return {
99
+ address: authorization.contract as Hex,
100
+ nonce: Number(authorization.nonce),
101
+ chainId: Number(authorization.chain_id),
102
+ yParity: authorization.y_parity,
103
+ r: authorization.r as Hex,
104
+ s: authorization.s as Hex,
105
+ };
106
+ },
107
+ });
108
+ }
109
+
110
+ /**
111
+ * Formats a viem transaction type to the JSON-RPC transaction type:
112
+ * - 'legacy' -> 0
113
+ * - 'eip2930' -> 1
114
+ * - 'eip1559' or undefined -> 2. This is the default EVM transaction type.
115
+ * - 'eip4844' -> 3 in theory, but will throw an error as we do not support this yet.
116
+ * - 'eip7702' -> 4 in theory, but will throw an error as we do not support this yet
117
+ * @param type viem transaction type
118
+ * @returns 0 | 1 | 2
119
+ */
120
+ const formatViemTransactionType = (type: SignTransactionParameters['transaction']['type']) => {
121
+ if (type === 'legacy') {
122
+ return 0 as const;
123
+ } else if (type === 'eip2930') {
124
+ return 1 as const;
125
+ } else if (type == 'eip1559' || typeof type === 'undefined') {
126
+ // Type 2 (EIP-1559) is the default transaction type
127
+ return 2 as const;
128
+ } else {
129
+ // We do not yet support EIP4844 (type 3) and EIP7702 (type 4) transaction types
130
+ throw new PrivyAPIError('EIP4844 and EIP7702 transaction types are not yet supported.');
131
+ }
132
+ };
133
+
134
+ /**
135
+ * Formats viem quantities, which are represented as `bigint | undefined` to our internal
136
+ * `Quantity` type. This is done by converting bigints into a hexstring.
137
+ *
138
+ * @param input {bigint | undefined} bigint quantity to format
139
+ * @returns input as hex string
140
+ */
141
+ const formatViemQuantity = (input: bigint): Hex => {
142
+ return `0x${input.toString(16)}` as Hex;
143
+ };
144
+
145
+ /**
146
+ * Formats a `message` input to viem's `signMessage` function to the format needed for our wallet API.
147
+ *
148
+ * If the `message` is a string, we use it directly.
149
+ *
150
+ * If the `message` is an object containing a `raw` field, it indicates that the use case is signing raw bytes,
151
+ * in which case we convert `message.raw` into a `Uint8Array` before passing it to our own `signMessage`
152
+ * @param message input to viem's `signMessage` function
153
+ * @returns message as a utf-8 `string` or `Uint8Array`
154
+ */
155
+ const formatViemPersonalSignMessage = (message: SignMessageParameters['message']): string | Uint8Array => {
156
+ if (typeof message === 'string') return message;
157
+
158
+ if (typeof message.raw === 'string') {
159
+ // We `.slice(2)` to remove the `0x` prefix
160
+ return Uint8Array.from(Buffer.from(message.raw.slice(2), 'hex'));
161
+ } else {
162
+ return message.raw;
163
+ }
164
+ };
165
+
166
+ /**
167
+ * Formats a viem transaction into our internal transaction type. For the most part, this converts
168
+ * bigints to hexstrings so they can be passed over the wire.
169
+ *
170
+ * @param tx input to viem's `sendTransaction` function
171
+ * @returns transaction {EthereumSignTransactionInputType} as our own type
172
+ */
173
+ const formatViemTransaction = (
174
+ tx: SignTransactionParameters['transaction'],
175
+ ): WalletRpcParams.EthereumSignTransactionRpcInput.Params.Transaction => {
176
+ return {
177
+ type: formatViemTransactionType(tx.type),
178
+ ...(tx.to ? { to: tx.to } : {}),
179
+ ...(tx.nonce ? { nonce: tx.nonce } : {}),
180
+ ...(tx.chainId ? { chain_id: tx.chainId } : {}),
181
+ ...(tx.data ? { data: tx.data } : {}),
182
+ ...(tx.value ? { value: formatViemQuantity(tx.value) } : {}),
183
+ ...(tx.gas ? { gas_limit: formatViemQuantity(tx.gas) } : {}),
184
+ ...(tx.gasPrice ? { gas_price: formatViemQuantity(tx.gasPrice) } : {}),
185
+ ...(tx.maxFeePerGas ? { max_fee_per_gas: formatViemQuantity(tx.maxFeePerGas) } : {}),
186
+ ...(tx.maxPriorityFeePerGas ?
187
+ { max_priority_fee_per_gas: formatViemQuantity(tx.maxPriorityFeePerGas) }
188
+ : {}),
189
+ };
190
+ };
191
+
192
+ // replaceBigInts courtesy of ponder.sh:
193
+ // https://github.com/ponder-sh/ponder/blob/bc65b865898b6145e87031314192c59f9e8b621f/packages/utils/src/replaceBigInts.ts
194
+ type _ReplaceBigInts<
195
+ arr extends readonly unknown[],
196
+ type,
197
+ result extends readonly unknown[] = [],
198
+ > = arr extends [infer first, ...infer rest] ?
199
+ _ReplaceBigInts<rest, type, readonly [...result, first extends bigint ? type : first]>
200
+ : result;
201
+
202
+ type ReplaceBigInts<obj, type> =
203
+ obj extends bigint ? type
204
+ : obj extends unknown[] ? _ReplaceBigInts<Readonly<obj>, type>
205
+ : obj extends readonly [] ? _ReplaceBigInts<obj, type>
206
+ : obj extends object ? { [key in keyof obj]: ReplaceBigInts<obj[key], type> }
207
+ : obj;
208
+
209
+ const replaceBigInts = <const T, const type>(
210
+ obj: T,
211
+ replacer: (x: bigint) => type,
212
+ ): ReplaceBigInts<T, type> => {
213
+ if (typeof obj === 'bigint') return replacer(obj) as ReplaceBigInts<T, type>;
214
+ if (Array.isArray(obj)) {
215
+ return obj.map((x) => replaceBigInts(x, replacer)) as ReplaceBigInts<T, type>;
216
+ }
217
+ if (obj && typeof obj === 'object') {
218
+ return Object.fromEntries(
219
+ Object.entries(obj).map(([k, v]) => [k, replaceBigInts(v, replacer)]),
220
+ ) as ReplaceBigInts<T, type>;
221
+ }
222
+ return obj as ReplaceBigInts<T, type>;
223
+ };
package/version.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.1.0-alpha.2";
1
+ export declare const VERSION = "0.1.0";
2
2
  //# sourceMappingURL=version.d.mts.map
package/version.d.mts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"version.d.mts","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,kBAAkB,CAAC"}
1
+ {"version":3,"file":"version.d.mts","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UAAU,CAAC"}
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.1.0-alpha.2";
1
+ export declare const VERSION = "0.1.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,kBAAkB,CAAC"}
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UAAU,CAAC"}
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '0.1.0-alpha.2'; // x-release-please-version
4
+ exports.VERSION = '0.1.0'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,eAAe,CAAC,CAAC,2BAA2B"}
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,OAAO,CAAC,CAAC,2BAA2B"}
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '0.1.0-alpha.2'; // x-release-please-version
1
+ export const VERSION = '0.1.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map
package/version.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"version.mjs","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,eAAe,CAAC,CAAC,2BAA2B"}
1
+ {"version":3,"file":"version.mjs","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,2BAA2B"}
package/viem.d.mts ADDED
@@ -0,0 +1,22 @@
1
+ import type { Hex } from 'viem';
2
+ import { type LocalAccount } from 'viem/accounts';
3
+ import type { AuthorizationContext } from "./lib/authorization.mjs";
4
+ import type { PrivyClient } from "./public-api/PrivyClient.mjs";
5
+ export interface CreateViemAccountInput {
6
+ /** ID for the wallet. */
7
+ walletId: string;
8
+ /** Ethereum address for the wallet. */
9
+ address: Hex;
10
+ /** Authorization context for the wallet. */
11
+ authorizationContext?: AuthorizationContext;
12
+ }
13
+ /**
14
+ * Creates a viem `Account` instance for a Privy wallet given its ID, which can be used to sign
15
+ * messages and send transactions with the wallet.
16
+ *
17
+ * @param client instance of the Privy client.
18
+ * @param input an object specifying the details of the wallet to create the viem account for.
19
+ * @returns viem `Account` instance for the wallet.
20
+ */
21
+ export declare function createViemAccount(client: PrivyClient, { walletId, address, authorizationContext }: CreateViemAccountInput): LocalAccount;
22
+ //# sourceMappingURL=viem.d.mts.map
package/viem.d.mts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"viem.d.mts","sourceRoot":"","sources":["src/viem.ts"],"names":[],"mappings":"OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM;OACxB,EACL,KAAK,YAAY,EAIlB,MAAM,eAAe;OAGf,KAAK,EAAE,oBAAoB,EAAE;OAC7B,KAAK,EAAE,WAAW,EAAE;AAG3B,MAAM,WAAW,sBAAsB;IACrC,yBAAyB;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,uCAAuC;IACvC,OAAO,EAAE,GAAG,CAAC;IACb,4CAA4C;IAC5C,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;CAC7C;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,WAAW,EACnB,EAAE,QAAQ,EAAE,OAAO,EAAE,oBAAoB,EAAE,EAAE,sBAAsB,GAClE,YAAY,CA0Ed"}
package/viem.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { Hex } from 'viem';
2
+ import { type LocalAccount } from 'viem/accounts';
3
+ import type { AuthorizationContext } from "./lib/authorization.js";
4
+ import type { PrivyClient } from "./public-api/PrivyClient.js";
5
+ export interface CreateViemAccountInput {
6
+ /** ID for the wallet. */
7
+ walletId: string;
8
+ /** Ethereum address for the wallet. */
9
+ address: Hex;
10
+ /** Authorization context for the wallet. */
11
+ authorizationContext?: AuthorizationContext;
12
+ }
13
+ /**
14
+ * Creates a viem `Account` instance for a Privy wallet given its ID, which can be used to sign
15
+ * messages and send transactions with the wallet.
16
+ *
17
+ * @param client instance of the Privy client.
18
+ * @param input an object specifying the details of the wallet to create the viem account for.
19
+ * @returns viem `Account` instance for the wallet.
20
+ */
21
+ export declare function createViemAccount(client: PrivyClient, { walletId, address, authorizationContext }: CreateViemAccountInput): LocalAccount;
22
+ //# sourceMappingURL=viem.d.ts.map
package/viem.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"viem.d.ts","sourceRoot":"","sources":["src/viem.ts"],"names":[],"mappings":"OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM;OACxB,EACL,KAAK,YAAY,EAIlB,MAAM,eAAe;OAGf,KAAK,EAAE,oBAAoB,EAAE;OAC7B,KAAK,EAAE,WAAW,EAAE;AAG3B,MAAM,WAAW,sBAAsB;IACrC,yBAAyB;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,uCAAuC;IACvC,OAAO,EAAE,GAAG,CAAC;IACb,4CAA4C;IAC5C,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;CAC7C;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,WAAW,EACnB,EAAE,QAAQ,EAAE,OAAO,EAAE,oBAAoB,EAAE,EAAE,sBAAsB,GAClE,YAAY,CA0Ed"}