github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/accounts/keystore/keystore_wallet.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:26</date>
    10  //</624342585632100352>
    11  
    12  
    13  package keystore
    14  
    15  import (
    16  	"math/big"
    17  
    18  	ethereum "github.com/ethereum/go-ethereum"
    19  	"github.com/ethereum/go-ethereum/accounts"
    20  	"github.com/ethereum/go-ethereum/core/types"
    21  )
    22  
    23  //keystorewallet实现原始帐户的accounts.wallet接口
    24  //密钥存储区。
    25  type keystoreWallet struct {
    26  account  accounts.Account //钱包里有一个账户
    27  keystore *KeyStore        //帐户来源的密钥库
    28  }
    29  
    30  //url实现accounts.wallet,返回帐户的url。
    31  func (w *keystoreWallet) URL() accounts.URL {
    32  	return w.account.URL
    33  }
    34  
    35  //状态实现accounts.wallet,返回
    36  //密钥存储钱包是否已解锁。
    37  func (w *keystoreWallet) Status() (string, error) {
    38  	w.keystore.mu.RLock()
    39  	defer w.keystore.mu.RUnlock()
    40  
    41  	if _, ok := w.keystore.unlocked[w.account.Address]; ok {
    42  		return "Unlocked", nil
    43  	}
    44  	return "Locked", nil
    45  }
    46  
    47  //打开工具帐户。钱包,但是普通钱包的一个noop,因为那里
    48  //访问帐户列表不需要连接或解密步骤。
    49  func (w *keystoreWallet) Open(passphrase string) error { return nil }
    50  
    51  //close实现帐户。wallet,但对于普通钱包来说是一个noop,因为它不是
    52  //有意义的开放式操作。
    53  func (w *keystoreWallet) Close() error { return nil }
    54  
    55  //帐户实现帐户。钱包,返回包含
    56  //普通的Kestore钱包中包含的单个帐户。
    57  func (w *keystoreWallet) Accounts() []accounts.Account {
    58  	return []accounts.Account{w.account}
    59  }
    60  
    61  //包含implements accounts.wallet,返回特定帐户是否为
    62  //或未被此钱包实例包装。
    63  func (w *keystoreWallet) Contains(account accounts.Account) bool {
    64  	return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
    65  }
    66  
    67  //派生实现了accounts.wallet,但对于普通的钱包来说是一个noop,因为
    68  //对于普通的密钥存储帐户,不存在分层帐户派生的概念。
    69  func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
    70  	return accounts.Account{}, accounts.ErrNotSupported
    71  }
    72  
    73  //Selfderive实现了accounts.wallet,但对于普通的钱包来说是一个noop,因为
    74  //对于普通密钥库帐户,没有层次结构帐户派生的概念。
    75  func (w *keystoreWallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {}
    76  
    77  //sign hash实现accounts.wallet,尝试用
    78  //给定的帐户。如果钱包没有包裹这个特定的账户,
    79  //返回错误以避免帐户泄漏(即使在理论上我们可能
    80  //能够通过我们的共享密钥库后端进行签名)。
    81  func (w *keystoreWallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) {
    82  //确保请求的帐户包含在
    83  	if account.Address != w.account.Address {
    84  		return nil, accounts.ErrUnknownAccount
    85  	}
    86  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    87  		return nil, accounts.ErrUnknownAccount
    88  	}
    89  //帐户似乎有效,请求密钥库签名
    90  	return w.keystore.SignHash(account, hash)
    91  }
    92  
    93  //signtx实现accounts.wallet,尝试签署给定的交易
    94  //与给定的帐户。如果钱包没有包裹这个特定的账户,
    95  //返回一个错误以避免帐户泄漏(即使在理论上我们可以
    96  //能够通过我们的共享密钥库后端进行签名)。
    97  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
    98  //确保请求的帐户包含在
    99  	if account.Address != w.account.Address {
   100  		return nil, accounts.ErrUnknownAccount
   101  	}
   102  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   103  		return nil, accounts.ErrUnknownAccount
   104  	}
   105  //帐户似乎有效,请求密钥库签名
   106  	return w.keystore.SignTx(account, tx, chainID)
   107  }
   108  
   109  //
   110  //使用密码短语作为额外身份验证的给定帐户的给定哈希。
   111  func (w *keystoreWallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
   112  //确保请求的帐户包含在
   113  	if account.Address != w.account.Address {
   114  		return nil, accounts.ErrUnknownAccount
   115  	}
   116  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   117  		return nil, accounts.ErrUnknownAccount
   118  	}
   119  //帐户似乎有效,请求密钥库签名
   120  	return w.keystore.SignHashWithPassphrase(account, passphrase, hash)
   121  }
   122  
   123  //signtxwithpassphrase实现accounts.wallet,尝试对给定的
   124  //使用密码短语作为额外身份验证的给定帐户的事务。
   125  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   126  //确保请求的帐户包含在
   127  	if account.Address != w.account.Address {
   128  		return nil, accounts.ErrUnknownAccount
   129  	}
   130  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   131  		return nil, accounts.ErrUnknownAccount
   132  	}
   133  //帐户似乎有效,请求密钥库签名
   134  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   135  }
   136