use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.
the class JKeySerializerTest 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
*/
private static Key genSampleComplexKey(final int depth, final Map<String, PrivateKey> pubKey2privKeyMap) {
Key rv = null;
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 FeeBuilderTest method assertCalculateKeysMetadataThresholdKey.
@Test
void assertCalculateKeysMetadataThresholdKey() {
int[] countKeyMetatData = { 0, 0 };
KeyList thresholdKeyList = KeyList.newBuilder().addKeys(Key.newBuilder().setEd25519(ByteString.copyFromUtf8("aaaaaaaa")).build()).addKeys(Key.newBuilder().setEd25519(ByteString.copyFromUtf8("bbbbbbbbbbbbbbbbbbbbb")).build()).build();
ThresholdKey thresholdKey = ThresholdKey.newBuilder().setKeys(thresholdKeyList).setThreshold(2).build();
Key validED25519Keys = Key.newBuilder().setThresholdKey(thresholdKey).build();
assertEquals(countKeyMetatData.length, FeeBuilder.calculateKeysMetadata(validED25519Keys, countKeyMetatData).length);
assertEquals(2, FeeBuilder.calculateKeysMetadata(validED25519Keys, countKeyMetatData)[1]);
}
use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.
the class SignedTxnAccessorTest method parsesLegacyCorrectly.
@Test
void parsesLegacyCorrectly() throws Exception {
final Key aPrimitiveKey = Key.newBuilder().setEd25519(ByteString.copyFromUtf8("01234567890123456789012345678901")).build();
final ByteString aNewAlias = aPrimitiveKey.toByteString();
final AliasManager aliasManager = mock(AliasManager.class);
given(aliasManager.lookupIdBy(any())).willReturn(EntityNum.MISSING_NUM);
final long offeredFee = 100_000_000L;
var xferNoAliases = RequestBuilder.getCryptoTransferRequest(1234l, 0l, 0l, 3l, 0l, 0l, offeredFee, Timestamp.getDefaultInstance(), Duration.getDefaultInstance(), false, zeroByteMemo, 5678l, -70000l, 5679l, 70000l);
xferNoAliases = xferNoAliases.toBuilder().setSigMap(expectedMap).build();
var xferWithAutoCreation = RequestBuilder.getHbarCryptoTransferRequestToAlias(1234l, 0l, 0l, 3l, 0l, 0l, offeredFee, Timestamp.getDefaultInstance(), Duration.getDefaultInstance(), false, zeroByteMemo, 5678l, -70000l, aNewAlias, 70000l);
xferWithAutoCreation = xferWithAutoCreation.toBuilder().setSigMap(expectedMap).build();
var xferWithAliasesNoAutoCreation = RequestBuilder.getTokenTransferRequestToAlias(1234l, 0l, 0l, 3l, 0l, 0l, offeredFee, Timestamp.getDefaultInstance(), Duration.getDefaultInstance(), false, zeroByteMemo, 5678l, 5555l, -70000l, ByteString.copyFromUtf8("aaaa"), 70000l);
xferWithAliasesNoAutoCreation = xferWithAliasesNoAutoCreation.toBuilder().setSigMap(expectedMap).build();
final var body = CommonUtils.extractTransactionBody(xferNoAliases);
var accessor = SignedTxnAccessor.uncheckedFrom(xferNoAliases);
accessor.countAutoCreationsWith(aliasManager);
final var txnUsageMeta = accessor.baseUsageMeta();
assertEquals(xferNoAliases, accessor.getSignedTxnWrapper());
assertArrayEquals(xferNoAliases.toByteArray(), accessor.getSignedTxnWrapperBytes());
assertEquals(body, accessor.getTxn());
assertArrayEquals(body.toByteArray(), accessor.getTxnBytes());
assertEquals(body.getTransactionID(), accessor.getTxnId());
assertEquals(1234l, accessor.getPayer().getAccountNum());
assertEquals(HederaFunctionality.CryptoTransfer, accessor.getFunction());
assertEquals(offeredFee, accessor.getOfferedFee());
assertArrayEquals(CommonUtils.noThrowSha384HashOf(xferNoAliases.toByteArray()), accessor.getHash());
assertEquals(expectedMap, accessor.getSigMap());
assertArrayEquals("irst".getBytes(), accessor.getPkToSigsFn().sigBytesFor("f".getBytes()));
assertArrayEquals(zeroByteMemoUtf8Bytes, accessor.getMemoUtf8Bytes());
assertTrue(accessor.memoHasZeroByte());
assertEquals(FeeBuilder.getSignatureCount(accessor.getSignedTxnWrapper()), accessor.numSigPairs());
assertEquals(FeeBuilder.getSignatureSize(accessor.getSignedTxnWrapper()), accessor.sigMapSize());
assertEquals(zeroByteMemo, accessor.getMemo());
assertEquals(false, accessor.isTriggeredTxn());
assertEquals(false, accessor.canTriggerTxn());
assertEquals(0, accessor.getNumAutoCreations());
assertEquals(memoUtf8Bytes.length, txnUsageMeta.memoUtf8Bytes());
accessor = SignedTxnAccessor.uncheckedFrom(xferWithAutoCreation);
accessor.countAutoCreationsWith(aliasManager);
assertEquals(1, accessor.getNumAutoCreations());
accessor = SignedTxnAccessor.uncheckedFrom(xferWithAliasesNoAutoCreation);
accessor.countAutoCreationsWith(aliasManager);
assertEquals(0, accessor.getNumAutoCreations());
}
use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.
the class ContractInfoAsserts method adminKey.
public ContractInfoAsserts adminKey(String expectedKeyName) {
registerProvider((spec, o) -> {
Key expectedKey = spec.registry().getKey(expectedKeyName);
assertEquals(expectedKey, object2ContractInfo(o).getAdminKey(), "Bad admin key!");
});
return this;
}
use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.
the class NewSpecKey method submitOp.
@Override
protected boolean submitOp(HapiApiSpec spec) throws Throwable {
final var keyGen = generator.orElse(DEFAULT_KEY_GEN);
Key key;
if (shape.isPresent()) {
if (labels.isPresent()) {
key = spec.keys().generateSubjectTo(spec, shape.get(), keyGen, labels.get());
} else {
key = spec.keys().generateSubjectTo(spec, shape.get(), keyGen);
}
} else {
key = spec.keys().generate(spec, type.orElse(KeyType.SIMPLE), keyGen);
}
spec.registry().saveKey(name, key);
if (immediateExportLoc.isPresent() && immediateExportPass.isPresent()) {
final var exportLoc = immediateExportLoc.get();
final var exportPass = immediateExportPass.get();
exportWithPass(spec, name, exportLoc, exportPass);
if (verboseLoggingOn && yahcliLogger) {
COMMON_MESSAGES.info("Exported a newly generated key in PEM format to " + exportLoc);
}
}
if (verboseLoggingOn && !yahcliLogger) {
if (type.orElse(KeyType.SIMPLE) == KeyType.SIMPLE) {
log.info("Created simple '{}' w/ Ed25519 public key {}", name, CommonUtils.hex(key.getEd25519().toByteArray()));
} else {
log.info("Created a complex key...");
}
}
return false;
}
Aggregations