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)));
}
}
}
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;
}
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()));
}
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));
}
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;
}
Aggregations