use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.
the class SafetyRules method createVote.
public Vote createVote(VerifiedVertex proposedVertex, BFTHeader proposedHeader, long timestamp, HighQC highQC) {
final VoteData voteData = constructVoteData(proposedVertex, proposedHeader);
final var voteHash = Vote.getHashOfData(hasher, voteData, timestamp);
// TODO make signing more robust by including author in signed hash
final ECDSASignature signature = this.signer.sign(voteHash);
return new Vote(this.self, voteData, timestamp, signature, highQC, Optional.empty());
}
use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.
the class REParser method parse.
public ParsedTxn parse(Txn txn) throws TxnParseException {
UInt256 feePaid = null;
ECDSASignature sig = null;
int sigPosition = 0;
var parserState = new ParserState(txn);
var buf = ByteBuffer.wrap(txn.getPayload());
while (buf.hasRemaining()) {
if (sig != null) {
throw new TxnParseException(parserState, "Signature must be last");
}
var curPos = buf.position();
parserState.pos(curPos);
final var inst = readInstruction(parserState, buf);
parserState.nextInstruction(inst);
if (inst.isStateUpdate()) {
parserState.substateUpdate(inst.getMicroOp().getOp());
} else if (inst.getMicroOp().getOp() == REOp.READ || inst.getMicroOp().getOp() == REOp.READINDEX) {
parserState.read();
} else if (inst.getMicroOp() == REInstruction.REMicroOp.HEADER) {
parserState.header(inst.getData());
} else if (inst.getMicroOp() == REInstruction.REMicroOp.SYSCALL) {
try {
var callData = inst.<CallData>getData();
byte id = callData.get(0);
var syscall = Syscall.of(id).orElseThrow(() -> new TxnParseException(parserState, "Invalid call data type: " + id));
switch(syscall) {
case FEE_RESERVE_PUT:
if (feePaid != null) {
throw new TxnParseException(parserState, "Should only pay fees once.");
}
feePaid = callData.getUInt256(1);
break;
case FEE_RESERVE_TAKE:
if (feePaid == null) {
throw new TxnParseException(parserState, "No fees paid");
}
var takeAmount = callData.getUInt256(1);
if (takeAmount.compareTo(feePaid) > 0) {
throw new TxnParseException(parserState, "Trying to take more fees than paid");
}
feePaid = feePaid.subtract(takeAmount);
break;
case READDR_CLAIM:
break;
// TODO: Along with FeeConstraintScrypt.java
default:
throw new TxnParseException(parserState, "Invalid call data type: " + id);
}
} catch (CallDataAccessException | TrailingBytesException e) {
throw new TxnParseException(parserState, e);
}
} else if (inst.getMicroOp() == REInstruction.REMicroOp.MSG) {
parserState.msg(inst.getData());
} else if (inst.getMicroOp() == REInstruction.REMicroOp.END) {
parserState.end();
} else if (inst.getMicroOp() == REInstruction.REMicroOp.SIG) {
sigPosition = curPos;
sig = inst.getData();
} else {
throw new TxnParseException(parserState, "Unknown CM Op " + inst.getMicroOp());
}
}
parserState.finish();
return new ParsedTxn(txn, feePaid, parserState.instructions, parserState.msg, sig == null ? null : Pair.of(calculatePayloadHash(txn, sigPosition), sig), parserState.disableResourceAllocAndDestroy);
}
use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.
the class KeyVerifyTestScenarioRunner method doRunTestVector.
@Override
public void doRunTestVector(KeyVerifyTestVector testVector) throws AssertionError {
ECPublicKey publicKey = null;
try {
publicKey = ECPublicKey.fromBytes(fromHexString(testVector.input.publicKeyUncompressed));
} catch (Exception e) {
throw new AssertionError("Failed to construct public key from hex", e);
}
ECDSASignature signature = ECDSASignature.decodeFromDER(fromHexString(testVector.input.signatureDerEncoded));
byte[] hashedMessageToVerify = sha256Hash(fromHexString(testVector.input.msg));
assertEquals(testVector.expected.isValid, publicKey.verify(hashedMessageToVerify, signature));
}
use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.
the class KeySignTestScenarioRunner method doRunTestVector.
@Override
public void doRunTestVector(KeySignTestVector testVector) throws AssertionError {
ECKeyPair keyPair = null;
try {
keyPair = ECKeyPair.fromPrivateKey(Bytes.fromHexString(testVector.input.privateKey));
} catch (Exception e) {
throw new AssertionError("Failed to construct private key from hex", e);
}
byte[] unhashedEncodedMessage = testVector.input.messageToSign.getBytes(StandardCharsets.UTF_8);
byte[] hashedMessageToSign = sha256Hash(unhashedEncodedMessage);
ECDSASignature signature = keyPair.sign(hashedMessageToSign, true, true);
assertEquals(testVector.expected.signature.r, signature.getR().toString(16));
assertEquals(testVector.expected.signature.s, signature.getS().toString(16));
}
use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.
the class SafetyRules method signProposal.
/**
* Create a signed proposal from a vertex
*
* @param proposedVertex vertex to sign
* @param highestCommittedQC highest known committed QC
* @param highestTC highest known TC
* @return signed proposal object for consensus
*/
public Optional<Proposal> signProposal(VerifiedVertex proposedVertex, QuorumCertificate highestCommittedQC, Optional<TimeoutCertificate> highestTC) {
final Builder safetyStateBuilder = this.state.toBuilder();
if (!checkLocked(proposedVertex, safetyStateBuilder)) {
return Optional.empty();
}
this.state = safetyStateBuilder.build();
final ECDSASignature signature = this.signer.sign(proposedVertex.getId());
return Optional.of(new Proposal(proposedVertex.toSerializable(), highestCommittedQC, signature, highestTC));
}
Aggregations