Search in sources :

Example 1 with NativeContractIllegalArgumentException

use of co.rsk.pcc.exception.NativeContractIllegalArgumentException in project rskj by rsksmart.

the class GetUncleCoinbaseAddress method internalExecute.

@Override
protected Object internalExecute(Block block, Object[] arguments) throws NativeContractIllegalArgumentException {
    List<BlockHeader> uncles = block.getUncleList();
    if (uncles == null) {
        return ByteUtil.EMPTY_BYTE_ARRAY;
    }
    short uncleIndex;
    try {
        uncleIndex = ((BigInteger) arguments[1]).shortValueExact();
    } catch (ArithmeticException e) {
        return ByteUtil.EMPTY_BYTE_ARRAY;
    }
    if (uncleIndex < 0) {
        throw new NativeContractIllegalArgumentException(String.format("Invalid uncle index '%d' (should be a non-negative value)", uncleIndex));
    }
    if (uncleIndex >= uncles.size()) {
        return ByteUtil.EMPTY_BYTE_ARRAY;
    }
    BlockHeader uncle = uncles.get(uncleIndex);
    return uncle.getCoinbase().getBytes();
}
Also used : NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException) BlockHeader(org.ethereum.core.BlockHeader)

Example 2 with NativeContractIllegalArgumentException

use of co.rsk.pcc.exception.NativeContractIllegalArgumentException in project rskj by rsksmart.

the class DeriveExtendedPublicKey method execute.

@Override
public Object execute(Object[] arguments) throws NativeContractIllegalArgumentException {
    if (arguments == null) {
        throw new NativeContractIllegalArgumentException("Must provide xpub and path arguments. None was provided");
    }
    String xpub = (String) arguments[0];
    String path = (String) arguments[1];
    NetworkParameters params = helper.validateAndExtractNetworkFromExtendedPublicKey(xpub);
    DeterministicKey key;
    try {
        key = DeterministicKey.deserializeB58(xpub, params);
    } catch (IllegalArgumentException e) {
        throw new NativeContractIllegalArgumentException("Invalid extended public key", e);
    }
    // just in case.
    if (path == null || path.length() == 0 || !isDecimal(path.substring(0, 1)) || !isDecimal(path.substring(path.length() - 1, path.length()))) {
        throwInvalidPath(path);
    }
    String[] pathChunks = path.split("/");
    if (pathChunks.length > 10) {
        throw new NativeContractIllegalArgumentException("Path should contain 10 levels at most");
    }
    if (Arrays.stream(pathChunks).anyMatch(s -> !isDecimal(s))) {
        throwInvalidPath(path);
    }
    List<ChildNumber> pathList;
    try {
        pathList = HDUtils.parsePath(path);
    } catch (NumberFormatException ex) {
        throw new NativeContractIllegalArgumentException(getInvalidPathErrorMessage(path), ex);
    }
    DeterministicKey derived = key;
    for (ChildNumber pathItem : pathList) {
        derived = HDKeyDerivation.deriveChildKey(derived, pathItem.getI());
    }
    return derived.serializePubB58(params);
}
Also used : NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException) ChildNumber(co.rsk.bitcoinj.crypto.ChildNumber) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException)

Example 3 with NativeContractIllegalArgumentException

use of co.rsk.pcc.exception.NativeContractIllegalArgumentException in project rskj by rsksmart.

the class GetMultisigScriptHash method execute.

@Override
public Object execute(Object[] arguments) throws NativeContractIllegalArgumentException {
    if (arguments == null || arguments[0] == null) {
        throw new NativeContractIllegalArgumentException(REQUIRED_SIGNATURE_NULL_OR_ZERO);
    }
    int minimumSignatures = ((BigInteger) arguments[0]).intValueExact();
    Object[] publicKeys = (Object[]) arguments[1];
    if (minimumSignatures <= 0) {
        throw new NativeContractIllegalArgumentException(REQUIRED_SIGNATURE_NULL_OR_ZERO);
    }
    if (publicKeys == null || publicKeys.length < MINIMUM_REQUIRED_KEYS) {
        throw new NativeContractIllegalArgumentException(PUBLIC_KEYS_NULL_OR_ONE);
    }
    if (publicKeys.length < minimumSignatures) {
        throw new NativeContractIllegalArgumentException(String.format(INVALID_REQUIRED_SIGNATURE_AND_PUBLIC_KEYS_PAIR, publicKeys.length, minimumSignatures));
    }
    if (publicKeys.length > MAXIMUM_ALLOWED_KEYS) {
        throw new NativeContractIllegalArgumentException(String.format("Given public keys (%d) are more than the maximum allowed signatures (%d)", publicKeys.length, MAXIMUM_ALLOWED_KEYS));
    }
    List<BtcECKey> btcPublicKeys = new ArrayList<>();
    for (Object o : publicKeys) {
        byte[] publicKey = (byte[]) o;
        if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH && publicKey.length != UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
            throw new NativeContractIllegalArgumentException(String.format("Invalid public key length: %d", publicKey.length));
        }
        // Avoid extra work by not recalculating compressed keys on already compressed keys
        try {
            BtcECKey btcPublicKey = BtcECKey.fromPublicOnly(publicKey);
            if (publicKey.length == UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
                btcPublicKey = BtcECKey.fromPublicOnly(btcPublicKey.getPubKeyPoint().getEncoded(true));
            }
            btcPublicKeys.add(btcPublicKey);
        } catch (IllegalArgumentException e) {
            throw new NativeContractIllegalArgumentException(String.format("Invalid public key format: %s", ByteUtil.toHexString(publicKey)), e);
        }
    }
    Script multisigScript = ScriptBuilder.createP2SHOutputScript(minimumSignatures, btcPublicKeys);
    return multisigScript.getPubKeyHash();
}
Also used : Script(co.rsk.bitcoinj.script.Script) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException)

Example 4 with NativeContractIllegalArgumentException

use of co.rsk.pcc.exception.NativeContractIllegalArgumentException in project rskj by rsksmart.

the class NativeContract method execute.

@Override
public byte[] execute(byte[] data) throws VMException {
    try {
        // Preliminary validation: we need an execution environment
        if (executionEnvironment == null) {
            throw new VMException("Execution environment is null");
        }
        // Preliminary validation: the transaction on which we execute cannot be null
        if (executionEnvironment.getTransaction() == null) {
            throw new VMException("RSK Transaction is null");
        }
        Optional<NativeMethod.WithArguments> methodWithArguments = parseData(data);
        // No function found with the given data? => halt!
        if (!methodWithArguments.isPresent()) {
            String errorMessage = String.format("Invalid data given: %s.", ByteUtil.toHexString(data));
            logger.info(errorMessage);
            throw new NativeContractIllegalArgumentException(errorMessage);
        }
        NativeMethod method = methodWithArguments.get().getMethod();
        if (!executionEnvironment.isLocalCall() && method.onlyAllowsLocalCalls()) {
            String errorMessage = String.format("Non-local-call to %s. Returning without execution.", method.getName());
            logger.info(errorMessage);
            throw new NativeContractIllegalArgumentException(errorMessage);
        }
        before();
        Object result;
        try {
            result = methodWithArguments.get().execute();
        } catch (NativeContractIllegalArgumentException ex) {
            String errorMessage = String.format("Error executing: %s", method.getName());
            logger.warn(errorMessage, ex);
            throw new NativeContractIllegalArgumentException(errorMessage, ex);
        }
        after();
        // Note: this is hacky, but ultimately very short and to the point.
        if (result == null) {
            return null;
        } else if (result.getClass() == Optional.class) {
            Optional<?> optionalResult = (Optional<?>) result;
            if (!optionalResult.isPresent()) {
                return null;
            } else {
                result = optionalResult.get();
            }
        }
        return method.getFunction().encodeOutputs(result);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        panicProcessor.panic("nativecontractexecute", ex.getMessage());
        throw new VMException(String.format("Exception executing native contract: %s", ex.getMessage()), ex);
    }
}
Also used : Optional(java.util.Optional) VMException(org.ethereum.vm.exception.VMException) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException) VMException(org.ethereum.vm.exception.VMException)

Example 5 with NativeContractIllegalArgumentException

use of co.rsk.pcc.exception.NativeContractIllegalArgumentException in project rskj by rsksmart.

the class ExtractPublicKeyFromExtendedPublicKey method execute.

@Override
public Object execute(Object[] arguments) throws NativeContractIllegalArgumentException {
    if (arguments == null) {
        throw new NativeContractIllegalArgumentException(String.format(INVALID_EXTENDED_PUBLIC_KEY, null));
    }
    String xpub = (String) arguments[0];
    NetworkParameters params = helper.validateAndExtractNetworkFromExtendedPublicKey(xpub);
    DeterministicKey key;
    try {
        key = DeterministicKey.deserializeB58(xpub, params);
    } catch (IllegalArgumentException e) {
        throw new NativeContractIllegalArgumentException(String.format(INVALID_EXTENDED_PUBLIC_KEY, xpub), e);
    }
    return key.getPubKeyPoint().getEncoded(true);
}
Also used : NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) NativeContractIllegalArgumentException(co.rsk.pcc.exception.NativeContractIllegalArgumentException)

Aggregations

NativeContractIllegalArgumentException (co.rsk.pcc.exception.NativeContractIllegalArgumentException)5 NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)2 DeterministicKey (co.rsk.bitcoinj.crypto.DeterministicKey)2 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)1 ChildNumber (co.rsk.bitcoinj.crypto.ChildNumber)1 Script (co.rsk.bitcoinj.script.Script)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 BlockHeader (org.ethereum.core.BlockHeader)1 VMException (org.ethereum.vm.exception.VMException)1