0

Wondering how would I access an unlocked account on the node (private geth) in golang. The flow I'm looking for is: 1. user unlocks their account (using personal.unlockAccount for example) 2. my app will signTx's and interact with erc20 contracts.

So far I've ran in circles. Using geth attach > personal.listWallets shows the account as 'unlocked'. The keystore is in same dir as the geth dir, and I've loaded it correctly.

Here is the implementation I'm trying:

func SellDollar(address common.Address, amount *big.Int, client *ethclient.Client) (bool, error) {

    // erc20 & keystore instances
    stableTokenAddress := common.HexToAddress("0x5c7197e1147ebf98658a2a8bc3d32bebf1692829")
    tokenInstance, err := contracts.NewStabletoken(stableTokenAddress, client)
    if err != nil {
        return false, err
    }
    ks := keystore.NewKeyStore(
        "/path/to/keystore",
        keystore.LightScryptN,
        keystore.LightScryptP)

    // get unlocked account
    var account accounts.Account
    for _, acc := range ks.Accounts() {
        if acc.Address.String() == address.String() {
            account, err = ks.Find(acc)
            if err != nil {
                return false, err
            }
        }
    }

    // I think this is where the issue is
    // txOpts isn't being returned a valid signer?
    txOpts, err := bind.NewKeyStoreTransactor(ks, account)
    if err != nil {
        return false, err
    }

    session := &contracts.StabletokenSession{
        Contract: tokenInstance,
        CallOpts: bind.CallOpts{
            Pending: true,
            From:    address,
        },
        TransactOpts: *txOpts,
    }

    // fails here with `authentication needed: password or unlock`
    tx, err := session.Approve(address, amount)
    if err != nil {
        log.Fatal(err)
        return false, err
    }

    return true, nil
}

One obvious thing is that if go could pull the unlocked account from node (I presume by requesting the private key in geth's memory) then it would at least need client *ethclient.Client but can't find anything in go-ethereum method signatures that would suit this. Or can I find anything online.

Is what Im trying to do possible?

  • For now I'm going to require users to load password into app through env_var, can decrypt keystore to sign transactions. But my flow was for them to unlock account through geth attach and thus keep pswds away from my cli – Daithí Jan 28 at 17:30

Your Answer

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

Browse other questions tagged or ask your own question.