Skip to content

feat: add fiber-sdk#194

Open
ashuralyk wants to merge 46 commits intockb-devrel:masterfrom
ashuralyk:feat/fiber-sdk
Open

feat: add fiber-sdk#194
ashuralyk wants to merge 46 commits intockb-devrel:masterfrom
ashuralyk:feat/fiber-sdk

Conversation

@ashuralyk
Copy link
Copy Markdown
Contributor

@ashuralyk ashuralyk commented Apr 16, 2025

Background

Taking advantage of fiber-js to implement a basic usable fiber-sdk in ccc, which contains only features of channel, invoice and payment.

Inside test cases, there're two native fiber nodes started by fiber-js with interconnection, all the cases would interact with them to validate channel, invoice and payment functionalities, without needs of any outside fiber nodes.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 16, 2025

🦋 Changeset detected

Latest commit: 4415268

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@ckb-ccc/fiber Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ashuralyk ashuralyk marked this pull request as ready for review April 19, 2025 00:52
@Hanssen0 Hanssen0 force-pushed the master branch 2 times, most recently from 4f1083c to 1d5d04d Compare June 7, 2025 20:12
ashuralyk and others added 23 commits February 4, 2026 10:03
- 修改 Script 接口中 args 的类型从 string[] 改为 string
- 修改 shutdownChannel 函数中的 close_script.args 参数格式
- 确保所有参数都符合 API 期望的十六进制字符串格式
- 修改 Script 接口中 args 的类型从 string[] 改为 string
- 修改 shutdownChannel 函数中的 close_script.args 参数格式
- 确保所有参数都符合 API 期望的十六进制字符串格式
- 更新相关依赖和工具函数

这个修改解决了通道关闭时出现的 'invalid type: sequence, expected a 0x-prefixed hex string' 错误。
@netlify
Copy link
Copy Markdown

netlify Bot commented Feb 23, 2026

Deploy Preview for appccc ready!

Name Link
🔨 Latest commit 4415268
🔍 Latest deploy log https://app.netlify.com/projects/appccc/deploys/69ea262d157ab90008aeb506
😎 Deploy Preview https://deploy-preview-194--appccc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 64 (🔴 down 22 from production)
Accessibility: 89 (🟢 up 1 from production)
Best Practices: 92 (🔴 down 8 from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link
Copy Markdown

netlify Bot commented Feb 23, 2026

Deploy Preview for apiccc ready!

Name Link
🔨 Latest commit 4415268
🔍 Latest deploy log https://app.netlify.com/projects/apiccc/deploys/69ea262d476d22000817b79e
😎 Deploy Preview https://deploy-preview-194--apiccc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 72 (🔴 down 10 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 94 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@ashuralyk
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new package, @ckb-ccc/fiber, which provides a comprehensive SDK for interacting with the Fiber payment channel network. The implementation is robust, featuring a well-designed API, thorough type definitions, and an impressive testing setup that includes a Node.js runtime for the browser-based fiber-js library. The code quality is high, and the documentation is clear. I've included a couple of suggestions to improve code clarity and maintainability.

Comment thread packages/fiber/README.md Outdated
Comment on lines +22 to +41
export class OpenChannelParams {
constructor(
public readonly peerId: string,
public readonly fundingAmount: ccc.Hex,
private readonly _public?: boolean,
public readonly fundingUdtTypeScript?: ccc.Script,
public readonly shutdownScript?: ccc.Script,
public readonly commitmentDelayEpoch?: ccc.Hex,
public readonly commitmentFeeRate?: ccc.Hex,
public readonly fundingFeeRate?: ccc.Hex,
public readonly tlcExpiryDelta?: ccc.Hex,
public readonly tlcMinValue?: ccc.Hex,
public readonly tlcFeeProportionalMillionths?: ccc.Hex,
public readonly maxTlcValueInFlight?: ccc.Hex,
public readonly maxTlcNumberInFlight?: ccc.Hex,
) {}

get public(): boolean | undefined {
return this._public;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The use of a private _public property with a getter to handle the public keyword is a bit complex. Since public can be used as an object property name without issue, you can simplify this class by using public directly as a constructor parameter. This will make the code more straightforward and easier to maintain.

Suggested change
export class OpenChannelParams {
constructor(
public readonly peerId: string,
public readonly fundingAmount: ccc.Hex,
private readonly _public?: boolean,
public readonly fundingUdtTypeScript?: ccc.Script,
public readonly shutdownScript?: ccc.Script,
public readonly commitmentDelayEpoch?: ccc.Hex,
public readonly commitmentFeeRate?: ccc.Hex,
public readonly fundingFeeRate?: ccc.Hex,
public readonly tlcExpiryDelta?: ccc.Hex,
public readonly tlcMinValue?: ccc.Hex,
public readonly tlcFeeProportionalMillionths?: ccc.Hex,
public readonly maxTlcValueInFlight?: ccc.Hex,
public readonly maxTlcNumberInFlight?: ccc.Hex,
) {}
get public(): boolean | undefined {
return this._public;
}
export class OpenChannelParams {
constructor(
public readonly peerId: string,
public readonly fundingAmount: ccc.Hex,
public readonly public?: boolean,
public readonly fundingUdtTypeScript?: ccc.Script,
public readonly shutdownScript?: ccc.Script,
public readonly commitmentDelayEpoch?: ccc.Hex,
public readonly commitmentFeeRate?: ccc.Hex,
public readonly fundingFeeRate?: ccc.Hex,
public readonly tlcExpiryDelta?: ccc.Hex,
public readonly tlcMinValue?: ccc.Hex,
public readonly tlcFeeProportionalMillionths?: ccc.Hex,
public readonly maxTlcValueInFlight?: ccc.Hex,
public readonly maxTlcNumberInFlight?: ccc.Hex,
) {}

Copy link
Copy Markdown
Contributor Author

@ashuralyk ashuralyk Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use public as member in a class is prohibited in TypeScript

@Hanssen0
Copy link
Copy Markdown
Member

/canary

@github-actions
Copy link
Copy Markdown
Contributor

❌ Canary version deployment failed. View workflow run

@Hanssen0
Copy link
Copy Markdown
Member

/canary

@github-actions
Copy link
Copy Markdown
Contributor

❌ Canary version deployment failed. View workflow run

@ashuralyk
Copy link
Copy Markdown
Contributor Author

/canary

1 similar comment
@Hanssen0
Copy link
Copy Markdown
Member

/canary

@github-actions
Copy link
Copy Markdown
Contributor

🚀 Canary version published successfully! View workflow run

The following packages have been published to npm:

  • @ckb-ccc/ccc@0.0.0-canary-20260330023358
  • ckb-ccc@0.0.0-canary-20260330023358
  • @ckb-ccc/connector@0.0.0-canary-20260330023358
  • @ckb-ccc/connector-react@0.0.0-canary-20260330023358
  • @ckb-ccc/core@0.0.0-canary-20260330023358
  • @ckb-ccc/eip6963@0.0.0-canary-20260330023358
  • @ckb-ccc/fiber@0.0.0-canary-20260330023358
  • @ckb-ccc/joy-id@0.0.0-canary-20260330023358
  • @ckb-ccc/lumos-patches@0.0.0-canary-20260330023358
  • @ckb-ccc/nip07@0.0.0-canary-20260330023358
  • @ckb-ccc/okx@0.0.0-canary-20260330023358
  • @ckb-ccc/rei@0.0.0-canary-20260330023358
  • @ckb-ccc/shell@0.0.0-canary-20260330023358
  • @ckb-ccc/spore@0.0.0-canary-20260330023358
  • @ckb-ccc/ssri@0.0.0-canary-20260330023358
  • @ckb-ccc/udt@0.0.0-canary-20260330023358
  • @ckb-ccc/uni-sat@0.0.0-canary-20260330023358
  • @ckb-ccc/utxo-global@0.0.0-canary-20260330023358
  • @ckb-ccc/xverse@0.0.0-canary-20260330023358

@gpBlockchain
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Member

@Hanssen0 Hanssen0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM, and I really like the idea of separating APIs into different classes to improve maintainability. However, the manual composition of API classes into an SDK class is annoying to me.

I suggest using the mixins pattern to eliminate design complexity. Such a design pattern is also utilised by CCC's molecule codec part to reduce coding tasks for devs.

@ashuralyk
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the @ckb-ccc/fiber package, a comprehensive TypeScript SDK for the Fiber payment channel network. The implementation features a high-level FiberSDK built with mixins for channel, invoice, payment, and peer management, supported by a low-level JSON-RPC client that handles automatic camelCase to snake_case conversion and hex serialization. Additionally, the PR includes a specialized testing runtime that polyfills browser-only features like Web Workers and IndexedDB to enable integration testing in Node.js. The review feedback identifies critical issues in the serialization logic, specifically regarding the handling of class instances and Uint8Array types, as well as a bug where private fields with getters are lost during object spreading when preparing RPC payloads.

Comment thread packages/fiber/src/utils.ts Outdated
Comment thread packages/fiber/src/rpc.ts
Comment thread packages/fiber/src/types/channel.ts Outdated
Comment thread packages/fiber/src/types/channel.ts Outdated
Comment thread packages/fiber/src/types/invoice.ts Outdated
Comment thread packages/fiber/src/types/channel.ts Outdated
@ashuralyk
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the @ckb-ccc/fiber package, providing a TypeScript SDK for the Fiber payment channel network. The implementation includes a modular FiberSDK using mixins for various domains, a JSON-RPC client with automatic case conversion, and a specialized testing runtime for Node.js. Review feedback identifies several technical issues, including serialization bugs caused by using private fields with getters in classes intended for spreading, incorrect nesting of custom record objects in RPC payloads, and missing file extensions required for ESM compatibility. Additionally, improvements were suggested for safer object property checks and preventing the mangling of user-defined keys during recursive case conversion.

Comment thread packages/fiber/src/types/channel.ts Outdated
Comment thread packages/fiber/src/types/channel.ts Outdated
Comment thread packages/fiber/src/types/payment.ts Outdated
Comment thread packages/fiber/src/types/channel.ts Outdated
Comment thread packages/fiber/src/api/invoice.ts Outdated
Comment on lines +14 to +31
function convertKeys(value: unknown, keyFn: (key: string) => string): unknown {
if (value === null || value === undefined) {
return value;
}
if (Array.isArray(value)) {
return value.map((item) => convertKeys(item, keyFn));
}
if (typeof value === "object" && !(value instanceof Uint8Array)) {
const obj = value as Record<string, unknown>;
const out: Record<string, unknown> = {};
for (const key of Object.keys(obj)) {
const newKey = keyFn(key);
out[newKey] = convertKeys(obj[key], keyFn);
}
return out;
}
return value;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The convertKeys function recursively transforms all object keys between camelCase and snake_case. This can mangle user-defined keys in data maps like customRecords. Consider adding a mechanism to skip key conversion for specific fields or objects intended to be used as opaque maps.

@ashuralyk ashuralyk requested a review from Hanssen0 April 23, 2026 00:53
@ashuralyk
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the @ckb-ccc/fiber package, providing a TypeScript/JavaScript SDK for the Fiber payment channel network. The implementation includes a high-level FiberSDK with domain-specific mixins for channels, invoices, and payments, supported by a custom JSON-RPC client that handles automatic camelCase to snake_case conversion and hex serialization. Feedback identifies a bug in the toHex utility where zero values are incorrectly treated as falsy, missing .js extensions in ESM relative imports, and a missing null check in payment parameter validation. Additionally, it is recommended to update the Attribute type definitions to camelCase to align with the SDK's runtime transformation logic.

Comment thread packages/fiber/src/utils.ts Outdated
Comment thread packages/fiber/src/types/invoice.ts Outdated
Comment thread packages/fiber/src/types/payment.ts Outdated
Comment thread packages/fiber/src/api/payment.ts Outdated
Comment thread packages/fiber/src/types/invoice.ts Outdated
ashuralyk and others added 2 commits April 23, 2026 21:08
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@ashuralyk
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the @ckb-ccc/fiber package, providing a TypeScript SDK for interacting with Fiber nodes. It includes API mixins for channels, invoices, payments, node info, and peers, along with necessary RPC serialization and type definitions. I have suggested an improvement to the openChannelWithExternalFunding method to ensure the returned unsignedFundingTx is automatically converted into a ccc.Transaction instance, enhancing consistency and developer experience.

Comment on lines +61 to +70
async openChannelWithExternalFunding(
params: fiber.OpenChannelWithExternalFundingParamsLike,
): Promise<fiber.OpenChannelWithExternalFundingResult> {
const normalized =
fiber.OpenChannelWithExternalFundingParams.from(params);
return this.rpc.call<fiber.OpenChannelWithExternalFundingResult>(
"open_channel_with_external_funding",
[{ ...normalized }],
);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The unsignedFundingTx returned by the RPC is a plain object after snakeToCamel conversion. To provide a better developer experience and maintain consistency with the CCC ecosystem, this object should be converted into a ccc.Transaction instance. This allows callers to directly use methods like .hash() on the returned transaction. This implementation also follows the rule of accepting generic 'Like' types for parameters and performing internal normalization.

    async openChannelWithExternalFunding(
      params: fiber.OpenChannelWithExternalFundingParamsLike,
    ): Promise<fiber.OpenChannelWithExternalFundingResult> {
      const normalized =
        fiber.OpenChannelWithExternalFundingParams.from(params);
      const res = await this.rpc.call<fiber.OpenChannelWithExternalFundingResult>(
        "open_channel_with_external_funding",
        [{ ...normalized }],
      );
      return {
        ...res,
        unsignedFundingTx: ccc.Transaction.from(res.unsignedFundingTx),
      };
    }
References
  1. Functions should accept more generic types like HexLike and perform necessary type conversions internally, rather than requiring the caller to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants