github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/mobile/accounts.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  //版权所有2016 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  //包含来自帐户包的所有包装器以支持客户端密钥
    26  //移动平台管理。
    27  
    28  package geth
    29  
    30  import (
    31  	"errors"
    32  	"time"
    33  
    34  	"github.com/ethereum/go-ethereum/accounts"
    35  	"github.com/ethereum/go-ethereum/accounts/keystore"
    36  	"github.com/ethereum/go-ethereum/common"
    37  	"github.com/ethereum/go-ethereum/crypto"
    38  )
    39  
    40  const (
    41  //标准加密是加密算法的n个参数,使用256MB
    42  //在现代处理器上占用大约1秒的CPU时间。
    43  	StandardScryptN = int(keystore.StandardScryptN)
    44  
    45  //StandardScryptP是加密算法的P参数,使用256MB
    46  //在现代处理器上占用大约1秒的CPU时间。
    47  	StandardScryptP = int(keystore.StandardScryptP)
    48  
    49  //lightscryptn是加密算法的n个参数,使用4MB
    50  //在现代处理器上占用大约100毫秒的CPU时间。
    51  	LightScryptN = int(keystore.LightScryptN)
    52  
    53  //lightscryptp是加密算法的p参数,使用4MB
    54  //在现代处理器上占用大约100毫秒的CPU时间。
    55  	LightScryptP = int(keystore.LightScryptP)
    56  )
    57  
    58  //帐户表示存储的密钥。
    59  type Account struct{ account accounts.Account }
    60  
    61  //帐户代表帐户的一部分。
    62  type Accounts struct{ accounts []accounts.Account }
    63  
    64  //SIZE返回切片中的帐户数。
    65  func (a *Accounts) Size() int {
    66  	return len(a.accounts)
    67  }
    68  
    69  //GET从切片返回给定索引处的帐户。
    70  func (a *Accounts) Get(index int) (account *Account, _ error) {
    71  	if index < 0 || index >= len(a.accounts) {
    72  		return nil, errors.New("index out of bounds")
    73  	}
    74  	return &Account{a.accounts[index]}, nil
    75  }
    76  
    77  //set在切片中的给定索引处设置帐户。
    78  func (a *Accounts) Set(index int, account *Account) error {
    79  	if index < 0 || index >= len(a.accounts) {
    80  		return errors.New("index out of bounds")
    81  	}
    82  	a.accounts[index] = account.account
    83  	return nil
    84  }
    85  
    86  //GetAddress检索与帐户关联的地址。
    87  func (a *Account) GetAddress() *Address {
    88  	return &Address{a.account.Address}
    89  }
    90  
    91  //GetURL检索帐户的规范URL。
    92  func (a *Account) GetURL() string {
    93  	return a.account.URL.String()
    94  }
    95  
    96  //keystore管理磁盘上的密钥存储目录。
    97  type KeyStore struct{ keystore *keystore.KeyStore }
    98  
    99  //newkeystore为给定目录创建一个keystore。
   100  func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore {
   101  	return &KeyStore{keystore: keystore.NewKeyStore(keydir, scryptN, scryptP)}
   102  }
   103  
   104  //hasAddress报告具有给定地址的密钥是否存在。
   105  func (ks *KeyStore) HasAddress(address *Address) bool {
   106  	return ks.keystore.HasAddress(address.address)
   107  }
   108  
   109  //getaccounts返回目录中存在的所有密钥文件。
   110  func (ks *KeyStore) GetAccounts() *Accounts {
   111  	return &Accounts{ks.keystore.Accounts()}
   112  }
   113  
   114  //如果密码短语正确,则删除帐户匹配的密钥。
   115  //如果a不包含文件名,则地址必须与唯一键匹配。
   116  func (ks *KeyStore) DeleteAccount(account *Account, passphrase string) error {
   117  	return ks.keystore.Delete(account.account, passphrase)
   118  }
   119  
   120  //signhash为给定哈希计算ECDSA签名。生成的签名
   121  //格式为[R V],其中V为0或1。
   122  func (ks *KeyStore) SignHash(address *Address, hash []byte) (signature []byte, _ error) {
   123  	return ks.keystore.SignHash(accounts.Account{Address: address.address}, common.CopyBytes(hash))
   124  }
   125  
   126  //signtx用请求的帐户对给定的事务进行签名。
   127  func (ks *KeyStore) SignTx(account *Account, tx *Transaction, chainID *BigInt) (*Transaction, error) {
   128  if chainID == nil { //从移动应用程序传递的空值
   129  		chainID = new(BigInt)
   130  	}
   131  	signed, err := ks.keystore.SignTx(account.account, tx.tx, chainID.bigint)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	return &Transaction{signed}, nil
   136  }
   137  
   138  //如果与给定地址匹配的私钥可以
   139  //用给定的密码短语解密。生成的签名在
   140  //[R_S_V]格式,其中V为0或1。
   141  func (ks *KeyStore) SignHashPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) {
   142  	return ks.keystore.SignHashWithPassphrase(account.account, passphrase, common.CopyBytes(hash))
   143  }
   144  
   145  //signtxpassphrase如果私钥与
   146  //给定的地址可以用给定的密码短语解密。
   147  func (ks *KeyStore) SignTxPassphrase(account *Account, passphrase string, tx *Transaction, chainID *BigInt) (*Transaction, error) {
   148  if chainID == nil { //从移动应用程序传递的空值
   149  		chainID = new(BigInt)
   150  	}
   151  	signed, err := ks.keystore.SignTxWithPassphrase(account.account, passphrase, tx.tx, chainID.bigint)
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  	return &Transaction{signed}, nil
   156  }
   157  
   158  //解锁无限期地解锁给定的帐户。
   159  func (ks *KeyStore) Unlock(account *Account, passphrase string) error {
   160  	return ks.keystore.TimedUnlock(account.account, passphrase, 0)
   161  }
   162  
   163  //lock从内存中删除具有给定地址的私钥。
   164  func (ks *KeyStore) Lock(address *Address) error {
   165  	return ks.keystore.Lock(address.address)
   166  }
   167  
   168  //timedunlock使用密码短语解锁给定的帐户。帐户保持不变
   169  //在超时期间解锁(纳秒)。超时0将解锁
   170  //帐户,直到程序退出。帐户必须与唯一的密钥文件匹配。
   171  //
   172  //如果帐户地址在一段时间内已解锁,则TimedUnlock将扩展或
   173  //缩短活动解锁超时。如果地址以前是解锁的
   174  //无限期地超时不会改变。
   175  func (ks *KeyStore) TimedUnlock(account *Account, passphrase string, timeout int64) error {
   176  	return ks.keystore.TimedUnlock(account.account, passphrase, time.Duration(timeout))
   177  }
   178  
   179  //newaccount生成一个新密钥并将其存储到密钥目录中,
   180  //用密码短语加密。
   181  func (ks *KeyStore) NewAccount(passphrase string) (*Account, error) {
   182  	account, err := ks.keystore.NewAccount(passphrase)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	return &Account{account}, nil
   187  }
   188  
   189  //更新帐户更改现有帐户的密码。
   190  func (ks *KeyStore) UpdateAccount(account *Account, passphrase, newPassphrase string) error {
   191  	return ks.keystore.Update(account.account, passphrase, newPassphrase)
   192  }
   193  
   194  //exportkey作为json密钥导出,用newpassphrase加密。
   195  func (ks *KeyStore) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) {
   196  	return ks.keystore.Export(account.account, passphrase, newPassphrase)
   197  }
   198  
   199  //importkey将给定的加密JSON密钥存储到密钥目录中。
   200  func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) {
   201  	acc, err := ks.keystore.Import(common.CopyBytes(keyJSON), passphrase, newPassphrase)
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  	return &Account{acc}, nil
   206  }
   207  
   208  //importecdsakey将给定的加密JSON密钥存储到密钥目录中。
   209  func (ks *KeyStore) ImportECDSAKey(key []byte, passphrase string) (account *Account, _ error) {
   210  	privkey, err := crypto.ToECDSA(common.CopyBytes(key))
   211  	if err != nil {
   212  		return nil, err
   213  	}
   214  	acc, err := ks.keystore.ImportECDSA(privkey, passphrase)
   215  	if err != nil {
   216  		return nil, err
   217  	}
   218  	return &Account{acc}, nil
   219  }
   220  
   221  //importpresalekey解密给定的以太坊预售钱包和商店
   222  //密钥目录中的密钥文件。密钥文件使用相同的密码短语加密。
   223  func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) {
   224  	account, err := ks.keystore.ImportPreSaleKey(common.CopyBytes(keyJSON), passphrase)
   225  	if err != nil {
   226  		return nil, err
   227  	}
   228  	return &Account{account}, nil
   229  }