Skip to content

Commit

Permalink
Generate valid secp256k1 key during key creation
Browse files Browse the repository at this point in the history
Signed-off-by: sdimitrov9 <stoyan.dimitrov@limechain.tech>
  • Loading branch information
sdimitrov9 committed Jan 6, 2025
1 parent 1520f14 commit d3483f1
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1147,14 +1147,23 @@ public byte[] key(KeyCase keyCase) {
var key =
switch (keyCase) {
case ECDSA_SECP256K1 -> Key.newBuilder()
.setECDSASecp256K1(ByteString.copyFrom(bytes(KEY_LENGTH_ECDSA)));
.setECDSASecp256K1(ByteString.copyFrom(generateSecp256k1Key()));
case ED25519 -> Key.newBuilder().setEd25519(ByteString.copyFrom(bytes(KEY_LENGTH_ED25519)));
default -> throw new UnsupportedOperationException("Key type not supported");
};

return key.build().toByteArray();
}

private byte[] generateSecp256k1Key() {
byte[] xCoordinate = bytes(32); // The x-coordinate of the elliptic curve point
byte prefix = (byte) (Math.random() < 0.5 ? 0x02 : 0x03); // Randomly chosen even or odd y-coordinate
ByteBuffer compressedKey = ByteBuffer.allocate(33);
compressedKey.put(prefix);
compressedKey.put(xCoordinate);
return compressedKey.array();
}

public long number() {
return id.incrementAndGet();
}
Expand Down

0 comments on commit d3483f1

Please sign in to comment.