github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/mobile/accounts.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Contains all the wrappers from the accounts package to support client side key 18 // management on mobile platforms. 19 20 package gvnt 21 22 import ( 23 "errors" 24 "time" 25 26 "github.com/vntchain/go-vnt/accounts" 27 "github.com/vntchain/go-vnt/accounts/keystore" 28 "github.com/vntchain/go-vnt/common" 29 "github.com/vntchain/go-vnt/crypto" 30 ) 31 32 const ( 33 // StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB 34 // memory and taking approximately 1s CPU time on a modern processor. 35 StandardScryptN = int(keystore.StandardScryptN) 36 37 // StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB 38 // memory and taking approximately 1s CPU time on a modern processor. 39 StandardScryptP = int(keystore.StandardScryptP) 40 41 // LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB 42 // memory and taking approximately 100ms CPU time on a modern processor. 43 LightScryptN = int(keystore.LightScryptN) 44 45 // LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB 46 // memory and taking approximately 100ms CPU time on a modern processor. 47 LightScryptP = int(keystore.LightScryptP) 48 ) 49 50 // Account represents a stored key. 51 type Account struct{ account accounts.Account } 52 53 // Accounts represents a slice of accounts. 54 type Accounts struct{ accounts []accounts.Account } 55 56 // Size returns the number of accounts in the slice. 57 func (a *Accounts) Size() int { 58 return len(a.accounts) 59 } 60 61 // Get returns the account at the given index from the slice. 62 func (a *Accounts) Get(index int) (account *Account, _ error) { 63 if index < 0 || index >= len(a.accounts) { 64 return nil, errors.New("index out of bounds") 65 } 66 return &Account{a.accounts[index]}, nil 67 } 68 69 // Set sets the account at the given index in the slice. 70 func (a *Accounts) Set(index int, account *Account) error { 71 if index < 0 || index >= len(a.accounts) { 72 return errors.New("index out of bounds") 73 } 74 a.accounts[index] = account.account 75 return nil 76 } 77 78 // GetAddress retrieves the address associated with the account. 79 func (a *Account) GetAddress() *Address { 80 return &Address{a.account.Address} 81 } 82 83 // GetURL retrieves the canonical URL of the account. 84 func (a *Account) GetURL() string { 85 return a.account.URL.String() 86 } 87 88 // KeyStore manages a key storage directory on disk. 89 type KeyStore struct{ keystore *keystore.KeyStore } 90 91 // NewKeyStore creates a keystore for the given directory. 92 func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore { 93 return &KeyStore{keystore: keystore.NewKeyStore(keydir, scryptN, scryptP)} 94 } 95 96 // NewKeyStoreWithSecureEnclave creates a keystore for the given directory. 97 func NewKeyStoreWithSecureEnclave(keyjson []byte) *KeyStore { 98 return &KeyStore{keystore: keystore.NewSecureEnclaveKeyStore(keyjson)} 99 } 100 101 // HasAddress reports whether a key with the given address is present. 102 func (ks *KeyStore) HasAddress(address *Address) bool { 103 return ks.keystore.HasAddress(address.address) 104 } 105 106 // GetAccounts returns all key files present in the directory. 107 func (ks *KeyStore) GetAccounts() *Accounts { 108 return &Accounts{ks.keystore.Accounts()} 109 } 110 111 // DeleteAccount deletes the key matched by account if the passphrase is correct. 112 // If a contains no filename, the address must match a unique key. 113 func (ks *KeyStore) DeleteAccount(account *Account, passphrase string) error { 114 return ks.keystore.Delete(account.account, passphrase) 115 } 116 117 // SignHash calculates a ECDSA signature for the given hash. The produced signature 118 // is in the [R || S || V] format where V is 0 or 1. 119 func (ks *KeyStore) SignHash(address *Address, hash []byte) (signature []byte, _ error) { 120 return ks.keystore.SignHash(accounts.Account{Address: address.address}, common.CopyBytes(hash)) 121 } 122 123 // SignTx signs the given transaction with the requested account. 124 func (ks *KeyStore) SignTx(account *Account, tx *Transaction, chainID *BigInt) (*Transaction, error) { 125 if chainID == nil { // Null passed from mobile app 126 chainID = new(BigInt) 127 } 128 signed, err := ks.keystore.SignTx(account.account, tx.tx, chainID.bigint) 129 if err != nil { 130 return nil, err 131 } 132 return &Transaction{signed}, nil 133 } 134 135 // SignHashPassphrase signs hash if the private key matching the given address can 136 // be decrypted with the given passphrase. The produced signature is in the 137 // [R || S || V] format where V is 0 or 1. 138 func (ks *KeyStore) SignHashPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) { 139 return ks.keystore.SignHashWithPassphrase(account.account, passphrase, common.CopyBytes(hash)) 140 } 141 142 // SignTxPassphrase signs the transaction if the private key matching the 143 // given address can be decrypted with the given passphrase. 144 func (ks *KeyStore) SignTxPassphrase(account *Account, passphrase string, tx *Transaction, chainID *BigInt) (*Transaction, error) { 145 if chainID == nil { // Null passed from mobile app 146 chainID = new(BigInt) 147 } 148 signed, err := ks.keystore.SignTxWithPassphrase(account.account, passphrase, tx.tx, chainID.bigint) 149 if err != nil { 150 return nil, err 151 } 152 return &Transaction{signed}, nil 153 } 154 155 // Unlock unlocks the given account indefinitely. 156 func (ks *KeyStore) Unlock(account *Account, passphrase string) error { 157 return ks.keystore.TimedUnlock(account.account, passphrase, 0) 158 } 159 160 // Lock removes the private key with the given address from memory. 161 func (ks *KeyStore) Lock(address *Address) error { 162 return ks.keystore.Lock(address.address) 163 } 164 165 // TimedUnlock unlocks the given account with the passphrase. The account stays 166 // unlocked for the duration of timeout (nanoseconds). A timeout of 0 unlocks the 167 // account until the program exits. The account must match a unique key file. 168 // 169 // If the account address is already unlocked for a duration, TimedUnlock extends or 170 // shortens the active unlock timeout. If the address was previously unlocked 171 // indefinitely the timeout is not altered. 172 func (ks *KeyStore) TimedUnlock(account *Account, passphrase string, timeout int64) error { 173 return ks.keystore.TimedUnlock(account.account, passphrase, time.Duration(timeout)) 174 } 175 176 // NewAccount generates a new key and stores it into the key directory, 177 // encrypting it with the passphrase. 178 func (ks *KeyStore) NewAccount(passphrase string) (*Account, error) { 179 account, err := ks.keystore.NewAccount(passphrase) 180 if err != nil { 181 return nil, err 182 } 183 return &Account{account}, nil 184 } 185 186 // UpdateAccount changes the passphrase of an existing account. 187 func (ks *KeyStore) UpdateAccount(account *Account, passphrase, newPassphrase string) error { 188 return ks.keystore.Update(account.account, passphrase, newPassphrase) 189 } 190 191 // ExportKey exports as a JSON key, encrypted with newPassphrase. 192 func (ks *KeyStore) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) { 193 return ks.keystore.Export(account.account, passphrase, newPassphrase) 194 } 195 196 // ImportKey stores the given encrypted JSON key into the key directory. 197 func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) { 198 acc, err := ks.keystore.Import(common.CopyBytes(keyJSON), passphrase, newPassphrase) 199 if err != nil { 200 return nil, err 201 } 202 return &Account{acc}, nil 203 } 204 205 // ImportECDSAKey stores the given encrypted JSON key into the key directory. 206 func (ks *KeyStore) ImportECDSAKey(key []byte, passphrase string) (account *Account, _ error) { 207 privkey, err := crypto.ToECDSA(common.CopyBytes(key)) 208 if err != nil { 209 return nil, err 210 } 211 acc, err := ks.keystore.ImportECDSA(privkey, passphrase) 212 if err != nil { 213 return nil, err 214 } 215 return &Account{acc}, nil 216 } 217 218 // ImportPreSaleKey decrypts the given VNT presale wallet and stores 219 // a key file in the key directory. The key file is encrypted with the same passphrase. 220 func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) { 221 account, err := ks.keystore.ImportPreSaleKey(common.CopyBytes(keyJSON), passphrase) 222 if err != nil { 223 return nil, err 224 } 225 return &Account{account}, nil 226 }