accounts, internal, mobile: polish accounts API, extend Android tests

This commit is contained in:
Péter Szilágyi
2016-12-08 14:42:31 +02:00
parent bbce726c8a
commit b37d175e59
5 changed files with 54 additions and 28 deletions

View File

@ -109,15 +109,17 @@ func (am *AccountManager) DeleteAccount(account *Account, passphrase string) err
}, passphrase)
}
// Sign signs hash with an unlocked private key matching the given address.
// Sign calculates a ECDSA signature for the given hash. The produced signature
// is in the [R || S || V] format where V is 0 or 1.
func (am *AccountManager) Sign(address *Address, hash []byte) (signature []byte, _ error) {
return am.manager.Sign(address.address, hash)
}
// SignWithPassphrase signs hash if the private key matching the given address can be
// decrypted with the given passphrase.
func (am *AccountManager) SignWithPassphrase(address *Address, passphrase string, hash []byte) (signature []byte, _ error) {
return am.manager.SignWithPassphrase(address.address, passphrase, hash)
// SignWithPassphrase signs hash if the private key matching the given address
// can be decrypted with the given passphrase. The produced signature is in the
// [R || S || V] format where V is 0 or 1.
func (am *AccountManager) SignWithPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) {
return am.manager.SignWithPassphrase(account.account, passphrase, hash)
}
// Unlock unlocks the given account indefinitely.
@ -130,15 +132,15 @@ func (am *AccountManager) Lock(address *Address) error {
return am.manager.Lock(address.address)
}
// TimedUnlock unlocks the given account with the passphrase. The account
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
// until the program exits. The account must match a unique key file.
// TimedUnlock unlocks the given account with the passphrase. The account stays
// unlocked for the duration of timeout (nanoseconds). A timeout of 0 unlocks the
// account until the program exits. The account must match a unique key file.
//
// If the account address is already unlocked for a duration, TimedUnlock extends or
// shortens the active unlock timeout. If the address was previously unlocked
// indefinitely the timeout is not altered.
func (am *AccountManager) TimedUnlock(a *Account, passphrase string, timeout int64) error {
return am.manager.TimedUnlock(a.account, passphrase, time.Duration(timeout))
func (am *AccountManager) TimedUnlock(account *Account, passphrase string, timeout int64) error {
return am.manager.TimedUnlock(account.account, passphrase, time.Duration(timeout))
}
// NewAccount generates a new key and stores it into the key directory,
@ -165,8 +167,8 @@ func (am *AccountManager) ImportKey(keyJSON []byte, passphrase, newPassphrase st
return &Account{acc}, nil
}
// Update changes the passphrase of an existing account.
func (am *AccountManager) Update(account *Account, passphrase, newPassphrase string) error {
// UpdateAccount changes the passphrase of an existing account.
func (am *AccountManager) UpdateAccount(account *Account, passphrase, newPassphrase string) error {
return am.manager.Update(account.account, passphrase, newPassphrase)
}

View File

@ -14,9 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains all the wrappers from the accounts package to support client side key
// management on mobile platforms.
package geth
import (
@ -46,14 +43,42 @@ public class AndroidTest extends InstrumentationTestCase {
public AndroidTest() {}
public void testAccountManagement() {
try {
AccountManager am = new AccountManager(getInstrumentation().getContext().getFilesDir() + "/keystore", Geth.LightScryptN, Geth.LightScryptP);
// Create an encrypted keystore manager with light crypto parameters.
AccountManager am = new AccountManager(getInstrumentation().getContext().getFilesDir() + "/keystore", Geth.LightScryptN, Geth.LightScryptP);
try {
// Create a new account with the specified encryption passphrase.
Account newAcc = am.newAccount("Creation password");
// Export the newly created account with a different passphrase. The returned
// data from this method invocation is a JSON encoded, encrypted key-file.
byte[] jsonAcc = am.exportKey(newAcc, "Creation password", "Export password");
am.deleteAccount(newAcc, "Creation password");
// Update the passphrase on the account created above inside the local keystore.
am.updateAccount(newAcc, "Creation password", "Update password");
// Delete the account updated above from the local keystore.
am.deleteAccount(newAcc, "Update password");
// Import back the account we've exported (and then deleted) above with yet
// again a fresh passphrase.
Account impAcc = am.importKey(jsonAcc, "Export password", "Import password");
// Create a new account to sign transactions with
Account signer = am.newAccount("Signer password");
Hash txHash = new Hash("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
// Sign a transaction with a single authorization
byte[] signature = am.signWithPassphrase(signer, "Signer password", txHash.getBytes());
// Sign a transaction with multiple manually cancelled authorizations
am.unlock(signer, "Signer password");
signature = am.sign(signer.getAddress(), txHash.getBytes());
am.lock(signer.getAddress());
// Sign a transaction with multiple automatically cancelled authorizations
am.timedUnlock(signer, "Signer password", 1000000000);
signature = am.sign(signer.getAddress(), txHash.getBytes());
} catch (Exception e) {
fail(e.toString());
}