i wanna to craete wallet for user in nodejs and substrate . i using these codes for connect to substrate node and create a wallet :
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');
const { cryptoWaitReady, mnemonicGenerate, mnemonicToMiniSecret, schnorrkelKeypairFromSeed } = require('@polkadot/util-crypto');
const { TypeRegistry } = require('@polkadot/types'); // Add this import
class PolkadotWrapper {
constructor(endpoint) {
this.endpoint = endpoint;
this.api = null;
}
async connect() {
const wsProvider = new WsProvider(this.endpoint);
const registry = new TypeRegistry();
this.api = await ApiPromise.create({
provider: wsProvider,
registry,
types: {},
});
await this.api.isReady;
}
createAccount() {
const mnemonic = mnemonicGenerate();
const keyring = new Keyring({ type: 'sr25519' });
const newAccount = keyring.createFromUri(mnemonic);
return {
instance: newAccount,
mnemonic,
address: newAccount.address,
publicKey: newAccount.publicKey.toString('hex'),
};
}
and it create a wallet for user but when i need to call this function whith that created account with `createAccount` function :
const newAccount = wrapper.createAccount();
console.log('New account:', newAccount.instance);
transafer function :
async transfer(fromAccount, toAddress, amount) {
if (!this.api) throw new Error('API not connected');
// Prepare the extrinsic
const extrinsic = this.api.tx.balances.transfer(toAddress, amount);
// Sign and send the extrinsic
const { nonce } = await this.api.query.system.account(fromAccount.address);
const signedExtrinsic = extrinsic.sign(fromAccount, { nonce });
// Submit the transaction and return the result
const result = await new Promise((resolve, reject) => {
signedExtrinsic.send(({ status, events }) => {
if (status.isInBlock || status.isFinalized) {
events.forEach(({ event }) => {
if (event.section === 'system' && event.method === 'ExtrinsicFailed') {
reject(new Error('Extrinsic failed'));
} else if (event.section === 'system' && event.method === 'ExtrinsicSuccess') {
resolve({ blockHash: status.asInBlock.toHex() });
}
});
}
}).catch((error) => {
reject(error);
});
});
return result;
}
it show me this error :
Transaction failed: Cannot read properties of undefined (reading 'specVersion')
what the problem ?