@pay-skill/sdk 0.1.8 → 0.2.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 (60) hide show
  1. package/README.md +143 -154
  2. package/dist/auth.d.ts +11 -6
  3. package/dist/auth.d.ts.map +1 -1
  4. package/dist/auth.js +19 -7
  5. package/dist/auth.js.map +1 -1
  6. package/dist/errors.d.ts +4 -2
  7. package/dist/errors.d.ts.map +1 -1
  8. package/dist/errors.js +8 -3
  9. package/dist/errors.js.map +1 -1
  10. package/dist/index.d.ts +2 -13
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -6
  13. package/dist/index.js.map +1 -1
  14. package/dist/keychain.d.ts +8 -0
  15. package/dist/keychain.d.ts.map +1 -0
  16. package/dist/keychain.js +17 -0
  17. package/dist/keychain.js.map +1 -0
  18. package/dist/wallet.d.ts +135 -104
  19. package/dist/wallet.d.ts.map +1 -1
  20. package/dist/wallet.js +631 -276
  21. package/dist/wallet.js.map +1 -1
  22. package/jsr.json +13 -13
  23. package/knip.json +5 -5
  24. package/package.json +51 -48
  25. package/src/auth.ts +210 -200
  26. package/src/eip3009.ts +79 -79
  27. package/src/errors.ts +55 -48
  28. package/src/index.ts +24 -51
  29. package/src/keychain.ts +18 -0
  30. package/src/wallet.ts +1111 -445
  31. package/tests/test_auth_rejection.ts +102 -154
  32. package/tests/test_crypto.ts +138 -251
  33. package/tests/test_e2e.ts +99 -158
  34. package/tests/test_errors.ts +44 -36
  35. package/tests/test_ows.ts +153 -0
  36. package/tests/test_wallet.ts +194 -0
  37. package/dist/client.d.ts +0 -94
  38. package/dist/client.d.ts.map +0 -1
  39. package/dist/client.js +0 -443
  40. package/dist/client.js.map +0 -1
  41. package/dist/models.d.ts +0 -78
  42. package/dist/models.d.ts.map +0 -1
  43. package/dist/models.js +0 -2
  44. package/dist/models.js.map +0 -1
  45. package/dist/ows-signer.d.ts +0 -75
  46. package/dist/ows-signer.d.ts.map +0 -1
  47. package/dist/ows-signer.js +0 -130
  48. package/dist/ows-signer.js.map +0 -1
  49. package/dist/signer.d.ts +0 -46
  50. package/dist/signer.d.ts.map +0 -1
  51. package/dist/signer.js +0 -111
  52. package/dist/signer.js.map +0 -1
  53. package/src/client.ts +0 -644
  54. package/src/models.ts +0 -77
  55. package/src/ows-signer.ts +0 -223
  56. package/src/signer.ts +0 -147
  57. package/tests/test_ows_integration.ts +0 -92
  58. package/tests/test_ows_signer.ts +0 -365
  59. package/tests/test_signer.ts +0 -47
  60. package/tests/test_validation.ts +0 -66
package/src/eip3009.ts CHANGED
@@ -1,79 +1,79 @@
1
- /**
2
- * EIP-3009 TransferWithAuthorization signing for x402 direct settlement.
3
- *
4
- * Unlike EIP-2612 permits (which need a nonce from the USDC contract),
5
- * EIP-3009 uses a random nonce chosen by the signer. Fully local — no
6
- * server round-trip needed.
7
- *
8
- * Domain: { name: "USD Coin", version: "2", chainId, verifyingContract: usdcAddress }
9
- * Type: TransferWithAuthorization(address from, address to, uint256 value,
10
- * uint256 validAfter, uint256 validBefore, bytes32 nonce)
11
- */
12
-
13
- import { type Hex, type Address } from "viem";
14
- import { privateKeyToAccount } from "viem/accounts";
15
- import { randomBytes } from "node:crypto";
16
-
17
- export interface TransferAuthorization {
18
- from: string;
19
- to: string;
20
- amount: number;
21
- nonce: string;
22
- v: number;
23
- r: string;
24
- s: string;
25
- }
26
-
27
- const TRANSFER_AUTH_TYPES = {
28
- TransferWithAuthorization: [
29
- { name: "from", type: "address" },
30
- { name: "to", type: "address" },
31
- { name: "value", type: "uint256" },
32
- { name: "validAfter", type: "uint256" },
33
- { name: "validBefore", type: "uint256" },
34
- { name: "nonce", type: "bytes32" },
35
- ],
36
- } as const;
37
-
38
- export async function signTransferAuthorization(
39
- privateKey: Hex,
40
- to: Address,
41
- amount: number,
42
- chainId: number,
43
- usdcAddress: Address,
44
- ): Promise<TransferAuthorization> {
45
- const account = privateKeyToAccount(privateKey);
46
- const nonce = ("0x" + randomBytes(32).toString("hex")) as Hex;
47
-
48
- const signature = await account.signTypedData({
49
- domain: {
50
- name: "USD Coin",
51
- version: "2",
52
- chainId,
53
- verifyingContract: usdcAddress,
54
- },
55
- types: TRANSFER_AUTH_TYPES,
56
- primaryType: "TransferWithAuthorization",
57
- message: {
58
- from: account.address,
59
- to,
60
- value: BigInt(amount),
61
- validAfter: 0n,
62
- validBefore: 0n,
63
- nonce,
64
- },
65
- });
66
-
67
- const sigHex = signature.slice(2);
68
- const r = `0x${sigHex.slice(0, 64)}`;
69
- const s = `0x${sigHex.slice(64, 128)}`;
70
- const v = parseInt(sigHex.slice(128, 130), 16);
71
-
72
- return { from: account.address, to, amount, nonce, v, r, s };
73
- }
74
-
75
- export function combinedSignature(auth: TransferAuthorization): string {
76
- const r = auth.r.startsWith("0x") ? auth.r.slice(2) : auth.r;
77
- const s = auth.s.startsWith("0x") ? auth.s.slice(2) : auth.s;
78
- return `0x${r}${s}${auth.v.toString(16).padStart(2, "0")}`;
79
- }
1
+ /**
2
+ * EIP-3009 TransferWithAuthorization signing for x402 direct settlement.
3
+ *
4
+ * Unlike EIP-2612 permits (which need a nonce from the USDC contract),
5
+ * EIP-3009 uses a random nonce chosen by the signer. Fully local — no
6
+ * server round-trip needed.
7
+ *
8
+ * Domain: { name: "USD Coin", version: "2", chainId, verifyingContract: usdcAddress }
9
+ * Type: TransferWithAuthorization(address from, address to, uint256 value,
10
+ * uint256 validAfter, uint256 validBefore, bytes32 nonce)
11
+ */
12
+
13
+ import { type Hex, type Address } from "viem";
14
+ import { privateKeyToAccount } from "viem/accounts";
15
+ import { randomBytes } from "node:crypto";
16
+
17
+ export interface TransferAuthorization {
18
+ from: string;
19
+ to: string;
20
+ amount: number;
21
+ nonce: string;
22
+ v: number;
23
+ r: string;
24
+ s: string;
25
+ }
26
+
27
+ const TRANSFER_AUTH_TYPES = {
28
+ TransferWithAuthorization: [
29
+ { name: "from", type: "address" },
30
+ { name: "to", type: "address" },
31
+ { name: "value", type: "uint256" },
32
+ { name: "validAfter", type: "uint256" },
33
+ { name: "validBefore", type: "uint256" },
34
+ { name: "nonce", type: "bytes32" },
35
+ ],
36
+ } as const;
37
+
38
+ export async function signTransferAuthorization(
39
+ privateKey: Hex,
40
+ to: Address,
41
+ amount: number,
42
+ chainId: number,
43
+ usdcAddress: Address,
44
+ ): Promise<TransferAuthorization> {
45
+ const account = privateKeyToAccount(privateKey);
46
+ const nonce = ("0x" + randomBytes(32).toString("hex")) as Hex;
47
+
48
+ const signature = await account.signTypedData({
49
+ domain: {
50
+ name: "USD Coin",
51
+ version: "2",
52
+ chainId,
53
+ verifyingContract: usdcAddress,
54
+ },
55
+ types: TRANSFER_AUTH_TYPES,
56
+ primaryType: "TransferWithAuthorization",
57
+ message: {
58
+ from: account.address,
59
+ to,
60
+ value: BigInt(amount),
61
+ validAfter: 0n,
62
+ validBefore: 0n,
63
+ nonce,
64
+ },
65
+ });
66
+
67
+ const sigHex = signature.slice(2);
68
+ const r = `0x${sigHex.slice(0, 64)}`;
69
+ const s = `0x${sigHex.slice(64, 128)}`;
70
+ const v = parseInt(sigHex.slice(128, 130), 16);
71
+
72
+ return { from: account.address, to, amount, nonce, v, r, s };
73
+ }
74
+
75
+ export function combinedSignature(auth: TransferAuthorization): string {
76
+ const r = auth.r.startsWith("0x") ? auth.r.slice(2) : auth.r;
77
+ const s = auth.s.startsWith("0x") ? auth.s.slice(2) : auth.s;
78
+ return `0x${r}${s}${auth.v.toString(16).padStart(2, "0")}`;
79
+ }
package/src/errors.ts CHANGED
@@ -1,48 +1,55 @@
1
- /** Base error for all pay SDK errors. */
2
- export class PayError extends Error {
3
- readonly code: string;
4
-
5
- constructor(message: string, code = "pay_error") {
6
- super(message);
7
- this.name = "PayError";
8
- this.code = code;
9
- }
10
- }
11
-
12
- /** Input validation failed. */
13
- export class PayValidationError extends PayError {
14
- readonly field?: string;
15
-
16
- constructor(message: string, field?: string) {
17
- super(message, "validation_error");
18
- this.name = "PayValidationError";
19
- this.field = field;
20
- }
21
- }
22
-
23
- /** Network or server communication failed. */
24
- export class PayNetworkError extends PayError {
25
- constructor(message: string) {
26
- super(message, "network_error");
27
- this.name = "PayNetworkError";
28
- }
29
- }
30
-
31
- /** Server returned an error response. */
32
- export class PayServerError extends PayError {
33
- readonly statusCode: number;
34
-
35
- constructor(message: string, statusCode: number) {
36
- super(message, "server_error");
37
- this.name = "PayServerError";
38
- this.statusCode = statusCode;
39
- }
40
- }
41
-
42
- /** Insufficient USDC balance. */
43
- export class PayInsufficientFundsError extends PayError {
44
- constructor(message = "Insufficient USDC balance") {
45
- super(message, "insufficient_funds");
46
- this.name = "PayInsufficientFundsError";
47
- }
48
- }
1
+ /** Base error for all pay SDK errors. */
2
+ export class PayError extends Error {
3
+ readonly code: string;
4
+
5
+ constructor(message: string, code = "pay_error") {
6
+ super(message);
7
+ this.name = "PayError";
8
+ this.code = code;
9
+ }
10
+ }
11
+
12
+ /** Input validation failed. */
13
+ export class PayValidationError extends PayError {
14
+ readonly field?: string;
15
+
16
+ constructor(message: string, field?: string) {
17
+ super(message, "validation_error");
18
+ this.name = "PayValidationError";
19
+ this.field = field;
20
+ }
21
+ }
22
+
23
+ /** Network or server communication failed. */
24
+ export class PayNetworkError extends PayError {
25
+ constructor(message: string) {
26
+ super(message, "network_error");
27
+ this.name = "PayNetworkError";
28
+ }
29
+ }
30
+
31
+ /** Server returned an error response. */
32
+ export class PayServerError extends PayError {
33
+ readonly statusCode: number;
34
+
35
+ constructor(message: string, statusCode: number) {
36
+ super(message, "server_error");
37
+ this.name = "PayServerError";
38
+ this.statusCode = statusCode;
39
+ }
40
+ }
41
+
42
+ /** Insufficient USDC balance. Includes fund link hint for agents. */
43
+ export class PayInsufficientFundsError extends PayError {
44
+ readonly balance: number;
45
+ readonly required: number;
46
+
47
+ constructor(message: string, balance = 0, required = 0) {
48
+ const hint =
49
+ '\nUse wallet.createFundLink({ message: "Need funds" }) to request funding.';
50
+ super(message + hint, "insufficient_funds");
51
+ this.name = "PayInsufficientFundsError";
52
+ this.balance = balance;
53
+ this.required = required;
54
+ }
55
+ }
package/src/index.ts CHANGED
@@ -1,51 +1,24 @@
1
- export { PayClient, DEFAULT_API_URL } from "./client.js";
2
- export type { PayClientOptions } from "./client.js";
3
-
4
- export {
5
- PayError,
6
- PayValidationError,
7
- PayNetworkError,
8
- PayServerError,
9
- PayInsufficientFundsError,
10
- } from "./errors.js";
11
-
12
- export type {
13
- DirectPaymentResult,
14
- DiscoverOptions,
15
- DiscoverService,
16
- Tab,
17
- TabStatus,
18
- StatusResponse,
19
- WebhookRegistration,
20
- } from "./models.js";
21
-
22
- export type { Signer } from "./signer.js";
23
- export {
24
- CliSigner,
25
- RawKeySigner,
26
- CallbackSigner,
27
- createSigner,
28
- } from "./signer.js";
29
-
30
- export { OwsSigner } from "./ows-signer.js";
31
- export type { OwsSignerOptions } from "./ows-signer.js";
32
-
33
- export { Wallet, PrivateKeySigner } from "./wallet.js";
34
- export type {
35
- WalletOptions,
36
- FundLinkOptions,
37
- PermitResult,
38
- } from "./wallet.js";
39
-
40
- export {
41
- buildAuthHeaders,
42
- buildAuthHeadersWithSigner,
43
- computeEip712Hash,
44
- } from "./auth.js";
45
- export type { AuthConfig, AuthHeaders } from "./auth.js";
46
-
47
- export {
48
- signTransferAuthorization,
49
- combinedSignature,
50
- } from "./eip3009.js";
51
- export type { TransferAuthorization } from "./eip3009.js";
1
+ export {
2
+ PayError,
3
+ PayValidationError,
4
+ PayNetworkError,
5
+ PayServerError,
6
+ PayInsufficientFundsError,
7
+ } from "./errors.js";
8
+
9
+ export { Wallet, discover } from "./wallet.js";
10
+ export type {
11
+ Amount,
12
+ WalletOptions,
13
+ OwsWalletOptions,
14
+ SendResult,
15
+ Tab,
16
+ ChargeResult,
17
+ Balance,
18
+ Status,
19
+ DiscoverService,
20
+ DiscoverOptions,
21
+ FundLinkOptions,
22
+ WebhookRegistration,
23
+ MintResult,
24
+ } from "./wallet.js";
@@ -0,0 +1,18 @@
1
+ /**
2
+ * OS keychain reader — reads the private key stored by `pay` CLI.
3
+ * Service: "pay", account: "default".
4
+ *
5
+ * keytar is an optional dependency. If not installed, returns null.
6
+ */
7
+
8
+ export async function readFromKeychain(): Promise<string | null> {
9
+ try {
10
+ const moduleName = "keytar";
11
+ const keytar = (await import(moduleName)) as {
12
+ default: { getPassword(service: string, account: string): Promise<string | null> };
13
+ };
14
+ return await keytar.default.getPassword("pay", "default");
15
+ } catch {
16
+ return null;
17
+ }
18
+ }