read account from wallet

This commit is contained in:
amalraj.manigmail.com 2018-10-31 15:15:34 +08:00
parent 46cf4ddfb6
commit 01233f583e
8 changed files with 102 additions and 35 deletions

View File

@ -26,7 +26,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
"github.com/ethereum/go-ethereum/accounts"
)
// NewTransactor is a utility method to easily create a transaction signer from
// an encrypted json key stream and the associated passphrase.
@ -60,3 +61,17 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
},
}
}
func NewWalletTransactor(w accounts.Wallet, from accounts.Account) *TransactOpts {
return &TransactOpts{
From: from.Address,
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
signature, err := w.SignHash(from, signer.Hash(tx).Bytes())
if err != nil {
return nil, err
}
return tx.WithSignature(signer, signature)
},
}
}

File diff suppressed because one or more lines are too long

View File

@ -87,6 +87,11 @@ contract Permissions {
/* public and external functions */
// view functions
// Get number of nodes
function getNumberofVoters() public view returns (uint)
{
return accountList.length;
}
// Get number of nodes
function getNetworkBootStatus() public view returns (bool)
{

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -7,28 +7,30 @@ import (
"path/filepath"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/params"
"strings"
"github.com/ethereum/go-ethereum/p2p/discover"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/log"
pbind "github.com/ethereum/go-ethereum/controls/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/accounts"
"strings"
)
var defaultGasLimit = uint64(47000)
var defaultGasLimit = uint64(4700000)
var defaultGasPrice = big.NewInt(0)
type PermissionAPI struct {
txPool *core.TxPool
ethClnt *ethclient.Client
transOpts *bind.TransactOpts
permContr *pbind.Permissions
txPool *core.TxPool
ethClnt *ethclient.Client
am *accounts.Manager
trnOpt *bind.TransactOpts
permContr *pbind.Permissions
clustContr *pbind.Cluster
}
func NewPermissionAPI(tp *core.TxPool) *PermissionAPI {
pa := &PermissionAPI{tp, nil, nil, nil, nil}
func NewPermissionAPI(tp *core.TxPool, am *accounts.Manager) *PermissionAPI {
pa := &PermissionAPI{tp, nil, am, nil, nil, nil}
return pa
}
@ -53,13 +55,21 @@ func (p *PermissionAPI) Init(ethClnt *ethclient.Client, datadir string) error {
if err != nil {
return err
}
p.transOpts = auth
p.trnOpt = auth
return nil
}
func (s *PermissionAPI) AddVoter(addr common.Address) bool {
ps := s.newPermSession()
acct := accounts.Account{Address: addr}
w, err := s.am.Find(acct)
if err != nil {
return false
}
ps := s.newPermSession1(w, acct)
nonce := s.txPool.Nonce(acct.Address)
ps.TransactOpts.Nonce = new(big.Int).SetUint64(nonce)
tx, err := ps.AddVoter(addr)
if err != nil {
log.Warn("Failed to add voter", "err", err)
@ -82,8 +92,17 @@ func (s *PermissionAPI) RemoveVoter(addr common.Address) bool {
return true
}
func (s *PermissionAPI) ProposeNode(from common.Address, nodeId string) bool {
acct := accounts.Account{Address: from}
w, err := s.am.Find(acct)
if err != nil {
return false
}
ps := s.newPermSession1(w, acct)
nonce := s.txPool.Nonce(acct.Address)
ps.TransactOpts.Nonce = new(big.Int).SetUint64(nonce)
func (s *PermissionAPI) ProposeNode(nodeId string) bool {
node, err := discover.ParseNode(nodeId)
if err != nil {
log.Error("invalid node id: %v", err)
@ -95,10 +114,6 @@ func (s *PermissionAPI) ProposeNode(nodeId string) bool {
discPort := fmt.Sprintf("%v", node.UDP)
raftPort := fmt.Sprintf("%v", node.RaftPort)
ipAddrPort := ipAddr + ":" + port
nonce := s.txPool.Nonce(s.transOpts.From)
s.transOpts.Nonce = new(big.Int).SetUint64(nonce)
ps := s.newPermSession()
tx, err := ps.ProposeNode(enodeID, ipAddrPort, discPort, raftPort)
if err != nil {
@ -119,8 +134,6 @@ func (s *PermissionAPI) ApproveNode(nodeId string) bool {
return false
}
enodeID := node.ID.String()
nonce := s.txPool.Nonce(s.transOpts.From)
s.transOpts.Nonce = new(big.Int).SetUint64(nonce)
ps := s.newPermSession()
tx, err := ps.ApproveNode(enodeID)
@ -140,8 +153,6 @@ func (s *PermissionAPI) DeactivateNode(nodeId string) bool {
return false
}
enodeID := node.ID.String()
nonce := s.txPool.Nonce(s.transOpts.From)
s.transOpts.Nonce = new(big.Int).SetUint64(nonce)
ps := s.newPermSession()
tx, err := ps.DeactivateNode(enodeID)
@ -161,8 +172,6 @@ func (s *PermissionAPI) ApproveDeactivateNode(nodeId string) bool {
return false
}
enodeID := node.ID.String()
nonce := s.txPool.Nonce(s.transOpts.From)
s.transOpts.Nonce = new(big.Int).SetUint64(nonce)
ps := s.newPermSession()
//TODO change it to approveDeactivate node once contract is updated
@ -176,7 +185,6 @@ func (s *PermissionAPI) ApproveDeactivateNode(nodeId string) bool {
return true
}
func (s *PermissionAPI) newPermSession() *pbind.PermissionsSession {
return &pbind.PermissionsSession{
Contract: s.permContr,
@ -184,14 +192,30 @@ func (s *PermissionAPI) newPermSession() *pbind.PermissionsSession {
Pending: true,
},
TransactOpts: bind.TransactOpts{
From: s.transOpts.From,
Signer: s.transOpts.Signer,
From: s.trnOpt.From,
Signer: s.trnOpt.Signer,
GasLimit: defaultGasLimit,
GasPrice: defaultGasPrice,
},
}
}
func (s *PermissionAPI) newPermSession1(w accounts.Wallet, acct accounts.Account) *pbind.PermissionsSession {
auth := bind.NewWalletTransactor(w, acct)
return &pbind.PermissionsSession{
Contract: s.permContr,
CallOpts: bind.CallOpts{
Pending: true,
},
TransactOpts: bind.TransactOpts{
From: acct.Address,
GasLimit: defaultGasLimit,
GasPrice: defaultGasPrice,
Signer: auth.Signer,
},
}
}
func (s *PermissionAPI) newClusterSession() *pbind.ClusterSession {
return &pbind.ClusterSession{
Contract: s.clustContr,
@ -199,8 +223,6 @@ func (s *PermissionAPI) newClusterSession() *pbind.ClusterSession {
Pending: true,
},
TransactOpts: bind.TransactOpts{
From: s.transOpts.From,
Signer: s.transOpts.Signer,
GasLimit: defaultGasLimit,
GasPrice: defaultGasPrice,
},
@ -231,8 +253,6 @@ func (s *PermissionAPI) RemoveOrgKey(orgId string, pvtKey string) bool {
return true
}
func getKeyFromKeyStore(datadir string) (string, error) {
files, err := ioutil.ReadDir(filepath.Join(datadir, "keystore"))

View File

@ -330,7 +330,7 @@ func (s *Ethereum) APIs() []rpc.API {
{
Namespace: "quorum",
Version: "1.0",
Service: quorum.NewPermissionAPI(s.txPool),
Service: quorum.NewPermissionAPI(s.txPool, s.accountManager),
Public: true,
},

View File

@ -698,7 +698,8 @@ web3._extend({
new web3._extend.Method({
name: 'proposeNode',
call: 'quorum_proposeNode',
params: 1
params: 2,
inputFormatter: [web3._extend.formatters.inputAddressFormatter,null]
}),
new web3._extend.Method({
name: 'approveNode',