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);
}
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;
}
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);
}
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;
}
Aggregations