Search in sources :

Example 1 with ChildNumber

use of co.rsk.bitcoinj.crypto.ChildNumber 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)

Aggregations

NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)1 ChildNumber (co.rsk.bitcoinj.crypto.ChildNumber)1 DeterministicKey (co.rsk.bitcoinj.crypto.DeterministicKey)1 NativeContractIllegalArgumentException (co.rsk.pcc.exception.NativeContractIllegalArgumentException)1