Search in sources :

Example 1 with ByteArray

use of org.aion.rpc.types.RPCTypes.ByteArray in project aion by aionnetwork.

the class PersonalRPCImplTest method executePersonal_ecRecover.

@Test
public void executePersonal_ecRecover() {
    ECKey ecKey = ECKeyFac.inst().create();
    String helloMessage = "Hello World";
    ByteArray helloByteMessage = ByteArray.wrap(helloMessage.getBytes());
    // Create the signed message
    ByteArray signedMessage = ByteArray.wrap(ecKey.sign(helloByteMessage.toBytes()).toBytes());
    String pubKey = ByteUtil.toHexString(ecKey.getAddress());
    // well formed request
    Request request = new Request(idGenerator.generateID(), ecRecoverMethod, EcRecoverParamsConverter.encode(new EcRecoverParams(helloByteMessage, signedMessage)), VersionType.Version2);
    assertEquals(pubKey, execute(request, AddressConverter::decode).toString());
    // incorrect method name
    request = new Request(idGenerator.generateID(), ecRecoverMethod + "y", EcRecoverParamsConverter.encode(new EcRecoverParams(helloByteMessage, signedMessage)), VersionType.Version2);
    try {
        execute(request, AddressConverter::decode);
        fail();
    } catch (MethodNotFoundRPCException e) {
    }
    // incorrect params
    request = new Request(idGenerator.generateID(), ecRecoverMethod, ParamUnion.wrap(new VoidParams()).encode(), VersionType.Version2);
    try {
        execute(request, AddressConverter::decode);
        fail();
    } catch (InvalidParamsRPCException e) {
    }
}
Also used : MethodNotFoundRPCException(org.aion.rpc.errors.RPCExceptions.MethodNotFoundRPCException) EcRecoverParams(org.aion.rpc.types.RPCTypes.EcRecoverParams) InvalidParamsRPCException(org.aion.rpc.errors.RPCExceptions.InvalidParamsRPCException) Request(org.aion.rpc.types.RPCTypes.Request) AddressConverter(org.aion.rpc.types.RPCTypesConverter.AddressConverter) VoidParams(org.aion.rpc.types.RPCTypes.VoidParams) ByteArray(org.aion.rpc.types.RPCTypes.ByteArray) ECKey(org.aion.crypto.ECKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with ByteArray

use of org.aion.rpc.types.RPCTypes.ByteArray in project aion by aionnetwork.

the class PersonalRPCImplTest method personal_ecRecover.

@Test
public void personal_ecRecover() {
    ECKey ecKey = ECKeyFac.inst().create();
    String helloMessage = "Hello World";
    String byeMessage = "Bye World";
    ByteArray helloByteMessage = ByteArray.wrap(helloMessage.getBytes());
    ByteArray byeByteMessage = ByteArray.wrap(byeMessage.getBytes());
    // Create the signed message
    ByteArray signedMessage = ByteArray.wrap(ecKey.sign(helloByteMessage.toBytes()).toBytes());
    // append 0x to make params valid
    String hexString = "0x" + ByteUtil.toHexString(signedMessage.toBytes());
    String pubKey = "0x" + ByteUtil.toHexString(ecKey.getAddress());
    // recover public key and validate that the message was signed
    AionAddress recoveredKey = rpc.personal_ecRecover(helloByteMessage, DataHexStringConverter.decode(hexString));
    System.out.printf("Public Key: %s %nRecovered Key: %s%n ", DataHexStringConverter.decode(pubKey), recoveredKey);
    assertEquals(AddressConverter.decode(pubKey), recoveredKey);
    // attempt to recover the public key and fail since the incorrect message was used
    recoveredKey = rpc.personal_ecRecover(byeByteMessage, DataHexStringConverter.decode(hexString));
    System.out.printf("Public Key: %s %nRecovered Key: %s%n ", DataHexStringConverter.decode(pubKey), recoveredKey);
    assertNotEquals(AddressConverter.decode(pubKey), recoveredKey);
    try {
        // attempt to recover a pk with an incorrect signature
        rpc.personal_ecRecover(helloByteMessage, DataHexStringConverter.decode(hexString + "1"));
        fail();
    } catch (Exception e) {
    // We expect an exception
    }
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArray(org.aion.rpc.types.RPCTypes.ByteArray) ECKey(org.aion.crypto.ECKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MethodNotFoundRPCException(org.aion.rpc.errors.RPCExceptions.MethodNotFoundRPCException) InvalidParamsRPCException(org.aion.rpc.errors.RPCExceptions.InvalidParamsRPCException) Test(org.junit.Test)

Aggregations

ECKey (org.aion.crypto.ECKey)2 InvalidParamsRPCException (org.aion.rpc.errors.RPCExceptions.InvalidParamsRPCException)2 MethodNotFoundRPCException (org.aion.rpc.errors.RPCExceptions.MethodNotFoundRPCException)2 ByteArray (org.aion.rpc.types.RPCTypes.ByteArray)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 EcRecoverParams (org.aion.rpc.types.RPCTypes.EcRecoverParams)1 Request (org.aion.rpc.types.RPCTypes.Request)1 VoidParams (org.aion.rpc.types.RPCTypes.VoidParams)1 AddressConverter (org.aion.rpc.types.RPCTypesConverter.AddressConverter)1 AionAddress (org.aion.types.AionAddress)1