taquito: sodium.randombytes_buf is not a function
Description
When trying to use the wallet in the frontend with a bundler (tested: parcel, browserify) I can’t get the app to work at all. It seems like it’s an issue with the way __importStar is working in the ES5/CJS build from beacon-sdk module; it does not pull in the entire libsodium object, but a clone of it with less functions.
Steps To Reproduce
Paste this in a new main.js file:
import { TezosToolkit } from "@taquito/taquito";
import { BeaconWallet } from "@taquito/beacon-wallet";
const provider = "https://mainnet.smartpy.io";
const NETWORK = "mainnet";
sync();
async function sync() {
const wallet = new BeaconWallet({
name: "MyAwesomeDapp",
});
}
And create an index.html file:
<body><script src="./main.js"></script></body>
Now run from terminal:
npx parcel main.js --open
In console you will get an error:
TypeError: sodium.crypto_generichash is not a function
EDIT: This is fixed with parcel by adding the following in your app’s package.json (not sure if browserslist is really needed, but the rest resolves to ES Module imports instead of CJS outputs).
...
"browserslist": "last 2 Chrome versions",
"alias": {
"@airgap/beacon-sdk": "./node_modules/@airgap/beacon-sdk/dist/esm/index.js",
"@taquito/beacon-wallet": "./node_modules/@taquito/beacon-wallet/dist/taquito-beacon-wallet.es5.js",
"@taquito/signer": "./node_modules/@taquito/signer/dist/taquito-signer.es5.js",
"@taquito/taquito": "./node_modules/@taquito/taquito/dist/taquito.es5.js"
}
...
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 33 (17 by maintainers)
Hi, I had same issue. Solution in https://github.com/ecadlabs/taquito/issues/882#issuecomment-999753605 works well, but in case you would like to use stable version of beacon-sdk instead of beta, it can be done as follows:
vite-compatible-readable-streamas mentioned in https://github.com/ecadlabs/taquito/issues/882#issuecomment-999753605src/vendor/walletbeacon.min.jsvite.config.jsas follows:This fixed vite dev setup for me. Build works out of box for me so the resolve is applied only to for dev.
Vite solution
Setup
Also works with
@taquito/beacon-wallet@11&@taquito/taquito@11, but bundles:@taquito/taquito@9: 382.79 KiB@taquito/taquito@11: 560.05 KiB@taquito/beacon-walletis same.Steps
@airgap/beacon-sdk@2.4.0-beta.2, and for node polyfillsvite-compatible-readable-streamandbuffer.npm i @airgap/beacon-sdk@2.4.0-beta.2 vite-compatible-readable-stream buffervite.config.jsindex.htmlVoila!
Why da hell it it needs
cjson dev andesmon build… Seems like it’s Vite issue, gotta open one there.P.S. I advice to use dynamic imports for
@airgap/beacon-sdk&@taquitostuff to care about your bundle.Related https://github.com/airgap-it/beacon-sdk/issues/138#issuecomment-974860143 -
@airgap/beacon-sdk@2.4.0-beta.2https://github.com/vitejs/vite/issues/3817#issuecomment-864450199 - polyfills https://github.com/nodejs/readable-stream/issues/439#issuecomment-940643578 -vite-compatible-readable-streamhttps://github.com/originjs/vite-plugins/issues/9 -transformMixedEsModules: trueI had the same problem in a Remix codebase. Got it working by using
requireinstead ofimportand by polyfillingBufferin the client code:By using
require, the CJS build of beacon-sdk is used, instead of the ESM build.It looks like I made SvelteKit work! The
@airgap/beacon-sdk/package contains a minified version of the code, so I am using this one in thesvelte.config.jsfile:The dapp builds in dev mode (haven’t tested in prod for now) and I was able to send a transaction to a contract on Hangzhounet with this config. If anyone wants to give it a try and report any issue, that would be awesome!
same issue with vitejs / sveltekit here
Version 3 of the Beacon SDK broke all the workarounds that were found on my side 😬 Anybody noticed the same problem coming back after updating the version of the Beacon SDK/Taquito for Svelte/SvelteKit/ViteJs?
@souljorje yes it works ! lifesaver, thx
@clemsos check update, now it works 100%!
https://github.com/ecadlabs/taquito/issues/882#issuecomment-1075096459 worked for me. I updated it to beacon-sdk version 3.1.3. I am using VUE 3 and Vite 3.2.37.
If anyone has insight into what the beacon devs might look into, please comment here: https://github.com/airgap-it/beacon-sdk/issues/380
This made wallet work on build, but still can’t use it on dev Can you help me out on #1729
Hi Claude!
It does work indeed! I’m using SvelteKit (no webpack)
@souljorje Oops, my error, I didn’t see your const definition above. Thanks!
yes, works on dev but throws on build for me as well. >
Uncaught TypeError: Cannot read properties of undefined (reading 'TRANSPORT_P2P_PEERS_DAPP')@souljorje I don’t remember that being an issue. Here is a link to the code where I create the client and wallet. (warning: clojure ahead)
https://github.com/Trewaters/ACDao/blob/feat-init-app/src/tequito/core.cljs
It worked fine. Although, I didn’t get further than connecting a wallet.
Just a remark:
did not fix the problem for me (“@taquito/taquito”: “^9.2.0”, “@taquito/beacon-wallet”: “^9.2.0”).
This bug should be a failing test and a showstopper.