Remove the stellar-sdk npm aliases and deduplicate stellar-sdk copies

Open
#2,973 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Quiet
Tech stack
typescript, webpack

Research direction

Start by comparing the dependency entries in extension/package.json, @shared/constants/package.json, @shared/api/package.json, and @shared/helpers/package.json, then inspect getSdk() in @shared/helpers/stellar.ts and the lavamoat path at extension/package.json:125-126. Use webpack-bundle-analyzer to measure the bundle and run yarn install, yarn tsc, yarn lint, yarn test, yarn build, and the e2e suite. Done means fewer SDK copies with key-manager, swap, signing, and account workflows still passing.

Written by the indexing model from the issue text.

Description

Summary

The dependency tree contains four copies of @stellar/stellar-sdk. Two of them exist
because we install the same package twice under npm aliases. The other two come from
third-party packages that pin old SDK versions. All four are reachable from the built
extension.

This costs bundle size, install size, and supply-chain surface. This issue proposes
removing the aliases and reducing the number of copies.

Current state

Package name in node_modules Version Comes from Size on disk
stellar-sdk (alias) 16.0.0-rc.1 our code, ~100 files 33M
stellar-sdk-next (alias) 16.0.0-rc.1 our code, 5 files 33M
@stellar/stellar-sdk 15.0.1 @stellar/[email protected] (background / key manager) 17M
@stellar/stellar-sdk (nested) 11.3.0 [email protected] (popup swap) 3.8M

The aliases are declared in four workspaces:
extension/package.json:87, @shared/constants/package.json:6,
@shared/api/package.json:12, @shared/helpers/package.json:7.

Note that Yarn already treats the two aliases as one package at resolution time:

yarn.lock:22301
"stellar-sdk-next@npm:@stellar/[email protected], stellar-sdk@npm:@stellar/[email protected]":

One locator, two descriptors. The duplication happens only at link time, because the
two package names are different, so they are installed into two directories. Webpack
then builds two separate module graphs from them. Yarn cannot merge this; only removing
the alias can.

Why the alias exists today

stellar-sdk-next was added to stage a protocol upgrade. The idea was to point -next
at a release candidate while stellar-sdk stayed on the stable release, then select one
per network at runtime with getSdk(networkPassphrase) (@shared/helpers/stellar.ts:22).

That is not what happens now:

  • Both aliases are pinned to the same version. We always bump them together.
  • isNextSdk is [""].includes(networkPassphrase), which is always false. The -next
    branch is dead code.

So the second copy currently gives us nothing.

Proposed change

  1. Replace "stellar-sdk": "npm:@stellar/stellar-sdk@<version>" with
    "@stellar/stellar-sdk": "<version>" in the four workspace package.json files.
  2. Update the imports. About 100 files import stellar-sdk. This is a mechanical
    find-and-replace.
  3. Remove stellar-sdk-next and the 5 imports that use it. Keep getSdk() as a
    one-line function that always returns the single SDK, so the call sites do not
    change in this PR.
  4. Reduce the third-party copies:
    • @stellar/[email protected] pins SDK 15.0.1. Check for a newer
      release built against SDK 16. If there is none, open an upstream request to make
      @stellar/stellar-sdk a peer dependency with a range, not an exact pin.
    • [email protected] pins ^11.3.0. Check whether a newer release exists.
    • A Yarn resolutions entry can force a single version, but this touches the key
      manager, which is on the signing path. Do not do this without testing account
      import, account recovery, and hardware wallet signing.
  5. Measure the bundle before and after with webpack-bundle-analyzer, and record the
    numbers in the PR.

Trade-off to decide

Removing stellar-sdk-next also removes the mechanism we would use for the next
protocol upgrade. Two options:

  • (a) Remove it now, add it back only during a transition. Add the alias when we
    need to run an RC beside the stable SDK, then delete it after activation.
  • (b) Keep it. Accept a permanent duplicate copy for a mechanism that is used for a
    few weeks per protocol release.

Recommendation: (a). The current setup pays the cost all the time.

Verification

  • yarn install and confirm node_modules has fewer @stellar/stellar-sdk copies.
  • yarn tsc, yarn lint, yarn test all pass.
  • yarn build (webpack) succeeds, and lavamoat / allowScripts still resolves the
    stellar-sdk>stellar-base>sodium-native path, which is keyed by package name
    (extension/package.json:125-126). This path changes when the alias is removed.
  • Manual check of the key manager: create account, import account, recover account.
  • Manual check of a swap, which uses soroswap-router-sdk.
  • e2e suite passes.

Out of scope

Runtime behaviour changes. This is a packaging change only.

Dominant language
TypeScript
Stars
122
Forks
62
Avg merge
4d 19h
Merged PRs (30d)
41

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stellar/freighter

All issues in stellar/freighter

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.