@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/dist/ows-signer.js
DELETED
|
@@ -1,130 +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
|
-
* Signer backed by the Open Wallet Standard.
|
|
20
|
-
*
|
|
21
|
-
* - Keys live in OWS's encrypted vault (~/.ows/wallets/), never in env vars.
|
|
22
|
-
* - Signing calls go through OWS FFI, which evaluates policy rules before signing.
|
|
23
|
-
* - Use the static {@link create} factory - the constructor is private because
|
|
24
|
-
* address resolution requires a synchronous FFI call that must happen before
|
|
25
|
-
* sign() can work.
|
|
26
|
-
*/
|
|
27
|
-
export class OwsSigner {
|
|
28
|
-
#walletId;
|
|
29
|
-
#address;
|
|
30
|
-
#owsApiKey;
|
|
31
|
-
#ows;
|
|
32
|
-
constructor(walletId, address, owsModule, owsApiKey) {
|
|
33
|
-
this.#walletId = walletId;
|
|
34
|
-
this.#address = address;
|
|
35
|
-
this.#ows = owsModule;
|
|
36
|
-
this.#owsApiKey = owsApiKey;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Create an OwsSigner by resolving the wallet address from OWS.
|
|
40
|
-
*
|
|
41
|
-
* Lazily imports @open-wallet-standard/core so the module is only required
|
|
42
|
-
* when OWS is actually used (keeps it an optional peer dependency).
|
|
43
|
-
*/
|
|
44
|
-
static async create(options) {
|
|
45
|
-
let owsModule;
|
|
46
|
-
if (options._owsModule) {
|
|
47
|
-
owsModule = options._owsModule;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
try {
|
|
51
|
-
// Dynamic string prevents TypeScript from statically resolving this
|
|
52
|
-
// optional peer dependency at compile time.
|
|
53
|
-
const moduleName = "@open-wallet-standard/core";
|
|
54
|
-
owsModule = (await import(moduleName));
|
|
55
|
-
}
|
|
56
|
-
catch {
|
|
57
|
-
throw new Error("OWS_WALLET_ID is set but @open-wallet-standard/core is not installed. " +
|
|
58
|
-
"Install it with: npm install @open-wallet-standard/core");
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
const walletInfo = owsModule.getWallet(options.walletId);
|
|
62
|
-
const evmAccount = walletInfo.accounts.find((a) => a.chainId === "evm" || a.chainId.startsWith("eip155:"));
|
|
63
|
-
if (!evmAccount) {
|
|
64
|
-
throw new Error(`No EVM account found in OWS wallet '${options.walletId}'. ` +
|
|
65
|
-
`Available chains: ${walletInfo.accounts.map((a) => a.chainId).join(", ") || "none"}.`);
|
|
66
|
-
}
|
|
67
|
-
return new OwsSigner(options.walletId, evmAccount.address, owsModule, options.owsApiKey);
|
|
68
|
-
}
|
|
69
|
-
get address() {
|
|
70
|
-
return this.#address;
|
|
71
|
-
}
|
|
72
|
-
sign(_hash) {
|
|
73
|
-
throw new Error("OwsSigner does not support raw hash signing. " +
|
|
74
|
-
"OWS only supports EIP-712 typed data signing. " +
|
|
75
|
-
"Use CliSigner or RawKeySigner for operations that require sign().");
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Sign EIP-712 typed data via OWS FFI.
|
|
79
|
-
*
|
|
80
|
-
* This is the primary signing method for OWS. Pay's auth module should
|
|
81
|
-
* call this instead of sign() when an OwsSigner is detected.
|
|
82
|
-
*/
|
|
83
|
-
async signTypedData(domain, types, value) {
|
|
84
|
-
// 1. Derive primaryType: first key in types that is NOT "EIP712Domain".
|
|
85
|
-
const primaryType = Object.keys(types).filter((k) => k !== "EIP712Domain")[0] ?? "Request";
|
|
86
|
-
// 2. Build EIP712Domain type array dynamically from domain object fields.
|
|
87
|
-
const eip712DomainType = [];
|
|
88
|
-
if (domain.name !== undefined)
|
|
89
|
-
eip712DomainType.push({ name: "name", type: "string" });
|
|
90
|
-
if (domain.version !== undefined)
|
|
91
|
-
eip712DomainType.push({ name: "version", type: "string" });
|
|
92
|
-
if (domain.chainId !== undefined)
|
|
93
|
-
eip712DomainType.push({ name: "chainId", type: "uint256" });
|
|
94
|
-
if (domain.verifyingContract !== undefined)
|
|
95
|
-
eip712DomainType.push({
|
|
96
|
-
name: "verifyingContract",
|
|
97
|
-
type: "address",
|
|
98
|
-
});
|
|
99
|
-
// 3. Assemble full EIP-712 typed data structure.
|
|
100
|
-
const fullTypedData = {
|
|
101
|
-
types: { EIP712Domain: eip712DomainType, ...types },
|
|
102
|
-
primaryType,
|
|
103
|
-
domain,
|
|
104
|
-
message: value,
|
|
105
|
-
};
|
|
106
|
-
// 4. Serialize - handle BigInt values.
|
|
107
|
-
const json = JSON.stringify(fullTypedData, (_key, v) => typeof v === "bigint" ? v.toString() : v);
|
|
108
|
-
// 5. Call OWS FFI. Chain is always "evm" for EVM signing.
|
|
109
|
-
const result = this.#ows.signTypedData(this.#walletId, "evm", json, this.#owsApiKey);
|
|
110
|
-
// 6. Concatenate r+s+v into 65-byte Ethereum signature.
|
|
111
|
-
const sig = result.signature.startsWith("0x")
|
|
112
|
-
? result.signature.slice(2)
|
|
113
|
-
: result.signature;
|
|
114
|
-
// If OWS already returns r+s+v (130 hex chars), use as-is.
|
|
115
|
-
if (sig.length === 130) {
|
|
116
|
-
return `0x${sig}`;
|
|
117
|
-
}
|
|
118
|
-
// Otherwise it's r+s (128 hex chars), append v.
|
|
119
|
-
const v = (result.recoveryId ?? 0) + 27;
|
|
120
|
-
return `0x${sig}${v.toString(16).padStart(2, "0")}`;
|
|
121
|
-
}
|
|
122
|
-
/** Prevent wallet ID / key leakage in serialization. */
|
|
123
|
-
toJSON() {
|
|
124
|
-
return { address: this.#address, walletId: this.#walletId };
|
|
125
|
-
}
|
|
126
|
-
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
127
|
-
return `OwsSigner { address: '${this.#address}', wallet: '${this.#walletId}' }`;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
//# sourceMappingURL=ows-signer.js.map
|
package/dist/ows-signer.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ows-signer.js","sourceRoot":"","sources":["../src/ows-signer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAmDH;;;;;;;;GAQG;AACH,MAAM,OAAO,SAAS;IACX,SAAS,CAAS;IAClB,QAAQ,CAAS;IACjB,UAAU,CAAqB;IAC/B,IAAI,CAAY;IAEzB,YACE,QAAgB,EAChB,OAAe,EACf,SAAoB,EACpB,SAAkB;QAElB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAyB;QAC3C,IAAI,SAAoB,CAAC;QACzB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvB,SAAS,GAAG,OAAO,CAAC,UAAuB,CAAC;QAC9C,CAAC;aAAM,CAAC;YACN,IA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,CAAC,aAAa,CACpC,IAAI,CAAC,SAAS,EACd,KAAK,EACL,IAAI,EACJ,IAAI,CAAC,UAAU,CAChB,CAAC;QAEF,wDAAwD;QACxD,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC;YAC3C,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;YAC3B,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;QAErB,2DAA2D;QAC3D,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YACvB,OAAO,KAAK,GAAG,EAAE,CAAC;QACpB,CAAC;QAED,gDAAgD;QAChD,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;QACxC,OAAO,KAAK,GAAG,GAAG,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;IACtD,CAAC;IAED,wDAAwD;IACxD,MAAM;QACJ,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC;IAC9D,CAAC;IAED,CAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;QACxC,OAAO,yBAAyB,IAAI,CAAC,QAAQ,eAAe,IAAI,CAAC,SAAS,KAAK,CAAC;IAClF,CAAC;CACF"}
|
package/dist/signer.d.ts
DELETED
|
@@ -1,46 +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
|
-
/** Abstract signer interface. */
|
|
10
|
-
export interface Signer {
|
|
11
|
-
/** Sign a 32-byte hash. Returns 65-byte signature (r || s || v). */
|
|
12
|
-
sign(hash: Uint8Array): Uint8Array;
|
|
13
|
-
/** The signer's Ethereum address (0x-prefixed, checksummed). */
|
|
14
|
-
readonly address: string;
|
|
15
|
-
}
|
|
16
|
-
/** Signs via the `pay sign` CLI subprocess. */
|
|
17
|
-
export declare class CliSigner implements Signer {
|
|
18
|
-
private readonly command;
|
|
19
|
-
readonly address: string;
|
|
20
|
-
constructor(command?: string, address?: string);
|
|
21
|
-
sign(hash: Uint8Array): Uint8Array;
|
|
22
|
-
}
|
|
23
|
-
/** Signs with a raw private key using viem. Dev/testing only. */
|
|
24
|
-
export declare class RawKeySigner implements Signer {
|
|
25
|
-
private readonly _key;
|
|
26
|
-
readonly address: string;
|
|
27
|
-
constructor(key?: string);
|
|
28
|
-
sign(_hash: Uint8Array): Uint8Array;
|
|
29
|
-
/** Async sign — preferred over sync sign(). */
|
|
30
|
-
signAsync(hash: Uint8Array): Promise<Uint8Array>;
|
|
31
|
-
}
|
|
32
|
-
/** Delegates signing to a user-provided callback. */
|
|
33
|
-
export declare class CallbackSigner implements Signer {
|
|
34
|
-
private readonly callback;
|
|
35
|
-
readonly address: string;
|
|
36
|
-
constructor(callback: (hash: Uint8Array) => Uint8Array, address?: string);
|
|
37
|
-
sign(hash: Uint8Array): Uint8Array;
|
|
38
|
-
}
|
|
39
|
-
/** Factory for creating signers. */
|
|
40
|
-
export declare function createSigner(mode?: "cli" | "raw" | "custom", options?: {
|
|
41
|
-
command?: string;
|
|
42
|
-
key?: string;
|
|
43
|
-
address?: string;
|
|
44
|
-
callback?: (hash: Uint8Array) => Uint8Array;
|
|
45
|
-
}): Signer;
|
|
46
|
-
//# sourceMappingURL=signer.d.ts.map
|
package/dist/signer.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],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C,IAAI,EAAE,UAAU,KAAK,UAAU,CAAC;CACxC,GACL,MAAM,CAcR"}
|
package/dist/signer.js
DELETED
|
@@ -1,111 +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
|
-
import { execFileSync } from "node:child_process";
|
|
10
|
-
import { privateKeyToAccount, sign as viemSign, serializeSignature, } from "viem/accounts";
|
|
11
|
-
/** Signs via the `pay sign` CLI subprocess. */
|
|
12
|
-
export class CliSigner {
|
|
13
|
-
command;
|
|
14
|
-
address;
|
|
15
|
-
constructor(command = "pay", address = "") {
|
|
16
|
-
this.command = command;
|
|
17
|
-
if (address) {
|
|
18
|
-
this.address = address;
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
try {
|
|
22
|
-
this.address = execFileSync(this.command, ["address"], {
|
|
23
|
-
encoding: "utf-8",
|
|
24
|
-
timeout: 10_000,
|
|
25
|
-
}).trim();
|
|
26
|
-
}
|
|
27
|
-
catch {
|
|
28
|
-
this.address = "";
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
sign(hash) {
|
|
33
|
-
const hexHash = Buffer.from(hash).toString("hex");
|
|
34
|
-
const result = execFileSync(this.command, ["sign"], {
|
|
35
|
-
input: hexHash,
|
|
36
|
-
encoding: "utf-8",
|
|
37
|
-
timeout: 30_000,
|
|
38
|
-
});
|
|
39
|
-
return Buffer.from(result.trim(), "hex");
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/** Signs with a raw private key using viem. Dev/testing only. */
|
|
43
|
-
export class RawKeySigner {
|
|
44
|
-
_key;
|
|
45
|
-
address;
|
|
46
|
-
constructor(key) {
|
|
47
|
-
const rawKey = key ?? process.env.PAYSKILL_KEY ?? "";
|
|
48
|
-
if (!rawKey) {
|
|
49
|
-
throw new Error("No key provided and PAYSKILL_KEY not set");
|
|
50
|
-
}
|
|
51
|
-
this._key = rawKey.startsWith("0x")
|
|
52
|
-
? rawKey
|
|
53
|
-
: `0x${rawKey}`;
|
|
54
|
-
const account = privateKeyToAccount(this._key);
|
|
55
|
-
this.address = account.address;
|
|
56
|
-
}
|
|
57
|
-
sign(_hash) {
|
|
58
|
-
// viem's sign() does raw ECDSA signing (no EIP-191 prefix)
|
|
59
|
-
// sign() is async but Signer interface is sync — we use the sync internal
|
|
60
|
-
// For the sync interface, we need a workaround. Since RawKeySigner is
|
|
61
|
-
// dev/testing only, we'll do a sync computation via signSync.
|
|
62
|
-
// viem doesn't expose sync sign, but we can construct manually.
|
|
63
|
-
// Use the async path via signAsync helper pattern.
|
|
64
|
-
throw new Error("RawKeySigner.sign() is synchronous but viem signing is async. " +
|
|
65
|
-
"Use RawKeySigner.signAsync() or use buildAuthHeaders() directly with the private key.");
|
|
66
|
-
}
|
|
67
|
-
/** Async sign — preferred over sync sign(). */
|
|
68
|
-
async signAsync(hash) {
|
|
69
|
-
const hashHex = ("0x" + Buffer.from(hash).toString("hex"));
|
|
70
|
-
const raw = await viemSign({ hash: hashHex, privateKey: this._key });
|
|
71
|
-
const sigHex = serializeSignature(raw);
|
|
72
|
-
return hexToBytes(sigHex);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/** Delegates signing to a user-provided callback. */
|
|
76
|
-
export class CallbackSigner {
|
|
77
|
-
callback;
|
|
78
|
-
address;
|
|
79
|
-
constructor(callback, address = "") {
|
|
80
|
-
this.callback = callback;
|
|
81
|
-
this.address = address;
|
|
82
|
-
}
|
|
83
|
-
sign(hash) {
|
|
84
|
-
return this.callback(hash);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
/** Factory for creating signers. */
|
|
88
|
-
export function createSigner(mode = "cli", options = {}) {
|
|
89
|
-
switch (mode) {
|
|
90
|
-
case "cli":
|
|
91
|
-
return new CliSigner(options.command, options.address);
|
|
92
|
-
case "raw":
|
|
93
|
-
return new RawKeySigner(options.key);
|
|
94
|
-
case "custom":
|
|
95
|
-
if (!options.callback) {
|
|
96
|
-
throw new Error("custom signer requires a callback");
|
|
97
|
-
}
|
|
98
|
-
return new CallbackSigner(options.callback, options.address);
|
|
99
|
-
default:
|
|
100
|
-
throw new Error(`Unknown signer mode: ${mode}`);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
function hexToBytes(hex) {
|
|
104
|
-
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
105
|
-
const bytes = new Uint8Array(clean.length / 2);
|
|
106
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
107
|
-
bytes[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
108
|
-
}
|
|
109
|
-
return bytes;
|
|
110
|
-
}
|
|
111
|
-
//# sourceMappingURL=signer.js.map
|
package/dist/signer.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"signer.js","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EACL,mBAAmB,EACnB,IAAI,IAAI,QAAQ,EAChB,kBAAkB,GACnB,MAAM,eAAe,CAAC;AAUvB,+CAA+C;AAC/C,MAAM,OAAO,SAAS;IACH,OAAO,CAAS;IACxB,OAAO,CAAS;IAEzB,YAAY,OAAO,GAAG,KAAK,EAAE,OAAO,GAAG,EAAE;QACvC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACzB,CAAC;aAAM,CAAC;YACN,IAAI,CAAC;gBACH,IAAI,CAAC,OAAO,GAAG,YAAY,CAAC,IAA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