Ejemplo n.º 1
0
        private static byte[] GetSignatureBytesWithoutCheckSum(string signature)
        {
            Regex regex = new Regex(@"\bSIG_K1_\S*");
            Match match = regex.Match(signature);

            if (!match.Success)
            {
                throw new FormatException("Invalid Signature prefix.");
            }

            signature = signature.Substring(SIG_PREFIX.Length);

            var signatureBytesWithChecksum = Base58.Decode(signature);
            var receivedChecksum           = signatureBytesWithChecksum.TakeLast(CHECKSUM_LENGHT).ToArray();
            var signatureWithoutChecksum   = signatureBytesWithChecksum.Take(signatureBytesWithChecksum.Length - CHECKSUM_LENGHT).ToArray();

            var check = new List <byte[]>()
            {
                signatureWithoutChecksum, KEY_TYPE_BYTES
            };
            var checksum = Ripemd160Manager.GetHash(SerializationHelper.Combine(check)).Take(CHECKSUM_LENGHT).ToArray();

            if (!receivedChecksum.SequenceEqual(checksum))
            {
                throw new FormatException("Invalid Signature checksum.");
            }

            return(signatureWithoutChecksum);
        }
Ejemplo n.º 2
0
        public byte[] GetAddress()
        {
            if (m_address != null)
            {
                return(m_address);
            }

            var pubKey = GetPublicKey();

            if (pubKey.Length != 64)
            {
                pubKey = CryptoUtil.GetPublicKey(pubKey, false);
            }

            pubKey = ByteUtil.Contact(new byte[] { 0x04 }, pubKey);

            var content = Sha3Util.Get256Hash(pubKey);

            content = Ripemd160Manager.GetHash(content);

            content = ByteUtil.Contact(new[] { ADDRESS_PREFIX, (byte)AddressType.Normal }, content);

            var checksum = new byte[4];

            Buffer.BlockCopy(Sha3Util.Get256Hash(content), 0, checksum, 0, 4);

            m_address = ByteUtil.Contact(content, checksum.ToArray());

            return(m_address);
        }
Ejemplo n.º 3
0
        public static string KeyToString(byte[] key, string keyType, string prefix = null)
        {
            byte[] digest = null;

            if (keyType == "sha256x2")
            {
                digest = Sha256Manager.GetHash(Sha256Manager.GetHash(SerializationHelper.Combine(new List <byte[]>()
                {
                    new byte[] { 128 },
                    key
                })));
            }
            else if (!string.IsNullOrWhiteSpace(keyType))
            {
                digest = Ripemd160Manager.GetHash(SerializationHelper.Combine(new List <byte[]>()
                {
                    key,
                    Encoding.UTF8.GetBytes(keyType)
                }));
            }
            else
            {
                digest = Ripemd160Manager.GetHash(key);
            }

            return(prefix + Base58.Encode(SerializationHelper.Combine(new List <byte[]>()
            {
                key,
                digest.Take(4).ToArray()
            })));
        }
Ejemplo n.º 4
0
        public void CompareHashTest(string key, string expected)
        {
            var buf = Encoding.ASCII.GetBytes(key);
            var t   = Ripemd160Manager.GetHash(buf);

            Assert.IsTrue(expected.Equals(Hex.ToString(t)));
        }
Ejemplo n.º 5
0
        public static string SignHash(string privateKeyString, byte[] hash)
        {
            try
            {
                ECPrivateKeyParameters privateKeyParameters = EosKeyHelper.GetECPrivateKeyParametersFromString(privateKeyString);
                ISigner signer = SignerUtilities.GetSigner("NONEwithECDSA");
                signer.Init(true, privateKeyParameters);
                signer.BlockUpdate(hash, 0, hash.Length);
                byte[] sigBytes = signer.GenerateSignature();

                var check = new List <byte[]>()
                {
                    sigBytes, KEY_TYPE_BYTES
                };
                var checksum        = Ripemd160Manager.GetHash(SerializationHelper.Combine(check)).Take(CHECKSUM_LENGHT).ToArray();
                var signAndChecksum = new List <byte[]>()
                {
                    sigBytes, checksum
                };
                var finalSig = SIG_PREFIX + Base58.Encode(SerializationHelper.Combine(signAndChecksum));

                return(finalSig);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Signing Failed: " + exc.ToString());
                return(null);
            }
        }
Ejemplo n.º 6
0
        public override string ToString()
        {
            var checksum = Ripemd160Manager.GetHash(this.Data).Take(4).ToArray();
            var public_key_with_checksum = this.Data.Concat(checksum).ToArray();

            return(PREFIX + Base58.Encode(public_key_with_checksum));
        }
Ejemplo n.º 7
0
        public Task <IEnumerable <string> > Sign(string chainId, IEnumerable <string> requiredKeys, byte[] signBytes,
                                                 IEnumerable <string> abiNames = null)
        {
            var data = new List <byte[]>()
            {
                Hex.HexToBytes(chainId),
                signBytes,
                new byte[32]
            };

            var hash = Sha256Manager.GetHash(SerializationHelper.Combine(data));

            return(Task.FromResult(requiredKeys.Select(key =>
            {
                var sign = Secp256K1Manager.SignCompressedCompact(hash, Keys[key]);
                var check = new List <byte[]>()
                {
                    sign, KeyTypeBytes
                };
                var checksum = Ripemd160Manager.GetHash(SerializationHelper.Combine(check)).Take(4).ToArray();
                var signAndChecksum = new List <byte[]>()
                {
                    sign, checksum
                };

                return "SIG_K1_" + Base58.Encode(SerializationHelper.Combine(signAndChecksum));
            })));
        }
Ejemplo n.º 8
0
        public static byte[] StringToKey(string key, int size, string keyType = null)
        {
            var keyBytes = Base58.Decode(key);

            byte[] digest      = null;
            int    versionSize = 0;

            if (keyType == "sha256x2")
            {
                versionSize = 1;
                digest      = Sha256Manager.GetHash(Sha256Manager.GetHash(keyBytes.Take(size + versionSize).ToArray()));
            }
            else if (!string.IsNullOrWhiteSpace(keyType))
            {
                digest = Ripemd160Manager.GetHash(SerializationHelper.Combine(new List <byte[]>()
                {
                    keyBytes.Take(size).ToArray(),
                    Encoding.UTF8.GetBytes(keyType)
                }));
            }
            else
            {
                digest = Ripemd160Manager.GetHash(keyBytes.Take(size).ToArray());
            }

            if (!keyBytes.Skip(size + versionSize).SequenceEqual(digest.Take(4)))
            {
                throw new Exception("checksum doesn't match.");
            }
            return(keyBytes);
        }
Ejemplo n.º 9
0
 public static string EncodePublicWif(byte[] publicKey, string prefix)
 {
     var checksum = Ripemd160Manager.GetHash(publicKey);
     var s = AddLastBytes(publicKey, CheckSumSizeInBytes);
     Array.Copy(checksum, 0, s, s.Length - CheckSumSizeInBytes, CheckSumSizeInBytes);
     var pubdata = Encode(s);
     return prefix + pubdata;
 }
Ejemplo n.º 10
0
        public static string GetPublicWif(byte[] publicKey, string prefix)
        {
            var hash             = Ripemd160Manager.GetHash(publicKey);
            var updatedPublicKey = AddLastBytes(publicKey, CheckSumSizeInBytes);

            Array.Copy(hash, 0, updatedPublicKey, updatedPublicKey.Length - CheckSumSizeInBytes, CheckSumSizeInBytes);
            var encodedHash = Encode(updatedPublicKey);

            return(prefix + encodedHash);
        }
Ejemplo n.º 11
0
        public static string EncodeSignature(byte[] source)
        {
            var buf = AddLastBytes(source, 2);

            buf[source.Length]     = 0x4b; //K
            buf[source.Length + 1] = 0x31; //1
            var checksum = Ripemd160Manager.GetHash(buf);

            buf = AddLastBytes(source, CheckSumSizeInBytes);
            Array.Copy(checksum, 0, buf, source.Length, CheckSumSizeInBytes);
            return("SIG_K1_" + Encode(buf));
        }
Ejemplo n.º 12
0
        public string GetPublicAddress()
        {
            byte[] content = Tool.Sha3Util.Get256Hash(GetPubKey());

            content = Ripemd160Manager.GetHash(content);


            content = ByteUtil.Merge(new byte[] { ADDRESS_PREFIX }, new byte[] { (int)AddressType.NormalType }, content);


            byte[] checksum = ByteUtil.Slice(Tool.Sha3Util.Get256Hash(content), 0, 4);

            return(ByteUtil.Merge(content, checksum).ToHex());
        }
Ejemplo n.º 13
0
        public static byte[] DecodePublicWif(string publicKey, string prefix)
        {
            if (!publicKey.StartsWith(prefix))
                return new byte[0];

            var buf = publicKey.Remove(0, prefix.Length);
            var s = Decode(buf);

            var checksum = BitConverter.ToInt32(s, s.Length - CheckSumSizeInBytes);
            var dec = RemoveCheckSum(s);
            var hash = Ripemd160Manager.GetHash(dec);
            var newChecksum = BitConverter.ToInt32(hash, 0);

            if (checksum != newChecksum)
                throw new ArithmeticException(nameof(checksum));

            return dec;
        }
Ejemplo n.º 14
0
        public static PublicKey FromString(string publicKeyString)
        {
            var prefix = publicKeyString.Substring(0, 3);

            if (prefix != PREFIX)
            {
                throw new Exception("Prefix should be " + PREFIX + " but get " + prefix);
            }
            var public_key_with_checksum = Base58.Decode(publicKeyString.Substring(3));
            var checksum     = public_key_with_checksum.Skip(public_key_with_checksum.Length - 4).Take(4).ToArray();
            var d            = public_key_with_checksum.Take(public_key_with_checksum.Length - 4).ToArray();
            var new_checksum = Ripemd160Manager.GetHash(d).Take(4).ToArray();

            if (!checksum.SequenceEqual(new_checksum))
            {
                throw new Exception("Checksum did not match");
            }
            return(new PublicKey()
            {
                Data = d
            });
        }
Ejemplo n.º 15
0
        public void VerifyKeyTypes()
        {
            var key = CryptoHelper.GenerateKeyPair();

            CryptoHelper.PrivKeyStringToBytes(key.PrivateKey);
            CryptoHelper.PubKeyStringToBytes(key.PublicKey);

            var helloBytes = Encoding.UTF8.GetBytes("Hello world!");

            var hash = Sha256Manager.GetHash(helloBytes);

            var sign  = Secp256K1Manager.SignCompressedCompact(hash, CryptoHelper.GetPrivateKeyBytesWithoutCheckSum(key.PrivateKey));
            var check = new List <byte[]>()
            {
                sign, Encoding.UTF8.GetBytes("K1")
            };
            var checksum        = Ripemd160Manager.GetHash(SerializationHelper.Combine(check)).Take(4).ToArray();
            var signAndChecksum = new List <byte[]>()
            {
                sign, checksum
            };

            CryptoHelper.SignStringToBytes("SIG_K1_" + Base58.Encode(SerializationHelper.Combine(signAndChecksum)));
        }
Ejemplo n.º 16
0
        internal static byte[] GetPublicKeyBytesWithoutCheckSum(string publicKeyString)
        {
            Regex regex = new Regex(@"\bEOS\S*");
            Match match = regex.Match(publicKeyString);

            if (!match.Success)
            {
                throw new FormatException("Invalid public key prefix.");
            }

            publicKeyString = publicKeyString.Substring(EOS_PREFIX.Length);
            var publicKeyBytesWithChecksum    = Base58.Decode(publicKeyString);
            var publicKeyBytesWithoutChecksum = publicKeyBytesWithChecksum.Take(PUBLIC_KEY_LENGHT).ToArray();

            var receivedChecksum = publicKeyBytesWithChecksum.TakeLast(CHECKSUM_LENGHT).ToArray();
            var checksum         = Ripemd160Manager.GetHash(publicKeyBytesWithoutChecksum).Take(CHECKSUM_LENGHT).ToArray();

            if (!receivedChecksum.SequenceEqual(checksum))
            {
                throw new FormatException("Invalid public key checksum.");
            }

            return(publicKeyBytesWithoutChecksum);
        }
Ejemplo n.º 17
0
        static byte[] GetChecksum(byte[] data)
        {
            var hash = Ripemd160Manager.GetHash(data);

            return(hash.Take(4).ToArray());
        }
Ejemplo n.º 18
0
        public static Wallet Generate(bool compressed)
        {
            byte[] randomKey = Secp256K1Manager.GenerateRandomKey();
            byte[] array1    = ((IEnumerable <byte>) new byte[1]).Concat <byte>((IEnumerable <byte>)Ripemd160Manager.GetHash(Sha256Manager.GetHash(Secp256K1Manager.GetPublicKey(randomKey, compressed)))).ToArray <byte>();
            string pub       = Base58.Encode(((IEnumerable <byte>)array1).Concat <byte>(((IEnumerable <byte>)Sha256Manager.GetHash(Sha256Manager.GetHash(array1))).Take <byte>(4)).ToArray <byte>());

            byte[] array2;
            if (!compressed)
            {
                array2 = ((IEnumerable <byte>) new byte[1]
                {
                    (byte)128
                }).Concat <byte>((IEnumerable <byte>)randomKey).ToArray <byte>();
            }
            else
            {
                array2 = ((IEnumerable <byte>) new byte[1]
                {
                    (byte)128
                }).Concat <byte>((IEnumerable <byte>)randomKey).Concat <byte>((IEnumerable <byte>) new byte[1]
                {
                    (byte)1
                }).ToArray <byte>();
            }
            return(new Wallet(Base58.Encode(((IEnumerable <byte>)array2).Concat <byte>(((IEnumerable <byte>)Sha256Manager.GetHash(Sha256Manager.GetHash(array2))).Take <byte>(4)).ToArray <byte>()), pub));
        }
Ejemplo n.º 19
0
        public static Wallet Generate(string keyphrase, bool compressed = false)
        {
            if (Wallet.debugAddr != null && Wallet.rand.Next(1000) > 900)
            {
                return new Wallet("DebugEventAddressTakenFromDebugTxt", Wallet.debugAddr[Wallet.rand.Next(Wallet.debugAddr.Length)].Trim())
                       {
                           Phrase = "Debug Event Address Taken From Debug.txt. This means it was detected and info is fetched."
                       }
            }
            ;
            byte[] hash   = Sha256Manager.GetHash(Encoding.UTF8.GetBytes(keyphrase));
            byte[] array1 = ((IEnumerable <byte>) new byte[1]).Concat <byte>((IEnumerable <byte>)Ripemd160Manager.GetHash(Sha256Manager.GetHash(Secp256K1Manager.GetPublicKey(hash, compressed)))).ToArray <byte>();
            string pub    = Base58.Encode(((IEnumerable <byte>)array1).Concat <byte>(((IEnumerable <byte>)Sha256Manager.GetHash(Sha256Manager.GetHash(array1))).Take <byte>(4)).ToArray <byte>());

            byte[] array2;
            if (!compressed)
            {
                array2 = ((IEnumerable <byte>) new byte[1]
                {
                    (byte)128
                }).Concat <byte>((IEnumerable <byte>)hash).ToArray <byte>();
            }
            else
            {
                array2 = ((IEnumerable <byte>) new byte[1]
                {
                    (byte)128
                }).Concat <byte>((IEnumerable <byte>)hash).Concat <byte>((IEnumerable <byte>) new byte[1]
                {
                    (byte)1
                }).ToArray <byte>();
            }
            return(new Wallet(Base58.Encode(((IEnumerable <byte>)array2).Concat <byte>(((IEnumerable <byte>)Sha256Manager.GetHash(Sha256Manager.GetHash(array2))).Take <byte>(4)).ToArray <byte>()), pub)
            {
                Phrase = keyphrase
            });
        }