0

I am writing a seed to add data on the smart contract after I deploy it. I'm using ropsten network, setted up like this in truffle-config:

ropsten: {
      provider: function () {
        return new HDWalletProvider(MNEMONIC, INFURA_KEY)
      },
      network_id: 3,
      gas: 8000000      //make sure this gas allocation isn't over 8M, which is the max
    }

After deploying I want to call my script with truffle exec scriptName.js. In my script I basically need to do two things: 1) Add a brand (which saves the address of another wallet) 2) Call another function acting as the brand I added above. This function can be called only by the brand address.

I am able to add the brand, but I am unable to call the second function because its still called by the owner of the smart contract and not by the brand.

Here is my code:

const contract = artifacts.require('contract');
const brandAddress = '0x73aCAEf54005FE8499Ff1159A244617C123F1a53'

module.exports = function (done) {
    console.log("Getting deployed version of the contract...")

    contract.deployed().then(async (instance) => {
         console.log('Adding first Brand...')
         await instance.addBrandAddress(brandAddress);
         console.log('Brand address added: '+ brandAddress)

        //from now on act as the brand
        console.log('Creating first Token...')
        let tokenOne = [
            '1576827018',
            true,
            '43.782988;11.282723',
            '',
            'B2529234802149001329150000E20030',
            '',
            '',
            'Test data'
        ]
        await instance.createToken(tokenOne, {from: brandAddress})
        console.log('First token created: ', tokenOne)
    }).then(() => {
        console.log('Seed finished.')
        done();
    }).catch((err) => {
        console.log(err)
        done();
    })
}

This is the error I get:

Error: Unknown address - unable to sign transaction for this address: "0x73aCAEf54005FE8499Ff1159A244617C123F1a53"    at PromiEvent (/home/dan/.nvm/versions/node/v13.7.0/lib/node_modules/truffle/build/webpack:/packages/contract/lib/promievent.js:9:1)
    at TruffleContract.createToken (/home/dan/.nvm/versions/node/v13.7.0/lib/node_modules/truffle/build/webpack:/packages/contract/lib/execute.js:169:1)
    at /var/www/html/initialContractSeed.js:26:24
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  hijackedStack: 'Error: Unknown address - unable to sign transaction for this address: "0x970355374fa0eab7ab02ca56e611144cc2e8fdf0"\n' +
    '    at /var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:397:1\n' +
    '    at cb (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:441:1)\n' +
    '    at h.cb [as getAccounts] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/src/index.js:99:1)\n' +
    '    at h.getAccounts [as validateSender] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:438:1)\n' +
    '    at h.validateSender [as validateTransaction] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:395:1)\n' +
    '    at validateTransaction (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:112:1)\n' +
    '    at apply (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/async/waterfall.js:16:1)\n' +
    '    at nextTask (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/async/waterfall.js:26:1)\n' +
    '    at h.waterfall [as handleRequest] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:111:1)\n' +
    '    at handleRequest (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/index.js:119:1)\n' +
    '    at l.next [as _handleAsync] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/index.js:106:1)\n' +
    '    at Timeout._handleAsync [as _onTimeout] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/index.js:90:1)\n' +
    '    at listOnTimeout (internal/timers.js:549:17)\n' +
    '    at processTimers (internal/timers.js:492:7)'
}

Instead of from: brandAddress I tried to use the private key of the address (so from: brandAddressPK) but that one still didn't worked.

Maybe I somehow could do it by using web3.js, but I need to send a tuple as a parameter and last thing I remember about web3.js is that wasn't possible (so I switched to ethers.js).

EDIT: What I did to resolve this was to create a js script (without using truffle). By using ethers.js first I create a provider with the private key of the owner, then when I need to act as a brand I create a new provider with the private key of the brand.

It wasn't what I wanted but it works good and do the job.

  • You need to unlock this address on the node that you're communicating with (which is not possible with Infura as far as I'm aware of). – goodvibration Apr 11 at 18:42
  • You could expose multiple addresses with the truffle-hdwallet-provider like this: new HDWalletProvider(MNEMONIC, INFURA_KEY, 0, 2) and then use the second address from the mnemonic as brand address. – Markus - soliditydeveloper.com Apr 12 at 0:59
  • I tried it but still didn't succeed. What I did to resolve this was to create a js script (without using truffle). By using ethers.js first I create a provider with the private key of the owner, then when I need to act as a brand I create a new provider with the private key of the brand. It works very nice. – Agilulfo Apr 12 at 14:19
0

It is possible to solve using a custom HDWallet provider.

HDWallet provider accepts an array of private keys instead of a mnemonic.

const contract = artifacts.require('contract');
const HDWalletProvider = require('@truffle/hdwallet-provider');

const OWNER_ADDR = '0x1234...';  // Ethereum address
const OWNER_PKEY = 'ABCD...';    // Private key (without 0x at the beginning)
const BRAND_ADDR = '0x4433...';  // Ethereum address
const BRAND_PKEY = 'B1C1...';    // Private key

module.exports = async function (done) {
  const provider = new HDWalletProvider([
      OWNER_PKEY,
      BRAND_PKEY,
    ],
    'http://localhost:8545',
    0,  // start at position 0
    2,  // and use 2 private keys
  );

  contract.setProvider(provider);

  const instance = await contract.deployed();

  // To call a method as owner
  await instance.onwerOnlyMethod({ from: OWNER_ADDR });

  // To call a method as brand
  await instance.brandOnlyMethod({ from: BRAND_ADDR });

  done();
};

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.