Search in sources :

Example 1 with UnsignedByteArray

use of org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray in project xrpl4j by XRPLF.

the class SignatureUtilsTest method toSignableBytes.

@Test
public void toSignableBytes() throws JsonProcessingException {
    UnsignedByteArray actual = signatureUtils.toSignableBytes(transactionMock);
    assertThat(actual.length()).isEqualTo(1);
    verify(objectMapperMock).writeValueAsString(transactionMock);
    verifyNoMoreInteractions(objectMapperMock);
    verify(xrplBinaryCodecMock).encodeForSigning(anyString());
    verifyNoMoreInteractions(xrplBinaryCodecMock);
}
Also used : UnsignedByteArray(org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray) Test(org.junit.jupiter.api.Test)

Example 2 with UnsignedByteArray

use of org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray in project xrpl4j by XRPLF.

the class BcKeyUtils method toPublicKey.

/**
 * Convert from a {@link Ed25519PublicKeyParameters} to a {@link PublicKey}.
 *
 * @param ed25519PublicKeyParameters A {@link Ed25519PublicKeyParameters}.
 *
 * @return A {@link PublicKey}.
 */
public static PublicKey toPublicKey(final Ed25519PublicKeyParameters ed25519PublicKeyParameters) {
    Objects.requireNonNull(ed25519PublicKeyParameters);
    // XRPL ED25519 keys are prefixed with 0xED so that the keys are 33 bytes and match the length of sekp256k1 keys.
    // Bouncy Castle only deals with 32 byte keys, so we need to manually add the prefix
    UnsignedByteArray prefixedPublicKey = UnsignedByteArray.of(PrivateKey.PREFIX).append(UnsignedByteArray.of(ed25519PublicKeyParameters.getEncoded()));
    return PublicKey.builder().value(prefixedPublicKey).build();
}
Also used : UnsignedByteArray(org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray)

Example 3 with UnsignedByteArray

use of org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray in project xrpl4j by XRPLF.

the class SingleKeySignatureService method ecDsaSign.

@SuppressWarnings("checkstyle:LocalVariableName")
@Override
protected synchronized Signature ecDsaSign(final KeyMetadata keyMetadata, final UnsignedByteArray signableTransactionBytes) {
    Objects.requireNonNull(keyMetadata);
    Objects.requireNonNull(signableTransactionBytes);
    final UnsignedByteArray messageHash = HashUtils.sha512Half(signableTransactionBytes);
    final BigInteger privateKeyInt = new BigInteger(privateKey.base16Encoded(), 16);
    final ECPrivateKeyParameters parameters = new ECPrivateKeyParameters(privateKeyInt, Secp256k1.ecDomainParameters);
    ecdsaSigner.init(true, parameters);
    final BigInteger[] signatures = ecdsaSigner.generateSignature(messageHash.toByteArray());
    final BigInteger r = signatures[0];
    BigInteger s = signatures[1];
    final BigInteger otherS = Secp256k1.ecDomainParameters.getN().subtract(s);
    if (s.compareTo(otherS) > 0) {
        s = otherS;
    }
    final EcDsaSignature sig = EcDsaSignature.builder().r(r).s(s).build();
    UnsignedByteArray sigBytes = sig.der();
    return Signature.builder().value(sigBytes).build();
}
Also used : UnsignedByteArray(org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) EcDsaSignature(org.xrpl.xrpl4j.keypairs.EcDsaSignature) BigInteger(java.math.BigInteger)

Example 4 with UnsignedByteArray

use of org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray in project xrpl4j by XRPLF.

the class SingleKeySignatureService method edDsaSign.

@Override
protected synchronized Signature edDsaSign(final KeyMetadata privateKeyMetadata, final UnsignedByteArray signableTransactionBytes) {
    Objects.requireNonNull(privateKeyMetadata);
    Objects.requireNonNull(signableTransactionBytes);
    Ed25519PrivateKeyParameters privateKeyParameters = new Ed25519PrivateKeyParameters(// Remove ED prefix byte
    BaseEncoding.base16().decode(privateKey.base16Encoded().substring(2)), 0);
    ed25519Signer.reset();
    ed25519Signer.init(true, privateKeyParameters);
    ed25519Signer.update(signableTransactionBytes.toByteArray(), 0, signableTransactionBytes.getUnsignedBytes().size());
    final UnsignedByteArray sigBytes = UnsignedByteArray.of(ed25519Signer.generateSignature());
    return Signature.builder().value(sigBytes).build();
}
Also used : UnsignedByteArray(org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 5 with UnsignedByteArray

use of org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray in project xrpl4j by XRPLF.

the class AbstractSignatureService method signWithBehavior.

@Override
public Signature signWithBehavior(final KeyMetadata keyMetadata, final Transaction transaction, final SigningBehavior behavior) {
    Objects.requireNonNull(keyMetadata);
    Objects.requireNonNull(transaction);
    final PublicKey publicKey = this.getPublicKey(keyMetadata);
    final UnsignedByteArray signableTransactionBytes = behavior == SigningBehavior.SINGLE ? this.signatureUtils.toSignableBytes(transaction) : this.signatureUtils.toMultiSignableBytes(transaction, keyPairService.deriveAddress(publicKey.value()).value());
    final Signature signature;
    switch(publicKey.versionType()) {
        case ED25519:
            {
                signature = this.edDsaSign(keyMetadata, signableTransactionBytes);
                break;
            }
        case SECP256K1:
            {
                signature = this.ecDsaSign(keyMetadata, signableTransactionBytes);
                break;
            }
        default:
            {
                throw new IllegalArgumentException("Unhandled PrivateKey VersionType: {}" + keyMetadata);
            }
    }
    return signature;
}
Also used : UnsignedByteArray(org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray) PublicKey(org.xrpl.xrpl4j.crypto.PublicKey)

Aggregations

UnsignedByteArray (org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray)36 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 BigInteger (java.math.BigInteger)7 BinarySerializer (org.xrpl.xrpl4j.codec.binary.serdes.BinarySerializer)4 PublicKey (org.xrpl.xrpl4j.crypto.PublicKey)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)2 Ed25519PrivateKeyParameters (org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)2 Test (org.junit.jupiter.api.Test)2 UnsignedByte (org.xrpl.xrpl4j.codec.addresses.UnsignedByte)2 FieldInstance (org.xrpl.xrpl4j.codec.binary.definitions.FieldInstance)2 BinaryParser (org.xrpl.xrpl4j.codec.binary.serdes.BinaryParser)2 EcDsaSignature (org.xrpl.xrpl4j.keypairs.EcDsaSignature)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)1