Пример #1
0
        public async Task <bool> UpdateApp(byte[] data, UInt16 type, UInt16 content, UInt16 version)
        {
            try
            {
                var    hash     = NeoDun.SignTool.ComputeSHA256(data, 0, data.Length);
                string str_hash = NeoDun.SignTool.Bytes2HexString(hash, 0, hash.Length);
                int    num      = data.Length / (1024 * 50) + 1;

                List <UInt32> remoteids = new List <UInt32>();

                for (var i = 0; i < num; i++)
                {
                    byte[]           _data     = data.Skip(1024 * 50 * i).Take(1024 * 50).ToArray();
                    var              _hash     = NeoDun.SignTool.ComputeSHA256(_data, 0, _data.Length);
                    string           _str_hash = NeoDun.SignTool.Bytes2HexString(_hash, 0, _hash.Length);
                    NeoDun.DataBlock block     = signer.dataTable.newOrGet(_str_hash, (UInt32)_data.Length, NeoDun.DataBlockFrom.FromDriver);
                    block.data = _data;
                    signer.SendDataBlock(block);

                    var  __block  = signer.dataTable.getBlockBySha256(_str_hash);
                    uint remoteid = await __block.GetRemoteid();

                    if (remoteid == 0)
                    {
                        return(false);
                    }
                    remoteids.Add((uint)remoteid);
                    __block.dataidRemote = 0;
                }
                NeoDun.Message signMsg = new NeoDun.Message();
                signMsg.tag1  = 0x03;
                signMsg.tag2  = 0x01;
                signMsg.msgid = NeoDun.SignTool.RandomShort();
                signMsg.writeUInt16(0, type);
                signMsg.writeUInt16(2, content);
                Array.Copy(hash, 0, signMsg.data, 4, hash.Length);
                signMsg.writeUInt16(36, version);
                //这个dataid 要上一个block 传送完毕了才知道
                //for (var i = 0; i < remoteids.Count; i++)
                //{
                //    signMsg.writeUInt32(42, remoteids[i]);
                //}
                signer.SendMessage(signMsg, true);
                applyResult = false;
                needLoop    = true;
                int time = 0;
                while (needLoop && time <= waitTime)
                {
                    await Task.Delay(100);

                    time += 100;
                }
                needLoop = false;
                return(applyResult);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
Пример #2
0
        void SendBlock(NeoDun.DataBlock data)
        {
            Message msg = new Message();

            msg.tag1  = 0x01;                   //0字节
            msg.tag2  = 0x01;                   //1字节
            msg.msgid = SignTool.RandomShort(); // 2 3 字节

            //4~7 字节
            var byteLength = BitConverter.GetBytes((UInt32)data.data.Length);

            for (var i = 0; i < 4; i++)
            {
                msg.data[i] = byteLength[i];
            }
            var sha = SignTool.HexString2Bytes(data.sha256);

            //8 到39 字节
            for (var i = 0; i < 32; i++)
            {
                msg.data[i + 4] = sha[i];
            }

            SendMsg(msg, true);
        }
Пример #3
0
        public async Task <string> AddAddressByWif(string wif)
        {
            byte[] privateKey;
            byte[] publicKey;
            string str_address = "";

            try
            {
                privateKey  = NeoDun.SignTool.GetPrivateKeyFromWif(wif);
                publicKey   = NeoDun.SignTool.GetPublicKeyFromPrivateKey(privateKey);
                str_address = NeoDun.SignTool.GetAddressFromPublicKey(publicKey);
            }
            catch (Exception e)
            {
                return("私钥格式错误");
            }

            //地址查重
            foreach (var add in signer.addressPool.addresses)
            {
                if (add.AddressText == str_address)
                {
                    return("该私钥已导入");
                }
            }
            try
            {
                string str_addressType = "Neo";
                UInt16 addressType     = (UInt16)Enum.Parse(typeof(AddressType), str_addressType);
                byte[] bytes_address   = NeoDun.SignTool.DecodeBase58(str_address);
                byte[] hash            = NeoDun.SignTool.ComputeSHA256(privateKey, 0, privateKey.Length);
                string str_hash        = NeoDun.SignTool.Bytes2HexString(hash, 0, hash.Length);

                NeoDun.DataBlock block = signer.dataTable.newOrGet(str_hash, (UInt32)privateKey.Length, NeoDun.DataBlockFrom.FromDriver);
                block.data = privateKey;
                signer.SendDataBlock(block);

                var  __block  = signer.dataTable.getBlockBySha256(str_hash);
                uint remoteid = await __block.GetRemoteid();

                if (remoteid == 0)
                {
                    return("suc");
                }
                __block.dataidRemote = 0;

                NeoDun.Message signMsg = new NeoDun.Message();
                signMsg.tag1  = 0x02;
                signMsg.tag2  = 0x04;//增
                signMsg.msgid = NeoDun.SignTool.RandomShort();
                signMsg.writeUInt16(0, addressType);
                Array.Copy(bytes_address, 0, signMsg.data, 2, bytes_address.Length);
                //这个dataid 要上一个block 传送完毕了才知道
                signMsg.writeUInt32(42, (uint)remoteid);
                signer.SendMessage(signMsg, true);

                needLoop  = true;
                addResult = false;
                int time = 0;
                while (needLoop && time <= waitTime)
                {
                    await Task.Delay(100);

                    time += 100;
                }
                needLoop = false;
                return(addResult ? "suc" : "Neodun拒绝");
            }
            catch (Exception e)
            {
                return("请输入正确的私钥");
            }
        }
Пример #4
0
        public async override Task <bool> SendMsg(params object[] _params)
        {
            string wif = (string)_params[0];

            byte[] privateKey;
            byte[] publicKey;
            string str_address = "";

            try
            {
                privateKey  = NeoDun.SignTool.GetPrivateKeyFromWif(wif);
                publicKey   = NeoDun.SignTool.GetPublicKeyFromPrivateKey(privateKey);
                str_address = NeoDun.SignTool.GetAddressFromPublicKey(publicKey);
            }
            catch (Exception e)
            {
                result.msgCode   = EnumMsgCode.AddAddressFailed;
                result.errorCode = EnumErrorCode.IncorrectWif;
                return(false);
            }

            //地址查重
            foreach (var add in signer.addressPool.addresses)
            {
                if (add.AddressText == str_address)
                {
                    result.msgCode   = EnumMsgCode.AddAddressFailed;
                    result.errorCode = EnumErrorCode.DuplicateWif;
                    return(false);
                }
            }
            try
            {
                //将私钥进行aes加密
                privateKey = SignTool.AesEncrypt(privateKey, ECDH.Ins.M);

                string str_addressType = "Neo";
                UInt16 addressType     = (UInt16)Enum.Parse(typeof(AddressType), str_addressType);
                byte[] bytes_address   = NeoDun.SignTool.DecodeBase58(str_address);
                byte[] hash            = NeoDun.SignTool.ComputeSHA256(privateKey, 0, privateKey.Length);
                string str_hash        = NeoDun.SignTool.Bytes2HexString(hash, 0, hash.Length);

                NeoDun.DataBlock block = signer.dataTable.newOrGet(str_hash, (UInt32)privateKey.Length, NeoDun.DataBlockFrom.FromDriver);
                block.data = privateKey;
                signer.SendDataBlock(block);

                uint remoteid;
                var  __block = signer.dataTable.getBlockBySha256(str_hash);
                while (true)
                {
                    await Task.Delay(100);

                    if (__block.dataidRemote > 0 && __block.Check())
                    {
                        remoteid = __block.dataidRemote;
                        break;
                    }
                }
                if (remoteid == 0)
                {
                    result.msgCode   = EnumMsgCode.AddAddressFailed;
                    result.errorCode = EnumErrorCode.IncorrectHash;
                    Release();
                    return(false);
                }
                __block.dataidRemote = 0;

                NeoDun.Message signMsg = new NeoDun.Message();
                signMsg.tag1  = 0x02;
                signMsg.tag2  = 0x04;//增
                signMsg.msgid = NeoDun.SignTool.RandomShort();
                signMsg.writeUInt16(0, addressType);
                Array.Copy(bytes_address, 0, signMsg.data, 2, bytes_address.Length);
                //这个dataid 要上一个block 传送完毕了才知道
                signMsg.writeUInt32(42, (uint)remoteid);
                signer.SendMessage(signMsg, true);
                return(true);
            }
            catch (Exception e)
            {
                result.errorCode = EnumErrorCode.IncorrectWif;
                result.msgCode   = EnumMsgCode.AddAddressFailed;
                Release();
                return(false);
            }
        }