Skip to content

Commit f85d864

Browse files
committed
* Work around for geth bug with integer->hex in json-rpc serialization (cannot handle the leading zero-hex digit, 0x0f vs 0xf).
* Fix null ref exception in Meadow.Cli account commands when password is not used.
1 parent 4f3e7e3 commit f85d864

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

Meadow.Cli/Commands/AccountCommands.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected override void EndProcessing()
150150
}
151151
else
152152
{
153-
if (Password.Length != 0)
153+
if (Password != null && Password.Length > 0)
154154
{
155155
Host.UI.WriteErrorLine($"Password parameter specified but accounts are encryped in file {FilePath}");
156156
return;
@@ -204,13 +204,13 @@ protected override void EndProcessing()
204204
filePath = Path.GetFullPath(Path.Join(SessionState.Path.CurrentLocation.Path, FilePath));
205205
}
206206

207-
if (EncryptData && Password.Length == 0)
207+
if (EncryptData && (Password == null || Password.Length == 0))
208208
{
209209
Host.UI.WriteErrorLine($"No '{nameof(Password)}' parameter is provided. To write without encryption set the '{nameof(EncryptData)}' parameter to false");
210210
return;
211211
}
212212

213-
if (Password.Length != 0 && !EncryptData)
213+
if ((Password != null && Password.Length > 0) && !EncryptData)
214214
{
215215
Host.UI.WriteErrorLine($"The '{nameof(EncryptData)}' parameter is set to false but the '{nameof(Password)}' parameter is provided. Pick one.");
216216
return;

Meadow.Cli/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Once local accounts and network configuration have been setup, the workspace can
7575

7676
```pwsh
7777
PS> Initialize-Workspace console
78-
# RPC client connected to server https://kovan.infura.io:443. Network version: 42
78+
# RPC client connected to server https://rinkeby.infura.io:443. Network version: 4
7979
# Watching for file changes in source directory: Contracts
8080
```
8181

Meadow.Core/Utils/HexConverter.cs

+44-17
Original file line numberDiff line numberDiff line change
@@ -45,62 +45,62 @@ public static string GetHexFromInteger(in byte s, bool hexPrefix = false)
4545
{
4646
Span<byte> bytes = stackalloc byte[sizeof(byte)];
4747
bytes[0] = s;
48-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
48+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
4949
}
5050

5151
public static string GetHexFromInteger(in sbyte s, bool hexPrefix = false)
5252
{
5353
Span<byte> bytes = stackalloc byte[sizeof(sbyte)];
5454
bytes[0] = unchecked((byte)s);
55-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
55+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
5656
}
5757

5858
public static string GetHexFromInteger(in short s, bool hexPrefix = false)
5959
{
6060
Span<byte> bytes = stackalloc byte[sizeof(short)];
6161
BinaryPrimitives.WriteInt16BigEndian(bytes, s);
62-
StripLeadingZeros(ref bytes);
63-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
62+
StripLeadingZeroBytes(ref bytes);
63+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
6464
}
6565

6666
public static string GetHexFromInteger(in ushort s, bool hexPrefix = false)
6767
{
6868
Span<byte> bytes = stackalloc byte[sizeof(ushort)];
6969
BinaryPrimitives.WriteUInt16BigEndian(bytes, s);
70-
StripLeadingZeros(ref bytes);
71-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
70+
StripLeadingZeroBytes(ref bytes);
71+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
7272
}
7373

7474
public static string GetHexFromInteger(in int s, bool hexPrefix = false)
7575
{
7676
Span<byte> bytes = stackalloc byte[sizeof(int)];
7777
BinaryPrimitives.WriteInt32BigEndian(bytes, s);
78-
StripLeadingZeros(ref bytes);
79-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
78+
StripLeadingZeroBytes(ref bytes);
79+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
8080
}
8181

8282
public static string GetHexFromInteger(in uint s, bool hexPrefix = false)
8383
{
8484
Span<byte> bytes = stackalloc byte[sizeof(uint)];
8585
BinaryPrimitives.WriteUInt32BigEndian(bytes, s);
86-
StripLeadingZeros(ref bytes);
87-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
86+
StripLeadingZeroBytes(ref bytes);
87+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
8888
}
8989

9090
public static string GetHexFromInteger(in long s, bool hexPrefix = false)
9191
{
9292
Span<byte> bytes = stackalloc byte[sizeof(long)];
9393
BinaryPrimitives.WriteInt64BigEndian(bytes, s);
94-
StripLeadingZeros(ref bytes);
95-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
94+
StripLeadingZeroBytes(ref bytes);
95+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
9696
}
9797

9898
public static string GetHexFromInteger(in ulong s, bool hexPrefix = false)
9999
{
100100
Span<byte> bytes = stackalloc byte[sizeof(ulong)];
101101
BinaryPrimitives.WriteUInt64BigEndian(bytes, s);
102-
StripLeadingZeros(ref bytes);
103-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
102+
StripLeadingZeroBytes(ref bytes);
103+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
104104
}
105105

106106
public static string GetHexFromInteger(in UInt256 s, bool hexPrefix = false)
@@ -115,11 +115,11 @@ public static string GetHexFromInteger(in UInt256 s, bool hexPrefix = false)
115115
bytes.Reverse();
116116
}
117117

118-
StripLeadingZeros(ref bytes);
119-
return GetHexFromBytes(bytes, hexPrefix: hexPrefix);
118+
StripLeadingZeroBytes(ref bytes);
119+
return GetZeroStrippedHexFromBytes(bytes, hexPrefix: hexPrefix);
120120
}
121121

122-
static void StripLeadingZeros(ref Span<byte> span)
122+
static void StripLeadingZeroBytes(ref Span<byte> span)
123123
{
124124
int startIndex = 0;
125125
for (; startIndex < span.Length && span[startIndex] == 0x0; startIndex++) { }
@@ -129,6 +129,33 @@ static void StripLeadingZeros(ref Span<byte> span)
129129
}
130130
}
131131

132+
133+
static string GetZeroStrippedHexFromBytes(ReadOnlySpan<byte> bytes, bool hexPrefix)
134+
{
135+
// Strips a leading 0 hex char from a hex sequence.
136+
// Example: 0x0fff -> 0xfff
137+
// This is a stupid work-around for geth which throws exceptions trying to parse an integer from (valid) hex string
138+
// that begins has a single 0 after the 0x prefix.
139+
var hex = GetHexFromBytes(bytes, hexPrefix: hexPrefix);
140+
if (hexPrefix)
141+
{
142+
if (hex.Length > 2 && hex[2] == '0')
143+
{
144+
hex = "0x" + hex.Substring(3);
145+
}
146+
}
147+
else
148+
{
149+
if (hex.Length > 0 && hex[0] == '0')
150+
{
151+
hex = hex.Substring(1);
152+
}
153+
}
154+
155+
return hex;
156+
}
157+
158+
132159
public static string GetHex<T>(in T s, bool hexPrefix = false) where T : unmanaged
133160
{
134161
Span<T> span = stackalloc T[1]

pwsh_testing/meadow.config.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<add key="DefaultGasPrice" value="100000000" />
88
<add key="AccountCount" value="25" />
99
<add key="AccountBalance" value="1500" />
10-
<add key="NetworkHost" value="10.0.0.25" />
10+
<add key="NetworkHost" value="https://rinkeby.infura.io:443" />
1111
<add key="NetworkPort" value="90000" />
12-
<add key="SourceDirectory" value="C:\Users\mmiller\Documents\Meadow.Cli\TestServer" />
1312
</appSettings>
1413
</configuration>

0 commit comments

Comments
 (0)