github.com/codingfuture/orig-energi3@v0.8.4/energi/api/ephemeral.go (about) 1 // Copyright 2019 The Energi Core Authors 2 // This file is part of the Energi Core library. 3 // 4 // The Energi Core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The Energi Core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "crypto/ecdsa" 21 crand "crypto/rand" 22 "math/big" 23 24 ethereum "github.com/ethereum/go-ethereum" 25 26 "github.com/ethereum/go-ethereum/accounts" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/event" 30 31 energi_params "energi.world/core/gen3/energi/params" 32 ) 33 34 type EphemeralWallet struct{} 35 36 func (ew *EphemeralWallet) URL() accounts.URL { 37 return accounts.URL{"ephemeral", ""} 38 } 39 40 func (ew *EphemeralWallet) Status() (string, error) { 41 return "Unlocked", nil 42 } 43 44 func (ew *EphemeralWallet) Open(passphrase string) (err error) { 45 return 46 } 47 48 func (ew *EphemeralWallet) Close() error { 49 return nil 50 } 51 52 func (ew *EphemeralWallet) Accounts() []accounts.Account { 53 return []accounts.Account{ 54 {energi_params.Energi_Ephemeral, ew.URL()}, 55 } 56 } 57 58 func (ew *EphemeralWallet) Contains(account accounts.Account) bool { 59 return account.Address == energi_params.Energi_Ephemeral 60 } 61 62 func (ew *EphemeralWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { 63 return accounts.Account{}, accounts.ErrNotSupported 64 } 65 66 func (ew *EphemeralWallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {} 67 68 func (ew *EphemeralWallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) { 69 if !ew.Contains(account) { 70 return nil, accounts.ErrUnknownAccount 71 } 72 73 privateKey, err := ecdsa.GenerateKey(crypto.S256(), crand.Reader) 74 if err != nil { 75 return nil, err 76 } 77 78 return crypto.Sign(hash, privateKey) 79 } 80 81 func (ew *EphemeralWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 82 if !ew.Contains(account) { 83 return nil, accounts.ErrUnknownAccount 84 } 85 86 privateKey, err := ecdsa.GenerateKey(crypto.S256(), crand.Reader) 87 if err != nil { 88 return nil, err 89 } 90 91 if chainID != nil { 92 return types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) 93 } 94 return types.SignTx(tx, types.HomesteadSigner{}, privateKey) 95 } 96 97 func (ew *EphemeralWallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) { 98 return ew.SignHash(account, hash) 99 } 100 101 func (ew *EphemeralWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 102 return ew.SignTx(account, tx, chainID) 103 } 104 105 func (ew *EphemeralWallet) IsUnlockedForStaking(account accounts.Account) bool { 106 return false 107 } 108 109 type EphemeralAccount struct { 110 wallet EphemeralWallet 111 updateFeed event.Feed 112 updateScope event.SubscriptionScope 113 } 114 115 func NewEphemeralAccount() (*EphemeralAccount, error) { 116 ea := &EphemeralAccount{} 117 return ea, ea.wallet.Open("") 118 } 119 120 func (ea *EphemeralAccount) Wallets() []accounts.Wallet { 121 return []accounts.Wallet{&ea.wallet} 122 } 123 124 func (ea *EphemeralAccount) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { 125 return ea.updateScope.Track(ea.updateFeed.Subscribe(sink)) 126 }