I am building an NPM SDK in which I need to generate a Fuel account using Wallet.generate()
method which comes from fuels SDK.
When I try to generate the account using the same method in a React app, it works fine and account is successfully generated. But when I am trying to do the same through my NPM SDK which I am consuming in a React app, it throws me the following error -
error: TypeError: import_crypto2.default.randomBytes is not a function
at randomBytes (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:5432:67)
at Signer.generatePrivateKey (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:26578:190)
at Wallet.generate (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:29361:35)
at generateNewWallet (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:33013:44)
at http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:33143:62 at Generator.next (<anonymous>)
at http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:32595:65 at new Promise (<anonymous>)
at __async (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:32579:14)
at initializeLocalStorage (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:33124:44)
message: "import_crypto2.default.randomBytes is not a function"
stack: "TypeError: import_crypto2.default.randomBytes is not a function
at randomBytes (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:5432:67)
at Signer.generatePrivateKey (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:26578:190)
at Wallet.generate (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:29361:35)
at generateNewWallet (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:33013:44)
at http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:33143:62
at Generator.next (<anonymous>)
at http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:32595:65
at new Promise (<anonymous>)
at __async (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:32579:14)
at initializeLocalStorage (http://localhost:3000/node_modules/.vite/deps/@lyncworld_fuel-tap-sdk.js?v=bfe760eb:33124:44)"
I think this is because Wallet.generate()
method uses crypto
module to generate random bytes when generating an account and since crypto module works differently in browser environment and node environment, It is giving the error - that randomBytes is not a function
.
How can I generate a new wallet using my NPM SDK?
I am using version 0.94.6
of fuels NPM package.
Here is the code snippets that I am using to generate the account -
import { Provider, Wallet, WalletUnlocked } from "fuels";
export const generateNewWallet = (provider: Provider) => {
if (!provider) throw new Error("Provider is required for generating wallet.");
const account: WalletUnlocked = Wallet.generate({ provider });
const publicKey = account.address.toB256();
const privateKey = account.privateKey;
return {
account,
publicKey,
privateKey,
} as const;
};
Here is how I am creating the provider -
import { Provider } from "fuels";
const FUEL_RPC_URL_TESTNET = "https://testnet.fuel.network/v1/graphql";
const provider = await Provider.create(FUEL_RPC_URL_TESTNET);