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