github.com/core-coin/go-core/v2@v2.1.9/mobile/accounts.go (about) 1 // Copyright 2016 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-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 go-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 go-core 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 gocore 21 22 import ( 23 "errors" 24 "time" 25 26 "github.com/core-coin/go-core/v2/accounts" 27 "github.com/core-coin/go-core/v2/accounts/keystore" 28 "github.com/core-coin/go-core/v2/common" 29 "github.com/core-coin/go-core/v2/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 // HasAddress reports whether a key with the given address is present. 97 func (ks *KeyStore) HasAddress(address *Address) bool { 98 return ks.keystore.HasAddress(address.address) 99 } 100 101 // GetAccounts returns all key files present in the directory. 102 func (ks *KeyStore) GetAccounts() *Accounts { 103 return &Accounts{ks.keystore.Accounts()} 104 } 105 106 // DeleteAccount deletes the key matched by account if the passphrase is correct. 107 // If a contains no filename, the address must match a unique key. 108 func (ks *KeyStore) DeleteAccount(account *Account, passphrase string) error { 109 return ks.keystore.Delete(account.account, passphrase) 110 } 111 112 // SignHash calculates a EDDSA signature for the given hash. 113 func (ks *KeyStore) SignHash(address *Address, hash []byte) (signature []byte, _ error) { 114 return ks.keystore.SignHash(accounts.Account{Address: address.address}, common.CopyBytes(hash)) 115 } 116 117 // SignTx signs the given transaction with the requested account. 118 func (ks *KeyStore) SignTx(account *Account, tx *Transaction, networkID *BigInt) (*Transaction, error) { 119 if networkID == nil { // Null passed from mobile app 120 networkID = new(BigInt) 121 } 122 signed, err := ks.keystore.SignTx(account.account, tx.tx, networkID.bigint) 123 if err != nil { 124 return nil, err 125 } 126 return &Transaction{signed}, nil 127 } 128 129 // SignHashPassphrase signs hash if the private key matching the given address can 130 // be decrypted with the given passphrase. 131 func (ks *KeyStore) SignHashPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) { 132 return ks.keystore.SignHashWithPassphrase(account.account, passphrase, common.CopyBytes(hash)) 133 } 134 135 // SignTxPassphrase signs the transaction if the private key matching the 136 // given address can be decrypted with the given passphrase. 137 func (ks *KeyStore) SignTxPassphrase(account *Account, passphrase string, tx *Transaction, networkID *BigInt) (*Transaction, error) { 138 if networkID == nil { // Null passed from mobile app 139 networkID = new(BigInt) 140 } 141 signed, err := ks.keystore.SignTxWithPassphrase(account.account, passphrase, tx.tx, networkID.bigint) 142 if err != nil { 143 return nil, err 144 } 145 return &Transaction{signed}, nil 146 } 147 148 // Unlock unlocks the given account indefinitely. 149 func (ks *KeyStore) Unlock(account *Account, passphrase string) error { 150 return ks.keystore.TimedUnlock(account.account, passphrase, 0) 151 } 152 153 // Lock removes the private key with the given address from memory. 154 func (ks *KeyStore) Lock(address *Address) error { 155 return ks.keystore.Lock(address.address) 156 } 157 158 // TimedUnlock unlocks the given account with the passphrase. The account stays 159 // unlocked for the duration of timeout (nanoseconds). A timeout of 0 unlocks the 160 // account until the program exits. The account must match a unique key file. 161 // 162 // If the account address is already unlocked for a duration, TimedUnlock extends or 163 // shortens the active unlock timeout. If the address was previously unlocked 164 // indefinitely the timeout is not altered. 165 func (ks *KeyStore) TimedUnlock(account *Account, passphrase string, timeout int64) error { 166 return ks.keystore.TimedUnlock(account.account, passphrase, time.Duration(timeout)) 167 } 168 169 // NewAccount generates a new key and stores it into the key directory, 170 // encrypting it with the passphrase. 171 func (ks *KeyStore) NewAccount(passphrase string) (*Account, error) { 172 account, err := ks.keystore.NewAccount(passphrase) 173 if err != nil { 174 return nil, err 175 } 176 return &Account{account}, nil 177 } 178 179 // UpdateAccount changes the passphrase of an existing account. 180 func (ks *KeyStore) UpdateAccount(account *Account, passphrase, newPassphrase string) error { 181 return ks.keystore.Update(account.account, passphrase, newPassphrase) 182 } 183 184 // ExportKey exports as a JSON key, encrypted with newPassphrase. 185 func (ks *KeyStore) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) { 186 return ks.keystore.Export(account.account, passphrase, newPassphrase) 187 } 188 189 // ImportKey stores the given encrypted JSON key into the key directory. 190 func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) { 191 acc, err := ks.keystore.Import(common.CopyBytes(keyJSON), passphrase, newPassphrase) 192 if err != nil { 193 return nil, err 194 } 195 return &Account{acc}, nil 196 } 197 198 // ImportEDDSAKey stores the given encrypted JSON key into the key directory. 199 func (ks *KeyStore) ImportEDDSAKey(key []byte, passphrase string) (account *Account, _ error) { 200 privkey, err := crypto.UnmarshalPrivateKey(common.CopyBytes(key)) 201 if err != nil { 202 return nil, err 203 } 204 acc, err := ks.keystore.ImportEDDSA(privkey, passphrase) 205 if err != nil { 206 return nil, err 207 } 208 return &Account{acc}, nil 209 }