Ejemplo n.º 1
0
        internal static object Create(byte[] scriptHash, byte[] entityName, byte[] name)
        {
            if (!Runtime.CheckWitness(scriptHash))
            {
                return(false);
            }

            BigInteger entityID, entityCount;
            string     entityMaxIdKey, entityKey, entityCountKey;

            entityMaxIdKey = Key.Generate(Prefix.EntityMaxId, entityName, scriptHash);
            entityCountKey = Key.Generate(Prefix.EntityCount, entityName, scriptHash);


            byte[] entityMaxIdBytes = DB.Get(entityMaxIdKey);
            byte[] entityCountBytes = DB.Get(entityCountKey);

            if (entityMaxIdBytes == null)
            {
                DB.Put(entityMaxIdKey, 1);
                DB.Put(entityCountKey, 1);
                entityID = 1;
            }
            else
            {
                entityID    = NSFH.AsBigInteger(entityMaxIdBytes) + 1;
                entityCount = NSFH.AsBigInteger(entityCountBytes) + 1;
                DB.Put(entityMaxIdKey, entityID);
            }
            entityKey = Key.Generate(entityName, scriptHash, entityID);
            DB.Put(entityKey, name);
            Runtime.Notify(entityName + " created successfully", entityID, name.AsString());
            return(entityID);
        }
        public static Certificate converyNativeCert(CertificateStr nativeCert, byte[] encodedCertValue)
        {
            Certificate certificate = new Certificate();

            certificate.AuthorityKeyIdentifier = nativeCert.AuthorityKeyIdentifier;
            certificate.BasicConstraints       = nativeCert.BasicConstraints;
            certificate.DNsNames              = convertStringMapToArray(nativeCert.DnsNames);
            certificate.EmailAddresses        = convertStringMapToArray(nativeCert.EmailAddresses);
            certificate.ExtendedKeyUsage      = convertExtendedKeyUsage(nativeCert.ExtendedKeyUsage);
            certificate.IpAddresses           = convertStringMapToArray(nativeCert.IpAddresses);
            certificate.Issuer                = convertNativeName(nativeCert.issuer);
            certificate.Subject               = convertNativeName(nativeCert.subject);
            certificate.KeyUsage              = convertKeyUsage(nativeCert.KeyUsage);
            certificate.PublicKeyAlgName      = nativeCert.PublicKeyAlgName;
            certificate.SerialNumber          = Helper.AsBigInteger(nativeCert.SerialNumber); // todo : possibly problematic
            certificate.Signature             = nativeCert.Signature;
            certificate.SignatureAlgorithm    = nativeCert.SignatureAlgorithm;
            certificate.SubjectKeyIdentifier  = nativeCert.SubjectKeyIdentifier;
            certificate.SubjectPublicKeyInfo  = nativeCert.SubjectPublicKeyInfo;
            certificate.TbsCertificate        = nativeCert.TbsCertificate;
            certificate.TBSSignatureAlgorithm = nativeCert.TBSSignatureAlgorithm;
            certificate.Urls         = convertStringMapToArray(nativeCert.Urls);
            certificate.Validity     = nativeCert.Validity;
            certificate.Version      = nativeCert.Version;
            certificate.IsLoaded     = true;
            certificate.EncodedValue = encodedCertValue;
            return(certificate);
        }
        /*
         * 2 bytes data read for ushort
         */
        public static BigInteger GetBigInteger(byte[] data)
        {
            byte[] zero = new byte[] { 0x00 };
            byte[] temp = Helper.Concat(data, zero);

            BigInteger val = Helper.AsBigInteger(temp);

            return(val);
        }
Ejemplo n.º 4
0
        internal static object List(object[] args)
        {
            byte[] scriptHash = (byte[])args[0];
            byte[] entityName = (byte[])args[1];

            int itemPerView = 10;

            if (args.Length == 3)
            {
                itemPerView = (int)args[2];
            }

            int startId = 1;

            if (args.Length == 3)
            {
                startId = (int)args[3];
            }

            if (!Runtime.CheckWitness(scriptHash))
            {
                return(false);
            }

            BigInteger entityMaxId, endId;
            string     entityMaxIdKey;

            entityMaxIdKey = Key.Generate(Prefix.EntityMaxId, entityName, scriptHash);
            byte[] entityMaxIdBytes = DB.Get(entityMaxIdKey);
            if (entityMaxIdBytes == null)
            {
                Runtime.Notify("total " + entityName + " available", 0);
                return(NSFH.AsByteArray(0));
            }
            entityMaxId = NSFH.AsBigInteger(entityMaxIdBytes);
            if (itemPerView <= 0)
            {
                itemPerView = 10;
            }
            if (startId > entityMaxId)
            {
                startId = 1;
            }
            endId = startId + itemPerView - 1;

            for (int i = startId; i <= endId; i++)
            {
                string entityKey   = Key.Generate(entityName, scriptHash, i);
                byte[] entityValue = DB.Get(entityKey);
                if (entityValue != null)
                {
                    Runtime.Notify(entityName, i, entityValue.AsString());
                }
            }
            //TODO Changed to return Array of values
            return(NSFH.AsByteArray(1));
        }
        /*
         * Generic structure for array size. Each array structure begins with the leading size structure.
         * Size can be a octet, byte, uint or ulong, total size of this structure depends on the type of
         * the data, so size becomes 1, 3, 5, 9 byte(s) respectively.
         */

        public static VarSizeStr readVarSize(byte[] data, int idx)
        {
            byte[]     temp  = Helper.Range(data, idx, 1);
            byte[]     temp2 = new byte[] { 0x00 };
            byte[]     temp3 = Helper.Concat(temp, temp2);
            BigInteger temp4 = Helper.AsBigInteger(temp3);
            int        ind   = (int)temp4;

            byte[] val  = new byte[1];
            int    size = 0;

            if (ind == 0xfd)
            {
                // read next 2 bytes total 3 (ushort length string)
                size = 2;
                val  = new byte[3];
            }

            if (ind == 0xfe)
            {
                // read next 4 bytes total 5 (uint length string)
                size = 4;
                val  = new byte[5];
            }

            if (ind == 0xff)
            {
                // read next 8 bytes total 9 (ulong length string)
                size = 8;
                val  = new byte[9];
            }

            if (size > 0)
            {
                val = Helper.Range(data, idx + 1, size);

                val = Helper.Concat(val, temp2);
            }

            VarSizeStr res = new VarSizeStr();

            if (size == 0)
            {
                res.value = ind;
            }

            if (size > 0)
            {
                res.value = (int)Helper.AsBigInteger(val);
            }

            res.size = size + 1;

            return(res);
        }
Ejemplo n.º 6
0
        //首先在nep5基础上将账户分为两个部分
        //一个部分是saving
        //saving 有一个产生块的标记
        //一个部分是cash
        //当你收到一笔转账,他算在cash里面
        //当你花费一笔钱,先扣cash,扣完再扣saving

        //余额是saving 和 cash 的总和

        //cash 如何转换为为saving
        //通过claim   指令
        //claim指令 将领奖并且把所有的余额转换为saving,产生块标记为当前块

        //*claim指令
        //claim指令取得已经公布的奖励,仅有自己的saving的block <奖励的startblock 才可以领奖
        //领奖后全部资产+领到的资产变成saving状态

        //消耗池
        //所有花费掉的资产进入消耗池,等候公布奖励
        //公布奖励
        //将现有奖励从消耗池中取出,变成一个奖励,奖励规定只有savingblock <startblock 才可以领取。
        //根据 消耗池数量/总发行数量 确定一个领奖比例。
        //将同时保持五个公布奖励。
        //当公布第六个奖励时,第一个奖励被删除,并将他的余额丢入消耗池

        //*检查奖励指令
        //程序约定好公布奖励的block间隔,当有人检查奖励并且奖励的block间隔已经大于等于程序设置,则公布奖励。
        //用户可以用检查奖励指令获知最近的五个奖励,以此可以推算,如果领奖自己可以获取多少收益。

        //增加两条指令
        //checkpool*检查奖励
        //claim*领取奖励

        //可循环分配资产
        //最终确定加四个接口(暂定名Nep5.1)
        //检查奖励,只读(everyone)
        public static object[] checkBonus()
        {
            byte[] data = Storage.Get(Storage.CurrentContext, "!bonus:L");
            if (data.Length == 0)
            {
                return(null);
            }



            BigInteger lastBounsBlock = Helper.AsBigInteger(data);

            object[] retarray = new object[bonusCount];

            for (var i = 0; i < bonusCount; i++)
            {
                byte[]   bIndex      = Helper.AsByteArray("bonus").Concat(Helper.AsByteArray(lastBounsBlock));
                byte[]   bStartBlock = bIndex.Concat(Helper.AsByteArray(":S"));
                byte[]   bBonusValue = bIndex.Concat(Helper.AsByteArray(":V"));
                byte[]   bBonusCount = bIndex.Concat(Helper.AsByteArray(":C"));
                byte[]   bLastIndex  = bIndex.Concat(Helper.AsByteArray(":L"));
                byte[]   StartBlock  = Storage.Get(Storage.CurrentContext, bStartBlock);
                byte[]   BonusValue  = Storage.Get(Storage.CurrentContext, bBonusValue);
                byte[]   BonusCount  = Storage.Get(Storage.CurrentContext, bBonusCount);
                byte[]   LastIndex   = Storage.Get(Storage.CurrentContext, bLastIndex);
                object[] bonusItem   = new object[4];
                bonusItem[0] = StartBlock;
                bonusItem[1] = BonusCount;
                bonusItem[2] = BonusValue;
                bonusItem[3] = LastIndex;
                retarray[i]  = bonusItem;
                if (LastIndex.Length == 0)
                {
                    break;
                }
                lastBounsBlock = Helper.AsBigInteger(LastIndex);
                if (lastBounsBlock == 0)
                {
                    break;
                }
            }
            return(retarray);
        }
Ejemplo n.º 7
0
        //领取奖励(个人)
        public static BigInteger getBonus(byte[] to)
        {
            if (!Runtime.CheckWitness(to))
            {
                return(0);
            }

            byte[] data = Storage.Get(Storage.CurrentContext, "!bonus:L");
            if (data.Length == 0)
            {
                return(0);
            }

            var toinfo = GetAccountDetail(to);
            //var indexcashto = to.Concat(new byte[] { 0 });
            //var indexsavingto = to.Concat(new byte[] { 1 });
            //var indexsavingblockto = to.Concat(new byte[] { 2 });
            //BigInteger to_value_cash = Storage.Get(Storage.CurrentContext, indexcashto).AsBigInteger();
            //BigInteger to_value_saving = Storage.Get(Storage.CurrentContext, indexsavingto).AsBigInteger();
            //BigInteger to_value_savingblock = Storage.Get(Storage.CurrentContext, indexsavingblockto).AsBigInteger();

            BigInteger lastBonusBlock = Helper.AsBigInteger(data);
            BigInteger addValue       = 0;

            for (var i = 0; i < bonusCount; i++)
            {
                byte[] bIndex      = Helper.AsByteArray("bonus").Concat(Helper.AsByteArray(lastBonusBlock));
                byte[] bStartBlock = bIndex.Concat(Helper.AsByteArray(":S"));
                byte[] bBonusValue = bIndex.Concat(Helper.AsByteArray(":V"));
                byte[] bBonusCount = bIndex.Concat(Helper.AsByteArray(":C"));
                byte[] bLastIndex  = bIndex.Concat(Helper.AsByteArray(":L"));
                var    StartBlock  = Storage.Get(Storage.CurrentContext, bStartBlock).AsBigInteger();
                var    BonusValue  = Storage.Get(Storage.CurrentContext, bBonusValue).AsBigInteger();
                var    BonusCount  = Storage.Get(Storage.CurrentContext, bBonusCount).AsBigInteger();
                if (toinfo.savingblock < StartBlock)         //有领奖资格
                {
                    var cangot = toinfo.saving * BonusValue; //要领走多少
                    addValue += cangot;
                    Storage.Put(Storage.CurrentContext, bBonusCount, BonusCount - cangot);
                }
                byte[] LastIndex = Storage.Get(Storage.CurrentContext, bLastIndex);
                if (LastIndex.Length == 0)
                {
                    break;
                }
                lastBonusBlock = Helper.AsBigInteger(LastIndex);
                if (lastBonusBlock == 0)
                {
                    break;
                }
            }
            //领奖写入
            BigInteger balanceto = toinfo.saving * factor + toinfo.cash;

            {
                var lastv  = balanceto + addValue;
                var bigN   = lastv / (factor);
                var smallN = lastv % (factor);
                toinfo.cash        = smallN;
                toinfo.saving      = bigN;
                toinfo.savingblock = Blockchain.GetHeight();
                SetAccountDetail(to, toinfo);
                //Storage.Put(Storage.CurrentContext, indexcashto, smallN);
                //Storage.Put(Storage.CurrentContext, indexsavingto, bigN);
                //BigInteger block = (Blockchain.GetHeight());
                //Storage.Put(Storage.CurrentContext, indexsavingblockto, block);
            }
            return(0);
        }
Ejemplo n.º 8
0
        //新奖励,(everyone)随便调用,不符合规则就不会创建奖励,谁都可以调用这个,催促发奖励。
        public static BigInteger newBonus()
        {
            byte[] data = Storage.Get(Storage.CurrentContext, "!bonus:L");
            //if (data.Length == 0)
            //    ;

            BigInteger index = Blockchain.GetHeight() / bonusInterval;

            if (index < 1)
            {
                return(0);
            }

            BigInteger bounsheight = (index - 1) * bonusInterval;

            BigInteger lastBounsBlock = Helper.AsBigInteger(data);

            if (bounsheight == lastBounsBlock)
            {
                return(0);
            }

            //清掉奖池
            var poolv = Storage.Get(Storage.CurrentContext, "!pool").AsBigInteger();

            Storage.Delete(Storage.CurrentContext, "!pool");
            byte[] bIndex = Helper.AsByteArray("bonus").Concat(Helper.AsByteArray(bounsheight));

            //找到第一个奖池
            byte[] firstIndex  = bIndex;
            byte[] secondIndex = bIndex;
            bool   skipdel     = false;

            for (var i = 0; i < bonusCount; i++)
            {
                secondIndex = firstIndex;
                byte[] _bLastIndex = firstIndex.Concat(Helper.AsByteArray(":L"));
                var    _index      = Storage.Get(Storage.CurrentContext, _bLastIndex);//+1+2+3
                if (_index.Length == 0)
                {
                    Runtime.Log("skipdel");
                    skipdel = true;
                    break;
                }
                firstIndex = Helper.AsByteArray("bonus").Concat(_index);
            }
            if (skipdel == false)//删除最后一个的连接,并把他的钱也放进新奖池里
            {
                byte[] secondLastIndex = secondIndex.Concat(Helper.AsByteArray(":L"));
                Storage.Delete(Storage.CurrentContext, secondLastIndex);
                byte[] firstBonusCount = firstIndex.Concat(Helper.AsByteArray(":C"));
                var    count           = Storage.Get(Storage.CurrentContext, firstBonusCount).AsBigInteger();
                poolv += count;
            }


            byte[] bStartBlock = bIndex.Concat(Helper.AsByteArray(":S"));
            byte[] bBonusValue = bIndex.Concat(Helper.AsByteArray(":V"));
            byte[] bBonusCount = bIndex.Concat(Helper.AsByteArray(":C"));
            byte[] bLastIndex  = bIndex.Concat(Helper.AsByteArray(":L"));
            Storage.Put(Storage.CurrentContext, bStartBlock, bounsheight.AsByteArray());
            Storage.Put(Storage.CurrentContext, bBonusCount, poolv.AsByteArray());
            BigInteger bonusV = poolv / (totalSupply() / factor);//整数部分一发几

            Storage.Put(Storage.CurrentContext, bBonusValue, bonusV.AsByteArray());
            Storage.Put(Storage.CurrentContext, bLastIndex, lastBounsBlock.AsByteArray());

            //写入lastblock
            Storage.Put(Storage.CurrentContext, "!bonus:L", bounsheight.AsByteArray());


            return(bounsheight);
        }
Ejemplo n.º 9
0
        //消耗资产(个人)


        public static bool use(byte[] from, BigInteger value)
        {
            if (value <= 0)
            {
                return(false);
            }

            var detailfrom = GetAccountDetail(from);
            //var indexcash = from.Concat(new byte[] { 0 });
            //var indexsaving = from.Concat(new byte[] { 1 });
            //BigInteger from_value_cash = Storage.Get(Storage.CurrentContext, indexcash).AsBigInteger();
            //BigInteger from_value_saving = Storage.Get(Storage.CurrentContext, indexsaving).AsBigInteger();
            var balance = detailfrom.cash + detailfrom.saving * factor;

            if (balance < value)
            {
                return(false);
            }
            if (detailfrom.cash >= value)//零钱就够扣了
            {
                detailfrom.cash = detailfrom.cash - value;
                SetAccountDetail(from, detailfrom);
                //Storage.Put(Storage.CurrentContext, indexcash, from_value_cash - value);
            }
            else//零钱不够扣
            {
                var lastv  = balance - value;
                var bigN   = lastv / (factor);
                var smallN = lastv % (factor);
                detailfrom.cash   = smallN;
                detailfrom.saving = bigN;
                SetAccountDetail(from, detailfrom);
                //Storage.Put(Storage.CurrentContext, indexcash, smallN);
                //Storage.Put(Storage.CurrentContext, indexsaving, bigN);
            }


            //var indexcash = from.Concat(new byte[] { 0 });
            //var indexsaving = from.Concat(new byte[] { 1 });



            //BigInteger from_value_cash = Storage.Get(Storage.CurrentContext, indexcash).AsBigInteger();
            //BigInteger from_value_saving = Storage.Get(Storage.CurrentContext, indexsaving).AsBigInteger();
            //var balance = from_value_cash + from_value_saving * factor;
            //if (balance < value) return false;

            //if (from_value_cash >= value)//零钱就够扣了
            //{
            //    Storage.Put(Storage.CurrentContext, indexcash, from_value_cash - value);
            //}
            //else//零钱不够扣
            //{
            //    var lastv = balance - value;
            //    var bigN = lastv / (factor);
            //    var smallN = lastv % (factor);
            //    Storage.Put(Storage.CurrentContext, indexcash, smallN);
            //    Storage.Put(Storage.CurrentContext, indexsaving, bigN);
            //}

            byte[]     data = Storage.Get(Storage.CurrentContext, "!pool");
            BigInteger v    = Helper.AsBigInteger(data);

            v += value;
            Storage.Put(Storage.CurrentContext, "!pool", v);

            return(true);
        }