@keelcodes/sdk 0.0.0-stage → 2.0.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.
@@ -0,0 +1,15 @@
1
+ import { PaymentAPI } from './payment';
2
+ import { WalletAPI } from './wallet';
3
+ import { DataAPI } from './data';
4
+ import { AccountAPI } from './account';
5
+ import type { KeelConfig } from './types';
6
+ export { KeelApiError, isKeelApiError } from './errors';
7
+ export type { KeelConfig, KeelModule, KeelErrorBody, KeelWalletAuth } from './types';
8
+ export declare class Keel {
9
+ readonly payment: PaymentAPI;
10
+ readonly wallet: WalletAPI;
11
+ readonly data: DataAPI;
12
+ readonly account: AccountAPI;
13
+ constructor(config?: KeelConfig);
14
+ }
15
+ export default Keel;
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // ---------------------------------------------------------------------------
3
+ // @keelcodes/sdk — 聚合入口(单包,4 个 namespace)
4
+ // ---------------------------------------------------------------------------
5
+ // 用法:
6
+ // import { Keel } from '@keelcodes/sdk' // 全家桶
7
+ // import { PaymentAPI } from '@keelcodes/sdk/payment' // 按域 tree-shaking
8
+ // ---------------------------------------------------------------------------
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.Keel = exports.isKeelApiError = exports.KeelApiError = void 0;
11
+ const http_1 = require("./http");
12
+ const payment_1 = require("./payment");
13
+ const wallet_1 = require("./wallet");
14
+ const data_1 = require("./data");
15
+ const account_1 = require("./account");
16
+ var errors_1 = require("./errors");
17
+ Object.defineProperty(exports, "KeelApiError", { enumerable: true, get: function () { return errors_1.KeelApiError; } });
18
+ Object.defineProperty(exports, "isKeelApiError", { enumerable: true, get: function () { return errors_1.isKeelApiError; } });
19
+ class Keel {
20
+ constructor(config = {}) {
21
+ const http = new http_1.HttpClient(config);
22
+ this.payment = new payment_1.PaymentAPI(http);
23
+ this.wallet = new wallet_1.WalletAPI(http);
24
+ this.data = new data_1.DataAPI(http);
25
+ this.account = new account_1.AccountAPI(http);
26
+ }
27
+ }
28
+ exports.Keel = Keel;
29
+ exports.default = Keel;
@@ -0,0 +1,230 @@
1
+ import { HttpClient } from './http';
2
+ import type { Reference } from './types';
3
+ export interface ChargeParams {
4
+ payer: string;
5
+ amountWei: string;
6
+ /** 收费 rail(内部路由,对外仅暴露语义)。 */
7
+ method: 'x402' | 'a2a' | 'batch' | 'invite';
8
+ asset?: string;
9
+ chain?: string;
10
+ reference: Reference;
11
+ metadata?: Record<string, unknown>;
12
+ }
13
+ /** 链上收费返回收款地址 / 价格 / 网络。 */
14
+ export type ChargeResult = {
15
+ reference: string;
16
+ payTo: string;
17
+ priceWei: string;
18
+ network: string;
19
+ };
20
+ export interface PayParams {
21
+ payee: string;
22
+ amountWei: string;
23
+ asset?: string;
24
+ reference: Reference;
25
+ metadata?: Record<string, unknown>;
26
+ }
27
+ export interface PayResult {
28
+ reference: string;
29
+ settled: boolean;
30
+ transferId: string;
31
+ }
32
+ /** x402 v2 支付凭证校验结果(规范 §5.4 VerifyResponse)。 */
33
+ export interface X402VerifyResult {
34
+ isValid: boolean;
35
+ invalidReason?: string;
36
+ payer?: string;
37
+ }
38
+ /** x402 v2 `PaymentRequirements`(PaymentRequired.accepts 单项)。 */
39
+ export interface X402PaymentRequirements {
40
+ scheme: 'exact' | 'upto';
41
+ network: string;
42
+ asset: string;
43
+ payTo: string;
44
+ amount: string;
45
+ maxTimeoutSeconds?: number;
46
+ extra?: Record<string, unknown>;
47
+ }
48
+ /** x402 v2 facilitator 发现(规范 §7.3)。 */
49
+ export interface X402Supported {
50
+ kinds: Array<{
51
+ x402Version: 2;
52
+ scheme: 'exact' | 'upto';
53
+ network: string;
54
+ }>;
55
+ }
56
+ export interface BalanceResult {
57
+ address: string;
58
+ balanceWei: string;
59
+ }
60
+ export interface PaymentOrder {
61
+ intentId: string;
62
+ method: string;
63
+ subscriber: string | null;
64
+ asset: string | null;
65
+ amountWei: string | null;
66
+ status: string;
67
+ metadata: Record<string, unknown> | null;
68
+ createdAt: string;
69
+ updatedAt: string;
70
+ }
71
+ export interface OrdersParams {
72
+ limit?: number;
73
+ offset?: number;
74
+ status?: string;
75
+ subscriber?: string;
76
+ }
77
+ export interface OrdersResult {
78
+ orders: PaymentOrder[];
79
+ }
80
+ /** 单笔结算可核验读取(PC-4)。 */
81
+ export interface SettlementRecord {
82
+ reference: string;
83
+ payer: string;
84
+ amountWei: string;
85
+ asset: string;
86
+ chainId: number;
87
+ method?: string;
88
+ settledAt: string;
89
+ metadata?: Record<string, unknown>;
90
+ }
91
+ /** 402 待付款结构化(PC-3)。 */
92
+ export interface PaymentRequiredBody {
93
+ priceWei: string;
94
+ payTo: string;
95
+ resource: string;
96
+ resumeRef: string;
97
+ mode: 'topup' | 'subscribe';
98
+ network: string;
99
+ asset: string;
100
+ }
101
+ export declare class PaymentAPI {
102
+ private http;
103
+ constructor(http: HttpClient);
104
+ /** 发起收费(内部路由 x402/a2a/batch/invite)。 */
105
+ charge(params: ChargeParams): Promise<ChargeResult>;
106
+ /** 主动付费。 */
107
+ pay(params: PayParams): Promise<PayResult>;
108
+ /** 校验 x402 v2 支付凭证(base64 PAYMENT-SIGNATURE)——facilitator verify。 */
109
+ verifyPayment(paymentPayload: string, paymentRequirements?: X402PaymentRequirements): Promise<X402VerifyResult>;
110
+ /** x402 v2 facilitator 发现(支持的 scheme / network)。 */
111
+ supported(): Promise<X402Supported>;
112
+ /** 查余额。 */
113
+ balance(address: string, asset?: string): Promise<BalanceResult>;
114
+ /** 查交易记录。 */
115
+ orders(params?: OrdersParams): Promise<OrdersResult>;
116
+ /** 核验单笔结算(按幂等键 reference)。 */
117
+ settlement(reference: Reference): Promise<SettlementRecord>;
118
+ /** 访问判定/按次扣费(返回 active,或 402 抛 KeelApiError)。 */
119
+ access(subscriber: string, resource: unknown): Promise<{
120
+ active: boolean;
121
+ }>;
122
+ /** 按量套餐价目(公开;canonical `/v1/payment/plans`,由 mpc 承载)。 */
123
+ plans(): Promise<Record<string, unknown>>;
124
+ /** 预付费账本余额(canonical `/v1/payment/ledger-balance`,由 mpc 承载)。 */
125
+ ledgerBalance(params: {
126
+ token: string;
127
+ }): Promise<Record<string, unknown>>;
128
+ /** 支付通道发现:启用的 rail、各链收款钱包 / 价格 / 托管、各链可接受的稳定币。 */
129
+ info(chain?: string): Promise<Record<string, unknown>>;
130
+ /** 读取链上订阅详情(active / payToken / amountPaid / 周期 / 试用)。 */
131
+ subscriptionDetail(chain: string, subscriber: string, resourceId: number): Promise<Record<string, unknown>>;
132
+ /** 读取链上套餐定价(price / period / active)。 */
133
+ price(planId: number, chain?: string): Promise<Record<string, unknown>>;
134
+ /**
135
+ * 创建 a2a 支付意图(phase 1)。
136
+ * `settlementMode: 'direct'` 时 `payee` 必填,settle 只核验 payer → payee 的链上转账。
137
+ */
138
+ a2aCreate(params: {
139
+ subscriber: string;
140
+ valueWei: string;
141
+ asset?: string;
142
+ chain?: string;
143
+ payee?: string;
144
+ settlementMode?: 'collector' | 'direct';
145
+ expiresIn?: number;
146
+ metadata?: Record<string, unknown>;
147
+ }): Promise<Record<string, unknown>>;
148
+ /** 确认 a2a 支付意图(phase 2):mode=tx 校验链上交易;mode=balance 走账本扣减。 */
149
+ a2aSettle(params: {
150
+ paymentId: string;
151
+ mode?: 'tx' | 'balance';
152
+ txHash?: string;
153
+ chain?: string;
154
+ subscriber?: string;
155
+ amountWei?: string;
156
+ asset?: string;
157
+ ref?: string;
158
+ }): Promise<Record<string, unknown>>;
159
+ /** 结算 direct 意图(phase 2):按 intent 绑定的 payee/asset/精确金额核验,并按链确认数判定终局。 */
160
+ a2aDirectSettle(paymentId: string, txHash: string): Promise<Record<string, unknown>>;
161
+ /** 取消未结算的 direct 意图(幂等)。 */
162
+ a2aCancel(paymentId: string): Promise<Record<string, unknown>>;
163
+ /** 创建批量收款意图(一付款人对多收款人)。 */
164
+ batchCreate(params: {
165
+ subscriber: string;
166
+ items: Array<Record<string, unknown>>;
167
+ chain?: string;
168
+ metadata?: Record<string, unknown>;
169
+ }): Promise<Record<string, unknown>>;
170
+ /** 结算批量收款中的一项(提交链上 txHash)。 */
171
+ batchSettle(params: {
172
+ batchId: string;
173
+ itemId: string;
174
+ txHash: string;
175
+ chain?: string;
176
+ }): Promise<Record<string, unknown>>;
177
+ /** 读取批量收款状态。 */
178
+ batchGet(batchId: string): Promise<Record<string, unknown>>;
179
+ /** 取消批量收款。 */
180
+ batchCancel(batchId: string): Promise<Record<string, unknown>>;
181
+ /** 创建账单邀请(payer → payee)。 */
182
+ inviteCreate(params: {
183
+ payer: string;
184
+ payee: string;
185
+ valueWei: string;
186
+ asset?: string;
187
+ chain?: string;
188
+ dueAt?: string;
189
+ memo?: string;
190
+ metadata?: Record<string, unknown>;
191
+ }): Promise<Record<string, unknown>>;
192
+ /** 按地址 + 角色(payer|payee)列出邀请。 */
193
+ inviteList(p: {
194
+ address: string;
195
+ role: 'payer' | 'payee';
196
+ status?: string;
197
+ }): Promise<Record<string, unknown>>;
198
+ /** 读取单个邀请详情。 */
199
+ inviteGet(inviteId: string): Promise<Record<string, unknown>>;
200
+ /** 取消未结算的邀请。 */
201
+ inviteCancel(inviteId: string): Promise<Record<string, unknown>>;
202
+ /** 链上结算邀请(提交 payer txHash)。 */
203
+ inviteSettle(p: {
204
+ inviteId: string;
205
+ txHash: string;
206
+ chain?: string;
207
+ }): Promise<Record<string, unknown>>;
208
+ /** 用 payer 账本余额支付邀请(无链上交易)。 */
209
+ invitePay(inviteId: string): Promise<Record<string, unknown>>;
210
+ /** 请求一笔账本内转账(from → to)。 */
211
+ transferCreate(params: {
212
+ from: string;
213
+ to: string;
214
+ valueWei: string;
215
+ asset?: string;
216
+ reference?: string;
217
+ metadata?: Record<string, unknown>;
218
+ }): Promise<Record<string, unknown>>;
219
+ /** 按地址 + 角色(from|to)列出转账。 */
220
+ transferList(p: {
221
+ address: string;
222
+ role: 'from' | 'to';
223
+ }): Promise<Record<string, unknown>>;
224
+ /** 读取单笔转账详情。 */
225
+ transferGet(transferId: string): Promise<Record<string, unknown>>;
226
+ /** 确认并原子执行转账。 */
227
+ transferConfirm(transferId: string): Promise<Record<string, unknown>>;
228
+ /** 取消未执行的转账。 */
229
+ transferCancel(transferId: string): Promise<Record<string, unknown>>;
230
+ }
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ // ---------------------------------------------------------------------------
3
+ // @keelcodes/sdk — payment 模块(7 动作)
4
+ // ---------------------------------------------------------------------------
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PaymentAPI = void 0;
7
+ class PaymentAPI {
8
+ constructor(http) {
9
+ this.http = http;
10
+ }
11
+ /** 发起收费(内部路由 x402/a2a/batch/invite)。 */
12
+ charge(params) {
13
+ return this.http.post('/v1/payment/charge', params);
14
+ }
15
+ /** 主动付费。 */
16
+ pay(params) {
17
+ return this.http.post('/v1/payment/pay', params);
18
+ }
19
+ /** 校验 x402 v2 支付凭证(base64 PAYMENT-SIGNATURE)——facilitator verify。 */
20
+ verifyPayment(paymentPayload, paymentRequirements) {
21
+ return this.http.post('/v1/payment/x402/verify', { x402Version: 2, paymentPayload, paymentRequirements });
22
+ }
23
+ /** x402 v2 facilitator 发现(支持的 scheme / network)。 */
24
+ supported() {
25
+ return this.http.get('/v1/payment/x402/supported');
26
+ }
27
+ /** 查余额。 */
28
+ balance(address, asset) {
29
+ const q = new URLSearchParams({ address });
30
+ if (asset)
31
+ q.set('asset', asset);
32
+ return this.http.get('/v1/payment/balance?' + q.toString());
33
+ }
34
+ /** 查交易记录。 */
35
+ orders(params = {}) {
36
+ const q = new URLSearchParams();
37
+ if (params.limit !== undefined)
38
+ q.set('limit', String(params.limit));
39
+ if (params.offset !== undefined)
40
+ q.set('offset', String(params.offset));
41
+ if (params.status !== undefined)
42
+ q.set('status', params.status);
43
+ if (params.subscriber !== undefined)
44
+ q.set('subscriber', params.subscriber);
45
+ const qs = q.toString();
46
+ return this.http.get('/v1/payment/orders' + (qs ? '?' + qs : ''));
47
+ }
48
+ /** 核验单笔结算(按幂等键 reference)。 */
49
+ settlement(reference) {
50
+ return this.http.get('/v1/payment/settlement/' + encodeURIComponent(reference));
51
+ }
52
+ /** 访问判定/按次扣费(返回 active,或 402 抛 KeelApiError)。 */
53
+ access(subscriber, resource) {
54
+ return this.http.post('/v1/payment/access', { subscriber, resource });
55
+ }
56
+ /** 按量套餐价目(公开;canonical `/v1/payment/plans`,由 mpc 承载)。 */
57
+ plans() {
58
+ return this.http.get('/v1/payment/plans');
59
+ }
60
+ /** 预付费账本余额(canonical `/v1/payment/ledger-balance`,由 mpc 承载)。 */
61
+ ledgerBalance(params) {
62
+ return this.http.post('/v1/payment/ledger-balance', params);
63
+ }
64
+ // ── 旧 index MCP 能力补齐:通道发现 / 链上意图 / 批量 / 邀请 / 转账 ──
65
+ /** 支付通道发现:启用的 rail、各链收款钱包 / 价格 / 托管、各链可接受的稳定币。 */
66
+ info(chain) {
67
+ return this.http.get('/v1/payment/info' + (chain ? '?chain=' + encodeURIComponent(chain) : ''));
68
+ }
69
+ /** 读取链上订阅详情(active / payToken / amountPaid / 周期 / 试用)。 */
70
+ subscriptionDetail(chain, subscriber, resourceId) {
71
+ return this.http.get('/v1/payment/subscription-detail/' + encodeURIComponent(chain) + '/' + encodeURIComponent(subscriber) + '/' + encodeURIComponent(String(resourceId)));
72
+ }
73
+ /** 读取链上套餐定价(price / period / active)。 */
74
+ price(planId, chain) {
75
+ return this.http.get('/v1/payment/price?chain=' + encodeURIComponent(chain || 'bsc') + '&planId=' + encodeURIComponent(String(planId)));
76
+ }
77
+ /**
78
+ * 创建 a2a 支付意图(phase 1)。
79
+ * `settlementMode: 'direct'` 时 `payee` 必填,settle 只核验 payer → payee 的链上转账。
80
+ */
81
+ a2aCreate(params) {
82
+ return this.http.post('/v1/payment/a2a', params);
83
+ }
84
+ /** 确认 a2a 支付意图(phase 2):mode=tx 校验链上交易;mode=balance 走账本扣减。 */
85
+ a2aSettle(params) {
86
+ return this.http.post('/v1/payment/a2a/settle', params);
87
+ }
88
+ /** 结算 direct 意图(phase 2):按 intent 绑定的 payee/asset/精确金额核验,并按链确认数判定终局。 */
89
+ a2aDirectSettle(paymentId, txHash) {
90
+ return this.http.post('/v1/payment/a2a/' + encodeURIComponent(paymentId) + '/settle', { txHash });
91
+ }
92
+ /** 取消未结算的 direct 意图(幂等)。 */
93
+ a2aCancel(paymentId) {
94
+ return this.http.post('/v1/payment/a2a/' + encodeURIComponent(paymentId) + '/cancel', {});
95
+ }
96
+ /** 创建批量收款意图(一付款人对多收款人)。 */
97
+ batchCreate(params) {
98
+ return this.http.post('/v1/payment/batch', params);
99
+ }
100
+ /** 结算批量收款中的一项(提交链上 txHash)。 */
101
+ batchSettle(params) {
102
+ return this.http.post('/v1/payment/batch/settle', params);
103
+ }
104
+ /** 读取批量收款状态。 */
105
+ batchGet(batchId) {
106
+ return this.http.get('/v1/payment/batch?batchId=' + encodeURIComponent(batchId));
107
+ }
108
+ /** 取消批量收款。 */
109
+ batchCancel(batchId) {
110
+ return this.http.post('/v1/payment/batch/cancel', { batchId });
111
+ }
112
+ /** 创建账单邀请(payer → payee)。 */
113
+ inviteCreate(params) {
114
+ return this.http.post('/v1/payment/invites', params);
115
+ }
116
+ /** 按地址 + 角色(payer|payee)列出邀请。 */
117
+ inviteList(p) {
118
+ const q = new URLSearchParams({ address: p.address, role: p.role });
119
+ if (p.status)
120
+ q.set('status', p.status);
121
+ return this.http.get('/v1/payment/invites?' + q.toString());
122
+ }
123
+ /** 读取单个邀请详情。 */
124
+ inviteGet(inviteId) {
125
+ return this.http.get('/v1/payment/invites/' + encodeURIComponent(inviteId));
126
+ }
127
+ /** 取消未结算的邀请。 */
128
+ inviteCancel(inviteId) {
129
+ return this.http.post('/v1/payment/invites/' + encodeURIComponent(inviteId) + '/cancel');
130
+ }
131
+ /** 链上结算邀请(提交 payer txHash)。 */
132
+ inviteSettle(p) {
133
+ return this.http.post('/v1/payment/invites/' + encodeURIComponent(p.inviteId) + '/settle', { txHash: p.txHash, chain: p.chain });
134
+ }
135
+ /** 用 payer 账本余额支付邀请(无链上交易)。 */
136
+ invitePay(inviteId) {
137
+ return this.http.post('/v1/payment/invites/' + encodeURIComponent(inviteId) + '/pay');
138
+ }
139
+ /** 请求一笔账本内转账(from → to)。 */
140
+ transferCreate(params) {
141
+ return this.http.post('/v1/payment/transfers', params);
142
+ }
143
+ /** 按地址 + 角色(from|to)列出转账。 */
144
+ transferList(p) {
145
+ return this.http.get('/v1/payment/transfers?' + new URLSearchParams({ address: p.address, role: p.role }).toString());
146
+ }
147
+ /** 读取单笔转账详情。 */
148
+ transferGet(transferId) {
149
+ return this.http.get('/v1/payment/transfers/' + encodeURIComponent(transferId));
150
+ }
151
+ /** 确认并原子执行转账。 */
152
+ transferConfirm(transferId) {
153
+ return this.http.post('/v1/payment/transfers/' + encodeURIComponent(transferId) + '/confirm');
154
+ }
155
+ /** 取消未执行的转账。 */
156
+ transferCancel(transferId) {
157
+ return this.http.post('/v1/payment/transfers/' + encodeURIComponent(transferId) + '/cancel');
158
+ }
159
+ }
160
+ exports.PaymentAPI = PaymentAPI;
@@ -0,0 +1,30 @@
1
+ /** 对外四模块。 */
2
+ export type KeelModule = 'payment' | 'wallet' | 'data' | 'account';
3
+ /** 钱包签名鉴权(account/wallet 自助面需要;服务端按 EIP-191 校验)。 */
4
+ export interface KeelWalletAuth {
5
+ /** 钱包地址(0x...) */
6
+ address: string;
7
+ /** EIP-191 签名函数:入参为待签消息,返回签名 hex(可异步)。 */
8
+ sign: (message: string) => Promise<string> | string;
9
+ }
10
+ /** SDK 配置。 */
11
+ export interface KeelConfig {
12
+ /** baseUrl,默认 https://keel.codes */
13
+ baseUrl?: string;
14
+ /** Bearer API key(按模块订阅签发,`sk_<module>_*`) */
15
+ apiKey?: string;
16
+ /** 内部服务鉴权 key(`X-Service-Key`);供网关服务级旁路放行可信内部组件(如 keel-mcp)。 */
17
+ serviceKey?: string;
18
+ /** 请求超时(ms),默认 30000 */
19
+ timeout?: number;
20
+ /** 钱包签名鉴权;设置后自动附加 x-wallet-address/signature/timestamp 请求头。 */
21
+ wallet?: KeelWalletAuth;
22
+ }
23
+ /** 统一错误体(契约 §3)。 */
24
+ export interface KeelErrorBody {
25
+ code: string;
26
+ message: string;
27
+ details?: Record<string, unknown>;
28
+ }
29
+ /** 幂等键:写操作由调用方生成,相同 reference 不重复入账/扣费。 */
30
+ export type Reference = string;
package/dist/types.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ // ---------------------------------------------------------------------------
3
+ // @keelcodes/sdk — 公共类型(对齐 KEEL_CONTRACT_V1.md)
4
+ // ---------------------------------------------------------------------------
5
+ Object.defineProperty(exports, "__esModule", { value: true });