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