Skip to content

Commit

Permalink
personal endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dusannosovic-ethernal committed Jun 3, 2024
1 parent e5ed1ae commit bdb31b1
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 6 deletions.
18 changes: 12 additions & 6 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ func (f *funcData) numParams() int {
}

type endpoints struct {
Eth *Eth
Web3 *Web3
Net *Net
TxPool *TxPool
Bridge *Bridge
Debug *Debug
Eth *Eth
Web3 *Web3
Net *Net
TxPool *TxPool
Bridge *Bridge
Debug *Debug
Personal *Personal
}

// Dispatcher handles all json rpc requests by delegating
Expand Down Expand Up @@ -118,6 +119,7 @@ func (d *Dispatcher) registerEndpoints(store JSONRPCStore, manager *accounts.Man
store,
}
d.endpoints.Debug = NewDebug(store, d.params.concurrentRequestsDebug)
d.endpoints.Personal = &Personal{accManager: manager}

var err error

Expand All @@ -141,6 +143,10 @@ func (d *Dispatcher) registerEndpoints(store JSONRPCStore, manager *accounts.Man
return err
}

if err = d.registerService("personal", d.endpoints.Personal); err != nil {
return err
}

return d.registerService("debug", d.endpoints.Debug)
}

Expand Down
112 changes: 112 additions & 0 deletions jsonrpc/personal_endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package jsonrpc

import (
"errors"
"fmt"
"math"
"time"

"github.com/0xPolygon/polygon-edge/accounts"
"github.com/0xPolygon/polygon-edge/accounts/keystore"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/types"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
)

type Personal struct {
accManager *accounts.Manager
}

func (p *Personal) ListAccounts() []types.Address {
return p.accManager.Accounts()
}

func (p *Personal) NewAccount(password string) (types.Address, error) {
ks, err := getKeystore(p.accManager)
if err != nil {
return types.Address{}, err
}

acc, err := ks.NewAccount(password)
if err != nil {
return types.Address{}, fmt.Errorf("Cant create new account")

Check failure on line 32 in jsonrpc/personal_endpoint.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

ST1005: error strings should not be capitalized (stylecheck)
}

return acc.Address, nil
}

func (p *Personal) ImportRawKey(privKey string, password string) (types.Address, error) {
key, err := crypto.HexToECDSA(privKey)
if err != nil {
return types.Address{}, err
}

ks, err := getKeystore(p.accManager)
if err != nil {
return types.Address{}, err
}

acc, err := ks.ImportECDSA(key, password)

return acc.Address, err
}

func (p *Personal) UnlockAccount(addr types.Address, password string, duration uint64) (bool, error) {
const max = uint64(time.Duration(math.MaxInt64) / time.Second)
var d time.Duration

Check failure on line 56 in jsonrpc/personal_endpoint.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

declarations should never be cuddled (wsl)

if duration == 0 {

Check failure on line 58 in jsonrpc/personal_endpoint.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

ifElseChain: rewrite if-else to switch statement (gocritic)
d = 300 * time.Second
} else if duration > max {
return false, errors.New("unlock duration is too large")
} else {
d = time.Duration(duration) * time.Second
}

ks, err := getKeystore(p.accManager)
if err != nil {
return false, err
}

err = ks.TimedUnlock(accounts.Account{Address: addr}, password, d)
if err != nil {
return false, err
}

return true, nil
}

func (s *Personal) LockAccount(addr types.Address) bool {
if ks, err := getKeystore(s.accManager); err == nil {
return ks.Lock(addr) == nil
}

return false
}

func (p *Personal) EcRecover(data, sig []byte) (types.Address, error) {

Check failure on line 87 in jsonrpc/personal_endpoint.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

ST1016: methods on the same type should have the same receiver name (seen 1x "s", 5x "p") (stylecheck)
if len(sig) != crypto.ECDSASignatureLength {
return types.Address{}, fmt.Errorf("signature must be %d bytes long", crypto.ECDSASignatureLength)
}

if sig[64] != 27 && sig[64] != 28 {
return types.Address{}, errors.New("invalid Ethereum signature V is not 27 or 28")
}

sig[64] -= 27

rpk, _, err := ecdsa.RecoverCompact(sig, data)
if err != nil {
return types.Address{}, err
}

return crypto.PubKeyToAddress(rpk.ToECDSA()), nil
}

func getKeystore(am *accounts.Manager) (*keystore.KeyStore, error) {
if ks := am.Backends(keystore.KeyStoreType); len(ks) > 0 {
return ks[0].(*keystore.KeyStore), nil

Check failure on line 108 in jsonrpc/personal_endpoint.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

type assertion must be checked (forcetypeassert)
}

return nil, errors.New("local keystore not used")
}

0 comments on commit bdb31b1

Please sign in to comment.