Coverage Summary for Class: BtcTransactionFormatUtils (co.rsk.peg.utils)
Class |
Class, %
|
Method, %
|
Line, %
|
BtcTransactionFormatUtils |
0%
(0/1)
|
0%
(0/5)
|
0%
(0/14)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2018 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.peg.utils;
20
21 import co.rsk.bitcoinj.core.Sha256Hash;
22 import co.rsk.bitcoinj.core.VarInt;
23 import org.ethereum.config.blockchain.upgrades.ActivationConfig;
24 import org.ethereum.config.blockchain.upgrades.ConsensusRule;
25
26 public class BtcTransactionFormatUtils {
27
28 private static final int MIN_BLOCK_HEADER_SIZE = 80;
29 private static final int MAX_BLOCK_HEADER_SIZE = 85;
30
31 public static Sha256Hash calculateBtcTxHash(byte[] btcTxSerialized) {
32 return Sha256Hash.wrapReversed(Sha256Hash.hashTwice(btcTxSerialized));
33 }
34
35 public static long getInputsCount(byte[] btcTxSerialized) {
36 VarInt inputsCounter = new VarInt(btcTxSerialized, 4);
37 return inputsCounter.value;
38 }
39
40 public static long getInputsCountForSegwit(byte[] btcTxSerialized) {
41 VarInt inputsCounter = new VarInt(btcTxSerialized, 4);
42
43 if (inputsCounter.value != 0) {
44 return -1;
45 }
46
47 inputsCounter = new VarInt(btcTxSerialized, 5);
48
49 if (inputsCounter.value != 1) {
50 return -1;
51 }
52
53 inputsCounter = new VarInt(btcTxSerialized, 6);
54 return inputsCounter.value;
55 }
56
57 public static boolean isBlockHeaderSize(int size, ActivationConfig.ForBlock activations) {
58 return (activations.isActive(ConsensusRule.RSKIP124) && size == MIN_BLOCK_HEADER_SIZE) ||
59 (!activations.isActive(ConsensusRule.RSKIP124) && size >= MIN_BLOCK_HEADER_SIZE
60 && size <= MAX_BLOCK_HEADER_SIZE);
61 }
62 }