Search in sources :

Example 31 with Key

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

the class CryptoOpsUsageTest method estimatesUpdateWithAutoAssociationsAsExpected.

@Test
void estimatesUpdateWithAutoAssociationsAsExpected() {
    givenUpdateOpWithMaxAutoAssociations();
    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";
    int oldMaxAutoAssociations = maxAutoAssociations - 5;
    var ctx = ExtantCryptoContext.newBuilder().setCurrentExpiry(oldExpiry).setCurrentMemo(oldMemo).setCurrentKey(oldKey).setCurrentlyHasProxy(false).setCurrentNumTokenRels(numTokenRels).setCurrentMaxAutomaticAssociations(oldMaxAutoAssociations).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 + INT_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(oldMaxAutoAssociations * 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 32 with Key

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

the class CryptoOpsUsageTest method estimatesApprovalAsExpected.

@Test
void estimatesApprovalAsExpected() {
    givenApprovalOp();
    var expected = new UsageAccumulator();
    var baseMeta = new BaseTransactionMeta(0, 0);
    var opMeta = new CryptoApproveAllowanceMeta(txn.getCryptoApproveAllowance(), 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(Collections.emptyList()).setCurrentNftAllowances(Collections.emptyList()).setCurrentTokenAllowances(Collections.emptyList()).build();
    long msgBytesUsed = (approveOp.getCryptoAllowancesCount() * CRYPTO_ALLOWANCE_SIZE) + (approveOp.getTokenAllowancesCount() * TOKEN_ALLOWANCE_SIZE) + (approveOp.getNftAllowancesCount() * NFT_ALLOWANCE_SIZE) + countSerials(approveOp.getNftAllowancesList()) * LONG_SIZE;
    expected.addBpt(msgBytesUsed);
    long lifetime = ESTIMATOR_UTILS.relativeLifetime(txn, oldExpiry);
    expected.addRbs(msgBytesUsed * lifetime);
    var actual = new UsageAccumulator();
    subject.cryptoApproveAllowanceUsage(sigUsage, baseMeta, opMeta, ctx, actual);
    assertEquals(expected, actual);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) SigUsage(com.hedera.services.usage.SigUsage) 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 33 with Key

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

the class RequestBuilderTest method assertGetFileCreateBuilder.

@Test
void assertGetFileCreateBuilder() throws InvalidProtocolBufferException {
    Timestamp fileExpiration = Timestamp.newBuilder().setSeconds(123_456L).build();
    List<Key> keyList = List.of(validED25519Key);
    var transaction = RequestBuilder.getFileCreateBuilder(accountId.getAccountNum(), accountId.getRealmNum(), accountId.getShardNum(), nodeId.getAccountNum(), nodeId.getRealmNum(), nodeId.getShardNum(), transactionFee, startTime, transactionDuration, generateRecord, memo, hash, fileExpiration, keyList);
    var tb = TransactionBody.parseFrom(transaction.getBodyBytes());
    assertEquals(memo, tb.getMemo());
    assertEquals(generateRecord, tb.getGenerateRecord());
    assertEquals(transactionFee, tb.getTransactionFee());
    assertEquals(startTime, tb.getTransactionID().getTransactionValidStart());
    assertEquals(transactionDuration, tb.getTransactionValidDuration());
    assertEquals(transactionFee, tb.getTransactionFee());
    assertEquals(hash, tb.getFileCreate().getContents());
    assertEquals(fileExpiration, tb.getFileCreate().getExpirationTime());
    assertEquals(nodeId.getAccountNum(), tb.getNodeAccountID().getAccountNum());
    assertEquals(nodeId.getRealmNum(), tb.getNodeAccountID().getRealmNum());
    assertEquals(nodeId.getShardNum(), tb.getNodeAccountID().getShardNum());
    assertEquals(accountId.getAccountNum(), tb.getTransactionID().getAccountID().getAccountNum());
    assertEquals(accountId.getRealmNum(), tb.getTransactionID().getAccountID().getRealmNum());
    assertEquals(accountId.getShardNum(), tb.getTransactionID().getAccountID().getShardNum());
}
Also used : Timestamp(com.hederahashgraph.api.proto.java.Timestamp) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 34 with Key

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

the class SpecKey method registerPemWith.

private void registerPemWith(HapiApiSpec spec, RegistryForms forms) {
    var qPemLoc = qualifiedKeyLoc(pemLoc, spec);
    var aes256EncryptedPkcs8Pem = new File(qPemLoc);
    if (!aes256EncryptedPkcs8Pem.exists()) {
        if (!generateIfMissing) {
            throw new IllegalStateException(String.format("File missing at PEM loc '%s'!", qPemLoc));
        }
        Key simpleKey = spec.keys().generate(spec, KeyFactory.KeyType.SIMPLE);
        forms.completeIntake(spec.registry(), simpleKey);
        try {
            spec.keys().exportSimpleKey(qPemLoc, forms.name(), passphrase);
            log.info("Created new simple key at PEM loc '{}'.", qPemLoc);
        } catch (KeyStoreException e) {
            throw new IllegalStateException(String.format("Cannot generate key to PEM loc '%s'!", qPemLoc), e);
        }
        return;
    }
    var keyPair = readFirstKpFromPem(aes256EncryptedPkcs8Pem, passphrase);
    var publicKey = (EdDSAPublicKey) keyPair.getPublic();
    var hederaKey = asSimpleHederaKey(publicKey.getAbyte());
    forms.completeIntake(spec.registry(), hederaKey);
    /* When we incorporate the key into the spec's key factory, it will:
			(1) Update the mapping from hexed public keys to PrivateKeys; and,
			(2) Set the given SigControl as default for signing requests with the Key. */
    spec.keys().incorporate(forms.name(), CommonUtils.hex(publicKey.getAbyte()), keyPair.getPrivate(), SigControl.ON);
}
Also used : EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) KeyStoreException(java.security.KeyStoreException) File(java.io.File) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) Key(com.hederahashgraph.api.proto.java.Key) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey)

Example 35 with Key

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

the class LedgerBalanceChangesTest method happyPathTransfersWithAutoCreation.

@Test
void happyPathTransfersWithAutoCreation() {
    final Key aliasA = KeyFactory.getDefaultInstance().newEd25519();
    final AccountID a = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(10L).build();
    final AccountID validAliasAccountWithAlias = AccountID.newBuilder().setAlias(aliasA.toByteString()).build();
    final AccountID validAliasAccountWithId = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(11L).build();
    final AccountID funding = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(98L).build();
    final EntityNum validAliasEntityNum = new EntityNum(11);
    List<BalanceChange> changes = new ArrayList<>();
    changes.add(hbarChange(a, -100));
    changes.add(hbarChange(validAliasAccountWithAlias, 0));
    final var validAliasAccount = MerkleAccountFactory.newAccount().get();
    final var fundingAccount = MerkleAccountFactory.newAccount().get();
    final var aAccount = MerkleAccountFactory.newAccount().balance(aStartBalance).get();
    backingAccounts.put(a, aAccount);
    backingAccounts.put(validAliasAccountWithId, validAliasAccount);
    backingAccounts.put(funding, fundingAccount);
    given(autoCreationLogic.create(any(), eq(accountsLedger))).willAnswer(invocationOnMock -> {
        final var change = (BalanceChange) invocationOnMock.getArgument(0);
        change.replaceAliasWith(validAliasEntityNum.toGrpcAccountId());
        return Pair.of(OK, 100L);
    });
    given(dynamicProperties.fundingAccount()).willReturn(funding);
    subject.begin();
    assertDoesNotThrow(() -> subject.doZeroSum(changes));
    subject.commit();
    assertEquals(aStartBalance - 100, backingAccounts.getImmutableRef(a).getBalance());
    assertEquals(0, backingAccounts.getImmutableRef(validAliasEntityNum.toGrpcAccountId()).getBalance());
}
Also used : AccountID(com.hederahashgraph.api.proto.java.AccountID) ArrayList(java.util.ArrayList) Key(com.hederahashgraph.api.proto.java.Key) EntityNum(com.hedera.services.utils.EntityNum) 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