Skip to content

Commit 947e1b9

Browse files
committed
refactor: EIP-7702 implementation to fix Sonar issues
Signed-off-by: emiliyank <e.kadiyski@gmail.com>
1 parent e5db881 commit 947e1b9

2 files changed

Lines changed: 84 additions & 78 deletions

File tree

sdk/src/main/java/com/hedera/hashgraph/sdk/EthereumTransactionDataEip7702.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ public class EthereumTransactionDataEip7702 extends EthereumTransactionData {
9090
byte[] callData,
9191
List<byte[]> accessList,
9292
List<AuthorizationTuple> authorizationList,
93-
byte[] recoveryId,
94-
byte[] r,
95-
byte[] s) {
93+
SignatureData signatureData) {
9694
super(callData);
9795

9896
this.chainId = chainId;
@@ -104,9 +102,9 @@ public class EthereumTransactionDataEip7702 extends EthereumTransactionData {
104102
this.value = value;
105103
this.accessList = accessList;
106104
this.authorizationList = authorizationList;
107-
this.recoveryId = recoveryId;
108-
this.r = r;
109-
this.s = s;
105+
this.recoveryId = signatureData.recoveryId;
106+
this.r = signatureData.r;
107+
this.s = signatureData.s;
110108
}
111109

112110
/**
@@ -164,9 +162,10 @@ public static EthereumTransactionDataEip7702 fromBytes(byte[] bytes) {
164162
rlpList.get(7).data(),
165163
accessList,
166164
authorizationList,
167-
rlpList.get(10).data(),
168-
rlpList.get(11).data(),
169-
rlpList.get(12).data());
165+
new SignatureData(
166+
rlpList.get(10).data(),
167+
rlpList.get(11).data(),
168+
rlpList.get(12).data()));
170169
}
171170

172171
public byte[] toBytes() {
@@ -218,6 +217,21 @@ public String toString() {
218217
.toString();
219218
}
220219

220+
/**
221+
* A helper class to hold signature data for EIP-7702 transactions.
222+
*/
223+
static class SignatureData {
224+
public byte[] recoveryId;
225+
public byte[] r;
226+
public byte[] s;
227+
228+
public SignatureData(byte[] recoveryId, byte[] r, byte[] s) {
229+
this.recoveryId = recoveryId;
230+
this.r = r;
231+
this.s = s;
232+
}
233+
}
234+
221235
/**
222236
* A tuple describing an authorization entry for EIP-7702 transactions.
223237
*/

sdk/src/test/java/com/hedera/hashgraph/sdk/EthereumTransactionDataEip7702Test.java

Lines changed: 61 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public void eip7702ToFromBytes() {
2121
Hex.decode("11"),
2222
Hex.decode("12"));
2323

24+
var signatureData = new EthereumTransactionDataEip7702.SignatureData(
25+
Hex.decode("01"),
26+
Hex.decode("df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479"),
27+
Hex.decode("1aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66"));
28+
2429
var data = new EthereumTransactionDataEip7702(
2530
Hex.decode("012a"),
2631
Hex.decode("02"),
@@ -32,9 +37,7 @@ public void eip7702ToFromBytes() {
3237
Hex.decode("123456"),
3338
List.of(),
3439
List.of(authorizationTuple),
35-
Hex.decode("01"),
36-
Hex.decode("df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479"),
37-
Hex.decode("1aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66"));
40+
signatureData);
3841

3942
var encodedHex = Hex.toHexString(data.toBytes());
4043
var decoded = (EthereumTransactionDataEip7702) EthereumTransactionData.fromBytes(Hex.decode(encodedHex));
@@ -76,6 +79,11 @@ void manualEncodingMatchesHeadlongSequence() {
7679
Hex.decode("11"),
7780
Hex.decode("12"));
7881

82+
var signatureData = new EthereumTransactionDataEip7702.SignatureData(
83+
Hex.decode("01"),
84+
Hex.decode("df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479"),
85+
Hex.decode("1aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66"));
86+
7987
var data = new EthereumTransactionDataEip7702(
8088
Hex.decode("012a"),
8189
Hex.decode("00"),
@@ -87,9 +95,7 @@ void manualEncodingMatchesHeadlongSequence() {
8795
Hex.decode("123456"),
8896
List.of(),
8997
List.of(authorizationTuple),
90-
Hex.decode("01"),
91-
Hex.decode("df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479"),
92-
Hex.decode("1aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66"));
98+
signatureData);
9399

94100
var manualPayload = com.esaulpaugh.headlong.rlp.RLPEncoder.list(
95101
data.chainId,
@@ -122,76 +128,62 @@ void manualEncodingMatchesHeadlongSequence() {
122128

123129
@Test
124130
void toBytesAndFromBytesPreserveAllFieldValues() {
125-
// 1. Setup Data
126-
var chainId = Hex.decode("012a");
127-
var nonce = Hex.decode("00");
128-
var maxPriorityGas = Hex.decode("01");
129-
var maxGas = Hex.decode("d1385c7bf0");
130-
var gasLimit = Hex.decode("07A120");
131-
var to = Hex.decode("00000000000000000000000000000000000003f9");
132-
var value = new byte[0]; // Empty value
133-
var callData = Hex.decode("123456");
134-
List<byte[]> accessList = List.of(); // Empty access list
135-
var recId = Hex.decode("01");
136-
var r = Hex.decode("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890");
137-
var s = Hex.decode("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef");
138-
139-
// Authorization List setup
140-
var authChainId = Hex.decode("012a");
141-
var authAddress = Hex.decode("00000000000000000000000000000000000003f9");
142-
var authNonce = Hex.decode("00");
143-
var authYParity = Hex.decode("01");
144-
var authR = Hex.decode("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef");
145-
var authS = Hex.decode("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890");
146-
147-
// Use AuthorizationTuple
148-
var authData = new EthereumTransactionDataEip7702.AuthorizationTuple(
149-
authChainId, authAddress, authNonce, authYParity, authR, authS);
150-
151-
// 2. Create Instance
152-
var original = new EthereumTransactionDataEip7702(
153-
chainId,
154-
nonce,
155-
maxPriorityGas,
156-
maxGas,
157-
gasLimit,
158-
to,
159-
value,
160-
callData,
161-
accessList,
162-
List.of(authData), // authorizationList
163-
recId,
164-
r,
165-
s);
166-
167-
// 3. Round Trip
131+
var original = createEip7702Data();
132+
168133
var bytes = original.toBytes();
169134
var decoded = EthereumTransactionDataEip7702.fromBytes(bytes);
170135

171-
// 4. Assertions
172-
assertThat(decoded.chainId).containsExactly(chainId);
173-
assertThat(decoded.nonce).containsExactly(nonce);
174-
assertThat(decoded.maxPriorityGas).containsExactly(maxPriorityGas);
175-
assertThat(decoded.maxGas).containsExactly(maxGas);
176-
assertThat(decoded.gasLimit).containsExactly(gasLimit);
177-
assertThat(decoded.to).containsExactly(to);
178-
assertThat(decoded.value).containsExactly(value);
179-
assertThat(decoded.callData).containsExactly(callData);
180-
assertThat(decoded.recoveryId).containsExactly(recId);
181-
assertThat(decoded.r).containsExactly(r);
182-
assertThat(decoded.s).containsExactly(s);
136+
assertThat(decoded.chainId).containsExactly(original.chainId);
137+
assertThat(decoded.nonce).containsExactly(original.nonce);
138+
assertThat(decoded.maxPriorityGas).containsExactly(original.maxPriorityGas);
139+
assertThat(decoded.maxGas).containsExactly(original.maxGas);
140+
assertThat(decoded.gasLimit).containsExactly(original.gasLimit);
141+
assertThat(decoded.to).containsExactly(original.to);
142+
assertThat(decoded.value).containsExactly(original.value);
143+
assertThat(decoded.callData).containsExactly(original.callData);
144+
assertThat(decoded.recoveryId).containsExactly(original.recoveryId);
145+
assertThat(decoded.r).containsExactly(original.r);
146+
assertThat(decoded.s).containsExactly(original.s);
183147

184148
assertThat(decoded.authorizationList).hasSize(1);
185149

150+
var originalAuth = original.authorizationList.get(0);
186151
var decodedAuth = decoded.authorizationList.get(0);
187-
assertThat(decodedAuth.chainId).containsExactly(authChainId);
188-
assertThat(decodedAuth.address).containsExactly(authAddress);
189-
assertThat(decodedAuth.nonce).containsExactly(authNonce);
190-
assertThat(decodedAuth.yParity).containsExactly(authYParity);
191-
assertThat(decodedAuth.r).containsExactly(authR);
192-
assertThat(decodedAuth.s).containsExactly(authS);
193-
194-
// Verify the byte array matches exactly
152+
assertThat(decodedAuth.chainId).containsExactly(originalAuth.chainId);
153+
assertThat(decodedAuth.address).containsExactly(originalAuth.address);
154+
assertThat(decodedAuth.nonce).containsExactly(originalAuth.nonce);
155+
assertThat(decodedAuth.yParity).containsExactly(originalAuth.yParity);
156+
assertThat(decodedAuth.r).containsExactly(originalAuth.r);
157+
assertThat(decodedAuth.s).containsExactly(originalAuth.s);
158+
195159
assertThat(decoded.toBytes()).containsExactly(bytes);
196160
}
161+
162+
private EthereumTransactionDataEip7702 createEip7702Data() {
163+
var authData = new EthereumTransactionDataEip7702.AuthorizationTuple(
164+
Hex.decode("012a"),
165+
Hex.decode("00000000000000000000000000000000000003f9"),
166+
Hex.decode("00"),
167+
Hex.decode("01"),
168+
Hex.decode("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"),
169+
Hex.decode("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"));
170+
171+
var signatureData = new EthereumTransactionDataEip7702.SignatureData(
172+
Hex.decode("01"),
173+
Hex.decode("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"),
174+
Hex.decode("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"));
175+
176+
return new EthereumTransactionDataEip7702(
177+
Hex.decode("012a"),
178+
Hex.decode("00"),
179+
Hex.decode("01"),
180+
Hex.decode("d1385c7bf0"),
181+
Hex.decode("07A120"),
182+
Hex.decode("00000000000000000000000000000000000003f9"),
183+
new byte[0],
184+
Hex.decode("123456"),
185+
List.of(),
186+
List.of(authData),
187+
signatureData);
188+
}
197189
}

0 commit comments

Comments
 (0)