Search in sources :

Example 36 with Key

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

the class JKeySerializerTest method genSampleComplexKey.

/**
 * Generates a complex key of given depth with a mix of basic key, threshold key and key list.
 *
 * @param depth
 * 		of the generated key
 * @return generated key
 */
private static Key genSampleComplexKey(final int depth, final Map<String, PrivateKey> pubKey2privKeyMap) {
    Key rv = null;
    final int numKeys = 3;
    final int threshold = 2;
    if (depth == 1) {
        rv = genSingleEd25519Key(pubKey2privKeyMap);
        // verify the size
        final int size = computeNumOfExpandedKeys(rv, 1, new AtomicCounter());
        assertEquals(1, size);
    } else if (depth == 2) {
        final List<Key> keys = new ArrayList<>();
        keys.add(genSingleEd25519Key(pubKey2privKeyMap));
        keys.add(genSingleECDSASecp256k1Key(pubKey2privKeyMap));
        keys.add(genThresholdKeyInstance(numKeys, threshold, pubKey2privKeyMap));
        keys.add(genKeyListInstance(numKeys, pubKey2privKeyMap));
        rv = genKeyList(keys);
        // verify the size
        final int size = computeNumOfExpandedKeys(rv, 1, new AtomicCounter());
        assertEquals(2 + numKeys * 2, size);
    } else {
        throw new NotImplementedException("Not implemented yet.");
    }
    return rv;
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) AtomicCounter(com.hedera.services.legacy.proto.utils.AtomicCounter) ArrayList(java.util.ArrayList) KeyList(com.hederahashgraph.api.proto.java.KeyList) List(java.util.List) Key(com.hederahashgraph.api.proto.java.Key) PrivateKey(java.security.PrivateKey) ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey)

Example 37 with Key

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

the class FeeBuilderTest method assertCalculateKeysMetadataThresholdKey.

@Test
void assertCalculateKeysMetadataThresholdKey() {
    int[] countKeyMetatData = { 0, 0 };
    KeyList thresholdKeyList = KeyList.newBuilder().addKeys(Key.newBuilder().setEd25519(ByteString.copyFromUtf8("aaaaaaaa")).build()).addKeys(Key.newBuilder().setEd25519(ByteString.copyFromUtf8("bbbbbbbbbbbbbbbbbbbbb")).build()).build();
    ThresholdKey thresholdKey = ThresholdKey.newBuilder().setKeys(thresholdKeyList).setThreshold(2).build();
    Key validED25519Keys = Key.newBuilder().setThresholdKey(thresholdKey).build();
    assertEquals(countKeyMetatData.length, FeeBuilder.calculateKeysMetadata(validED25519Keys, countKeyMetatData).length);
    assertEquals(2, FeeBuilder.calculateKeysMetadata(validED25519Keys, countKeyMetatData)[1]);
}
Also used : ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) KeyList(com.hederahashgraph.api.proto.java.KeyList) Key(com.hederahashgraph.api.proto.java.Key) ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) Test(org.junit.jupiter.api.Test)

Example 38 with Key

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

the class SignedTxnAccessorTest method parsesLegacyCorrectly.

@Test
void parsesLegacyCorrectly() throws Exception {
    final Key aPrimitiveKey = Key.newBuilder().setEd25519(ByteString.copyFromUtf8("01234567890123456789012345678901")).build();
    final ByteString aNewAlias = aPrimitiveKey.toByteString();
    final AliasManager aliasManager = mock(AliasManager.class);
    given(aliasManager.lookupIdBy(any())).willReturn(EntityNum.MISSING_NUM);
    final long offeredFee = 100_000_000L;
    var xferNoAliases = RequestBuilder.getCryptoTransferRequest(1234l, 0l, 0l, 3l, 0l, 0l, offeredFee, Timestamp.getDefaultInstance(), Duration.getDefaultInstance(), false, zeroByteMemo, 5678l, -70000l, 5679l, 70000l);
    xferNoAliases = xferNoAliases.toBuilder().setSigMap(expectedMap).build();
    var xferWithAutoCreation = RequestBuilder.getHbarCryptoTransferRequestToAlias(1234l, 0l, 0l, 3l, 0l, 0l, offeredFee, Timestamp.getDefaultInstance(), Duration.getDefaultInstance(), false, zeroByteMemo, 5678l, -70000l, aNewAlias, 70000l);
    xferWithAutoCreation = xferWithAutoCreation.toBuilder().setSigMap(expectedMap).build();
    var xferWithAliasesNoAutoCreation = RequestBuilder.getTokenTransferRequestToAlias(1234l, 0l, 0l, 3l, 0l, 0l, offeredFee, Timestamp.getDefaultInstance(), Duration.getDefaultInstance(), false, zeroByteMemo, 5678l, 5555l, -70000l, ByteString.copyFromUtf8("aaaa"), 70000l);
    xferWithAliasesNoAutoCreation = xferWithAliasesNoAutoCreation.toBuilder().setSigMap(expectedMap).build();
    final var body = CommonUtils.extractTransactionBody(xferNoAliases);
    var accessor = SignedTxnAccessor.uncheckedFrom(xferNoAliases);
    accessor.countAutoCreationsWith(aliasManager);
    final var txnUsageMeta = accessor.baseUsageMeta();
    assertEquals(xferNoAliases, accessor.getSignedTxnWrapper());
    assertArrayEquals(xferNoAliases.toByteArray(), accessor.getSignedTxnWrapperBytes());
    assertEquals(body, accessor.getTxn());
    assertArrayEquals(body.toByteArray(), accessor.getTxnBytes());
    assertEquals(body.getTransactionID(), accessor.getTxnId());
    assertEquals(1234l, accessor.getPayer().getAccountNum());
    assertEquals(HederaFunctionality.CryptoTransfer, accessor.getFunction());
    assertEquals(offeredFee, accessor.getOfferedFee());
    assertArrayEquals(CommonUtils.noThrowSha384HashOf(xferNoAliases.toByteArray()), accessor.getHash());
    assertEquals(expectedMap, accessor.getSigMap());
    assertArrayEquals("irst".getBytes(), accessor.getPkToSigsFn().sigBytesFor("f".getBytes()));
    assertArrayEquals(zeroByteMemoUtf8Bytes, accessor.getMemoUtf8Bytes());
    assertTrue(accessor.memoHasZeroByte());
    assertEquals(FeeBuilder.getSignatureCount(accessor.getSignedTxnWrapper()), accessor.numSigPairs());
    assertEquals(FeeBuilder.getSignatureSize(accessor.getSignedTxnWrapper()), accessor.sigMapSize());
    assertEquals(zeroByteMemo, accessor.getMemo());
    assertEquals(false, accessor.isTriggeredTxn());
    assertEquals(false, accessor.canTriggerTxn());
    assertEquals(0, accessor.getNumAutoCreations());
    assertEquals(memoUtf8Bytes.length, txnUsageMeta.memoUtf8Bytes());
    accessor = SignedTxnAccessor.uncheckedFrom(xferWithAutoCreation);
    accessor.countAutoCreationsWith(aliasManager);
    assertEquals(1, accessor.getNumAutoCreations());
    accessor = SignedTxnAccessor.uncheckedFrom(xferWithAliasesNoAutoCreation);
    accessor.countAutoCreationsWith(aliasManager);
    assertEquals(0, accessor.getNumAutoCreations());
}
Also used : AliasManager(com.hedera.services.ledger.accounts.AliasManager) ByteString(com.google.protobuf.ByteString) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 39 with Key

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

the class ContractInfoAsserts method adminKey.

public ContractInfoAsserts adminKey(String expectedKeyName) {
    registerProvider((spec, o) -> {
        Key expectedKey = spec.registry().getKey(expectedKeyName);
        assertEquals(expectedKey, object2ContractInfo(o).getAdminKey(), "Bad admin key!");
    });
    return this;
}
Also used : Key(com.hederahashgraph.api.proto.java.Key)

Example 40 with Key

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

the class NewSpecKey method submitOp.

@Override
protected boolean submitOp(HapiApiSpec spec) throws Throwable {
    final var keyGen = generator.orElse(DEFAULT_KEY_GEN);
    Key key;
    if (shape.isPresent()) {
        if (labels.isPresent()) {
            key = spec.keys().generateSubjectTo(spec, shape.get(), keyGen, labels.get());
        } else {
            key = spec.keys().generateSubjectTo(spec, shape.get(), keyGen);
        }
    } else {
        key = spec.keys().generate(spec, type.orElse(KeyType.SIMPLE), keyGen);
    }
    spec.registry().saveKey(name, key);
    if (immediateExportLoc.isPresent() && immediateExportPass.isPresent()) {
        final var exportLoc = immediateExportLoc.get();
        final var exportPass = immediateExportPass.get();
        exportWithPass(spec, name, exportLoc, exportPass);
        if (verboseLoggingOn && yahcliLogger) {
            COMMON_MESSAGES.info("Exported a newly generated key in PEM format to " + exportLoc);
        }
    }
    if (verboseLoggingOn && !yahcliLogger) {
        if (type.orElse(KeyType.SIMPLE) == KeyType.SIMPLE) {
            log.info("Created simple '{}' w/ Ed25519 public key {}", name, CommonUtils.hex(key.getEd25519().toByteArray()));
        } else {
            log.info("Created a complex key...");
        }
    }
    return false;
}
Also used : Key(com.hederahashgraph.api.proto.java.Key)

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