use of com.github.dedis.popstellar.utility.error.keys.KeyGenerationException in project popstellar by dedis.
the class Wallet method generateKeyFromPath.
/**
* Generate a PoPToken (i.e. a key pair) from a given path.
*
* @param path a String path of the form: m/i/j/k/... where i,j,k,.. are 31-bit integer.
* @return the generated PoP Token
* @throws KeyGenerationException if an error occurs
* @throws UninitializedWalletException if the wallet is not initialized with a seed
*/
private PoPToken generateKeyFromPath(@NonNull String path) throws KeyGenerationException, UninitializedWalletException {
if (!isSetUp()) {
throw new UninitializedWalletException();
}
// convert the path string in an array of int
int[] pathValueInt = Arrays.stream(path.split("/")).skip(// remove the first element ('m')
1).mapToInt(Integer::parseInt).toArray();
try {
// derive private and public key
byte[] privateKey = SLIP10.deriveEd25519PrivateKey(aead.decrypt(seed, new byte[0]), pathValueInt);
Ed25519PrivateKeyParameters prK = new Ed25519PrivateKeyParameters(privateKey, 0);
Ed25519PublicKeyParameters puK = prK.generatePublicKey();
byte[] publicKey = puK.getEncoded();
return new PoPToken(privateKey, publicKey);
} catch (GeneralSecurityException e) {
throw new KeyGenerationException(e);
}
}
use of com.github.dedis.popstellar.utility.error.keys.KeyGenerationException in project popstellar by dedis.
the class KeyManagerTest method popTokenRetrievingFailsWhenWalletFails.
@Test
public void popTokenRetrievingFailsWhenWalletFails() throws KeyException {
PoPToken token = Base64DataUtils.generatePoPToken();
// create LAO and RollCall
Lao lao = new Lao("lao", Base64DataUtils.generatePublicKey(), 54213424);
RollCall rollCall = new RollCall(lao.getId(), 5421364, "rollcall");
lao.updateRollCall(rollCall.getId(), rollCall);
KeyManager manager = new KeyManager(androidKeysetManager, wallet);
// Test with every possible errors
when(wallet.recoverKey(any(), any(), any())).thenThrow(new KeyGenerationException(new GeneralSecurityException()));
assertThrows(KeyGenerationException.class, () -> manager.getValidPoPToken(lao));
verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
reset(wallet);
when(wallet.recoverKey(any(), any(), any())).thenThrow(new UninitializedWalletException());
assertThrows(UninitializedWalletException.class, () -> manager.getValidPoPToken(lao));
verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
reset(wallet);
when(wallet.recoverKey(any(), any(), any())).thenThrow(new InvalidPoPTokenException(token));
assertThrows(InvalidPoPTokenException.class, () -> manager.getValidPoPToken(lao));
verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
}
Aggregations