-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
211 lines (179 loc) · 8.53 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2022 Hekas Foundation
// This file is part of the Hekas Network packages.
//
// Hekas is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Hekas packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Hekas packages. If not, see https://github.com/hekas-network/hekas/blob/main/LICENSE
package personal
import (
"context"
"fmt"
"os"
"time"
"github.com/hekas-network/hekas/rpc/backend"
"github.com/hekas-network/hekas/crypto/hd"
"github.com/hekas-network/hekas/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
evmtypes "github.com/hekas-network/hekas/x/evm/types"
)
// PrivateAccountAPI is the personal_ prefixed set of APIs in the Web3 JSON-RPC spec.
type PrivateAccountAPI struct {
backend backend.EVMBackend
logger log.Logger
hdPathIter types.HDPathIterator
}
// NewAPI creates an instance of the public Personal Eth API.
func NewAPI(
logger log.Logger,
backend backend.EVMBackend,
) *PrivateAccountAPI {
cfg := sdk.GetConfig()
basePath := cfg.GetFullBIP44Path()
iterator, err := types.NewHDPathIterator(basePath, true)
if err != nil {
panic(err)
}
return &PrivateAccountAPI{
logger: logger.With("api", "personal"),
hdPathIter: iterator,
backend: backend,
}
}
// ImportRawKey armors and encrypts a given raw hex encoded ECDSA key and stores it into the key directory.
// The name of the key will have the format "personal_<length-keys>", where <length-keys> is the total number of
// keys stored on the keyring.
//
// NOTE: The key will be both armored and encrypted using the same passphrase.
func (api *PrivateAccountAPI) ImportRawKey(privkey, password string) (common.Address, error) {
api.logger.Debug("personal_importRawKey")
return api.backend.ImportRawKey(privkey, password)
}
// ListAccounts will return a list of addresses for accounts this node manages.
func (api *PrivateAccountAPI) ListAccounts() ([]common.Address, error) {
api.logger.Debug("personal_listAccounts")
return api.backend.ListAccounts()
}
// LockAccount will lock the account associated with the given address when it's unlocked.
// It removes the key corresponding to the given address from the API's local keys.
func (api *PrivateAccountAPI) LockAccount(address common.Address) bool {
api.logger.Debug("personal_lockAccount", "address", address.String())
api.logger.Info("personal_lockAccount not supported")
// TODO: Not supported. See underlying issue https://github.com/99designs/keyring/issues/85
return false
}
// NewAccount will create a new account and returns the address for the new account.
func (api *PrivateAccountAPI) NewAccount(password string) (common.Address, error) {
api.logger.Debug("personal_newAccount")
name := "key_" + time.Now().UTC().Format(time.RFC3339)
// create the mnemonic and save the account
hdPath := api.hdPathIter()
info, err := api.backend.NewMnemonic(name, keyring.English, hdPath.String(), password, hd.EthSecp256k1)
if err != nil {
return common.Address{}, err
}
pubKey, err := info.GetPubKey()
if err != nil {
return common.Address{}, err
}
addr := common.BytesToAddress(pubKey.Address().Bytes())
api.logger.Info("Your new key was generated", "address", addr.String())
api.logger.Info("Please backup your key file!", "path", os.Getenv("HOME")+"/.hekas/"+name) // TODO: pass the correct binary
api.logger.Info("Please remember your password!")
return addr, nil
}
// UnlockAccount will unlock the account associated with the given address with
// the given password for duration seconds. If duration is nil it will use a
// default of 300 seconds. It returns an indication if the account was unlocked.
func (api *PrivateAccountAPI) UnlockAccount(_ context.Context, addr common.Address, _ string, _ *uint64) (bool, error) {
api.logger.Debug("personal_unlockAccount", "address", addr.String())
// TODO: Not supported. See underlying issue https://github.com/99designs/keyring/issues/85
return false, nil
}
// SendTransaction will create a transaction from the given arguments and
// tries to sign it with the key associated with args.To. If the given password isn't
// able to decrypt the key it fails.
func (api *PrivateAccountAPI) SendTransaction(_ context.Context, args evmtypes.TransactionArgs, _ string) (common.Hash, error) {
api.logger.Debug("personal_sendTransaction", "address", args.To.String())
return api.backend.SendTransaction(args)
}
// Sign calculates an Ethereum ECDSA signature for:
// keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
//
// Note, the produced signature conforms to the secp256k1 curve R, S and V values,
// where the V value will be 27 or 28 for legacy reasons.
//
// The key used to calculate the signature is decrypted with the given password.
//
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
func (api *PrivateAccountAPI) Sign(_ context.Context, data hexutil.Bytes, addr common.Address, _ string) (hexutil.Bytes, error) {
api.logger.Debug("personal_sign", "data", data, "address", addr.String())
return api.backend.Sign(addr, data)
}
// EcRecover returns the address for the account that was used to create the signature.
// Note, this function is compatible with eth_sign and personal_sign. As such it recovers
// the address of:
// hash = keccak256("\x19Ethereum Signed Message:\n"${message length}${message})
// addr = ecrecover(hash, signature)
//
// Note, the signature must conform to the secp256k1 curve R, S and V values, where
// the V value must be 27 or 28 for legacy reasons.
//
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_ecRecove
func (api *PrivateAccountAPI) EcRecover(_ context.Context, data, sig hexutil.Bytes) (common.Address, error) {
api.logger.Debug("personal_ecRecover", "data", data, "sig", sig)
if len(sig) != crypto.SignatureLength {
return common.Address{}, fmt.Errorf("signature must be %d bytes long", crypto.SignatureLength)
}
if sig[crypto.RecoveryIDOffset] != 27 && sig[crypto.RecoveryIDOffset] != 28 {
return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)")
}
sig[crypto.RecoveryIDOffset] -= 27 // Transform yellow paper V from 27/28 to 0/1
pubkey, err := crypto.SigToPub(accounts.TextHash(data), sig)
if err != nil {
return common.Address{}, err
}
return crypto.PubkeyToAddress(*pubkey), nil
}
// Unpair deletes a pairing between wallet and ethermint.
func (api *PrivateAccountAPI) Unpair(_ context.Context, url, pin string) error {
api.logger.Debug("personal_unpair", "url", url, "pin", pin)
api.logger.Info("personal_unpair for smartcard wallet not supported")
// TODO: Smartcard wallet not supported yet, refer to: https://github.com/ethereum/go-ethereum/blob/master/accounts/scwallet/README.md
return fmt.Errorf("smartcard wallet not supported yet")
}
// InitializeWallet initializes a new wallet at the provided URL, by generating and returning a new private key.
func (api *PrivateAccountAPI) InitializeWallet(_ context.Context, url string) (string, error) {
api.logger.Debug("personal_initializeWallet", "url", url)
api.logger.Info("personal_initializeWallet for smartcard wallet not supported")
// TODO: Smartcard wallet not supported yet, refer to: https://github.com/ethereum/go-ethereum/blob/master/accounts/scwallet/README.md
return "", fmt.Errorf("smartcard wallet not supported yet")
}
// RawWallet is a JSON representation of an accounts.Wallet interface, with its
// data contents extracted into plain fields.
type RawWallet struct {
URL string `json:"url"`
Status string `json:"status"`
Failure string `json:"failure,omitempty"`
Accounts []accounts.Account `json:"accounts,omitempty"`
}
// ListWallets will return a list of wallets this node manages.
func (api *PrivateAccountAPI) ListWallets() []RawWallet {
api.logger.Debug("personal_ListWallets")
api.logger.Info("currently wallet level that manages accounts is not supported")
return ([]RawWallet)(nil)
}