Skip to content

Commit

Permalink
Merge branch 'port-netstandard'
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel1117 committed Jun 3, 2022
2 parents 3b11bc2 + 6f4a3ae commit 72314a3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Nano.Net.Tests/BlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public class BlockTest
Account = "nano_3tjhazni9juaoa8q9rw33nf3f6i45gswhpzrgrbrawxhh7a777ror9okstch",
Previous = "3865BFCD423CE3579C4A7C6010CE763BE4C63964AC06BDA451A63BBCAC9E3712",
Representative = "nano_18shbirtzhmkf7166h39nowj9c9zrpufeg75bkbyoobqwf1iu3srfm9eo3pz",
Balance = "0",
Balance = "33022",
Link = "nano_3tjhazni9juaoa8q9rw33nf3f6i45gswhpzrgrbrawxhh7a777ror9okstch",
Subtype = BlockSubtype.Send
};

[Fact]
public void HashBlock_ValidateHash_ValidHash()
{
Assert.Equal("66A580645BEE693103CDE784764043092F7CB966B44C4029576C5CE17C222F40", _testBlock.Hash, true);
Assert.Equal("F19FCBB3145E6A54D6390016245B2FDDCD864DDD5CA04961F05B6F56C3FD28C3", _testBlock.Hash, true);
}

[Fact]
Expand Down
5 changes: 4 additions & 1 deletion Nano.Net/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ public static Block CreateChangeBlock(Account account, string representativeAddr

private string GetHash()
{
byte[] balanceBytes = BigInteger.Parse(Balance).ToByteArray(true, true);
byte[] balanceBytes = BigInteger.Parse(Balance).ToByteArray();
if (balanceBytes.Length > 1 && balanceBytes.Last() == 0)
Array.Resize(ref balanceBytes, balanceBytes.Length - 1); // remove trailing 0 (sign byte)
balanceBytes = balanceBytes.Reverse().ToArray(); // convert to big endian

byte[] final = Blake2BHash
(
Expand Down
9 changes: 3 additions & 6 deletions Nano.Net/Nano.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>10</LangVersion>
<NoWarn>1570,1591</NoWarn>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Title>Nano.Net</Title>
<PackageVersion>1.5.0</PackageVersion>
<PackageVersion>1.6.0</PackageVersion>
<Description>A .NET library for the Nano cryptocurrency</Description>
<Copyright>miguel1117</Copyright>
<PackageProjectUrl>https://github.com/miguel1117/Nano.Net</PackageProjectUrl>
Expand All @@ -20,8 +21,4 @@
<PackageReference Include="Websocket.Client" Version="4.3.38" />
</ItemGroup>

<PropertyGroup>
<LangVersion>9</LangVersion>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions Nano.Net/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public async Task<ReceivableBlocksResponse> PendingBlocksAsync(string address, i
});

if (pendingBlocks?.PendingBlocks != null)
foreach ((string key, ReceivableBlock value) in pendingBlocks?.PendingBlocks)
value.Hash = key;
foreach (var block in pendingBlocks?.PendingBlocks)
block.Value.Hash = block.Key;

return pendingBlocks;
}
Expand Down
9 changes: 5 additions & 4 deletions Nano.Net/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static byte[] DecodeNanoBase32(string data)
foreach (char t in data)
stringBuilder.Append(NanoAddressEncoding[t]); // Decode each character into string representation of it's binary parts

var binaryString = stringBuilder.ToString()[4..]; // Remove leading 4 0s
var binaryString = stringBuilder.ToString().Substring(4); // Remove leading 4 0s

// Convert to bytes
var publicKeyBytes = new byte[32];
Expand Down Expand Up @@ -171,7 +171,8 @@ public static bool IsAddressValid(string address, string[] allowedPrefixes = nul

byte[] publicKey = DecodeNanoBase32(address);

return EncodedAddressChecksum(publicKey) == address[^8..];
var checksum = address.Substring(address.Length - 8);
return EncodedAddressChecksum(publicKey) == checksum;
}

/// <summary>
Expand All @@ -188,13 +189,13 @@ public static bool IsWorkValid(string hash, string powNonce, string threshold)
throw new ArgumentException("The threshold should not be a null or empty string.", nameof(threshold));

byte[] thresholdBytes = threshold.HexToBytes().Reverse().ToArray();
ulong thresholdValue = BitConverter.ToUInt64(thresholdBytes);
ulong thresholdValue = BitConverter.ToUInt64(thresholdBytes, 0);

byte[] hashBytes = hash.HexToBytes();
byte[] workBytes = powNonce.HexToBytes().Reverse().ToArray();

byte[] output = Blake2BHash(8, workBytes, hashBytes);
ulong outputValue = BitConverter.ToUInt64(output);
ulong outputValue = BitConverter.ToUInt64(output, 0);

return outputValue >= thresholdValue;
}
Expand Down
4 changes: 2 additions & 2 deletions Nano.Net/WebSockets/NanoWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ private void OnReconnect()
if (!_clientWebSocket.IsStarted)
return;

foreach ((string _, Topic topic) in _subscriptions)
SendTopic("subscribe", topic);
foreach (KeyValuePair<string, Topic> subscription in _subscriptions)
SendTopic("subscribe", subscription.Value);
}

/// <summary>
Expand Down

0 comments on commit 72314a3

Please sign in to comment.