Skip to content

Commit

Permalink
Tidying up (#255)
Browse files Browse the repository at this point in the history
* Tidying up

* Fix json deserilization of mintdistribution and tweaks to `BakerKeys`.

* Fix parsing bug in blocktransactionevents
  • Loading branch information
MilkywayPirate authored Jul 31, 2023
1 parent 09c4a16 commit 4716ff2
Show file tree
Hide file tree
Showing 46 changed files with 676 additions and 440 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## Unreleased changes
- Added typing for baker keys and moved `createBakerKeys` to `BakerKeys`. Likewise the `TransactionFactory.newUpdateBakerKeys` takes
the in generated baker keys.
- Added support for `getBlockTransactionEvents` for retrieving events emitted by transactions.
- Added support for `InvokeInstance` for executing a smart contract instance locally.
- Removed `Account` instead one should simply use `AccountAddress`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import com.concordium.grpc.v2.ProtocolVersion;
import com.concordium.grpc.v2.ReleaseSchedule;
import com.concordium.grpc.v2.*;
import com.concordium.sdk.crypto.bls.BLSPublicKey;
import com.concordium.sdk.crypto.bulletproof.BulletproofGenerators;
import com.concordium.sdk.crypto.ed25519.ED25519PublicKey;
import com.concordium.sdk.crypto.elgamal.ElgamalPublicKey;
import com.concordium.sdk.crypto.pedersencommitment.PedersenCommitmentKey;
import com.concordium.sdk.crypto.pointchevalsanders.PSPublicKey;
import com.concordium.sdk.crypto.vrf.VRFPublicKey;
import com.concordium.sdk.requests.AccountQuery;
import com.concordium.sdk.requests.BlockQuery;
import com.concordium.sdk.responses.Epoch;
Expand Down Expand Up @@ -346,8 +348,8 @@ static Baker to(com.concordium.grpc.v2.AccountStakingInfo.Baker stake) {
.stakedAmount(to(stake.getStakedAmount()))
.bakerPoolInfo(to(stake.getPoolInfo()))
.bakerId(to(stake.getBakerInfo().getBakerId()))
.bakerAggregationVerifyKey(ED25519PublicKey.from(stake.getBakerInfo().getAggregationKey().getValue().toByteArray()))
.bakerElectionVerifyKey(ED25519PublicKey.from(stake.getBakerInfo().getElectionKey().getValue().toByteArray()))
.bakerAggregationVerifyKey(BLSPublicKey.from(stake.getBakerInfo().getAggregationKey().getValue().toByteArray()))
.bakerElectionVerifyKey(VRFPublicKey.from(stake.getBakerInfo().getElectionKey().getValue().toByteArray()))
.bakerSignatureVerifyKey(ED25519PublicKey.from(stake.getBakerInfo().getSignatureKey().getValue().toByteArray()))
.build();
}
Expand Down Expand Up @@ -989,6 +991,7 @@ static Summary to(BlockItemSummary blockItemSummary) {
.type(Type.ACCOUNT_CREATION)
.accountCreationDetails(AccountCreationDetails.from(accountCreation))
.build());
break;
case UPDATE:
val details = ChainUpdateDetails.from(blockItemSummary.getUpdate());
summary.details(Details
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.concordium.sdk.crypto;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.codec.binary.Hex;

import java.io.IOException;

public class KeyJsonSerializer extends JsonSerializer<RawKey> {

@Override
public void serialize(RawKey value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(Hex.encodeHexString(value.getRawBytes()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.concordium.sdk.crypto;

/**
* Interface for keys exposed in a raw format i.e. a byte array.
*/
public interface RawKey {

byte[] getRawBytes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,68 @@
import com.concordium.sdk.crypto.CryptoJniNative;
import com.concordium.sdk.crypto.CryptoJniResultCode;
import com.concordium.sdk.crypto.NativeResolver;
import com.concordium.sdk.crypto.bls.BLSPublicKey;
import com.concordium.sdk.crypto.bls.BLSSecretKey;
import com.concordium.sdk.crypto.ed25519.ED25519PublicKey;
import com.concordium.sdk.crypto.ed25519.ED25519SecretKey;
import com.concordium.sdk.crypto.vrf.VRFPublicKey;
import com.concordium.sdk.crypto.vrf.VRFSecretKey;
import com.concordium.sdk.exceptions.CryptoJniException;
import com.concordium.sdk.requests.AccountQuery;
import com.concordium.sdk.requests.BlockQuery;
import com.concordium.sdk.responses.BakerId;
import com.concordium.sdk.responses.accountinfo.Baker;
import com.concordium.sdk.serializing.JsonMapper;
import com.concordium.sdk.transactions.TransactionFactory;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.val;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.*;
import lombok.extern.jackson.Jacksonized;

import java.io.IOException;
import java.io.Writer;

@Jacksonized
@Builder
@Data
public final class BakerKeys {
//static block to load native library
static {
NativeResolver.loadLib();
}

//Method to create baker keys
public static BakerKeysJniOutput createBakerKeys() {
/**
* New public key for participating in the election lottery.
*/
private final VRFPublicKey electionVerifyKey;
/**
* A private key for participating in the election lottery.
*/
private final VRFSecretKey electionPrivateKey;
/**
* New public key for verifying this baker's signature on finalization records.
*/
private final BLSPublicKey aggregationVerifyKey;
/**
* A secret key used by bakers and finalizers to sign finalization records.
*/
private final BLSSecretKey aggregationSignKey;
/**
* New public key for verifying this baker's signatures.
*/
private final ED25519PublicKey signatureVerifyKey;
/**
* A secret key used by a baker to sign blocks.
*/
private final ED25519SecretKey signatureSignKey;

/**
* Create a fresh set of baker keys.
* @return the generated baker keys.
*/
public static BakerKeys createBakerKeys() {

BakerKeysResult result = null;
try {
Expand All @@ -35,6 +84,31 @@ public static BakerKeysJniOutput createBakerKeys() {

}


/**
* Write the {@link BakerCredentials} to the provided Writer with the supplied baker id.
* Note that the supplied {@link BakerId} must be the one for the account that sent the
* {@link com.concordium.sdk.transactions.ConfigureBaker} transaction.
* <br>
* The baker id can be looked up via {@link com.concordium.sdk.ClientV2#getAccountInfo(BlockQuery, AccountQuery)} if
* the account is registered as a baker this will yield a {@link Baker#getBakerId()}
* @param writer where to output the baker credentials.
* @param bakerId the baker id
*/
@SneakyThrows
public void createBakerCredentials(Writer writer, BakerId bakerId) {
JsonMapper.INSTANCE.writeValue(writer, new BakerCredentials(bakerId, this));
}

@AllArgsConstructor
private static class BakerCredentials {
@JsonProperty("bakerId")
private final BakerId bakerId;
@JsonUnwrapped
private final BakerKeys bakerKeys;

}

}


This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BakerKeysResult {
* An optional `BakerKeysJniOutput` object, containing the output of the generate baker keys function.
*/
@JsonProperty("Ok")
private final Optional<BakerKeysJniOutput> ok;
private final Optional<BakerKeys> ok;

/**
* An optional `CryptoJniResultCode` object, containing an error code if the generate baker keys function failed.
Expand All @@ -28,7 +28,7 @@ public class BakerKeysResult {

@JsonCreator
BakerKeysResult(
@JsonProperty("Ok") BakerKeysJniOutput ok,
@JsonProperty("Ok") BakerKeys ok,
@JsonProperty("Err") CryptoJniResultCode err
) {
this.ok = Optional.ofNullable(ok);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public class ConfigureBakerKeysJniInput {
/**
* The baker keys that will be configured for the account
*/
private final BakerKeysJniOutput keys;
private final BakerKeys keys;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.concordium.sdk.crypto.bls;

import com.concordium.sdk.crypto.RawKey;
import com.concordium.sdk.crypto.KeyJsonSerializer;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.util.Arrays;

@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@JsonSerialize(using = KeyJsonSerializer.class)
public class BLSPublicKey implements RawKey {
/**
* The bytes of the public key.
*/
private final byte[] bytes;

@JsonCreator
public static BLSPublicKey from(String hexKey) {
try {
return from(Hex.decodeHex(hexKey));
} catch (DecoderException e) {
throw new IllegalArgumentException("Cannot create BLSPublicKey", e);
}
}

/**
* Creates an BLS Public Key from {@link byte} Array.
*
* @param bytes Input Byte Array
* @return Instance of {@link BLSPublicKey}
*/
public static BLSPublicKey from(final byte[] bytes) {
return new BLSPublicKey(Arrays.copyOf(bytes, bytes.length));
}

@Override
@JsonValue
public String toString() {
return Hex.encodeHexString(this.bytes);
}

@Override
public byte[] getRawBytes() {
return this.bytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.concordium.sdk.crypto.bls;

import com.concordium.sdk.crypto.RawKey;
import com.concordium.sdk.crypto.KeyJsonSerializer;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.util.Arrays;

@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@JsonSerialize(using = KeyJsonSerializer.class)
public class BLSSecretKey implements RawKey {
/**
* The bytes of the public key.
*/
private final byte[] bytes;

@JsonCreator
public static BLSSecretKey from(String hexKey) {
try {
return from(Hex.decodeHex(hexKey));
} catch (DecoderException e) {
throw new IllegalArgumentException("Cannot create BLSSecretKey", e);
}
}

/**
* Creates an BLS Public Key from {@link byte} Array.
*
* @param bytes Input Byte Array
* @return Instance of {@link BLSSecretKey}
*/
public static BLSSecretKey from(final byte[] bytes) {
return new BLSSecretKey(Arrays.copyOf(bytes, bytes.length));
}

@Override
@JsonValue
public String toString() {
return Hex.encodeHexString(this.bytes);
}

@Override
public byte[] getRawBytes() {
return this.bytes;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.concordium.sdk.crypto.ed25519;

import com.concordium.grpc.v2.BakerSignatureVerifyKey;
import com.concordium.sdk.crypto.RawKey;
import com.concordium.sdk.crypto.KeyJsonSerializer;
import com.concordium.sdk.exceptions.ED25519Exception;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
Expand All @@ -14,7 +17,8 @@

@Getter
@EqualsAndHashCode
public final class ED25519PublicKey {
@JsonSerialize(using = KeyJsonSerializer.class)
public final class ED25519PublicKey implements RawKey {
private final byte[] bytes;

private ED25519PublicKey(byte[] bytes) {
Expand Down Expand Up @@ -81,4 +85,9 @@ public static ED25519PublicKey from(BakerSignatureVerifyKey signKey) {
public String toString() {
return Hex.encodeHexString(this.getBytes());
}

@Override
public byte[] getRawBytes() {
return this.bytes;
}
}
Loading

0 comments on commit 4716ff2

Please sign in to comment.