예제 #1
0
        public static Address GenerateAddress(string _privateKey = "", bool _mainNet = true)
        {
            _privateKey = _privateKey == "" ? RandomPlus.RandomHex(64) : _privateKey;

            BigInteger _privateInt = BigInteger.Parse("0" + _privateKey, System.Globalization.NumberStyles.HexNumber);

            byte[] _publicKey = Secp256k1.PrivateKeyToPublicKey(_privateInt);

            SHA256Managed    _sha256 = new SHA256Managed();
            RIPEMD160Managed _ripemd = new RIPEMD160Managed();

            byte[] _ripemdHashed = _ripemd.ComputeHash(_sha256.ComputeHash(_publicKey));
            byte[] _addedVersion = new byte[_ripemdHashed.Length + 1];
            _addedVersion[0] = (byte)(_mainNet ? 0x00 : 0x6f);
            Array.Copy(_ripemdHashed, 0, _addedVersion, 1, _ripemdHashed.Length);

            byte[] _shaHashed = _sha256.ComputeHash(_sha256.ComputeHash(_addedVersion));
            Array.Resize(ref _shaHashed, 4);

            byte[] _result = new byte[_addedVersion.Length + _shaHashed.Length];
            Array.Copy(_addedVersion, 0, _result, 0, _addedVersion.Length);
            Array.Copy(_shaHashed, 0, _result, _addedVersion.Length, _shaHashed.Length);

            string _key1 = string.Join("", (_mainNet ? "80" : "ef"), _privateKey);
            string _key2 = HexPlus.ByteArrayToHexString(SHA.EncodeSHA256(SHA.EncodeSHA256(HexPlus.HexStringToByteArray(_key1))).Take(4).ToArray());

            Address _address = new Address();

            _address.Text       = Base58.Encode(_result);
            _address.PublicKey  = HexPlus.ByteArrayToHexString(_publicKey);
            _address.PrivateKey = Base58.Encode(_key1 + _key2);
            _address.Text       = (_mainNet ? (_address.Text.StartsWith("1") ? "" : "1") : "") + _address.Text;
            return(_address);
        }
예제 #2
0
        public static Address GetLegacyAddress(string _private = "", bool _mainnet = true)
        {
            _private = _private == "" ? RandomPlus.RandomHex(64) : _private;

            BigInteger _privateInt = BigInteger.Parse("0" + _private, NumberStyles.HexNumber);

            byte[] _public = Secp256k1.PrivateKeyToPublicKey(_privateInt, false);

            RIPEMD160Managed _ripemd = new RIPEMD160Managed();

            byte[] _ripemdHashed = _ripemd.ComputeHash(SHA.EncodeSHA256(_public));
            byte[] _addedVersion = new byte[_ripemdHashed.Length + 1];
            _addedVersion[0] = (byte)(_mainnet ? 0x00 : 0x6f);
            Array.Copy(_ripemdHashed, 0, _addedVersion, 1, _ripemdHashed.Length);

            byte[] _shaHashed = SHA.EncodeSHA256(SHA.EncodeSHA256(_addedVersion));
            Array.Resize(ref _shaHashed, 4);

            byte[] _result = new byte[_addedVersion.Length + _shaHashed.Length];
            Array.Copy(_addedVersion, 0, _result, 0, _addedVersion.Length);
            Array.Copy(_shaHashed, 0, _result, _addedVersion.Length, _shaHashed.Length);

            string _key1 = string.Join("", (_mainnet ? "80" : "ef"), _private);
            string _key2 = HexPlus.ByteArrayToHexString(SHA.EncodeSHA256(SHA.EncodeSHA256(HexPlus.HexStringToByteArray(_key1))).Take(4).ToArray());

            Address _address = new Address
            {
                Text    = Base58.Encode(_result),
                Public  = HexPlus.ByteArrayToHexString(_public),
                Private = Base58.Encode(_key1 + _key2)
            };

            return(_address);
        }
예제 #3
0
        public static Address Generate(string _privateKey = "")
        {
            Address _address = new Address();

            _address.Private = _privateKey == "" ? RandomPlus.RandomHex(64) : _privateKey;
            _address.Public  = HexPlus.ByteArrayToHexString(Secp256k1.PrivateKeyToPublicKey(_address.Private));
            _address.Public  = _address.Public.Substring(2);

            Keccak256 _keccakHasher = new Keccak256();
            string    _hexAddress   = _keccakHasher.ComputeHashByHex(_address.Public);

            _address.Text = "0x" + _hexAddress.Substring(_hexAddress.Length - 40);
            return(_address);
        }
예제 #4
0
        public static Address GetMultisignAddress(string[] _privates = null, int _required = 1, bool _mainnet = true)
        {
            if (_privates == null)
            {
                _privates = new string[1];
            }
            if (_privates.Length < _required)
            {
                throw new ArgumentException("Key count lower then require count");
            }

            for (int i = 0; i < _privates.Length; i++)
            {
                _privates[i] = string.IsNullOrWhiteSpace(_privates[i]) ? RandomPlus.RandomHex(64) : _privates[i];
            }

            string _publicScript = Privates2PublicScript(_privates, _required);

            byte[] _signArray = HexPlus.HexStringToByteArray(_publicScript);

            RIPEMD160Managed _ripemd = new RIPEMD160Managed();

            byte[] _ripemdHashed = _ripemd.ComputeHash(SHA.EncodeSHA256(_signArray));

            byte[] _addedVersion = new byte[_ripemdHashed.Length + 1];
            _addedVersion[0] = (byte)(_mainnet ? (byte)5 : (byte)196);
            Array.Copy(_ripemdHashed, 0, _addedVersion, 1, _ripemdHashed.Length);

            byte[] _shaHashed = SHA.EncodeSHA256(SHA.EncodeSHA256(_addedVersion));
            Array.Resize(ref _shaHashed, 4);

            byte[] _result = new byte[_addedVersion.Length + _shaHashed.Length];
            Array.Copy(_addedVersion, 0, _result, 0, _addedVersion.Length);
            Array.Copy(_shaHashed, 0, _result, _addedVersion.Length, _shaHashed.Length);

            for (int i = 0; i < _privates.Length; i++)
            {
                _privates[i] = Private2Wif(_privates[i], _mainnet, true);
            }

            Address _address = new Address
            {
                Text     = Base58.Encode(_result),
                Public   = _publicScript,
                Privates = _privates
            };

            return(_address);
        }
예제 #5
0
        public static string SignHex(string _hex, string _wif)
        {
            BigInteger _k = BigNumberPlus.HexToBigInt(RandomPlus.RandomHex());

            Encrypt.ECPoint _gk      = Secp256k1.G.Multiply(_k);
            BigInteger      _r       = _gk.X;
            BigInteger      _e       = BigNumberPlus.HexToBigInt(_hex);
            string          _private = Address.Wif2Private(_wif, out _, out bool _compressed);
            BigInteger      _d       = BigNumberPlus.HexToBigInt(_private);
            BigInteger      _s       = ((_r * _d + _e) * _k.ModInverse(Secp256k1.N)) % Secp256k1.N;

            if (_s.CompareTo(Secp256k1.HalfN) > 0)
            {
                _s = Secp256k1.N - _s;
            }

            List <byte> _rbytes   = _r.ToByteArray().Reverse().ToList();
            List <byte> _sbytes   = _s.ToByteArray().Reverse().ToList();
            List <byte> _result   = new List <byte>();
            BigInteger  _rsLength = _rbytes.Count() + _sbytes.Count() + 4;

            _result.Add(0x30);
            _result.AddRange(_rsLength.ToByteArray());
            _result.Add(0x02);
            _result.AddRange(((BigInteger)_rbytes.Count()).ToByteArray());
            _result.AddRange(_rbytes.ToArray());
            _result.Add(0x02);
            _result.AddRange(((BigInteger)_sbytes.Count()).ToByteArray());
            _result.AddRange(_sbytes.ToArray());
            _result.Add(0x01);
            BigInteger _publicBytesLength = BigNumberPlus.HexToBigInt(Address.Private2Public(_private, false, _compressed)).ToByteArray().Length;

            _result.AddRange(_publicBytesLength.ToByteArray());
            _result.InsertRange(0, ((BigInteger)(_result.Count - 1)).ToByteArray());

            return(HexPlus.ByteArrayToHexString(_result.ToArray()));
        }