2

Does anyone know if its possible to get a list of the accounts on a node using golang, after Dialing to the node, basic PoC would be assuming the accounts are already unlocked via the CLI

Basically looking for the same functionality as web3.eth.Accounts() or to list the account addresses on a remote geth node that are already unlocked via the CLI

2
  • Could you clarify/define "dialling into the node"? Dec 1, 2018 at 17:58
  • To connect to a node in go you use the ethclient.Dial()
    – Kravitz
    Dec 1, 2018 at 17:59

2 Answers 2

2

One way is to interact with the keystore, this is a package i wrote for interacting with keystore function "GetAllKeys" will list all the accounts. package keystore

import (
    "log"

    "github.com/ethereum/go-ethereum/accounts"
    "github.com/ethereum/go-ethereum/accounts/keystore"
    "github.com/ethereum/go-ethereum/common"
)

type KeyStore struct {
    Handle *keystore.KeyStore
}

func SetUpKeyStore(kp string) *KeyStore {
    ks := &KeyStore{}
    ks.Handle = keystore.NewKeyStore(kp, keystore.LightScryptN, keystore.LightScryptP)
    return ks
}

func (ks *KeyStore) CreateNewKeys(password string) accounts.Account {
    account, err := ks.Handle.NewAccount(password)
    if err != nil {
        log.Panic(err)
    }
    return account
}

func (ks *KeyStore) GetKeysByAddress(address string) accounts.Account {

    var account accounts.Account
    var err error
    if ks.Handle.HasAddress(common.HexToAddress(address)) {
        if account, err = ks.Handle.Find(accounts.Account{Address: common.HexToAddress(address)}); err != nil {
            log.Panic(err)
        }
    }
    return account
}

func (ks *KeyStore) GetAllKeys() []accounts.Account {

    return ks.Handle.Accounts() 
}
1
  • What is the implementation here? setupKeyStore(string), what string? Then pass *keystore to GetAllKeys?
    – Daithí
    Aug 28, 2019 at 14:32
0

Ended up writing my own implementation doing a post request to the node, pretty much what web3 does under the hook with RPC/WS endpoints

1
  • any more details on POST request or link to doc/rpc methods?
    – Daithí
    Aug 28, 2019 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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