/**
* Novacoin classes library
* Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
using System.Collections.Generic;
using System.Linq;
namespace Novacoin
{
///
/// Available address types
///
public enum AddrType
{
PUBKEY_ADDRESS = 8,
SCRIPT_ADDRESS = 20,
PUBKEY_ADDRESS_TEST = 111,
SCRIPT_ADDRESS_TEST = 196
};
///
/// Represents novacoin address
///
public class CNovacoinAddress
{
private byte nVersion;
private List addrData;
///
/// Initialize with custom data and version
///
///
///
public CNovacoinAddress(byte nVersionIn, byte[] addrDataIn)
{
nVersion = nVersionIn;
addrData = new List(addrDataIn);
}
///
/// Initialize new instance of PUBKEY_ADDRESS
///
/// CKeyID instance
public CNovacoinAddress(CKeyID keyID)
{
nVersion = (byte)AddrType.PUBKEY_ADDRESS;
addrData = new List(keyID.hashBytes);
}
public CNovacoinAddress(string strNovacoinAddress)
{
addrData = AddressTools.Base58DecodeCheck(strNovacoinAddress).ToList();
nVersion = addrData[0];
addrData.RemoveAt(0);
}
///
/// Initialize new instance of SCRIPT_ADDRESS
///
/// CScriptID instance
public CNovacoinAddress(CScriptID scriptID)
{
nVersion = (byte)AddrType.SCRIPT_ADDRESS;
addrData = new List(scriptID.hashBytes);
}
public Hash160 Hash
{
get { return new Hash160(addrData.ToArray()); }
}
///
/// Basic sanity checking
///
public bool IsValid
{
get
{
// Address data is normally generated by RIPEMD-160 hash function
int nExpectedSize = 20;
switch ((AddrType)nVersion)
{
case AddrType.PUBKEY_ADDRESS:
nExpectedSize = 20; // Hash of public key
break;
case AddrType.SCRIPT_ADDRESS:
nExpectedSize = 20; // Hash of CScript
break;
case AddrType.PUBKEY_ADDRESS_TEST:
nExpectedSize = 20;
break;
case AddrType.SCRIPT_ADDRESS_TEST:
nExpectedSize = 20;
break;
default:
return false;
}
return addrData.Count == nExpectedSize;
}
}
///
/// Generate base58 serialized representation of novacoin address
///
/// Base58(data + checksum)
public override string ToString()
{
var r = new List();
r.Add(nVersion);
r.AddRange(addrData);
return AddressTools.Base58EncodeCheck(r.ToArray());
}
}
}