Search in sources :

Example 26 with Key

use of com.hederahashgraph.api.proto.java.Key in project hedera-mirror-node by hashgraph.

the class EntityRepositoryTest method publicKeyUpdates.

@Test
void publicKeyUpdates() {
    Entity entity = domainBuilder.entity().customize(b -> b.key(null)).persist();
    // unset key should result in null public key
    assertThat(entityRepository.findById(entity.getId())).get().extracting(Entity::getPublicKey).isNull();
    // default proto key of single byte should result in empty public key
    entity.setKey(Key.getDefaultInstance().toByteArray());
    entityRepository.save(entity);
    assertThat(entityRepository.findById(entity.getId())).get().extracting(Entity::getPublicKey).isEqualTo("");
    // invalid key should be null
    entity.setKey("123".getBytes());
    entityRepository.save(entity);
    assertThat(entityRepository.findById(entity.getId())).get().extracting(Entity::getPublicKey).isNull();
    // valid key should not be null
    entity.setKey(Key.newBuilder().setEd25519(ByteString.copyFromUtf8("123")).build().toByteArray());
    entityRepository.save(entity);
    assertThat(entityRepository.findById(entity.getId())).get().extracting(Entity::getPublicKey).isNotNull();
    // null key like unset should result in null public key
    entity.setKey(null);
    entityRepository.save(entity);
    assertThat(entityRepository.findById(entity.getId())).get().extracting(Entity::getPublicKey).isNull();
}
Also used : ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Key(com.hederahashgraph.api.proto.java.Key) List(java.util.List) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DomainBuilder(com.hedera.mirror.common.domain.DomainBuilder) Resource(javax.annotation.Resource) RowMapper(org.springframework.jdbc.core.RowMapper) Entity(com.hedera.mirror.common.domain.entity.Entity) Entity(com.hedera.mirror.common.domain.entity.Entity) Test(org.junit.jupiter.api.Test)

Example 27 with Key

use of com.hederahashgraph.api.proto.java.Key in project hedera-mirror-node by hashgraph.

the class DomainUtils method getPublicKey.

/**
 * A key can either be a complex key (e.g. key list or threshold key) or a primitive key (e.g. ED25519 or
 * ECDSA_SECP256K1). If the protobuf encoding of a Key is a single primitive key or a complex key with exactly one
 * primitive key within it, return the key as a String with lowercase hex encoding.
 *
 * @param protobufKey
 * @return public key as a string in hex encoding, or null
 */
public static String getPublicKey(@Nullable byte[] protobufKey) {
    try {
        if (protobufKey == null) {
            return null;
        }
        if (ArrayUtils.isEmpty(protobufKey)) {
            // Key.getDefaultInstance() case
            return "";
        }
        Key key = Key.parseFrom(protobufKey);
        byte[] primitiveKey = getPublicKey(key, 1);
        return bytesToHex(primitiveKey);
    } catch (Exception e) {
        log.error("Unable to parse protobuf Key", e);
        return null;
    }
}
Also used : Key(com.hederahashgraph.api.proto.java.Key) IOException(java.io.IOException) InvalidEntityException(com.hedera.mirror.common.exception.InvalidEntityException)

Example 28 with Key

use of com.hederahashgraph.api.proto.java.Key in project hedera-mirror-node by hashgraph.

the class HistoricalAccountInfoMigrationTest method createEntity.

private <T extends AbstractEntity> T createEntity(long num, EntityType type, boolean afterReset) {
    T entity = EntityId.of(0L, 0L, num, type).toEntity();
    entity.setNum(num);
    entity.setRealm(0L);
    entity.setShard(0L);
    entity.setType(type);
    entity.setId(num);
    entity.setDeleted(false);
    entity.setMemo("");
    entity.setTimestampRange(Range.atLeast(0L));
    if (afterReset) {
        Key key = Key.newBuilder().setEd25519(ByteString.copyFromUtf8("123")).build();
        entity.setAutoRenewPeriod(5L);
        entity.setExpirationTimestamp(1L);
        entity.setKey(key.toByteArray());
        entity.setMemo("Bar");
        entity.setProxyAccountId(EntityId.of(0, 0, 3, EntityType.ACCOUNT));
    }
    if (type == EntityType.CONTRACT) {
        contractRepository.save((Contract) entity);
    } else {
        entityRepository.save((Entity) entity);
    }
    return entity;
}
Also used : Key(com.hederahashgraph.api.proto.java.Key)

Example 29 with Key

use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.

the class CryptoOpsUsageTest method estimatesUpdateWithOutAutoAssociationsAsExpected.

@Test
void estimatesUpdateWithOutAutoAssociationsAsExpected() {
    givenUpdateOpWithOutMaxAutoAssociations();
    var expected = new UsageAccumulator();
    var baseMeta = new BaseTransactionMeta(memo.length(), 0);
    var opMeta = new CryptoUpdateMeta(txn.getCryptoUpdateAccount(), txn.getTransactionID().getTransactionValidStart().getSeconds());
    expected.resetForTransaction(baseMeta, sigUsage);
    Key oldKey = FileOpsUsage.asKey(KeyUtils.A_KEY_LIST.getKeyList());
    long oldExpiry = expiry - 1_234L;
    String oldMemo = "Lettuce";
    var ctx = ExtantCryptoContext.newBuilder().setCurrentExpiry(oldExpiry).setCurrentMemo(oldMemo).setCurrentKey(oldKey).setCurrentlyHasProxy(false).setCurrentNumTokenRels(numTokenRels).setCurrentMaxAutomaticAssociations(maxAutoAssociations).setCurrentCryptoAllowances(Collections.emptyList()).setCurrentNftAllowances(Collections.emptyList()).setCurrentTokenAllowances(Collections.emptyList()).build();
    long keyBytesUsed = getAccountKeyStorageSize(key);
    long msgBytesUsed = BASIC_ENTITY_ID_SIZE + memo.getBytes().length + keyBytesUsed + LONG_SIZE + BASIC_ENTITY_ID_SIZE;
    expected.addBpt(msgBytesUsed);
    long newVariableBytes = memo.getBytes().length + keyBytesUsed + BASIC_ENTITY_ID_SIZE;
    long tokenRelBytes = numTokenRels * CRYPTO_ENTITY_SIZES.bytesInTokenAssocRepr();
    long sharedFixedBytes = CRYPTO_ENTITY_SIZES.fixedBytesInAccountRepr() + tokenRelBytes;
    long newLifetime = ESTIMATOR_UTILS.relativeLifetime(txn, expiry);
    long oldLifetime = ESTIMATOR_UTILS.relativeLifetime(txn, oldExpiry);
    long rbsDelta = ESTIMATOR_UTILS.changeInBsUsage(CRYPTO_ENTITY_SIZES.fixedBytesInAccountRepr() + ctx.currentNonBaseRb() + ctx.currentNumTokenRels() * CRYPTO_ENTITY_SIZES.bytesInTokenAssocRepr(), oldLifetime, sharedFixedBytes + newVariableBytes, newLifetime);
    if (rbsDelta > 0) {
        expected.addRbs(rbsDelta);
    }
    final var slotDelta = ESTIMATOR_UTILS.changeInBsUsage(maxAutoAssociations * CryptoOpsUsage.UPDATE_SLOT_MULTIPLIER, oldLifetime, maxAutoAssociations * CryptoOpsUsage.UPDATE_SLOT_MULTIPLIER, newLifetime);
    expected.addRbs(slotDelta);
    var actual = new UsageAccumulator();
    subject.cryptoUpdateUsage(sigUsage, baseMeta, opMeta, ctx, actual);
    assertEquals(expected, actual);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) ByteString(com.google.protobuf.ByteString) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 30 with Key

use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.

the class CryptoOpsUsageTest method estimatesAdjustAsExpected.

@Test
void estimatesAdjustAsExpected() {
    givenAdjustOp();
    GrantedCryptoAllowance existingCryptoAllowances = GrantedCryptoAllowance.newBuilder().setSpender(proxy).setAmount(100L).build();
    GrantedTokenAllowance existingTokenAllowances = GrantedTokenAllowance.newBuilder().setSpender(proxy).setAmount(100L).setTokenId(IdUtils.asToken("0.0.1000")).build();
    GrantedNftAllowance existingNftAllowances = GrantedNftAllowance.newBuilder().setSpender(proxy).setTokenId(IdUtils.asToken("0.0.1000")).addAllSerialNumbers(List.of()).build();
    var expected = new UsageAccumulator();
    var baseMeta = new BaseTransactionMeta(0, 0);
    var opMeta = new CryptoAdjustAllowanceMeta(txn.getCryptoAdjustAllowance(), txn.getTransactionID().getTransactionValidStart().getSeconds());
    SigUsage sigUsage = new SigUsage(1, sigSize, 1);
    expected.resetForTransaction(baseMeta, sigUsage);
    Key oldKey = FileOpsUsage.asKey(KeyUtils.A_KEY_LIST.getKeyList());
    long oldExpiry = expiry - 1_234L;
    String oldMemo = "Lettuce";
    var ctx = ExtantCryptoContext.newBuilder().setCurrentExpiry(oldExpiry).setCurrentMemo(oldMemo).setCurrentKey(oldKey).setCurrentlyHasProxy(false).setCurrentNumTokenRels(numTokenRels).setCurrentMaxAutomaticAssociations(maxAutoAssociations).setCurrentCryptoAllowances(List.of(existingCryptoAllowances)).setCurrentNftAllowances(List.of(existingNftAllowances)).setCurrentTokenAllowances(List.of(existingTokenAllowances)).build();
    long msgBytesUsed = (adjustOp.getCryptoAllowancesCount() * CRYPTO_ALLOWANCE_SIZE) + (adjustOp.getTokenAllowancesCount() * TOKEN_ALLOWANCE_SIZE) + (adjustOp.getNftAllowancesCount() * NFT_ALLOWANCE_SIZE) + countSerials(adjustOp.getNftAllowancesList()) * LONG_SIZE;
    expected.addBpt(msgBytesUsed);
    long lifetime = ESTIMATOR_UTILS.relativeLifetime(txn, oldExpiry);
    var expectedAdjustedBytes = LONG_SIZE;
    // 3990128
    expected.addRbs(expectedAdjustedBytes * lifetime);
    var actual = new UsageAccumulator();
    subject.cryptoAdjustAllowanceUsage(sigUsage, baseMeta, opMeta, ctx, actual);
    assertEquals(expected, actual);
}
Also used : GrantedTokenAllowance(com.hederahashgraph.api.proto.java.GrantedTokenAllowance) BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) GrantedCryptoAllowance(com.hederahashgraph.api.proto.java.GrantedCryptoAllowance) SigUsage(com.hedera.services.usage.SigUsage) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) ByteString(com.google.protobuf.ByteString) GrantedNftAllowance(com.hederahashgraph.api.proto.java.GrantedNftAllowance) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Aggregations

Key (com.hederahashgraph.api.proto.java.Key)43 ByteString (com.google.protobuf.ByteString)20 Test (org.junit.jupiter.api.Test)20 ThresholdKey (com.hederahashgraph.api.proto.java.ThresholdKey)14 KeyList (com.hederahashgraph.api.proto.java.KeyList)12 List (java.util.List)12 ArrayList (java.util.ArrayList)10 HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)8 PrivateKey (java.security.PrivateKey)8 Optional (java.util.Optional)7 LogManager (org.apache.logging.log4j.LogManager)7 Logger (org.apache.logging.log4j.Logger)7 HapiApiSuite (com.hedera.services.bdd.suites.HapiApiSuite)6 IOException (java.io.IOException)6 Map (java.util.Map)6 MoreObjects (com.google.common.base.MoreObjects)5 AccountID (com.hederahashgraph.api.proto.java.AccountID)5 HederaFunctionality (com.hederahashgraph.api.proto.java.HederaFunctionality)5 Transaction (com.hederahashgraph.api.proto.java.Transaction)5 TransactionBody (com.hederahashgraph.api.proto.java.TransactionBody)5