Search in sources :

Example 11 with Key

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

the class JKeySerializerTest method jKeyProtoSerDes.

@Test
void jKeyProtoSerDes() throws IOException, DecoderException {
    final Map<String, PrivateKey> pubKey2privKeyMap = new HashMap<>();
    Key protoKey;
    JKey jkey = null;
    List<JKey> jListBefore = null;
    // Jkey will have JEd25519Key,JECDSASecp256K1Key,JThresholdKey,JKeyList
    protoKey = genSampleComplexKey(2, pubKey2privKeyMap);
    jkey = JKey.mapKey(protoKey);
    jListBefore = jkey.getKeyList().getKeysList();
    byte[] serializedJKey = jkey.serialize();
    try (final var in = new ByteArrayInputStream(serializedJKey);
        final var dis = new DataInputStream(in)) {
        final JKey jKeyReborn = JKeySerializer.deserialize(dis);
        // Write Top Assertions Here
        assertAll("JKeyRebornChecks-Top Level", () -> assertNotNull(jKeyReborn), () -> assertTrue(jKeyReborn instanceof JKeyList), () -> assertTrue(jKeyReborn.hasKeyList()), () -> assertFalse(jKeyReborn.hasThresholdKey()));
        final var jListAfter = jKeyReborn.getKeyList().getKeysList();
        assertEquals(jListBefore.size(), jListAfter.size());
        for (int i = 0; i < jListBefore.size(); i++) {
            assertTrue(equalUpToDecodability(jListBefore.get(i), jListAfter.get(i)));
        }
    }
}
Also used : PrivateKey(java.security.PrivateKey) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) DataInputStream(java.io.DataInputStream) 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)

Example 12 with Key

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

the class JKeySerializerTest method genEd25519Keys.

/**
 * Generates a list of Ed25519 keys.
 *
 * @param pubKey2privKeyMap
 * 		map of public key hex string as key and the private key as value
 * @return a list of generated Ed25519 keys
 */
private static List<Key> genEd25519Keys(final int numKeys, final Map<String, PrivateKey> pubKey2privKeyMap) {
    final List<Key> rv = new ArrayList<>();
    for (int i = 0; i < numKeys; i++) {
        final var pair = new KeyPairGenerator().generateKeyPair();
        final var pubKey = ((EdDSAPublicKey) pair.getPublic()).getAbyte();
        final var key = Key.newBuilder().setEd25519(ByteString.copyFrom(pubKey)).build();
        final var pubKeyHex = CommonUtils.hex(pubKey);
        pubKey2privKeyMap.put(pubKeyHex, pair.getPrivate());
        rv.add(key);
    }
    return rv;
}
Also used : EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) ArrayList(java.util.ArrayList) KeyPairGenerator(net.i2p.crypto.eddsa.KeyPairGenerator) 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 13 with Key

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

the class JKeyTest method convertsECDSAsecp256k1Key.

@Test
void convertsECDSAsecp256k1Key() {
    ByteString edcsaSecp256K1Bytes = ByteString.copyFrom(new byte[] { 0x02 }).concat(TxnUtils.randomUtf8ByteString(JECDSASecp256k1Key.ECDSASECP256_COMPRESSED_BYTE_LENGTH - 1));
    final Key aKey = Key.newBuilder().setECDSASecp256K1(edcsaSecp256K1Bytes).build();
    var validEDCSAsecp256K1Key = assertDoesNotThrow(() -> JKey.convertKey(aKey, 1));
    assertTrue(validEDCSAsecp256K1Key instanceof JECDSASecp256k1Key);
    assertEquals(33, validEDCSAsecp256K1Key.getECDSASecp256k1Key().length);
    assertTrue(validEDCSAsecp256K1Key.isValid());
    assertTrue(Arrays.equals(edcsaSecp256K1Bytes.toByteArray(), validEDCSAsecp256K1Key.getECDSASecp256k1Key()));
}
Also used : ByteString(com.google.protobuf.ByteString) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 14 with Key

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

the class JKeyTest method positiveConvertKeyTest.

@Test
void positiveConvertKeyTest() {
    // given:
    final Key aKey = TxnHandlingScenario.COMPLEX_KEY_ACCOUNT_KT.asKey();
    // expect:
    assertDoesNotThrow(() -> JKey.convertKey(aKey, 1));
}
Also used : Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 15 with Key

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

the class JKeyUtils method genEd25519Keys.

/**
 * Generates a list of Ed25519 keys.
 *
 * @param pubKey2privKeyMap
 * 		map of public key hex string as key and the private key as value
 * @return a list of generated Ed25519 keys
 */
private static List<Key> genEd25519Keys(final int numKeys, final Map<String, PrivateKey> pubKey2privKeyMap) {
    final List<Key> rv = new ArrayList<>();
    for (int i = 0; i < numKeys; i++) {
        final var pair = new KeyPairGenerator().generateKeyPair();
        final var pubKey = ((EdDSAPublicKey) pair.getPublic()).getAbyte();
        final var key = Key.newBuilder().setEd25519(ByteString.copyFrom(pubKey)).build();
        final var pubKeyHex = CommonUtils.hex(pubKey);
        pubKey2privKeyMap.put(pubKeyHex, pair.getPrivate());
        rv.add(key);
    }
    return rv;
}
Also used : EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) ArrayList(java.util.ArrayList) KeyPairGenerator(net.i2p.crypto.eddsa.KeyPairGenerator) Key(com.hederahashgraph.api.proto.java.Key) PrivateKey(java.security.PrivateKey) ThresholdKey(com.hederahashgraph.api.proto.java.ThresholdKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey)

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