Skip to content

548 investigate the trac crypto api build#38

Open
lucasfernandes wants to merge 7 commits intomainfrom
548-investigate-the-trac-crypto-api-build
Open

548 investigate the trac crypto api build#38
lucasfernandes wants to merge 7 commits intomainfrom
548-investigate-the-trac-crypto-api-build

Conversation

@lucasfernandes
Copy link
Copy Markdown
Contributor

PR Description

This PR refactors the package structure and build system to ensure consistent behavior across Node, browser, and React Native.

What was done

• Introduced conditional exports in package.json for environment-specific resolution (Node, browser, RN)
• Unified all exports through a single entrypoint (index.js) to eliminate divergence
• Ensured compatibility for both default and named imports
• Fixed packaging to correctly include dist/ artifacts via "files"
• Validated integration across:
• Node (CJS + ESM)
• Browser (UMD + ESM via Vite)
• React Native
• Standardized local validation using npm pack and .tgz installation

Answers to open questions

Browser test entrypoint

• Confirmed browser tests use dist/trac-crypto-api.browser.js (UMD)
• Validated via manual HTML test that bundle loads correctly

→ No change required, this is the correct entrypoint


Unifying indexes

• Ensured all exports originate from index.js (single source of truth)
• Node, browser (via bundlers) and React Native now resolve from the same entrypoint
• Eliminated previous divergence between environments

→ Addressed


sodium-universal in React Native

• Tested directly in a React Native app using sign/verify flow
• Verified sodium-universal loads and operates via JS fallback (sodium-javascript)
• Cryptographic operations (keypair, sign, verify) executed successfully

→ Works correctly in current setup; no RN-specific implementation required


Conditional exports

• Added exports in package.json
• Validated resolution in Node, browser (Vite) and React Native

→ Fully addressed; environment-specific builds are resolved via conditional exports


Committing dist/

• Removed dist/ from repository
• Added dist/ to .gitignore
• Validated packaging still works via npm pack and .tgz install

→ Addressed; build artifacts are no longer versioned and can be generated in CI

Local testing

Confirmed working via:

npm pack
npm install ./trac-crypto-api-0.1.5.tgz

Summary of impact

•	Consistent exports across environments
•	Validated real-world usage (Node, RN, browser)
•	Improved packaging reliability
simulator_screenshot_1B3CD5B5-44E4-43EE-8051-1AC142B75D86 image

@lucasfernandes lucasfernandes self-assigned this Apr 10, 2026
@lucasfernandes lucasfernandes added the bug Something isn't working label Apr 10, 2026
"license": "ISC",
"dependencies": {
"trac-crypto-api": "file:../../"
"trac-crypto-api": "file:trac-crypto-api-0.1.5.tgz"
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.

Only one question, won't we need to remember this line with every release?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This dependency is auto-generated via npm pack during the browser test setup and is not meant to be manually maintained. It gets updated automatically with each release.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 12, 2026

Coverage

Metric Coverage Covered/Total
Lines 90.67% 1011/1115
Statements 90.67% 1011/1115
Functions 94.11% 48/51
Branches 79.89% 147/184

@leonardostsouza
Copy link
Copy Markdown
Collaborator

It would be a good idea to check if the README.md file in test/browser/manual is still up to date

index.js Outdated
module.exports.operation = api.operation;
module.exports.sign = api.sign;
module.exports.MAINNET_ID = api.MAINNET_ID;
module.exports.TESTNET_ID = api.TESTNET_ID;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can't we do it like this?

module.exports = {
...
}

index.js Outdated
};

// ===== NODE EXPORTS =====
api.data = data;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not put this directly in the object above?

const constants = require('./constants.js');
if (typeof document === "undefined") {
globalThis.document = { currentScript: { src: "" } };
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This will end up setting document and location in Node.js environments, which is not the intended behavior

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please, add a test to attest that Bare or Node do not contain browser/rn specific definitions. This may compromise environment detection downstream

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, I'll try to restrict this to browser only and also, add a test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is now restricted to browser environments only (isBrowser guard).
Also added a runtime test to ensure Node/Bare environments are not polluted with browser globals.

test("sodium is on window", () => {
expect(window.sodium).toBe(sodium);
// ⚠️ browser: sign pode não funcionar (sodium)
test("signature.sign: should throw in browser", async () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This contradicts the documentation and the html for manual tests in test/browser/ html

LEt's have a discussion about this. But, in any case:

  • Do not use emojis in commentaries
  • If we indeed keep this change, please update the manual test html for signature, because I am sure it expects a success in this case. We also need to update test/browser/manual/ README.md

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're correct. It was a little mistake that passed my attention. I'm fixing to reflect the documentation. Everything must work via browser injection as expected. Also removing annoying emogis planted.

} else if (window.crypto && window.crypto.getRandomValues) {
window.crypto.getRandomValues(buf);
}
return buf;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please, disable automatic code linter in your IDE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe I can leverage the moment to abosorb a linter configuration here like Barto said. Let's talk about it later.

try {
payload = api.transaction.build(txData, fromKeyPair.secretKey);
} catch (err) {
return;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This means that a throw in transaction.build will return early and this test will become green when an exception happens. Please, change this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're correct here again, same approach of signature. Browser tests must works as expected/documented. Fixing.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate the trac-crypto-api build

3 participants