Skip to content

Commit

Permalink
TxDestination internal representation goes from bytes to string
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Oct 15, 2014
1 parent b7ea740 commit 5b69064
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
36 changes: 24 additions & 12 deletions NBitcoin/KeyId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ namespace NBitcoin
{
public class TxDestination
{
string _Dest;
byte[] _DestBytes;

public TxDestination()
{
_Dest = "00";
_DestBytes = new byte[] { 0 };
}

public TxDestination(byte[] value)
{
_Dest = Encoders.Hex.EncodeData(value);
if(value == null)
throw new ArgumentNullException("value");
_DestBytes = value;
}
public TxDestination(uint160 value)
: this(value.ToBytes())
Expand All @@ -27,9 +30,8 @@ public TxDestination(uint160 value)

public TxDestination(string value)
{
//Ensure is hex
Encoders.Hex.DecodeData(value);
_Dest = value;
_DestBytes = Encoders.Hex.DecodeData(value);
_Str = value;
}

public BitcoinAddress GetAddress(Network network)
Expand All @@ -41,26 +43,33 @@ public virtual Script CreateScriptPubKey()
{
return null;
}

public byte[] ToBytes()
{
return Encoders.Hex.DecodeData(_Dest);
return ToBytes(false);
}
public byte[] ToBytes(bool @unsafe)
{
if(@unsafe)
return _DestBytes;
var array = new byte[_DestBytes.Length];
Array.Copy(_DestBytes, array, _DestBytes.Length);
return array;
}

public override bool Equals(object obj)
{
TxDestination item = obj as TxDestination;
if(item == null)
return false;
return _Dest.Equals(item._Dest);
return Utils.ArrayEqual(_DestBytes, item._DestBytes);
}
public static bool operator ==(TxDestination a, TxDestination b)
{
if(System.Object.ReferenceEquals(a, b))
return true;
if(((object)a == null) || ((object)b == null))
return false;
return a._Dest == b._Dest;
return Utils.ArrayEqual(a._DestBytes, b._DestBytes);
}

public static bool operator !=(TxDestination a, TxDestination b)
Expand All @@ -70,12 +79,15 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return _Dest.GetHashCode();
return Utils.GetHashCode(_DestBytes);
}

string _Str;
public override string ToString()
{
return _Dest;
if(_Str == null)
_Str = Encoders.Hex.EncodeData(_DestBytes);
return _Str;
}
}
public class KeyId : TxDestination
Expand Down
2 changes: 1 addition & 1 deletion NBitcoin/NBitcoin.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
And more check at https://github.com/NicolasDorier/NBitcoin/blob/master/README.md
</summary>
<projectUrl>https://github.com/NicolasDorier/NBitcoin</projectUrl>
<copyright>Copyright 2013</copyright>
<copyright>Copyright 2014</copyright>
<tags>bitcoin</tags>
</metadata>
<files></files>
Expand Down
2 changes: 1 addition & 1 deletion NBitcoin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.5.6")]
[assembly: AssemblyVersion("1.0.5.7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
17 changes: 17 additions & 0 deletions NBitcoin/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,5 +454,22 @@ public static IPEndPoint ParseIpEndpoint(string endpoint, int defaultPort)
}
return new IPEndPoint(address, port);
}

public static int GetHashCode(byte[] array)
{
unchecked
{
if(array == null)
{
return 0;
}
int hash = 17;
for(int i = 0 ; i < array.Length ; i++)
{
hash = hash * 31 + array[i];
}
return hash;
}
}
}
}

0 comments on commit 5b69064

Please sign in to comment.