Coverage Summary for Class: TransactionBuilder (org.ethereum.core)
Class |
Class, %
|
Method, %
|
Line, %
|
TransactionBuilder |
100%
(1/1)
|
85.7%
(18/21)
|
90%
(36/40)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.ethereum.core;
20
21 import co.rsk.core.Coin;
22 import co.rsk.core.RskAddress;
23 import org.bouncycastle.util.BigIntegers;
24 import org.bouncycastle.util.encoders.Hex;
25 import org.ethereum.util.ByteUtil;
26 import org.ethereum.util.RLP;
27
28 import java.math.BigInteger;
29
30 public final class TransactionBuilder {
31 private boolean isLocalCall = false;
32 private byte[] nonce = ByteUtil.cloneBytes(null);
33 private Coin value = Coin.ZERO;
34 private RskAddress receiveAddress = RskAddress.nullAddress();
35 private Coin gasPrice = Coin.ZERO;
36 private byte[] gasLimit = ByteUtil.cloneBytes(null);
37 private byte[] data = ByteUtil.cloneBytes(null);
38 private byte chainId = 0;
39
40 TransactionBuilder() {
41 }
42
43 public TransactionBuilder value(BigInteger value) {
44 return this.value(BigIntegers.asUnsignedByteArray(value));
45 }
46
47 public TransactionBuilder gasLimit(BigInteger limit) {
48 return this.gasLimit(BigIntegers.asUnsignedByteArray(limit));
49 }
50
51 public TransactionBuilder gasPrice(BigInteger price) {
52 return this.gasPrice(price.toByteArray());
53 }
54
55 public TransactionBuilder nonce(BigInteger nonce) {
56 return this.nonce(BigIntegers.asUnsignedByteArray(nonce));
57 }
58
59 public TransactionBuilder isLocalCall(boolean isLocalCall) {
60 this.isLocalCall = isLocalCall;
61 return this;
62 }
63
64 public TransactionBuilder nonce(byte[] nonce) {
65 this.nonce = ByteUtil.cloneBytes(nonce);
66 return this;
67 }
68
69 public TransactionBuilder value(Coin value) {
70 this.value = value;
71 return this;
72 }
73
74 public TransactionBuilder value(byte[] value) {
75 this.value(RLP.parseCoinNullZero(ByteUtil.cloneBytes(value)));
76 return this;
77 }
78
79 public TransactionBuilder destination(RskAddress receiveAddress) {
80 this.receiveAddress = receiveAddress;
81 return this;
82 }
83
84 public TransactionBuilder destination(byte[] receiveAddress) {
85 return this.destination(RLP.parseRskAddress(ByteUtil.cloneBytes(receiveAddress)));
86 }
87
88 public TransactionBuilder gasPrice(Coin gasPrice) {
89 this.gasPrice = gasPrice;
90 return this;
91 }
92
93 public TransactionBuilder gasPrice(byte[] gasPrice) {
94 this.gasPrice(RLP.parseCoinNonNullZero(ByteUtil.cloneBytes(gasPrice)));
95 return this;
96 }
97
98 public TransactionBuilder gasLimit(byte[] gasLimit) {
99 this.gasLimit = ByteUtil.cloneBytes(gasLimit);
100 return this;
101 }
102
103 public TransactionBuilder data(byte[] data) {
104 this.data = ByteUtil.cloneBytes(data);
105 return this;
106 }
107
108 public TransactionBuilder chainId(byte chainId) {
109 this.chainId = chainId;
110 return this;
111 }
112
113 public TransactionBuilder destination(String to) {
114 return this.destination(to == null ? null : Hex.decode(to));
115 }
116
117 public TransactionBuilder gasLimit(Coin value) {
118 return this.gasLimit(value.getBytes());
119 }
120
121 public TransactionBuilder nonce(Coin value) {
122 return this.nonce(value.getBytes());
123 }
124
125 public Transaction build() {
126 return new Transaction(this.nonce, this.gasPrice, this.gasLimit, this.receiveAddress, this.value, this.data, this.chainId, this.isLocalCall);
127 }
128
129 public TransactionBuilder data(String data) {
130 return this.data(data == null ? null: Hex.decode(data));
131 }
132 }