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