Search in sources :

Example 1 with DeterministicKey

use of co.rsk.bitcoinj.crypto.DeterministicKey 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 2 with DeterministicKey

use of co.rsk.bitcoinj.crypto.DeterministicKey in project rskj by rsksmart.

the class DeriveExtendedPublicKeyPerformanceTestCase method estimateDeriveExtendedPublicKey.

private ExecutionStats estimateDeriveExtendedPublicKey(int times, int pathLength, EnvironmentBuilder environmentBuilder) throws VMException {
    String name = String.format("%s-%d", function.name, pathLength);
    ExecutionStats stats = new ExecutionStats(name);
    Random rnd = new Random();
    byte[] chainCode = new byte[32];
    NetworkParameters networkParameters = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
    ABIEncoder abiEncoder = (int executionIndex) -> {
        rnd.nextBytes(chainCode);
        DeterministicKey key = HDKeyDerivation.createMasterPubKeyFromBytes(new ECKey().getPubKey(true), chainCode);
        int[] pathParts = new int[pathLength];
        for (int i = 0; i < pathLength; i++) {
            pathParts[i] = rnd.nextInt(MAX_CHILD);
        }
        String path = String.join("/", Arrays.stream(pathParts).mapToObj(i -> String.format("%d", i)).collect(Collectors.toList()));
        return function.encode(new Object[] { key.serializePubB58(networkParameters), path });
    };
    executeAndAverage(name, times, environmentBuilder, abiEncoder, Helper.getZeroValueTxBuilder(new ECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
        Object[] decodedResult = function.decodeResult(result);
        Assert.assertEquals(String.class, decodedResult[0].getClass());
        String address = (String) decodedResult[0];
        Assert.assertTrue(address.startsWith("xpub"));
    });
    return stats;
}
Also used : PrecompiledContractPerformanceTestCase(co.rsk.peg.performance.PrecompiledContractPerformanceTestCase) HDKeyDerivation(co.rsk.bitcoinj.crypto.HDKeyDerivation) ExecutionStats(co.rsk.peg.performance.ExecutionStats) VMException(org.ethereum.vm.exception.VMException) Arrays(java.util.Arrays) CallTransaction(org.ethereum.core.CallTransaction) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) Test(org.junit.Test) Random(java.util.Random) Collectors(java.util.stream.Collectors) CombinedExecutionStats(co.rsk.peg.performance.CombinedExecutionStats) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) TestSystemProperties(co.rsk.config.TestSystemProperties) Ignore(org.junit.Ignore) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) Assert(org.junit.Assert) ECKey(org.ethereum.crypto.ECKey) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) ECKey(org.ethereum.crypto.ECKey) Random(java.util.Random) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) ExecutionStats(co.rsk.peg.performance.ExecutionStats) CombinedExecutionStats(co.rsk.peg.performance.CombinedExecutionStats)

Example 3 with DeterministicKey

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

Example 4 with DeterministicKey

use of co.rsk.bitcoinj.crypto.DeterministicKey in project rskj by rsksmart.

the class ExtractPublicKeyFromExtendedPublicKeyPerformanceTestCase method estimateExtractPublicKeyFromExtendedPublicKey.

private ExecutionStats estimateExtractPublicKeyFromExtendedPublicKey(int times, EnvironmentBuilder environmentBuilder) throws VMException {
    ExecutionStats stats = new ExecutionStats(function.name);
    Random rnd = new Random();
    byte[] chainCode = new byte[32];
    NetworkParameters networkParameters = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
    byte[] publicKey = new ECKey().getPubKey(true);
    String expectedHexPublicKey = ByteUtil.toHexString(publicKey);
    ABIEncoder abiEncoder = (int executionIndex) -> {
        rnd.nextBytes(chainCode);
        DeterministicKey key = HDKeyDerivation.createMasterPubKeyFromBytes(publicKey, chainCode);
        return function.encode(new Object[] { key.serializePubB58(networkParameters) });
    };
    executeAndAverage(function.name, times, environmentBuilder, abiEncoder, Helper.getZeroValueTxBuilder(new ECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
        Object[] decodedResult = function.decodeResult(result);
        Assert.assertEquals(byte[].class, decodedResult[0].getClass());
        String hexPublicKey = ByteUtil.toHexString((byte[]) decodedResult[0]);
        Assert.assertEquals(expectedHexPublicKey, hexPublicKey);
    });
    return stats;
}
Also used : Random(java.util.Random) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) ECKey(org.ethereum.crypto.ECKey) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) ExecutionStats(co.rsk.peg.performance.ExecutionStats)

Aggregations

NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)4 DeterministicKey (co.rsk.bitcoinj.crypto.DeterministicKey)4 NativeContractIllegalArgumentException (co.rsk.pcc.exception.NativeContractIllegalArgumentException)2 ExecutionStats (co.rsk.peg.performance.ExecutionStats)2 Random (java.util.Random)2 ECKey (org.ethereum.crypto.ECKey)2 ChildNumber (co.rsk.bitcoinj.crypto.ChildNumber)1 HDKeyDerivation (co.rsk.bitcoinj.crypto.HDKeyDerivation)1 TestSystemProperties (co.rsk.config.TestSystemProperties)1 CombinedExecutionStats (co.rsk.peg.performance.CombinedExecutionStats)1 PrecompiledContractPerformanceTestCase (co.rsk.peg.performance.PrecompiledContractPerformanceTestCase)1 Arrays (java.util.Arrays)1 Collectors (java.util.stream.Collectors)1 CallTransaction (org.ethereum.core.CallTransaction)1 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)1 VMException (org.ethereum.vm.exception.VMException)1 Assert (org.junit.Assert)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1