Search in sources :

Example 6 with Key

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

the class KeyFactory method loadControlMap.

@SuppressWarnings("unchecked")
public void loadControlMap(String path) {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    log.info("Deserialize controlMap from : " + path);
    try {
        fis = new FileInputStream(path);
        ois = new ObjectInputStream(fis);
        controlMap = (Map<Key, SigControl>) ois.readObject();
        ois.close();
        fis.close();
        fis = null;
    } catch (Exception e) {
        log.error("De-serializable exception catched while working on " + path + ":" + e);
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            log.error("File closing exception catched while closing " + path + ":" + e);
        }
    }
    log.info(" Sucessfully de-serialized controlMap from " + path);
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PrivateKey(java.security.PrivateKey) ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) Ed25519PrivateKey(com.hedera.services.bdd.suites.utils.keypairs.Ed25519PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) SpecKey(com.hedera.services.bdd.spec.persistence.SpecKey) Key(com.hederahashgraph.api.proto.java.Key) KeyStoreException(java.security.KeyStoreException) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) ObjectInputStream(java.io.ObjectInputStream)

Example 7 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)

Example 8 with Key

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

the class JThresholdKeyTest method JThresholdKeyWithVariousThresholdTest.

@Test
void JThresholdKeyWithVariousThresholdTest() throws Exception {
    Key validContractIDKey = Key.newBuilder().setContractID(ContractID.newBuilder().setContractNum(1L).build()).build();
    Key validRSA3072Key = Key.newBuilder().setRSA3072(TxnUtils.randomUtf8ByteString(16)).build();
    KeyList validKeyList = KeyList.newBuilder().addKeys(validContractIDKey).addKeys(validRSA3072Key).build();
    assertFalse(jThresholdKey(validKeyList, 0).isValid());
    assertTrue(jThresholdKey(validKeyList, 1).isValid());
    assertTrue(jThresholdKey(validKeyList, 2).isValid());
    assertFalse(jThresholdKey(validKeyList, 3).isValid());
}
Also used : KeyList(com.hederahashgraph.api.proto.java.KeyList) Key(com.hederahashgraph.api.proto.java.Key) JKeyListTest.randomValidECDSASecp256K1Key(com.hedera.services.legacy.core.jproto.JKeyListTest.randomValidECDSASecp256K1Key) ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) Test(org.junit.jupiter.api.Test)

Example 9 with Key

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

the class JThresholdKeyTest method invalidJThresholdKeyTest.

@Test
void invalidJThresholdKeyTest() throws Exception {
    Key validED25519Key = Key.newBuilder().setEd25519(TxnUtils.randomUtf8ByteString(JEd25519Key.ED25519_BYTE_LENGTH)).build();
    Key validECDSA384Key = Key.newBuilder().setECDSA384(TxnUtils.randomUtf8ByteString(24)).build();
    Key validECDSASecp256Key = randomValidECDSASecp256K1Key();
    KeyList invalidKeyList1 = KeyList.newBuilder().build();
    Key invalidKey1 = thresholdKey(invalidKeyList1, 1);
    KeyList invalidKeyList2 = KeyList.newBuilder().addKeys(validED25519Key).addKeys(invalidKey1).build();
    Key invalidKey2 = thresholdKey(invalidKeyList2, 2);
    KeyList invalidKeyList3 = KeyList.newBuilder().addKeys(validECDSA384Key).addKeys(invalidKey2).build();
    Key invalidKey3 = thresholdKey(invalidKeyList2, 2);
    KeyList invalidKeyList4 = KeyList.newBuilder().addKeys(validECDSASecp256Key).addKeys(invalidKey3).build();
    JKey jThresholdKey1 = JKey.convertKey(invalidKey1, 1);
    assertFalse(jThresholdKey1.isValid());
    JKey jThresholdKey2 = JKey.convertKey(invalidKey2, 1);
    assertFalse(jThresholdKey2.isValid());
    assertFalse(jThresholdKey(invalidKeyList3, 1).isValid());
    assertFalse(jThresholdKey(invalidKeyList4, 1).isValid());
}
Also used : KeyList(com.hederahashgraph.api.proto.java.KeyList) Key(com.hederahashgraph.api.proto.java.Key) JKeyListTest.randomValidECDSASecp256K1Key(com.hedera.services.legacy.core.jproto.JKeyListTest.randomValidECDSASecp256K1Key) ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) Test(org.junit.jupiter.api.Test)

Example 10 with Key

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

the class JKeySerializerTest method jKeyECDSASecp256k1KeySerDes.

@Test
void jKeyECDSASecp256k1KeySerDes() throws Exception {
    final Map<String, PrivateKey> pubKey2privKeyMap = new HashMap<>();
    Key protoKey;
    protoKey = genSingleECDSASecp256k1Key(pubKey2privKeyMap);
    JKey jkey = JKey.mapKey(protoKey);
    byte[] serializedJKey = null;
    try {
        serializedJKey = jkey.serialize();
    } catch (IOException ignore) {
    }
    try (final var in = new ByteArrayInputStream(serializedJKey);
        final var dis = new DataInputStream(in)) {
        final JKey jKeyReborn = JKeySerializer.deserialize(dis);
        assertAll("JKeyRebornChecks-Top Level", () -> assertNotNull(jKeyReborn), () -> assertTrue(jKeyReborn instanceof JECDSASecp256k1Key), () -> assertFalse(jKeyReborn.hasKeyList()), () -> assertFalse(jKeyReborn.hasThresholdKey()));
    } catch (Exception e) {
        throw new IllegalStateException(String.format("Failed to deserialize!", e));
    }
}
Also used : PrivateKey(java.security.PrivateKey) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DecoderException(org.apache.commons.codec.DecoderException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) Key(com.hederahashgraph.api.proto.java.Key) PrivateKey(java.security.PrivateKey) ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) 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