Search in sources :

Example 16 with Key

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

the class JKeyUtils 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
 */
public static Key genSampleComplexKey(final int depth, final Map<String, PrivateKey> pubKey2privKeyMap) {
    Key rv;
    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) List(java.util.List) KeyList(com.hederahashgraph.api.proto.java.KeyList) 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 17 with Key

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

the class JKeyListTest method invalidJKeyListTest.

@Test
void invalidJKeyListTest() throws Exception {
    Key validED25519Key = Key.newBuilder().setEd25519(TxnUtils.randomUtf8ByteString(JEd25519Key.ED25519_BYTE_LENGTH)).build();
    Key invalidECDSAsecp256k1Key = randomInvalidECDSASecp256K1Key();
    KeyList invalidKeyList1 = KeyList.newBuilder().build();
    Key invalidKey1 = Key.newBuilder().setKeyList(invalidKeyList1).build();
    KeyList invalidKeyList2 = KeyList.newBuilder().addKeys(validED25519Key).addKeys(invalidKey1).build();
    Key invalidKey2 = Key.newBuilder().setKeyList(invalidKeyList2).build();
    KeyList invalidKeyList3 = KeyList.newBuilder().addKeys(validED25519Key).addKeys(invalidKey2).build();
    Key invalidKey3 = Key.newBuilder().setKeyList(invalidKeyList3).build();
    KeyList invalidKeyList4 = KeyList.newBuilder().addKeys(invalidECDSAsecp256k1Key).addKeys(invalidKey3).build();
    Key invalidKey4 = Key.newBuilder().setKeyList(invalidKeyList4).build();
    JKey jKeyList1 = JKey.convertKey(invalidKey1, 1);
    assertFalse(jKeyList1.isValid());
    JKey jKeyList2 = JKey.convertKey(invalidKey2, 1);
    assertFalse(jKeyList2.isValid());
    JKey jKeyList3 = JKey.convertKey(invalidKey3, 1);
    assertFalse(jKeyList3.isValid());
    JKey jKeyList4 = JKey.convertKey(invalidKey4, 1);
    assertFalse(jKeyList4.isValid());
}
Also used : KeyList(com.hederahashgraph.api.proto.java.KeyList) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 18 with Key

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

the class JKeyListTest method validJKeyListTest.

@Test
void validJKeyListTest() throws Exception {
    Key validED25519Key = Key.newBuilder().setEd25519(TxnUtils.randomUtf8ByteString(JEd25519Key.ED25519_BYTE_LENGTH)).build();
    Key validECDSAsecp256k1Key = randomValidECDSASecp256K1Key();
    Key validECDSA384Key = Key.newBuilder().setECDSA384(TxnUtils.randomUtf8ByteString(24)).build();
    KeyList validKeyList1 = KeyList.newBuilder().addKeys(validECDSA384Key).addKeys(validED25519Key).build();
    Key validKey1 = Key.newBuilder().setKeyList(validKeyList1).build();
    KeyList validKeyList2 = KeyList.newBuilder().addKeys(validED25519Key).addKeys(validKey1).build();
    Key validKey2 = Key.newBuilder().setKeyList(validKeyList2).build();
    KeyList validKeyList3 = KeyList.newBuilder().addKeys(validED25519Key).addKeys(validKey2).build();
    Key validKey3 = Key.newBuilder().setKeyList(validKeyList3).build();
    KeyList validKeyList4 = KeyList.newBuilder().addKeys(validECDSAsecp256k1Key).addKeys(validKey3).build();
    Key validKey4 = Key.newBuilder().setKeyList(validKeyList4).build();
    JKey jKeyList1 = JKey.convertKey(validKey1, 1);
    assertTrue(jKeyList1.isValid());
    JKey jKeyList2 = JKey.convertKey(validKey2, 1);
    assertTrue(jKeyList2.isValid());
    JKey jKeyList3 = JKey.convertKey(validKey3, 1);
    assertTrue(jKeyList3.isValid());
    JKey jKeyList4 = JKey.convertKey(validKey4, 1);
    assertTrue(jKeyList4.isValid());
}
Also used : KeyList(com.hederahashgraph.api.proto.java.KeyList) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 19 with Key

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

the class HapiSpecOperation method finalizedTxn.

protected Transaction finalizedTxn(HapiApiSpec spec, Consumer<TransactionBody.Builder> opDef, boolean forCost) throws Throwable {
    if ((forCost && useDefaultTxnAsCostAnswerPayment) || (!forCost && useDefaultTxnAsAnswerOnlyPayment)) {
        return Transaction.getDefaultInstance();
    }
    Transaction txn;
    Consumer<TransactionBody.Builder> minDef = bodyDef(spec);
    if (usdFee.isPresent()) {
        double centsFee = usdFee.getAsDouble() * 100.0;
        double tinybarFee = centsFee / spec.ratesProvider().rates().getCentEquiv() * spec.ratesProvider().rates().getHbarEquiv() * HapiApiSuite.ONE_HBAR;
        fee = Optional.of((long) tinybarFee);
    }
    Consumer<TransactionBody.Builder> netDef = fee.map(amount -> minDef.andThen(b -> b.setTransactionFee(amount))).orElse(minDef).andThen(opDef);
    setKeyControlOverrides(spec);
    List<Key> keys = signersToUseFor(spec);
    Transaction.Builder builder = spec.txns().getReadyToSign(netDef);
    Transaction provisional = getSigned(spec, builder, keys);
    if (fee.isPresent()) {
        txn = provisional;
    } else {
        Key payerKey = spec.registry().getKey(payer.orElse(spec.setup().defaultPayerName()));
        int numPayerKeys = hardcodedNumPayerKeys.orElse(spec.keys().controlledKeyCount(payerKey, overrides));
        long customFee = feeFor(spec, provisional, numPayerKeys);
        netDef = netDef.andThen(b -> b.setTransactionFee(customFee));
        txn = getSigned(spec, spec.txns().getReadyToSign(netDef), keys);
    }
    return finalizedTxnFromTxnWithBodyBytesAndSigMap(txn);
}
Also used : Arrays(java.util.Arrays) SigControl(com.hedera.services.bdd.spec.keys.SigControl) SignatureMap(com.hederahashgraph.api.proto.java.SignatureMap) Random(java.util.Random) ScheduleOpsUsage(com.hedera.services.usage.schedule.ScheduleOpsUsage) Duration(com.hederahashgraph.api.proto.java.Duration) HapiGetTxnRecord(com.hedera.services.bdd.spec.queries.meta.HapiGetTxnRecord) QueryVerbs.getTxnRecord(com.hedera.services.bdd.spec.queries.QueryVerbs.getTxnRecord) CommonUtils(com.hedera.services.legacy.proto.utils.CommonUtils) FileFeeBuilder(com.hederahashgraph.fee.FileFeeBuilder) Map(java.util.Map) EMPTY_LIST(java.util.Collections.EMPTY_LIST) ConsensusOpsUsage(com.hedera.services.usage.consensus.ConsensusOpsUsage) SigMapGenerator(com.hedera.services.bdd.spec.keys.SigMapGenerator) OpObs(com.hedera.services.bdd.spec.stats.OpObs) ControlForKey(com.hedera.services.bdd.spec.keys.ControlForKey) SmartContractFeeBuilder(com.hederahashgraph.fee.SmartContractFeeBuilder) UUID(java.util.UUID) UtilOp(com.hedera.services.bdd.spec.utilops.UtilOp) ByteString(com.google.protobuf.ByteString) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) Transaction(com.hederahashgraph.api.proto.java.Transaction) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) FeeBuilder(com.hederahashgraph.fee.FeeBuilder) OptionalDouble(java.util.OptionalDouble) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NodeConnectInfo(com.hedera.services.bdd.spec.props.NodeConnectInfo) TxnUtils(com.hedera.services.bdd.spec.transactions.TxnUtils) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) SignedTransaction(com.hederahashgraph.api.proto.java.SignedTransaction) AccountID(com.hederahashgraph.api.proto.java.AccountID) MoreObjects(com.google.common.base.MoreObjects) TransactionID(com.hederahashgraph.api.proto.java.TransactionID) HexFormat(java.util.HexFormat) CryptoFeeBuilder(com.hederahashgraph.fee.CryptoFeeBuilder) Consumer(java.util.function.Consumer) Key(com.hederahashgraph.api.proto.java.Key) Collectors.toList(java.util.stream.Collectors.toList) FileOpsUsage(com.hedera.services.usage.file.FileOpsUsage) TxnUtils.extractTxnId(com.hedera.services.bdd.spec.transactions.TxnUtils.extractTxnId) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) CryptoOpsUsage(com.hedera.services.usage.crypto.CryptoOpsUsage) Transaction(com.hederahashgraph.api.proto.java.Transaction) SignedTransaction(com.hederahashgraph.api.proto.java.SignedTransaction) FileFeeBuilder(com.hederahashgraph.fee.FileFeeBuilder) SmartContractFeeBuilder(com.hederahashgraph.fee.SmartContractFeeBuilder) FeeBuilder(com.hederahashgraph.fee.FeeBuilder) CryptoFeeBuilder(com.hederahashgraph.fee.CryptoFeeBuilder) ControlForKey(com.hedera.services.bdd.spec.keys.ControlForKey) Key(com.hederahashgraph.api.proto.java.Key)

Example 20 with Key

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

the class OverlappingKeyGenerator method genEd25519AndUpdateMap.

@Override
public Key genEd25519AndUpdateMap(Map<String, PrivateKey> mutablePkMap) {
    Key key = precomputed.get(nextKey);
    nextKey = (nextKey + 1) % precomputed.size();
    String hexPubKey = CommonUtils.hex(key.getEd25519().toByteArray());
    mutablePkMap.put(hexPubKey, pkMap.get(hexPubKey));
    return key;
}
Also used : ByteString(com.google.protobuf.ByteString) Key(com.hederahashgraph.api.proto.java.Key) PrivateKey(java.security.PrivateKey)

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