0

I want to call smart contract method via sendTransaction from one of migrations. I'm using Truffle. During this migration I create a new wallet with a mnemonic.

const seed = bip39.mnemonicToSeed(mnemonic)
const hdk = hdkey.fromMasterSeed(seed)
const addrNode = hdk.derivePath("m/44'/60'/0'/0/0")
const walletAddr = wallet.getAddressString()
await someFactory.createProfile.sendTransaction(detailsHash, { from: walletAddr })

During the transaction I receive an exception

Returned error: sender account not recognized

How to send transaction with a newly created from a mnemonic profile?

Oleg Korban
  • 249
  • 3
  • 14
  • You creating a wallet out of scope of your truffle provider – Vitaly Migunov Feb 11 '20 at 00:29
  • That's what I think too! Also, I tried to make it like this: const provider = new HDWalletProvider(mnemonic, "http://127.0.0.1:7545", 0, 1, true, "m/44'/60'/0'/0/0") web3.setProvider(provider) And use accounts[0], but the result is the same. How to inject a wallet created with ethereumjs-wallet into the provider? Or is there any better way to do this? – Oleg Korban Feb 11 '20 at 07:10
  • What do you wanna do with this code? it you just wanna deploy your contract from one address and call with another, then you can just specify private keys instead of mnemonic If the addresses aren't derived from the same mnemonic. You can specify array of private keys in truffle-config using the same HDWalletProvider. – Vitaly Migunov Feb 11 '20 at 11:09
  • I want to generate a random wallet during migrations, then create a contract with this wallet(via factory using { from: wallet }). And restore it on the Android device later with Web3J. I tried to use HDWalletProvider with the same mnemonic, but no luck, same error. – Oleg Korban Feb 11 '20 at 12:01

1 Answers1

0

You can set your provider to your contract instance then

const HDWalletProvider = require("@truffle/hdwallet-provider");
const mnemonic = "Your mnemonic"; //

module.exports = function(deployer) {

  deployer.deploy(SomeFactory).then(someFactory => {
  provider = new HDWalletProvider(mnemonic, "YourURL", 0);

  someFactory.contract.setProvider(provider);

  someFactory.createProfile.sendTransaction(detailsHash, { from:provider.addresses[0] })
  });
}; 
Vitaly Migunov
  • 4,297
  • 2
  • 19
  • 23