|
| 1 | +/* |
| 2 | + * Copyright (C) by MinterTeam. 2018 |
| 3 | + * @link <a href="https://github.com/MinterTeam">Org Github</a> |
| 4 | + * @link <a href="https://github.com/edwardstock">Maintainer Github</a> |
| 5 | + * |
| 6 | + * The MIT License |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +package network.minter.blockchain.models.operational; |
| 28 | + |
| 29 | +import com.edwardstock.secp256k1.NativeSecp256k1; |
| 30 | + |
| 31 | +import java.math.BigDecimal; |
| 32 | +import java.math.BigInteger; |
| 33 | + |
| 34 | +import network.minter.core.MinterSDK; |
| 35 | +import network.minter.core.crypto.BytesData; |
| 36 | +import network.minter.core.crypto.HashUtil; |
| 37 | +import network.minter.core.crypto.MinterAddress; |
| 38 | +import network.minter.core.crypto.PrivateKey; |
| 39 | +import network.minter.core.util.RLP; |
| 40 | + |
| 41 | +import static network.minter.core.internal.common.Preconditions.checkArgument; |
| 42 | +import static network.minter.core.internal.common.Preconditions.checkNotNull; |
| 43 | +import static network.minter.core.internal.helpers.StringHelper.strrpad; |
| 44 | + |
| 45 | +/** |
| 46 | + * minter-android-blockchain. 2018 |
| 47 | + * @author Eduard Maximovich [edward.vstock@gmail.com] |
| 48 | + */ |
| 49 | +public class CheckTransaction { |
| 50 | + private String mPassphrase; |
| 51 | + private BigInteger mNonce; |
| 52 | + private BigInteger mDueBlock; |
| 53 | + private String mCoin = MinterSDK.DEFAULT_COIN; |
| 54 | + private BigInteger mValue; |
| 55 | + private BytesData mLock; |
| 56 | + private SignatureSingleData mSignature; |
| 57 | + |
| 58 | + CheckTransaction(BigInteger nonce, String passphrase) { |
| 59 | + mNonce = nonce; |
| 60 | + mPassphrase = passphrase; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Create check proff |
| 65 | + * @param address minter address |
| 66 | + * @param passphrase check password |
| 67 | + * @return proof bytes data |
| 68 | + */ |
| 69 | + public static BytesData makeProof(MinterAddress address, String passphrase) { |
| 70 | + BytesData key = new BytesData(HashUtil.sha256(passphrase.getBytes())); |
| 71 | + BytesData encodedAddress = new BytesData(RLP.encode(new Object[]{address.getData()})).sha3Mutable(); |
| 72 | + |
| 73 | + NativeSecp256k1.RecoverableSignature signature; |
| 74 | + long ctx = NativeSecp256k1.contextCreate(); |
| 75 | + try { |
| 76 | + signature = NativeSecp256k1.signRecoverableSerialized(ctx, encodedAddress.getData(), key.getData()); |
| 77 | + } finally { |
| 78 | + NativeSecp256k1.contextCleanup(ctx); |
| 79 | + } |
| 80 | + |
| 81 | + // carefully, hack detected! |
| 82 | + if (signature.v[0] != 1) { |
| 83 | + signature.v[0] = 0; |
| 84 | + } |
| 85 | + |
| 86 | + return new BytesData(signature.r, signature.s, signature.v); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Use this to decrease object lifetime (especially if you need to create final instance of this object) |
| 91 | + */ |
| 92 | + public void cleanup() { |
| 93 | + mPassphrase = null; |
| 94 | + mNonce = null; |
| 95 | + mDueBlock = null; |
| 96 | + mCoin = null; |
| 97 | + mValue = null; |
| 98 | + mLock = null; |
| 99 | + mSignature = null; |
| 100 | + } |
| 101 | + |
| 102 | + public String getCoin() { |
| 103 | + return mCoin.replace("\0", ""); |
| 104 | + } |
| 105 | + |
| 106 | + public BigInteger getNonce() { |
| 107 | + return mNonce; |
| 108 | + } |
| 109 | + |
| 110 | + public BigInteger getDueBlock() { |
| 111 | + return mDueBlock; |
| 112 | + } |
| 113 | + |
| 114 | + public String getPassphrase() { |
| 115 | + return mPassphrase; |
| 116 | + } |
| 117 | + |
| 118 | + public BigInteger getValue() { |
| 119 | + return mValue; |
| 120 | + } |
| 121 | + |
| 122 | + public SignatureSingleData getSignature() { |
| 123 | + return mSignature; |
| 124 | + } |
| 125 | + |
| 126 | + public TransactionSign sign(PrivateKey privateKey) { |
| 127 | + BytesData hashBytes = new BytesData(encode(true)); |
| 128 | + BytesData hash = hashBytes.sha3Data(); |
| 129 | + BytesData pk = new BytesData(mPassphrase.getBytes()).sha256Mutable(); |
| 130 | + |
| 131 | + NativeSecp256k1.RecoverableSignature lockSig; |
| 132 | + |
| 133 | + long ctx = NativeSecp256k1.contextCreate(); |
| 134 | + try { |
| 135 | + lockSig = NativeSecp256k1.signRecoverableSerialized(ctx, hash.getData(), pk.getData()); |
| 136 | + } finally { |
| 137 | + NativeSecp256k1.contextCleanup(ctx); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + // carefully, hack detected! |
| 142 | + if (lockSig.v[0] != 1) { |
| 143 | + lockSig.v[0] = 0; |
| 144 | + } |
| 145 | + |
| 146 | + mLock = new BytesData(lockSig.r, lockSig.s, lockSig.v); |
| 147 | + |
| 148 | + BytesData withLock = new BytesData(encode(false)).sha3Mutable(); |
| 149 | + |
| 150 | + NativeSecp256k1.RecoverableSignature rsv; |
| 151 | + long ctx2 = NativeSecp256k1.contextCreate(); |
| 152 | + try { |
| 153 | + rsv = NativeSecp256k1.signRecoverableSerialized(ctx2, withLock.getData(), privateKey.getData()); |
| 154 | + } finally { |
| 155 | + NativeSecp256k1.contextCleanup(ctx2); |
| 156 | + } |
| 157 | + |
| 158 | + mSignature = new SignatureSingleData(); |
| 159 | + mSignature.setSign(rsv); |
| 160 | + |
| 161 | + String signedCheck = new BytesData(encode(false)).toHexString(MinterSDK.PREFIX_CHECK); |
| 162 | + |
| 163 | + return new TransactionSign(signedCheck); |
| 164 | + } |
| 165 | + |
| 166 | + private byte[] encode(boolean forSigning) { |
| 167 | + if (forSigning) { |
| 168 | + return RLP.encode(new Object[]{ |
| 169 | + mNonce, |
| 170 | + mDueBlock, |
| 171 | + mCoin, |
| 172 | + mValue |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + final byte[] lock = mLock.getData(); |
| 177 | + if (mSignature != null && mSignature.getV() != null && mSignature.getR() != null && mSignature.getS() != null) { |
| 178 | + return RLP.encode(new Object[]{ |
| 179 | + mNonce, |
| 180 | + mDueBlock, |
| 181 | + mCoin, |
| 182 | + mValue, |
| 183 | + lock, |
| 184 | + mSignature.getV(), |
| 185 | + mSignature.getR(), |
| 186 | + mSignature.getS(), |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + return RLP.encode(new Object[]{ |
| 191 | + mNonce, |
| 192 | + mDueBlock, |
| 193 | + mCoin, |
| 194 | + mValue, |
| 195 | + lock |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + public static final class Builder { |
| 200 | + private CheckTransaction mCheck; |
| 201 | + |
| 202 | + public Builder(BigInteger nonce, String passphrase) { |
| 203 | + mCheck = new CheckTransaction(nonce, passphrase); |
| 204 | + } |
| 205 | + |
| 206 | + public Builder setCoin(String coin) { |
| 207 | + checkArgument(coin != null && coin.length() >= 3 && coin.length() <= 10, String.format("Invalid coin passed: %s", coin)); |
| 208 | + mCheck.mCoin = strrpad(10, coin); |
| 209 | + return this; |
| 210 | + } |
| 211 | + |
| 212 | + public Builder setValue(BigDecimal value) { |
| 213 | + mCheck.mValue = value.multiply(Transaction.VALUE_MUL_DEC).toBigInteger(); |
| 214 | + return this; |
| 215 | + } |
| 216 | + |
| 217 | + public Builder setValue(double value) { |
| 218 | + return setValue(new BigDecimal(value)); |
| 219 | + } |
| 220 | + |
| 221 | + public Builder setDueBlock(BigInteger dueBlockNum) { |
| 222 | + mCheck.mDueBlock = dueBlockNum; |
| 223 | + return this; |
| 224 | + } |
| 225 | + |
| 226 | + public CheckTransaction build() { |
| 227 | + checkNotNull(mCheck.mValue, "Value must be set"); |
| 228 | + checkNotNull(mCheck.mDueBlock, "Due block must be set"); |
| 229 | + checkNotNull(mCheck.mNonce, "Nonce required"); |
| 230 | + checkNotNull(mCheck.mPassphrase, "Passphrase required"); |
| 231 | + return mCheck; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments