@pay-skill/sdk 0.1.7 → 0.1.10
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/README.md +80 -91
- package/dist/auth.d.ts +11 -6
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +19 -7
- package/dist/auth.js.map +1 -1
- package/dist/errors.d.ts +4 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +8 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +2 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -6
- package/dist/index.js.map +1 -1
- package/dist/keychain.d.ts +8 -0
- package/dist/keychain.d.ts.map +1 -0
- package/dist/keychain.js +17 -0
- package/dist/keychain.js.map +1 -0
- package/dist/wallet.d.ts +135 -104
- package/dist/wallet.d.ts.map +1 -1
- package/dist/wallet.js +683 -280
- package/dist/wallet.js.map +1 -1
- package/jsr.json +1 -1
- package/package.json +5 -2
- package/src/auth.ts +28 -18
- package/src/errors.ts +10 -3
- package/src/index.ts +12 -39
- package/src/keychain.ts +18 -0
- package/src/wallet.ts +1074 -355
- package/tests/test_auth_rejection.ts +43 -95
- package/tests/test_crypto.ts +59 -172
- package/tests/test_e2e.ts +46 -105
- package/tests/test_errors.ts +9 -1
- package/tests/test_ows.ts +153 -0
- package/tests/test_wallet.ts +194 -0
- package/dist/client.d.ts +0 -94
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -443
- package/dist/client.js.map +0 -1
- package/dist/models.d.ts +0 -78
- package/dist/models.d.ts.map +0 -1
- package/dist/models.js +0 -2
- package/dist/models.js.map +0 -1
- package/dist/ows-signer.d.ts +0 -75
- package/dist/ows-signer.d.ts.map +0 -1
- package/dist/ows-signer.js +0 -130
- package/dist/ows-signer.js.map +0 -1
- package/dist/signer.d.ts +0 -46
- package/dist/signer.d.ts.map +0 -1
- package/dist/signer.js +0 -111
- package/dist/signer.js.map +0 -1
- package/src/client.ts +0 -644
- package/src/models.ts +0 -77
- package/src/ows-signer.ts +0 -223
- package/src/signer.ts +0 -147
- package/tests/test_ows_integration.ts +0 -92
- package/tests/test_ows_signer.ts +0 -365
- package/tests/test_signer.ts +0 -47
- package/tests/test_validation.ts +0 -66
package/src/models.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/** Tab lifecycle states. */
|
|
2
|
-
export type TabStatus = "open" | "closed";
|
|
3
|
-
|
|
4
|
-
/** Result of a direct payment. */
|
|
5
|
-
export interface DirectPaymentResult {
|
|
6
|
-
txHash: string;
|
|
7
|
-
status: string;
|
|
8
|
-
/** Amount in USDC micro-units (6 decimals). */
|
|
9
|
-
amount: number;
|
|
10
|
-
/** Fee deducted in USDC micro-units. */
|
|
11
|
-
fee: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Tab state. */
|
|
15
|
-
export interface Tab {
|
|
16
|
-
tabId: string;
|
|
17
|
-
provider: string;
|
|
18
|
-
/** Total locked amount in USDC micro-units. */
|
|
19
|
-
amount: number;
|
|
20
|
-
/** Remaining balance in USDC micro-units. */
|
|
21
|
-
balanceRemaining: number;
|
|
22
|
-
/** Total charged so far in USDC micro-units. */
|
|
23
|
-
totalCharged: number;
|
|
24
|
-
/** Number of charges made. */
|
|
25
|
-
chargeCount: number;
|
|
26
|
-
/** Max per-charge limit in USDC micro-units. */
|
|
27
|
-
maxChargePerCall: number;
|
|
28
|
-
/** Total withdrawn so far in USDC micro-units. */
|
|
29
|
-
totalWithdrawn: number;
|
|
30
|
-
status: TabStatus;
|
|
31
|
-
/** Number of charges buffered awaiting batch settlement. */
|
|
32
|
-
pendingChargeCount: number;
|
|
33
|
-
/** Total amount of pending charges in USDC micro-units. */
|
|
34
|
-
pendingChargeTotal: number;
|
|
35
|
-
/** balance_remaining minus pending charges — the true available balance. */
|
|
36
|
-
effectiveBalance: number;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Wallet status. */
|
|
40
|
-
export interface StatusResponse {
|
|
41
|
-
address: string;
|
|
42
|
-
/** USDC balance in micro-units. */
|
|
43
|
-
balance: number;
|
|
44
|
-
openTabs: Tab[];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Discoverable service from the facilitator catalog. */
|
|
48
|
-
export interface DiscoverService {
|
|
49
|
-
name: string;
|
|
50
|
-
description: string;
|
|
51
|
-
/** Public base URL for pay request (e.g. "https://weather.example.com"). */
|
|
52
|
-
baseUrl: string;
|
|
53
|
-
category: string;
|
|
54
|
-
keywords: string[];
|
|
55
|
-
routes: { path: string; method?: string; price?: string; settlement?: string; free?: boolean }[];
|
|
56
|
-
/** URL to API documentation (OpenAPI spec, docs page, etc). */
|
|
57
|
-
docsUrl?: string;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Options for discover search. */
|
|
61
|
-
export interface DiscoverOptions {
|
|
62
|
-
/** Search query (matches keywords and description). */
|
|
63
|
-
query?: string;
|
|
64
|
-
/** Sort order: "volume" (default), "newest", "price_asc", "price_desc". */
|
|
65
|
-
sort?: string;
|
|
66
|
-
/** Filter by category. */
|
|
67
|
-
category?: string;
|
|
68
|
-
/** Filter by settlement mode: "direct" or "tab". */
|
|
69
|
-
settlement?: string;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Registered webhook. */
|
|
73
|
-
export interface WebhookRegistration {
|
|
74
|
-
webhookId: string;
|
|
75
|
-
url: string;
|
|
76
|
-
events: string[];
|
|
77
|
-
}
|
package/src/ows-signer.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OWS (Open Wallet Standard) signer adapter.
|
|
3
|
-
*
|
|
4
|
-
* Wraps the @open-wallet-standard/core FFI module to implement the Pay
|
|
5
|
-
* Signer interface. OWS handles encrypted key storage + policy-gated signing;
|
|
6
|
-
* this adapter translates between Pay's (domain, types, value) EIP-712
|
|
7
|
-
* calling convention and OWS's JSON string convention.
|
|
8
|
-
*
|
|
9
|
-
* Priority: Pay's own CLI signer is #1, OWS is #2. OWS only activates when
|
|
10
|
-
* OWS_WALLET_ID is set AND @open-wallet-standard/core is installed. If the
|
|
11
|
-
* env var is set but the package is missing, creation fails loud with
|
|
12
|
-
* install instructions.
|
|
13
|
-
*
|
|
14
|
-
* Usage:
|
|
15
|
-
* const signer = await OwsSigner.create({ walletId: "pay-my-agent" });
|
|
16
|
-
* const wallet = new Wallet({ signer });
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import type { Signer } from "./signer.js";
|
|
20
|
-
|
|
21
|
-
/** Subset of @open-wallet-standard/core we call at runtime. */
|
|
22
|
-
interface OwsModule {
|
|
23
|
-
getWallet(nameOrId: string, vaultPath?: string): {
|
|
24
|
-
id: string;
|
|
25
|
-
name: string;
|
|
26
|
-
accounts: Array<{
|
|
27
|
-
chainId: string;
|
|
28
|
-
address: string;
|
|
29
|
-
derivationPath: string;
|
|
30
|
-
}>;
|
|
31
|
-
createdAt: string;
|
|
32
|
-
};
|
|
33
|
-
signTypedData(
|
|
34
|
-
wallet: string,
|
|
35
|
-
chain: string,
|
|
36
|
-
typedDataJson: string,
|
|
37
|
-
passphrase?: string,
|
|
38
|
-
index?: number,
|
|
39
|
-
vaultPath?: string,
|
|
40
|
-
): { signature: string; recoveryId?: number };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** EIP-712 domain fields. */
|
|
44
|
-
export interface TypedDataDomain {
|
|
45
|
-
name?: string;
|
|
46
|
-
version?: string;
|
|
47
|
-
chainId?: number | bigint;
|
|
48
|
-
verifyingContract?: string;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** EIP-712 type definitions. */
|
|
52
|
-
export interface TypedDataTypes {
|
|
53
|
-
[typeName: string]: Array<{ name: string; type: string }>;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Options for {@link OwsSigner.create}. */
|
|
57
|
-
export interface OwsSignerOptions {
|
|
58
|
-
/** OWS wallet name or UUID (e.g. "pay-my-agent"). */
|
|
59
|
-
walletId: string;
|
|
60
|
-
/** Chain name - only used for logging, not passed to OWS. Default: "base". */
|
|
61
|
-
chain?: string;
|
|
62
|
-
/** OWS API key token (passed as passphrase to OWS signing calls). */
|
|
63
|
-
owsApiKey?: string;
|
|
64
|
-
/** @internal Inject OWS module for testing - bypasses dynamic import. */
|
|
65
|
-
_owsModule?: unknown;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Signer backed by the Open Wallet Standard.
|
|
70
|
-
*
|
|
71
|
-
* - Keys live in OWS's encrypted vault (~/.ows/wallets/), never in env vars.
|
|
72
|
-
* - Signing calls go through OWS FFI, which evaluates policy rules before signing.
|
|
73
|
-
* - Use the static {@link create} factory - the constructor is private because
|
|
74
|
-
* address resolution requires a synchronous FFI call that must happen before
|
|
75
|
-
* sign() can work.
|
|
76
|
-
*/
|
|
77
|
-
export class OwsSigner implements Signer {
|
|
78
|
-
readonly #walletId: string;
|
|
79
|
-
readonly #address: string;
|
|
80
|
-
readonly #owsApiKey: string | undefined;
|
|
81
|
-
readonly #ows: OwsModule;
|
|
82
|
-
|
|
83
|
-
private constructor(
|
|
84
|
-
walletId: string,
|
|
85
|
-
address: string,
|
|
86
|
-
owsModule: OwsModule,
|
|
87
|
-
owsApiKey?: string,
|
|
88
|
-
) {
|
|
89
|
-
this.#walletId = walletId;
|
|
90
|
-
this.#address = address;
|
|
91
|
-
this.#ows = owsModule;
|
|
92
|
-
this.#owsApiKey = owsApiKey;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Create an OwsSigner by resolving the wallet address from OWS.
|
|
97
|
-
*
|
|
98
|
-
* Lazily imports @open-wallet-standard/core so the module is only required
|
|
99
|
-
* when OWS is actually used (keeps it an optional peer dependency).
|
|
100
|
-
*/
|
|
101
|
-
static async create(options: OwsSignerOptions): Promise<OwsSigner> {
|
|
102
|
-
let owsModule: OwsModule;
|
|
103
|
-
if (options._owsModule) {
|
|
104
|
-
owsModule = options._owsModule as OwsModule;
|
|
105
|
-
} else {
|
|
106
|
-
try {
|
|
107
|
-
// Dynamic string prevents TypeScript from statically resolving this
|
|
108
|
-
// optional peer dependency at compile time.
|
|
109
|
-
const moduleName = "@open-wallet-standard/core";
|
|
110
|
-
owsModule = (await import(moduleName)) as unknown as OwsModule;
|
|
111
|
-
} catch {
|
|
112
|
-
throw new Error(
|
|
113
|
-
"OWS_WALLET_ID is set but @open-wallet-standard/core is not installed. " +
|
|
114
|
-
"Install it with: npm install @open-wallet-standard/core",
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const walletInfo = owsModule.getWallet(options.walletId);
|
|
120
|
-
const evmAccount = walletInfo.accounts.find(
|
|
121
|
-
(a) => a.chainId === "evm" || a.chainId.startsWith("eip155:"),
|
|
122
|
-
);
|
|
123
|
-
if (!evmAccount) {
|
|
124
|
-
throw new Error(
|
|
125
|
-
`No EVM account found in OWS wallet '${options.walletId}'. ` +
|
|
126
|
-
`Available chains: ${walletInfo.accounts.map((a) => a.chainId).join(", ") || "none"}.`,
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return new OwsSigner(
|
|
131
|
-
options.walletId,
|
|
132
|
-
evmAccount.address,
|
|
133
|
-
owsModule,
|
|
134
|
-
options.owsApiKey,
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
get address(): string {
|
|
139
|
-
return this.#address;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
sign(_hash: Uint8Array): Uint8Array {
|
|
143
|
-
throw new Error(
|
|
144
|
-
"OwsSigner does not support raw hash signing. " +
|
|
145
|
-
"OWS only supports EIP-712 typed data signing. " +
|
|
146
|
-
"Use CliSigner or RawKeySigner for operations that require sign().",
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Sign EIP-712 typed data via OWS FFI.
|
|
152
|
-
*
|
|
153
|
-
* This is the primary signing method for OWS. Pay's auth module should
|
|
154
|
-
* call this instead of sign() when an OwsSigner is detected.
|
|
155
|
-
*/
|
|
156
|
-
async signTypedData(
|
|
157
|
-
domain: TypedDataDomain,
|
|
158
|
-
types: TypedDataTypes,
|
|
159
|
-
value: Record<string, unknown>,
|
|
160
|
-
): Promise<string> {
|
|
161
|
-
// 1. Derive primaryType: first key in types that is NOT "EIP712Domain".
|
|
162
|
-
const primaryType =
|
|
163
|
-
Object.keys(types).filter((k) => k !== "EIP712Domain")[0] ?? "Request";
|
|
164
|
-
|
|
165
|
-
// 2. Build EIP712Domain type array dynamically from domain object fields.
|
|
166
|
-
const eip712DomainType: Array<{ name: string; type: string }> = [];
|
|
167
|
-
if (domain.name !== undefined)
|
|
168
|
-
eip712DomainType.push({ name: "name", type: "string" });
|
|
169
|
-
if (domain.version !== undefined)
|
|
170
|
-
eip712DomainType.push({ name: "version", type: "string" });
|
|
171
|
-
if (domain.chainId !== undefined)
|
|
172
|
-
eip712DomainType.push({ name: "chainId", type: "uint256" });
|
|
173
|
-
if (domain.verifyingContract !== undefined)
|
|
174
|
-
eip712DomainType.push({
|
|
175
|
-
name: "verifyingContract",
|
|
176
|
-
type: "address",
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// 3. Assemble full EIP-712 typed data structure.
|
|
180
|
-
const fullTypedData = {
|
|
181
|
-
types: { EIP712Domain: eip712DomainType, ...types },
|
|
182
|
-
primaryType,
|
|
183
|
-
domain,
|
|
184
|
-
message: value,
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
// 4. Serialize - handle BigInt values.
|
|
188
|
-
const json = JSON.stringify(fullTypedData, (_key, v) =>
|
|
189
|
-
typeof v === "bigint" ? v.toString() : (v as unknown),
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
// 5. Call OWS FFI. Chain is always "evm" for EVM signing.
|
|
193
|
-
const result = this.#ows.signTypedData(
|
|
194
|
-
this.#walletId,
|
|
195
|
-
"evm",
|
|
196
|
-
json,
|
|
197
|
-
this.#owsApiKey,
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
// 6. Concatenate r+s+v into 65-byte Ethereum signature.
|
|
201
|
-
const sig = result.signature.startsWith("0x")
|
|
202
|
-
? result.signature.slice(2)
|
|
203
|
-
: result.signature;
|
|
204
|
-
|
|
205
|
-
// If OWS already returns r+s+v (130 hex chars), use as-is.
|
|
206
|
-
if (sig.length === 130) {
|
|
207
|
-
return `0x${sig}`;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Otherwise it's r+s (128 hex chars), append v.
|
|
211
|
-
const v = (result.recoveryId ?? 0) + 27;
|
|
212
|
-
return `0x${sig}${v.toString(16).padStart(2, "0")}`;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/** Prevent wallet ID / key leakage in serialization. */
|
|
216
|
-
toJSON(): Record<string, string> {
|
|
217
|
-
return { address: this.#address, walletId: this.#walletId };
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
[Symbol.for("nodejs.util.inspect.custom")](): string {
|
|
221
|
-
return `OwsSigner { address: '${this.#address}', wallet: '${this.#walletId}' }`;
|
|
222
|
-
}
|
|
223
|
-
}
|
package/src/signer.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Signer interface and implementations.
|
|
3
|
-
*
|
|
4
|
-
* Three modes:
|
|
5
|
-
* 1. CLI signer (default): subprocess call to `pay sign`
|
|
6
|
-
* 2. Raw key: from PAYSKILL_KEY environment variable (dev/testing only)
|
|
7
|
-
* 3. Custom: user provides a sign(hash) -> signature callback
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { execFileSync } from "node:child_process";
|
|
11
|
-
import { type Hex } from "viem";
|
|
12
|
-
import {
|
|
13
|
-
privateKeyToAccount,
|
|
14
|
-
sign as viemSign,
|
|
15
|
-
serializeSignature,
|
|
16
|
-
} from "viem/accounts";
|
|
17
|
-
|
|
18
|
-
/** Abstract signer interface. */
|
|
19
|
-
export interface Signer {
|
|
20
|
-
/** Sign a 32-byte hash. Returns 65-byte signature (r || s || v). */
|
|
21
|
-
sign(hash: Uint8Array): Uint8Array;
|
|
22
|
-
/** The signer's Ethereum address (0x-prefixed, checksummed). */
|
|
23
|
-
readonly address: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Signs via the `pay sign` CLI subprocess. */
|
|
27
|
-
export class CliSigner implements Signer {
|
|
28
|
-
private readonly command: string;
|
|
29
|
-
readonly address: string;
|
|
30
|
-
|
|
31
|
-
constructor(command = "pay", address = "") {
|
|
32
|
-
this.command = command;
|
|
33
|
-
if (address) {
|
|
34
|
-
this.address = address;
|
|
35
|
-
} else {
|
|
36
|
-
try {
|
|
37
|
-
this.address = execFileSync(this.command, ["address"], {
|
|
38
|
-
encoding: "utf-8",
|
|
39
|
-
timeout: 10_000,
|
|
40
|
-
}).trim();
|
|
41
|
-
} catch {
|
|
42
|
-
this.address = "";
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
sign(hash: Uint8Array): Uint8Array {
|
|
48
|
-
const hexHash = Buffer.from(hash).toString("hex");
|
|
49
|
-
const result = execFileSync(this.command, ["sign"], {
|
|
50
|
-
input: hexHash,
|
|
51
|
-
encoding: "utf-8",
|
|
52
|
-
timeout: 30_000,
|
|
53
|
-
});
|
|
54
|
-
return Buffer.from(result.trim(), "hex");
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Signs with a raw private key using viem. Dev/testing only. */
|
|
59
|
-
export class RawKeySigner implements Signer {
|
|
60
|
-
private readonly _key: Hex;
|
|
61
|
-
readonly address: string;
|
|
62
|
-
|
|
63
|
-
constructor(key?: string) {
|
|
64
|
-
const rawKey = key ?? process.env.PAYSKILL_KEY ?? "";
|
|
65
|
-
if (!rawKey) {
|
|
66
|
-
throw new Error("No key provided and PAYSKILL_KEY not set");
|
|
67
|
-
}
|
|
68
|
-
this._key = rawKey.startsWith("0x")
|
|
69
|
-
? (rawKey as Hex)
|
|
70
|
-
: (`0x${rawKey}` as Hex);
|
|
71
|
-
const account = privateKeyToAccount(this._key);
|
|
72
|
-
this.address = account.address;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
sign(_hash: Uint8Array): Uint8Array {
|
|
76
|
-
// viem's sign() does raw ECDSA signing (no EIP-191 prefix)
|
|
77
|
-
// sign() is async but Signer interface is sync — we use the sync internal
|
|
78
|
-
// For the sync interface, we need a workaround. Since RawKeySigner is
|
|
79
|
-
// dev/testing only, we'll do a sync computation via signSync.
|
|
80
|
-
// viem doesn't expose sync sign, but we can construct manually.
|
|
81
|
-
// Use the async path via signAsync helper pattern.
|
|
82
|
-
throw new Error(
|
|
83
|
-
"RawKeySigner.sign() is synchronous but viem signing is async. " +
|
|
84
|
-
"Use RawKeySigner.signAsync() or use buildAuthHeaders() directly with the private key."
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Async sign — preferred over sync sign(). */
|
|
89
|
-
async signAsync(hash: Uint8Array): Promise<Uint8Array> {
|
|
90
|
-
const hashHex = ("0x" + Buffer.from(hash).toString("hex")) as Hex;
|
|
91
|
-
const raw = await viemSign({ hash: hashHex, privateKey: this._key });
|
|
92
|
-
const sigHex = serializeSignature(raw);
|
|
93
|
-
return hexToBytes(sigHex);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** Delegates signing to a user-provided callback. */
|
|
98
|
-
export class CallbackSigner implements Signer {
|
|
99
|
-
private readonly callback: (hash: Uint8Array) => Uint8Array;
|
|
100
|
-
readonly address: string;
|
|
101
|
-
|
|
102
|
-
constructor(
|
|
103
|
-
callback: (hash: Uint8Array) => Uint8Array,
|
|
104
|
-
address = ""
|
|
105
|
-
) {
|
|
106
|
-
this.callback = callback;
|
|
107
|
-
this.address = address;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
sign(hash: Uint8Array): Uint8Array {
|
|
111
|
-
return this.callback(hash);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Factory for creating signers. */
|
|
116
|
-
export function createSigner(
|
|
117
|
-
mode: "cli" | "raw" | "custom" = "cli",
|
|
118
|
-
options: {
|
|
119
|
-
command?: string;
|
|
120
|
-
key?: string;
|
|
121
|
-
address?: string;
|
|
122
|
-
callback?: (hash: Uint8Array) => Uint8Array;
|
|
123
|
-
} = {}
|
|
124
|
-
): Signer {
|
|
125
|
-
switch (mode) {
|
|
126
|
-
case "cli":
|
|
127
|
-
return new CliSigner(options.command, options.address);
|
|
128
|
-
case "raw":
|
|
129
|
-
return new RawKeySigner(options.key);
|
|
130
|
-
case "custom":
|
|
131
|
-
if (!options.callback) {
|
|
132
|
-
throw new Error("custom signer requires a callback");
|
|
133
|
-
}
|
|
134
|
-
return new CallbackSigner(options.callback, options.address);
|
|
135
|
-
default:
|
|
136
|
-
throw new Error(`Unknown signer mode: ${mode as string}`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function hexToBytes(hex: string): Uint8Array {
|
|
141
|
-
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
142
|
-
const bytes = new Uint8Array(clean.length / 2);
|
|
143
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
144
|
-
bytes[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
145
|
-
}
|
|
146
|
-
return bytes;
|
|
147
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OWS Signer integration test.
|
|
3
|
-
*
|
|
4
|
-
* Requires real @open-wallet-standard/core installed with native binaries.
|
|
5
|
-
* Auto-skips if OWS is not available — safe to run in CI without OWS.
|
|
6
|
-
*
|
|
7
|
-
* Tests:
|
|
8
|
-
* - Creating a real OWS wallet
|
|
9
|
-
* - Constructing OwsSigner from it
|
|
10
|
-
* - Signing EIP-712 Permit typed data
|
|
11
|
-
* - Verifying signature recovers to correct address (ecrecover round-trip)
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { describe, it } from "node:test";
|
|
15
|
-
import assert from "node:assert/strict";
|
|
16
|
-
|
|
17
|
-
// Check if OWS is available before running tests
|
|
18
|
-
let owsAvailable = false;
|
|
19
|
-
try {
|
|
20
|
-
const ows = await import("@open-wallet-standard/core");
|
|
21
|
-
ows.listWallets();
|
|
22
|
-
owsAvailable = true;
|
|
23
|
-
} catch {
|
|
24
|
-
// OWS not installed — tests will be skipped
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
describe("OwsSigner integration (real OWS)", { skip: !owsAvailable }, () => {
|
|
28
|
-
it("creates signer from real OWS wallet and signs EIP-712 data", async () => {
|
|
29
|
-
const { OwsSigner } = await import("../src/ows-signer.js");
|
|
30
|
-
const { verifyTypedData } = await import("viem");
|
|
31
|
-
|
|
32
|
-
// Import OWS module
|
|
33
|
-
const ows = await import("@open-wallet-standard/core");
|
|
34
|
-
|
|
35
|
-
// Create a test wallet (unique name to avoid collisions)
|
|
36
|
-
const walletName = `pay-test-${Date.now()}`;
|
|
37
|
-
const walletInfo = ows.createWallet(walletName);
|
|
38
|
-
const evmAccount = walletInfo.accounts.find(
|
|
39
|
-
(a: { chainId: string }) =>
|
|
40
|
-
a.chainId === "evm" || a.chainId.startsWith("eip155:"),
|
|
41
|
-
);
|
|
42
|
-
assert.ok(evmAccount, "OWS wallet must have an EVM account");
|
|
43
|
-
|
|
44
|
-
// Create OwsSigner from the real wallet
|
|
45
|
-
const signer = await OwsSigner.create({ walletId: walletName });
|
|
46
|
-
assert.equal(
|
|
47
|
-
signer.address.toLowerCase(),
|
|
48
|
-
evmAccount.address.toLowerCase(),
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
// Sign an EIP-712 Permit (the most common signing operation in Pay)
|
|
52
|
-
const domain = {
|
|
53
|
-
name: "USD Coin",
|
|
54
|
-
version: "2",
|
|
55
|
-
chainId: 84532,
|
|
56
|
-
verifyingContract:
|
|
57
|
-
"0x036CbD53842c5426634e7929541eC2318f3dCF7e" as `0x${string}`,
|
|
58
|
-
};
|
|
59
|
-
const types = {
|
|
60
|
-
Permit: [
|
|
61
|
-
{ name: "owner", type: "address" },
|
|
62
|
-
{ name: "spender", type: "address" },
|
|
63
|
-
{ name: "value", type: "uint256" },
|
|
64
|
-
{ name: "nonce", type: "uint256" },
|
|
65
|
-
{ name: "deadline", type: "uint256" },
|
|
66
|
-
],
|
|
67
|
-
};
|
|
68
|
-
const message = {
|
|
69
|
-
owner: signer.address,
|
|
70
|
-
spender: "0x0000000000000000000000000000000000000001",
|
|
71
|
-
value: "5000000",
|
|
72
|
-
nonce: "0",
|
|
73
|
-
deadline: "999999999999",
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const signature = await signer.signTypedData(domain, types, message);
|
|
77
|
-
|
|
78
|
-
// Verify the signature recovers to the signer's address
|
|
79
|
-
assert.ok(signature.startsWith("0x"), "signature must be 0x-prefixed");
|
|
80
|
-
assert.equal(signature.length, 132, "signature must be 65 bytes (132 hex)");
|
|
81
|
-
|
|
82
|
-
const valid = await verifyTypedData({
|
|
83
|
-
address: signer.address as `0x${string}`,
|
|
84
|
-
domain,
|
|
85
|
-
types,
|
|
86
|
-
primaryType: "Permit",
|
|
87
|
-
message: message as Record<string, unknown>,
|
|
88
|
-
signature: signature as `0x${string}`,
|
|
89
|
-
});
|
|
90
|
-
assert.ok(valid, "ecrecover must recover signer address");
|
|
91
|
-
});
|
|
92
|
-
});
|