|
| 1 | +package com.wavesplatform.transactions; |
| 2 | + |
| 3 | +import com.google.common.primitives.Ints; |
| 4 | +import com.wavesplatform.crypto.BlsUtils; |
| 5 | +import com.wavesplatform.crypto.Bytes; |
| 6 | +import com.wavesplatform.transactions.account.BlsPublicKey; |
| 7 | +import com.wavesplatform.transactions.account.BlsSignature; |
| 8 | +import com.wavesplatform.transactions.account.PrivateKey; |
| 9 | +import com.wavesplatform.transactions.account.PublicKey; |
| 10 | +import com.wavesplatform.transactions.common.Amount; |
| 11 | +import com.wavesplatform.transactions.common.Proof; |
| 12 | +import supranational.blst.SecretKey; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +public class CommitToGenerationTransaction extends Transaction { |
| 19 | + |
| 20 | + public static final int TYPE = 20; |
| 21 | + public static final int LATEST_VERSION = 1; |
| 22 | + public static final long MIN_FEE = 100_00000; |
| 23 | + |
| 24 | + private final Integer generationPeriodStart; |
| 25 | + private final BlsPublicKey endorserPublicKey; |
| 26 | + private final BlsSignature commitmentSignature; |
| 27 | + |
| 28 | + public CommitToGenerationTransaction(PublicKey sender, int generationPeriodStart, BlsPublicKey endorserPublicKey, BlsSignature commitmentSignature, Amount fee, int version, byte chainId, long timestamp, List<Proof> proofs) { |
| 29 | + super(TYPE, version, chainId, sender, fee, timestamp, proofs); |
| 30 | + this.generationPeriodStart = generationPeriodStart; |
| 31 | + this.endorserPublicKey = endorserPublicKey; |
| 32 | + this.commitmentSignature = commitmentSignature; |
| 33 | + } |
| 34 | + |
| 35 | + public static CommitToGenerationTransaction fromBytes(byte[] bytes) throws IOException { |
| 36 | + return (CommitToGenerationTransaction) Transaction.fromBytes(bytes); |
| 37 | + } |
| 38 | + |
| 39 | + public static CommitToGenerationTransaction fromJson(String json) throws IOException { |
| 40 | + return (CommitToGenerationTransaction) Transaction.fromJson(json); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public int generationPeriodStart() { |
| 45 | + return generationPeriodStart; |
| 46 | + } |
| 47 | + |
| 48 | + public BlsPublicKey endorserPublicKey() { |
| 49 | + return endorserPublicKey; |
| 50 | + } |
| 51 | + |
| 52 | + public BlsSignature commitmentSignature() { |
| 53 | + return commitmentSignature; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean equals(Object o) { |
| 58 | + if (this == o) return true; |
| 59 | + if (o == null || getClass() != o.getClass()) return false; |
| 60 | + if (!super.equals(o)) return false; |
| 61 | + CommitToGenerationTransaction that = (CommitToGenerationTransaction) o; |
| 62 | + return this.generationPeriodStart.equals(that.generationPeriodStart) |
| 63 | + && this.endorserPublicKey.equals(that.endorserPublicKey) |
| 64 | + && this.commitmentSignature.equals(that.commitmentSignature); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int hashCode() { |
| 69 | + return Objects.hash(super.hashCode(), generationPeriodStart, endorserPublicKey, commitmentSignature); |
| 70 | + } |
| 71 | + |
| 72 | + public static CommitToGenerationTransactionBuilder builder(int generationPeriodStart) { |
| 73 | + return new CommitToGenerationTransactionBuilder(generationPeriodStart); |
| 74 | + } |
| 75 | + |
| 76 | + public static class CommitToGenerationTransactionBuilder |
| 77 | + extends TransactionBuilder<CommitToGenerationTransactionBuilder, CommitToGenerationTransaction> { |
| 78 | + private final Integer generationPeriodStart; |
| 79 | + private BlsPublicKey endorserPublicKey; |
| 80 | + private BlsSignature commitmentSignature; |
| 81 | + |
| 82 | + protected CommitToGenerationTransactionBuilder(int generationPeriodStart) { |
| 83 | + super(LATEST_VERSION, MIN_FEE); |
| 84 | + this.generationPeriodStart = generationPeriodStart; |
| 85 | + this.endorserPublicKey = null; |
| 86 | + this.commitmentSignature = null; |
| 87 | + } |
| 88 | + |
| 89 | + public CommitToGenerationTransactionBuilder endorserPublicKey(BlsPublicKey pk) { |
| 90 | + this.endorserPublicKey = pk; |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + public CommitToGenerationTransactionBuilder commitmentSignature(BlsSignature commitmentSignature) { |
| 95 | + this.commitmentSignature = commitmentSignature; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + protected CommitToGenerationTransaction _build() { |
| 100 | + return new CommitToGenerationTransaction(sender, generationPeriodStart, endorserPublicKey, commitmentSignature, feeWithExtra(), version, chainId, timestamp, Proof.emptyList()); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public CommitToGenerationTransaction getSignedWith(PrivateKey privateKey) { |
| 105 | + if (sender == null) { |
| 106 | + sender = privateKey.publicKey(); |
| 107 | + } |
| 108 | + CommitToGenerationTransaction unsignedTx = getUnsigned(); |
| 109 | + SecretKey blsSk = BlsUtils.mkBlsSecretKey(privateKey.bytes()); |
| 110 | + |
| 111 | + byte[] blsPubBytes = this.endorserPublicKey != null |
| 112 | + ? this.endorserPublicKey.bytes() |
| 113 | + : BlsUtils.mkBlsPublicKey(blsSk); |
| 114 | + |
| 115 | + byte[] blsSigBytes = this.commitmentSignature != null |
| 116 | + ? this.commitmentSignature.bytes() |
| 117 | + : BlsUtils.sign(blsSk, Bytes.concat(blsPubBytes, Ints.toByteArray(unsignedTx.generationPeriodStart())) |
| 118 | + ); |
| 119 | + |
| 120 | + CommitToGenerationTransaction txWithBls = new CommitToGenerationTransaction( |
| 121 | + unsignedTx.sender(), |
| 122 | + unsignedTx.generationPeriodStart(), |
| 123 | + BlsPublicKey.as(blsPubBytes), |
| 124 | + BlsSignature.as(blsSigBytes), |
| 125 | + unsignedTx.fee(), |
| 126 | + unsignedTx.version(), |
| 127 | + unsignedTx.chainId(), |
| 128 | + unsignedTx.timestamp(), |
| 129 | + Proof.emptyList() |
| 130 | + ); |
| 131 | + |
| 132 | + return txWithBls.addProof(privateKey); |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | +} |
0 commit comments