Search in sources :

Example 1 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class EncryptedMessageTest method testTypeScriptCompatibility.

@Test
public void testTypeScriptCompatibility() {
    // This unit test recreates a message encrypted in one of the typescript unit
    // tests.
    // Encryption should be the same between the 2 sdk. If one sdk is encrypting a
    // message,
    // the other sdk should be able to decrypted if it knows the right keys.
    // Although using the same encryption algorithm, outcome may be different if the
    // encoding
    // process is different. Both TS and Java are using utf-8 and hex encodings,
    KeyPair sender = KeyPair.fromPrivate(PrivateKey.fromHexString("2602F4236B199B3DF762B2AAB46FC3B77D8DDB214F0B62538D3827576C46C108"));
    KeyPair recipient = KeyPair.fromPrivate(PrivateKey.fromHexString("B72F2950498111BADF276D6D9D5E345F04E0D5C9B8342DA983C3395B4CF18F08"));
    String typescriptEncryptedKey = "079A490A7F68CC42F7156D12F082AF1ADC193FD8E3DA93CF67FA1D3F880D5DCEF2A9734EFE39646501023D1A9B63A44E57AEDE";
    EncryptedMessage encryptedMessage = new EncryptedMessage(typescriptEncryptedKey);
    String plainMessage = encryptedMessage.decryptPayload(sender.getPublicKey(), recipient.getPrivateKey());
    Assertions.assertEquals("test transaction 漢字", plainMessage);
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) Test(org.junit.jupiter.api.Test)

Example 2 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class EncryptedMessageTest method testCreateEncryptedMessage.

@Test
public void testCreateEncryptedMessage() {
    String message = "This is a plain message 漢字";
    KeyPair sender = KeyPair.random();
    KeyPair recipient = KeyPair.random();
    EncryptedMessage encryptedMessage = EncryptedMessage.create(message, sender.getPrivateKey(), recipient.getPublicKey());
    Assertions.assertEquals(MessageType.ENCRYPTED_MESSAGE, encryptedMessage.getType());
    String plainMessage = encryptedMessage.decryptPayload(sender.getPublicKey(), recipient.getPrivateKey());
    Assertions.assertEquals(message, plainMessage);
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) Test(org.junit.jupiter.api.Test)

Example 3 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class PersistentHarvestingDelegationMessageTest method testCreateEncryptedMessage.

@Test
public void testCreateEncryptedMessage() {
    KeyPair signing = KeyPair.random();
    KeyPair vrf = KeyPair.random();
    KeyPair remote = KeyPair.random();
    PersistentHarvestingDelegationMessage message = PersistentHarvestingDelegationMessage.create(signing.getPrivateKey(), vrf.getPrivateKey(), remote.getPublicKey());
    Assertions.assertTrue(message.getPayloadHex().startsWith(MessageMarker.PERSISTENT_DELEGATION_UNLOCK));
    Assertions.assertEquals(message.getText(), message.getPayloadHex());
    Assertions.assertEquals(PersistentHarvestingDelegationMessage.HEX_PAYLOAD_SIZE, message.getPayloadHex().length());
    System.out.println(message.getPayloadHex().length());
    Assertions.assertEquals(MessageType.PERSISTENT_HARVESTING_DELEGATION_MESSAGE, message.getType());
    HarvestingKeys plainMessage = message.decryptPayload(remote.getPrivateKey());
    Assertions.assertEquals(signing.getPrivateKey(), plainMessage.getSigningPrivateKey());
    Assertions.assertEquals(vrf.getPrivateKey(), plainMessage.getVrfPrivateKey());
    Optional<Message> message2 = Message.createFromHexPayload(message.getPayloadHex());
    Assertions.assertEquals(message, message2.get());
    Assertions.assertTrue(message2.get() instanceof PersistentHarvestingDelegationMessage);
    Optional<Message> encryptedMessage3 = Message.createFromPayload(message.getPayloadByteBuffer().array());
    Assertions.assertEquals(message, encryptedMessage3.get());
    Assertions.assertTrue(encryptedMessage3.get() instanceof PersistentHarvestingDelegationMessage);
    System.out.println(message.getPayloadHex());
    Assertions.assertTrue(message.getPayloadHex().startsWith(MessageMarker.PERSISTENT_DELEGATION_UNLOCK));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) HarvestingKeys(io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys) Test(org.junit.jupiter.api.Test)

Example 4 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class TransferTransactionTest method createPersistentDelegationRequestTransaction.

@Test
void createPersistentDelegationRequestTransaction() {
    Duration epochAdjustment = Duration.ofSeconds(100);
    KeyPair signingPrivateKey = KeyPair.fromPrivate(PrivateKey.fromHexString("2602F4236B199B3DF762B2AAB46FC3B77D8DDB214F0B62538D3827576C46C111"));
    KeyPair vrfPrivateKey = KeyPair.fromPrivate(PrivateKey.fromHexString("2602F4236B199B3DF762B2AAB46FC3B77D8DDB214F0B62538D3827576C46C000"));
    KeyPair recipient = KeyPair.fromPrivate(PrivateKey.fromHexString("B72F2950498111BADF276D6D9D5E345F04E0D5C9B8342DA983C3395B4CF18F08"));
    TransferTransaction transferTransaction = TransferTransactionFactory.createPersistentDelegationRequestTransaction(networkType, Deadline.create(epochAdjustment), signingPrivateKey.getPrivateKey(), vrfPrivateKey.getPrivateKey(), recipient.getPublicKey()).build();
    assertEquals(networkType, transferTransaction.getNetworkType());
    assertEquals(1, (int) transferTransaction.getVersion());
    assertTrue(LocalDateTime.now().isBefore(transferTransaction.getDeadline().getLocalDateTime(epochAdjustment)));
    assertEquals(BigInteger.valueOf(0), transferTransaction.getMaxFee());
    assertEquals(Address.createFromPublicKey(recipient.getPublicKey().toHex(), networkType), transferTransaction.getRecipient());
    assertEquals(0, transferTransaction.getMosaics().size());
    assertNotNull(transferTransaction.getMessage());
    assertEquals(MessageType.PERSISTENT_HARVESTING_DELEGATION_MESSAGE, transferTransaction.getMessage().get().getType());
    assertNotNull(transferTransaction.getMessage().get().getText());
    PersistentHarvestingDelegationMessage message = (PersistentHarvestingDelegationMessage) transferTransaction.getMessage().get();
    HarvestingKeys decodedKeys = message.decryptPayload(recipient.getPrivateKey());
    Assertions.assertEquals(signingPrivateKey.getPrivateKey(), decodedKeys.getSigningPrivateKey());
    Assertions.assertEquals(vrfPrivateKey.getPrivateKey(), decodedKeys.getVrfPrivateKey());
    byte[] actual = transferTransaction.serialize();
    BinarySerialization serialization = new BinarySerializationImpl();
    TransferTransaction deserialized = (TransferTransaction) serialization.deserialize(actual);
    assertEquals(MessageType.PERSISTENT_HARVESTING_DELEGATION_MESSAGE, deserialized.getMessage().get().getType());
    PersistentHarvestingDelegationMessage deserializedMessage = (PersistentHarvestingDelegationMessage) deserialized.getMessage().get();
    HarvestingKeys decodedKeys2 = deserializedMessage.decryptPayload(recipient.getPrivateKey());
    Assertions.assertEquals(signingPrivateKey.getPrivateKey(), decodedKeys2.getSigningPrivateKey());
    Assertions.assertEquals(vrfPrivateKey.getPrivateKey(), decodedKeys2.getVrfPrivateKey());
}
Also used : BinarySerialization(io.nem.symbol.sdk.api.BinarySerialization) KeyPair(io.nem.symbol.core.crypto.KeyPair) PersistentHarvestingDelegationMessage(io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage) HarvestingKeys(io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys) Duration(java.time.Duration) BinarySerializationImpl(io.nem.symbol.sdk.infrastructure.BinarySerializationImpl) Test(org.junit.jupiter.api.Test)

Example 5 with KeyPair

use of io.nem.symbol.core.crypto.KeyPair in project nem2-sdk-java by nemtech.

the class TransferTransactionIntegrationTest method standaloneTransferTransactionEncryptedMessage.

@ParameterizedTest
@EnumSource(RepositoryType.class)
public void standaloneTransferTransactionEncryptedMessage(RepositoryType type) throws Exception {
    this.helper().sendMosaicFromNemesis(type, getRecipient(), false);
    String namespaceName = "standaloneTransferTransactionEncryptedMessagealias".toLowerCase();
    NamespaceId recipient = setAddressAlias(type, getRecipient(), namespaceName);
    System.out.println(recipient.getIdAsHex());
    Assertions.assertEquals("9960629109A48AFBC0000000000000000000000000000000", recipient.encoded(getNetworkType()));
    String message = "E2ETest:standaloneTransferTransaction:message 漢字";
    KeyPair senderKeyPair = KeyPair.random();
    KeyPair recipientKeyPair = KeyPair.random();
    Message encryptedMessage = EncryptedMessage.create(message, senderKeyPair.getPrivateKey(), recipientKeyPair.getPublicKey());
    Currency networkCurrency = getNetworkCurrency();
    Mosaic mosaic = new Mosaic(networkCurrency.getNamespaceId().get(), BigInteger.valueOf(10202020));
    TransferTransaction transferTransaction = TransferTransactionFactory.create(getNetworkType(), getDeadline(), recipient, Collections.singletonList(mosaic)).message(encryptedMessage).maxFee(maxFee).build();
    TransferTransaction processed = announceAndValidate(type, signerAccount, transferTransaction);
    assertTransferTransactions(transferTransaction, processed);
    assertEncryptedMessageTransaction(message, senderKeyPair, recipientKeyPair, processed);
    TransferTransaction restTransaction = (TransferTransaction) get(getRepositoryFactory(type).createTransactionRepository().getTransaction(TransactionGroup.CONFIRMED, processed.getTransactionInfo().get().getHash().get()));
    assertTransferTransactions(transferTransaction, restTransaction);
    assertEncryptedMessageTransaction(message, senderKeyPair, recipientKeyPair, restTransaction);
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) Message(io.nem.symbol.sdk.model.message.Message) EncryptedMessage(io.nem.symbol.sdk.model.message.EncryptedMessage) PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) PersistentHarvestingDelegationMessage(io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage) Currency(io.nem.symbol.sdk.model.mosaic.Currency) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

KeyPair (io.nem.symbol.core.crypto.KeyPair)25 Test (org.junit.jupiter.api.Test)19 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)13 DsaSignerTest (io.nem.symbol.core.crypto.DsaSignerTest)7 Signature (io.nem.symbol.core.crypto.Signature)7 BlockCipher (io.nem.symbol.core.crypto.BlockCipher)6 DsaSigner (io.nem.symbol.core.crypto.DsaSigner)6 PublicKey (io.nem.symbol.core.crypto.PublicKey)5 CryptoException (io.nem.symbol.core.crypto.CryptoException)3 KeyGenerator (io.nem.symbol.core.crypto.KeyGenerator)3 KeyGeneratorTest (io.nem.symbol.core.crypto.KeyGeneratorTest)3 BigInteger (java.math.BigInteger)3 BlockCipherTest (io.nem.symbol.core.crypto.BlockCipherTest)2 PersistentHarvestingDelegationMessage (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage)2 HarvestingKeys (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys)2 TransferTransaction (io.nem.symbol.sdk.model.transaction.TransferTransaction)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 PrivateKey (io.nem.symbol.core.crypto.PrivateKey)1 Ed25519CryptoEngine (io.nem.symbol.core.crypto.ed25519.Ed25519CryptoEngine)1