Пример #1
0
        public override string ToString()
        {
            var target = (Array.ConvertAll(this.hash.ToArray(), x => (x))).ToList();

            target.Insert(0, version);

            var checksum = Mint.DoubleSha256(target);
            var bytes    = target.Concat(checksum.Take(4)).ToArray();

            return(Encoders.Base58.EncodeData(bytes));
        }
Пример #2
0
        public List <byte> DecodeString(string address)
        {
            var bytes = Encoders.Base58.DecodeData(address);

            var firsthash = bytes.Take(21).ToArray();
            var checksum  = Mint.DoubleSha256(firsthash.ToList()).ToArray();

            if (firsthash[0] != PeercoinConstants.NetworkVersion ||
                checksum[0] != bytes[21] ||
                checksum[1] != bytes[22] ||
                checksum[2] != bytes[23] ||
                checksum[3] != bytes[24])
            {
                throw new ArgumentException(String.Format("{0} is not an address", address), "address");
            }

            return(firsthash.ToList().Skip(1).ToList());
        }
Пример #3
0
        public static CheckStakeResult CheckStakeKernelHash(MintTemplate template, UInt32 txTime, UInt64 stakeModifier)
        {
            var retobj = new CheckStakeResult
            {
                Id        = template.Id,
                OfAddress = template.OfAddress,
                txTime    = txTime
            };

            if (txTime < template.PrevTxTime)
            {
                // Transaction timestamp violation
                return(retobj);
            }


            if (template.BlockFromTime + PeercoinConstants.StakeMinAge > txTime)
            {
                // Min age requirement
                return(retobj);
            }

            var bnTargetPerCoinDay = Mint.CompactToBig(template.Bits.Value);

            long nTimeWeight = txTime - template.PrevTxTime;

            if (nTimeWeight > PeercoinConstants.StakeMaxAge)
            {
                nTimeWeight = PeercoinConstants.StakeMaxAge;
            }

            long timeReduction = PeercoinConstants.StakeMinAge;

            nTimeWeight -= timeReduction;

            var t1 = new BigInteger(24 * 60 * 60);
            var t2 = new BigInteger(PeercoinConstants.Coin);
            var t3 = new BigInteger(template.PrevTxOutValue);
            var t4 = new BigInteger(nTimeWeight);

            BigInteger bnCoinDayWeight = (((t3 * t4) / t2)) / t1;


            BigInteger targetInt = bnCoinDayWeight * (bnTargetPerCoinDay);

            //byte[] array = new byte[28];
            var buffer = new byte[28];

            var bufferindex = 0;


            byte[] arrStakemodifier = BitConverter.GetBytes(stakeModifier);

            //put stakemodifier in buffer
            for (var i = 0; i < 8; i++)
            {
                buffer[bufferindex] = arrStakemodifier[i];
                bufferindex++;
            }

            //put other data in buffer
            new List <UInt32> {
                (UInt32)template.BlockFromTime, (UInt32)template.PrevTxOffset, (UInt32)template.PrevTxTime, (UInt32)template.PrevTxOutIndex, (UInt32)txTime
            }
            .ForEach(num => {
                var dn = num;
                for (var i = 0; i < 4; i++)
                {
                    buffer[bufferindex] = (byte)(dn & 0xff);
                    dn >>= 8;
                    bufferindex++;
                }
            });


            //no reverse so keep it in little endian
            var hashProofOfStake = Mint.DoubleSha256(buffer.ToList()).ToArray();               //keep it in little-endian .Reverse().ToArray();

            //add zero to last in array to make it unsigned:
            // https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.-ctor?view=netframework-4.5.2#System_Numerics_BigInteger__ctor_System_Byte___
            if ((hashProofOfStake[hashProofOfStake.Length - 1] & 0x80) > 0)
            {
                byte[] temp = new byte[hashProofOfStake.Length];
                Array.Copy(hashProofOfStake, temp, hashProofOfStake.Length);
                hashProofOfStake = new byte[temp.Length + 1];
                Array.Copy(temp, hashProofOfStake, temp.Length);
            }

            var hashProofOfStakeInt = new BigInteger(hashProofOfStake);

            if (hashProofOfStakeInt > targetInt)
            {
                return(retobj);
            }

            //yeah, below target!

            retobj.minTarget = (hashProofOfStakeInt / bnCoinDayWeight) - new BigInteger(1);
            retobj.success   = true;
            retobj.hash      = hashProofOfStake;
            retobj.Id        = template.Id;


            var comp = Mint.IncCompact(
                Mint.BigToCompact(retobj.minTarget)
                );

            retobj.minimumDifficulty = Mint.CompactToDiff(comp);

            return(retobj);
        }