Пример #1
0
        public string GetPublicAddress()
        {
            if (!string.IsNullOrWhiteSpace(_publicAddress))
            {
                return(_publicAddress);
            }

            var initaddr = _ecKey.GetPubKeyNoPrefix().ToKeccakHash();
            var address  = new byte[initaddr.Length - 11];

            Array.Copy(initaddr, 12, address, 1, 20);
            address[0] = GetPublicAddressPrefix();

            var hash  = Base58Encoder.TwiceHash(address);
            var bytes = new byte[4];

            Array.Copy(hash, bytes, 4);
            var addressChecksum = new byte[25];

            Array.Copy(address, 0, addressChecksum, 0, 21);
            Array.Copy(bytes, 0, addressChecksum, 21, 4);

            if (_network == TronNetwork.MainNet)
            {
                _publicAddress = Base58Encoder.Encode(addressChecksum);
            }
            else
            {
                _publicAddress = addressChecksum.ToHex();
            }
            return(_publicAddress);
        }
Пример #2
0
        public static string GetPublicAddress(this ECKey key)
        {
            var initaddr = new Nethereum.ABI.Util.Sha3Keccack().CalculateHash(key.GetPubKeyNoPrefix());
            var addr     = new byte[initaddr.Length - 12];

            Array.Copy(initaddr, 12, addr, 0, initaddr.Length - 12);
            return(addr.ToHex());
        }
Пример #3
0
        public void ShouldSignEncodeTransactionAndRecoverPublicAddress()
        {
            var privateKey     = "b5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var sendersAddress = "12890d2cce102216644c59daE5baed380d84830c";
            var publicKey      =
                "87977ddf1e8e4c3f0a4619601fc08ac5c1dcf78ee64e826a63818394754cef52457a10a599cb88afb7c5a6473b7534b8b150d38d48a11c9b515dd01434cceb08";

            var key       = new ECKey(privateKey.HexToByteArray(), true);
            var hash      = "test".ToHexUTF8().HexToByteArray();
            var signature = key.Sign(hash);

            Assert.True(key.Verify(hash, signature));
            Assert.Equal(key.GetPubKeyNoPrefix().ToHex(), publicKey);
            Assert.Equal(sendersAddress.ToLower(), key.GetPublicAddress());
        }