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