Skip to content

Support for renaming JS methods when re-exporting#1010

Merged
palas merged 5 commits intomasterfrom
support-for-renaming-js-functions
Mar 13, 2026
Merged

Support for renaming JS methods when re-exporting#1010
palas merged 5 commits intomasterfrom
support-for-renaming-js-functions

Conversation

@palas
Copy link
Copy Markdown
Contributor

@palas palas commented Nov 13, 2025

Changelog

- description: |
    Add support for renaming JS methods re-exporting and use the functionality to simplify wallet testnet methods
  type:
  - breaking
  projects:
  - cardano-wasm

Context

See #1003 (comment). Names of functions exported by the API need to be unique, but we can rename them as we re-export them for the JS API function.

How to trust this PR

The tests passing gives strong guarantee the renaming is done correctly. Other than that, it could be that there is some file that wasn't updated that we missed. And also it would be a matter of checking whether the comments and code design is sensible.

I would look at the commit separately too, because the first one is a refactoring, and the second one is a simple change.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

Copy link
Copy Markdown
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

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

In the methodParams field is it possible to encode the idea of sum type? I.e require a network magic number or default to mainnet?

@palas
Copy link
Copy Markdown
Contributor Author

palas commented Nov 14, 2025

In the methodParams field is it possible to encode the idea of sum type? I.e require a network magic number or default to mainnet?

@Jimbo4350, the easiest we could do is have a nullable magic number parameter (if it is the last parameter I think it could also be optional), and if you don't provide it we can default to mainnet. Still we can want the renaming functionality. Because that way we can have methods with the same name in different objects, like toCbor, otherwise we'll need txToCbor and certToCbor, because they are in the same namespace. This will still be the case at the WASM level, but we can structure them better for JS

@palas palas requested a review from Jimbo4350 November 14, 2025 17:31
Comment thread cardano-wasm/src-lib/Cardano/Wasm/Api/Info.hs Outdated
Comment thread cardano-wasm/src-lib/Cardano/Wasm/Api/Info.hs Outdated
@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions Bot added the Stale label Jan 13, 2026
@palas palas removed the Stale label Jan 15, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 3, 2026

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions Bot added the Stale label Mar 3, 2026
Copilot AI review requested due to automatic review settings March 6, 2026 18:25
@palas palas force-pushed the support-for-renaming-js-functions branch from 30ff6c5 to 3e32d61 Compare March 6, 2026 18:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces support for renaming methods when re-exporting them into the JS API surface, enabling globally-unique exported function names while exposing shorter/more ergonomic names in the JS object hierarchy (notably simplifying wallet testnet method names).

Changes:

  • Extend MethodInfo with a methodSimpleName and include it in the API info JSON payload.
  • Update the JS wrapper and TypeScript declaration generation to use simpleName for JS-facing property names while still calling WASM exports by the globally-unique name.
  • Update JS tests and examples to use the simplified wallet testnet method names.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cardano-wasm/src-lib/Cardano/Wasm/Api/InfoToTypeScript.hs Generate TS interface method names from methodSimpleName (JS-facing name).
cardano-wasm/src-lib/Cardano/Wasm/Api/Info.hs Add methodSimpleName to method metadata and emit it in JSON; apply renames for testnet wallet methods.
cardano-wasm/npm-wrapper/api.test.js Update test to call renamed testnet wallet restore method.
cardano-wasm/lib-wrapper/main.js Use method.simpleName as the JS property name while still calling exports via method.name.
cardano-wasm/lib-wrapper/cardano-api.d.ts Update declaration file to reflect renamed testnet wallet methods.
cardano-wasm/examples/simple-wallet/example.js Update example usage to call the renamed testnet wallet methods (and correct wallet method location).
cardano-wasm/examples/basic/example.js Update example usage to call the renamed testnet wallet restore method.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +126 to +131
-- | Information about a single method of a virtual object.
data MethodInfo = MethodInfo
{ methodName :: String
-- ^ Name of the method in the virtual object of the JS API (which should match the exported function).
-- ^ Name of the global method name in the API (which should match the exported function and it must be unique globally).
, methodSimpleName :: String
-- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than methodName and does not need to be unique unlike methodName).
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

After adding the new required methodSimpleName field to MethodInfo, at least one MethodInfo { ... } construction in apiInfo wasn't updated: appendCertificateToTx (around Info.hs:319) is missing methodSimpleName, which will cause a compile error (missing field in record construction). Please add it there (likely equal to methodName).

Copilot uses AI. Check for mistakes.
-- ^ Name of the method in the virtual object of the JS API (which should match the exported function).
-- ^ Name of the global method name in the API (which should match the exported function and it must be unique globally).
, methodSimpleName :: String
-- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than methodName and does not need to be unique unlike methodName).
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The doc comment for methodSimpleName says it “does not need to be unique unlike methodName”. While it may not need to be globally unique, it still must be unique within its containing JS object/group; otherwise later assignments in lib-wrapper/main.js will overwrite earlier methods on the target object. Please clarify the comment to avoid implying that collisions are safe.

Suggested change
-- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than methodName and does not need to be unique unlike methodName).
-- ^ Name of the method in the virtual object of the API when accessed via JS (used for re-exporting to JS, may be shorter than 'methodName'; it does not need to be globally unique, but must be unique within its containing JS object/group to avoid overwriting other methods).

Copilot uses AI. Check for mistakes.
@palas palas requested a review from a team as a code owner March 6, 2026 20:20
@github-actions github-actions Bot removed the Stale label Mar 7, 2026
@palas palas added this pull request to the merge queue Mar 13, 2026
Merged via the queue into master with commit a47138b Mar 13, 2026
32 checks passed
@palas palas deleted the support-for-renaming-js-functions branch March 13, 2026 02:51
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