@keelcodes/sdk 2.3.0 → 2.3.1

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.
@@ -1,6 +1,5 @@
1
- import { type Address, type Hex } from 'viem';
2
- import type { SessionPolicy } from './types';
3
- import type { ChainAAConfig } from './types';
1
+ import { type Address, type Hex, type PublicClient } from 'viem';
2
+ import type { ChainAAConfig, SessionPolicy, Signer, UserOperationV7 } from './types';
4
3
  export declare const KEEL_POLICY_VERSION = 1n;
5
4
  /** bytes32 归一化(已是 0x+64 hex 直接用,否则左对齐补零) */
6
5
  export declare function toBytes32(id: string): Hex;
@@ -35,3 +34,37 @@ export declare function encodeKeelSessionSignature(p: {
35
34
  policyData: Hex;
36
35
  sig: Hex;
37
36
  }): Hex;
37
+ export interface BuildKeelSessionUserOpParams {
38
+ /** 未传 nonce 时用于读链上 getNonce;传 nonce(relay 预分配)时可不给 */
39
+ client?: PublicClient;
40
+ chainConfig: ChainAAConfig;
41
+ account: Address;
42
+ /** session 策略(复算 policyData = 链上 commitment,须与 enable 时逐字节一致) */
43
+ policy: SessionPolicy;
44
+ /** slot 序号(= validator 副本下标;一 key 一副本) */
45
+ slot: number;
46
+ /** 该 key 的 agent 通道号(nonceKey 隔离多 agent 并发);缺省 0 */
47
+ agentIndex?: number | bigint;
48
+ /**
49
+ * nonce 整值(= `nonceKey<<64 | sequence`),由 relay `POST /v1/wallet/session/nonce/allocate`
50
+ * 预分配后回传。缺省则读链上当前 getNonce(单 agent 串行场景)。
51
+ */
52
+ nonce?: bigint;
53
+ call: {
54
+ target: Address;
55
+ value?: bigint;
56
+ data: Hex;
57
+ };
58
+ /** agent(session key)签名者 */
59
+ agentSigner: Signer;
60
+ gas?: Partial<Pick<UserOperationV7, 'callGasLimit' | 'verificationGasLimit' | 'preVerificationGas' | 'maxFeePerGas' | 'maxPriorityFeePerGas'>>;
61
+ }
62
+ /**
63
+ * v1(Keel)agent 侧调用 UserOp 构造(已签名):
64
+ * nonce = 预分配整值,或 `EntryPoint.getNonce(account, validatorNonceKey(validator, agentIndex))`
65
+ * callData = `executeUserOp( execute(target,value,data) )` —— 挂真 hook 时必需
66
+ * signature= sessionId ‖ policyData ‖ agentSig(keccak256(userOpHash ‖ keccak256(policyData)))
67
+ *
68
+ * 与 aa-sdk 的 `buildKeelSessionUserOp` 逐字节一致(同输入同输出)。
69
+ */
70
+ export declare function buildKeelSessionUserOp(p: BuildKeelSessionUserOpParams): Promise<UserOperationV7>;
@@ -16,7 +16,10 @@ exports.encodeKeelPolicyData = encodeKeelPolicyData;
16
16
  exports.resolveKeelValidator = resolveKeelValidator;
17
17
  exports.keelSessionDigest = keelSessionDigest;
18
18
  exports.encodeKeelSessionSignature = encodeKeelSessionSignature;
19
+ exports.buildKeelSessionUserOp = buildKeelSessionUserOp;
19
20
  const viem_1 = require("viem");
21
+ const userop_1 = require("./userop");
22
+ const nonce_1 = require("./nonce");
20
23
  exports.KEEL_POLICY_VERSION = 1n;
21
24
  /** bytes32 归一化(已是 0x+64 hex 直接用,否则左对齐补零) */
22
25
  function toBytes32(id) {
@@ -88,3 +91,42 @@ function keelSessionDigest(userOpHash, policyData) {
88
91
  function encodeKeelSessionSignature(p) {
89
92
  return (0, viem_1.concatHex)([toBytes32(p.sessionId), p.policyData, p.sig]);
90
93
  }
94
+ /** EntryPoint v0.7 getNonce(address,uint192) */
95
+ const ENTRYPOINT_GETNONCE_ABI = (0, viem_1.parseAbi)([
96
+ 'function getNonce(address sender, uint192 key) view returns (uint256)',
97
+ ]);
98
+ /**
99
+ * v1(Keel)agent 侧调用 UserOp 构造(已签名):
100
+ * nonce = 预分配整值,或 `EntryPoint.getNonce(account, validatorNonceKey(validator, agentIndex))`
101
+ * callData = `executeUserOp( execute(target,value,data) )` —— 挂真 hook 时必需
102
+ * signature= sessionId ‖ policyData ‖ agentSig(keccak256(userOpHash ‖ keccak256(policyData)))
103
+ *
104
+ * 与 aa-sdk 的 `buildKeelSessionUserOp` 逐字节一致(同输入同输出)。
105
+ */
106
+ async function buildKeelSessionUserOp(p) {
107
+ const validator = resolveKeelValidator(p.chainConfig, p.slot);
108
+ // agentIndex(通道号)落在 nonceKey 低 16 位(uint16)⇒ 合法 0..65535;
109
+ // 超界会被 ValidatorLib 静默截断到别的通道,故在 SDK 边界显式拒绝。
110
+ const agentIndex = p.agentIndex ?? 0;
111
+ if (BigInt(agentIndex) < 0n || BigInt(agentIndex) > 65535n) {
112
+ throw new Error(`[keel-sdk] buildKeelSessionUserOp: agentIndex 必须在 0..65535(uint16 通道号),got ${agentIndex}`);
113
+ }
114
+ let nonce = p.nonce;
115
+ if (nonce === undefined) {
116
+ if (!p.client) {
117
+ throw new Error('[keel-sdk] buildKeelSessionUserOp: 未传 nonce 时必须提供 client(用于读链上 getNonce)');
118
+ }
119
+ nonce = (await p.client.readContract({
120
+ address: p.chainConfig.entryPoint,
121
+ abi: ENTRYPOINT_GETNONCE_ABI,
122
+ functionName: 'getNonce',
123
+ args: [p.account, (0, nonce_1.validatorNonceKey)(validator, agentIndex)],
124
+ }));
125
+ }
126
+ const callData = (0, userop_1.encodeExecuteUserOp)((0, userop_1.encodeExecute)(p.call.target, p.call.value ?? 0n, p.call.data));
127
+ const op = (0, userop_1.buildUserOpWithCallData)({ sender: p.account, nonce, callData, gas: p.gas });
128
+ const userOpHash = (0, userop_1.getUserOpHash)(op, p.chainConfig.entryPoint, p.chainConfig.chainId);
129
+ const policyData = encodeKeelPolicyData(p.policy);
130
+ const sig = await p.agentSigner.signUserOp(keelSessionDigest(userOpHash, policyData));
131
+ return { ...op, signature: encodeKeelSessionSignature({ sessionId: p.policy.sessionId, policyData, sig }) };
132
+ }
package/dist/http.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { KeelConfig } from './types';
2
2
  /** SDK 版本(契约 §5,随包版本同步)。 */
3
- export declare const SDK_VERSION = "2.3.0";
3
+ export declare const SDK_VERSION = "2.3.1";
4
4
  export declare class HttpClient {
5
5
  private baseUrl;
6
6
  private headers;
package/dist/http.js CHANGED
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HttpClient = exports.SDK_VERSION = void 0;
7
7
  const errors_1 = require("./errors");
8
8
  /** SDK 版本(契约 §5,随包版本同步)。 */
9
- exports.SDK_VERSION = '2.3.0';
9
+ exports.SDK_VERSION = '2.3.1';
10
10
  /**
11
11
  * 钱包签名的复用时长(ms):首次签名后在该窗口内复用,避免每个请求都触发钱包弹窗。
12
12
  * 服务端会话 TTL 为 24h,此处取 1h 留足余量。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keelcodes/sdk",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "Keel SDK — unified access to payment / wallet / data / account modules over https://keel.codes",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",