
org.xrpl.xrpl4j.keypairs.DefaultKeyPairService Maven / Gradle / Ivy
package org.xrpl.xrpl4j.keypairs;
/*-
* ========================LICENSE_START=================================
* xrpl4j :: keypairs
* %%
* Copyright (C) 2020 - 2022 XRPL Foundation and its contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
import com.google.common.collect.ImmutableMap;
import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray;
import org.xrpl.xrpl4j.codec.addresses.VersionType;
import java.util.Map;
import java.util.function.Supplier;
/**
* A {@link KeyPairService} that delegates to specific {@link KeyPairService} implementations.
*/
public class DefaultKeyPairService extends AbstractKeyPairService {
private static final KeyPairService INSTANCE = new DefaultKeyPairService();
/**
* A map of the existing {@link KeyPairService} implementations, keyed by {@link VersionType}.
*/
private static final Map> serviceMap =
new ImmutableMap.Builder>()
.put(VersionType.SECP256K1, Secp256k1KeyPairService::getInstance)
.put(VersionType.ED25519, Ed25519KeyPairService::getInstance)
.build();
private static KeyPairService getKeyPairServiceByType(VersionType type) {
return serviceMap.get(type).get();
}
/**
* Get a JVM wide instance of this {@link KeyPairService}.
*
* @return A {@link DefaultKeyPairService}.
*/
public static KeyPairService getInstance() {
return INSTANCE;
}
/**
* Generates a seed from the given entropy, encoded with {@link VersionType#ED25519}. Seeds can be generated
* using secp255k1, but all new seeds generated by this client use the ED25519 algorithm.
*
* @param entropy An {@link UnsignedByteArray} containing the bytes of entropy to encode into a seed.
*
* @return An ED25519 encoded 16 byte seed value, encoded in Base58Check.
*/
@Override
public String generateSeed(UnsignedByteArray entropy) {
return addressCodec.encodeSeed(entropy, VersionType.ED25519);
}
@Override
public KeyPair deriveKeyPair(String seed) {
return addressCodec.decodeSeed(seed).type()
.map(type -> DefaultKeyPairService.getKeyPairServiceByType(type).deriveKeyPair(seed))
.orElseThrow(() -> new IllegalArgumentException("Unsupported seed type."));
}
@Override
public String sign(UnsignedByteArray message, String privateKey) {
// ED25519 keys are prefixed with "ED" to make them 33 bytes.
VersionType privateKeyType = privateKey.startsWith("ED") ? VersionType.ED25519 : VersionType.SECP256K1;
return DefaultKeyPairService.getKeyPairServiceByType(privateKeyType).sign(message, privateKey);
}
@Override
public boolean verify(UnsignedByteArray message, String signature, String publicKey) {
// ED25519 keys are prefixed with "ED" to make them 33 bytes.
VersionType publicKeyType = publicKey.startsWith("ED") ? VersionType.ED25519 : VersionType.SECP256K1;
return DefaultKeyPairService.getKeyPairServiceByType(publicKeyType).verify(message, signature, publicKey);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy