Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using Blake2Fast;
using Bech32;
using Blake2Fast;
using IotaWalletNet.Domain.Common.Models.Address;
using IotaWalletNet.Domain.Common.Models.Coin;
using IotaWalletNet.Domain.Common.Models.Network;
using System.Buffers.Text;
using System.Text;

namespace IotaWalletNet.Domain.Common.Extensions
Expand Down Expand Up @@ -42,5 +47,35 @@ public static string ComputeBlake2bHash(this string hexEncoded)

return "0x" + Convert.ToHexString(hash);
}

public static string EncodeEd25519HashIntoBech32(this string blake2bHashOfEd25519, NetworkType networkType, TypeOfCoin typeOfCoin)
{
blake2bHashOfEd25519 = blake2bHashOfEd25519.Trim().ToLower();
if (blake2bHashOfEd25519.StartsWith("0x"))
blake2bHashOfEd25519 = blake2bHashOfEd25519.Substring(2); // remove the 0x of a hexstring eg 0x1337

string hrp = HumanReadablePart.GetHumanReadablePart(networkType, typeOfCoin);
const string ED25519_TYPE = "00";
blake2bHashOfEd25519 = ED25519_TYPE + blake2bHashOfEd25519;

string bech32 = Bech32Engine.Encode(hrp, Convert.FromHexString(blake2bHashOfEd25519));

const string HRP_CONSTANT = "1";

return $"{hrp}{HRP_CONSTANT}{bech32}";
}

public static string DecodeBech32IntoEd25519Hash(this string bech32, NetworkType networkType, TypeOfCoin typeOfCoin)
{
bech32 = bech32.Trim().ToLower();

string hrp = HumanReadablePart.GetHumanReadablePart(networkType, typeOfCoin);

string bech32OfInterest = bech32.Substring(4); //eg remove iota1 or smr1 or atoi1 or rms1

byte[] decoded = Bech32Engine.Decode(bech32OfInterest, hrp);

return "0x" + Convert.ToHexString(decoded);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using IotaWalletNet.Domain.Common.Models.Coin;
using IotaWalletNet.Domain.Common.Models.Network;

namespace IotaWalletNet.Domain.Common.Models.Address
{
/// <summary>
///
/// Bech32 for human-readable encoding
/// The human-readable encoding of the address is Bech32(as described in BIP-0173). A Bech32 string is at most 90 characters long and consists of:

/// The human-readable part(HRP), which conveys the IOTA protocol and distinguishes between Mainnet(the IOTA token) and Testnet(testing version):
/// iota is the human-readable part for Mainnet addresses
/// atoi is the human-readable part for Testnet addresses
/// The separator, which is always 1.
/// The data part, which consists of the Base32 encoded serialized address and the 6-character checksum.
/// </summary>
public static class HumanReadablePart
{
public enum HRP
{
iota,
atoi,
smr,
rms
}

private static IReadOnlyDictionary<TypeOfCoin, string> MainnetToHrp = new Dictionary<TypeOfCoin, string>()
{
{ TypeOfCoin.Iota, HRP.iota.ToString() },
{ TypeOfCoin.Shimmer, HRP.smr.ToString() },

};
private static IReadOnlyDictionary<TypeOfCoin, string> TestnetToHrp = new Dictionary<TypeOfCoin, string>()
{
{ TypeOfCoin.Iota, HRP.atoi.ToString() },
{ TypeOfCoin.Shimmer, HRP.rms.ToString() },

};

public static string GetHumanReadablePart(NetworkType networkType, TypeOfCoin typeOfCoin)
{
if (networkType == NetworkType.Mainnet)
return MainnetToHrp[typeOfCoin];
else
return TestnetToHrp[typeOfCoin];
}
}
}
Loading