Search in sources :

Example 1 with HarvestingKeys

use of io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys 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 2 with HarvestingKeys

use of io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys 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 3 with HarvestingKeys

use of io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys in project nem2-sdk-java by nemtech.

the class TransferTransactionIntegrationTest method assertPersistentDelegationTransaction.

private void assertPersistentDelegationTransaction(KeyPair recipientKeyPair, KeyPair signingKeyPair, KeyPair vrfPrivateKey, TransferTransaction transaction) {
    Assertions.assertTrue(transaction.getMessage().get() instanceof PersistentHarvestingDelegationMessage);
    Assertions.assertEquals(MessageType.PERSISTENT_HARVESTING_DELEGATION_MESSAGE, transaction.getMessage().get().getType());
    HarvestingKeys decryptedMessage = ((PersistentHarvestingDelegationMessage) transaction.getMessage().get()).decryptPayload(recipientKeyPair.getPrivateKey());
    Assertions.assertEquals(vrfPrivateKey.getPrivateKey(), decryptedMessage.getVrfPrivateKey());
    Assertions.assertEquals(signingKeyPair.getPrivateKey(), decryptedMessage.getSigningPrivateKey());
}
Also used : PersistentHarvestingDelegationMessage(io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage) HarvestingKeys(io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys)

Aggregations

HarvestingKeys (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys)3 KeyPair (io.nem.symbol.core.crypto.KeyPair)2 PersistentHarvestingDelegationMessage (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage)2 Test (org.junit.jupiter.api.Test)2 BinarySerialization (io.nem.symbol.sdk.api.BinarySerialization)1 BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)1 Duration (java.time.Duration)1