github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/accounts/keystore/keystore_wallet.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2017 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package keystore 26 27 import ( 28 "math/big" 29 30 ethereum "github.com/ethereum/go-ethereum" 31 "github.com/ethereum/go-ethereum/accounts" 32 "github.com/ethereum/go-ethereum/core/types" 33 ) 34 35 //keystorewallet实现原始帐户的accounts.wallet接口 36 //密钥存储区。 37 type keystoreWallet struct { 38 account accounts.Account //钱包里有一个账户 39 keystore *KeyStore //帐户来源的密钥库 40 } 41 42 //url实现accounts.wallet,返回帐户的url。 43 func (w *keystoreWallet) URL() accounts.URL { 44 return w.account.URL 45 } 46 47 //状态实现accounts.wallet,返回 48 //密钥存储钱包是否已解锁。 49 func (w *keystoreWallet) Status() (string, error) { 50 w.keystore.mu.RLock() 51 defer w.keystore.mu.RUnlock() 52 53 if _, ok := w.keystore.unlocked[w.account.Address]; ok { 54 return "Unlocked", nil 55 } 56 return "Locked", nil 57 } 58 59 //打开工具帐户。钱包,但是普通钱包的一个noop,因为那里 60 //访问帐户列表不需要连接或解密步骤。 61 func (w *keystoreWallet) Open(passphrase string) error { return nil } 62 63 //close实现帐户。wallet,但对于普通钱包来说是一个noop,因为它不是 64 //有意义的开放式操作。 65 func (w *keystoreWallet) Close() error { return nil } 66 67 //帐户实现帐户。钱包,返回包含 68 //普通的Kestore钱包中包含的单个帐户。 69 func (w *keystoreWallet) Accounts() []accounts.Account { 70 return []accounts.Account{w.account} 71 } 72 73 //包含implements accounts.wallet,返回特定帐户是否为 74 //或未被此钱包实例包装。 75 func (w *keystoreWallet) Contains(account accounts.Account) bool { 76 return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL) 77 } 78 79 //派生实现了accounts.wallet,但对于普通的钱包来说是一个noop,因为 80 //对于普通的密钥存储帐户,不存在分层帐户派生的概念。 81 func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { 82 return accounts.Account{}, accounts.ErrNotSupported 83 } 84 85 //Selfderive实现了accounts.wallet,但对于普通的钱包来说是一个noop,因为 86 //对于普通密钥库帐户,没有层次结构帐户派生的概念。 87 func (w *keystoreWallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {} 88 89 //sign hash实现accounts.wallet,尝试用 90 //给定的帐户。如果钱包没有包裹这个特定的账户, 91 //返回错误以避免帐户泄漏(即使在理论上我们可能 92 //能够通过我们的共享密钥库后端进行签名)。 93 func (w *keystoreWallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) { 94 //确保请求的帐户包含在 95 if account.Address != w.account.Address { 96 return nil, accounts.ErrUnknownAccount 97 } 98 if account.URL != (accounts.URL{}) && account.URL != w.account.URL { 99 return nil, accounts.ErrUnknownAccount 100 } 101 //帐户似乎有效,请求密钥库签名 102 return w.keystore.SignHash(account, hash) 103 } 104 105 //signtx实现accounts.wallet,尝试签署给定的交易 106 //与给定的帐户。如果钱包没有包裹这个特定的账户, 107 //返回一个错误以避免帐户泄漏(即使在理论上我们可以 108 //能够通过我们的共享密钥库后端进行签名)。 109 func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 110 //确保请求的帐户包含在 111 if account.Address != w.account.Address { 112 return nil, accounts.ErrUnknownAccount 113 } 114 if account.URL != (accounts.URL{}) && account.URL != w.account.URL { 115 return nil, accounts.ErrUnknownAccount 116 } 117 //帐户似乎有效,请求密钥库签名 118 return w.keystore.SignTx(account, tx, chainID) 119 } 120 121 // 122 //使用密码短语作为额外身份验证的给定帐户的给定哈希。 123 func (w *keystoreWallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) { 124 //确保请求的帐户包含在 125 if account.Address != w.account.Address { 126 return nil, accounts.ErrUnknownAccount 127 } 128 if account.URL != (accounts.URL{}) && account.URL != w.account.URL { 129 return nil, accounts.ErrUnknownAccount 130 } 131 //帐户似乎有效,请求密钥库签名 132 return w.keystore.SignHashWithPassphrase(account, passphrase, hash) 133 } 134 135 //signtxwithpassphrase实现accounts.wallet,尝试对给定的 136 //使用密码短语作为额外身份验证的给定帐户的事务。 137 func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 138 //确保请求的帐户包含在 139 if account.Address != w.account.Address { 140 return nil, accounts.ErrUnknownAccount 141 } 142 if account.URL != (accounts.URL{}) && account.URL != w.account.URL { 143 return nil, accounts.ErrUnknownAccount 144 } 145 //帐户似乎有效,请求密钥库签名 146 return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID) 147 }