|
| 1 | +// Copyright 2021 - See NOTICE file for copyright holders. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package simple |
| 16 | + |
| 17 | +import ( |
| 18 | + "crypto/ecdsa" |
| 19 | + "math/rand" |
| 20 | + |
| 21 | + "github.com/ethereum/go-ethereum/accounts" |
| 22 | + "github.com/ethereum/go-ethereum/common" |
| 23 | + "github.com/ethereum/go-ethereum/crypto" |
| 24 | + "github.com/ethereum/go-ethereum/crypto/secp256k1" |
| 25 | + "github.com/pkg/errors" |
| 26 | + |
| 27 | + ethwallet "perun.network/go-perun/backend/ethereum/wallet" |
| 28 | + "perun.network/go-perun/log" |
| 29 | + "perun.network/go-perun/wallet" |
| 30 | +) |
| 31 | + |
| 32 | +var _ wallet.Wallet = (*Wallet)(nil) |
| 33 | + |
| 34 | +// Wallet is a simple wallet.Wallet implementation holding a map of all included Accounts. |
| 35 | +type Wallet struct { |
| 36 | + Accounts map[common.Address]*Account |
| 37 | +} |
| 38 | + |
| 39 | +// NewWallet creates a new Wallet with Accounts corresponding to the privateKeys. |
| 40 | +func NewWallet(privateKeys ...*ecdsa.PrivateKey) *Wallet { |
| 41 | + accs := make(map[common.Address]*Account) |
| 42 | + for _, key := range privateKeys { |
| 43 | + addr := crypto.PubkeyToAddress(key.PublicKey) |
| 44 | + accs[addr] = createAccount(key) |
| 45 | + } |
| 46 | + return &Wallet{Accounts: accs} |
| 47 | +} |
| 48 | + |
| 49 | +// Contains checks whether this wallet contains the account corresponding to the given address. |
| 50 | +func (w *Wallet) Contains(addr common.Address) bool { |
| 51 | + _, ok := w.Accounts[addr] |
| 52 | + return ok |
| 53 | +} |
| 54 | + |
| 55 | +// NewRandomAccount creates a new pseudorandom account using the provided |
| 56 | +// randomness. The returned account is already unlocked. |
| 57 | +func (w *Wallet) NewRandomAccount(prng *rand.Rand) wallet.Account { |
| 58 | + privateKey, err := ecdsa.GenerateKey(secp256k1.S256(), prng) |
| 59 | + if err != nil { |
| 60 | + log.Panicf("Creating account: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + addr := crypto.PubkeyToAddress(privateKey.PublicKey) |
| 64 | + if !w.Contains(addr) { |
| 65 | + acc := Account{ |
| 66 | + Account: accounts.Account{Address: addr}, |
| 67 | + key: privateKey, |
| 68 | + } |
| 69 | + w.Accounts[addr] = &acc |
| 70 | + return &acc |
| 71 | + } |
| 72 | + return w.Accounts[addr] |
| 73 | +} |
| 74 | + |
| 75 | +// Unlock returns the account corresponding to the given address if the wallet |
| 76 | +// contains this account. |
| 77 | +func (w *Wallet) Unlock(address wallet.Address) (wallet.Account, error) { |
| 78 | + if _, ok := address.(*ethwallet.Address); !ok { |
| 79 | + return nil, errors.New("address must be ethwallet.Address") |
| 80 | + } |
| 81 | + |
| 82 | + if acc, ok := w.Accounts[ethwallet.AsEthAddr(address)]; ok { |
| 83 | + return acc, nil |
| 84 | + } |
| 85 | + return nil, errors.New("account not found in wallet") |
| 86 | +} |
| 87 | + |
| 88 | +// LockAll is called by the framework when a Client shuts down. |
| 89 | +func (w *Wallet) LockAll() {} |
| 90 | + |
| 91 | +// IncrementUsage is called whenever a new channel is created or restored. |
| 92 | +func (w *Wallet) IncrementUsage(address wallet.Address) {} |
| 93 | + |
| 94 | +// DecrementUsage is called whenever a channel is settled. |
| 95 | +func (w *Wallet) DecrementUsage(address wallet.Address) {} |
0 commit comments