Exemple #1
0
        private void VerifyWif(UserInfo user)
        {
            var pkBytes = Secp256K1Manager.GetPublicKey(user.ActiveKeys[0], true);
            var pk      = new PublicKeyType(pkBytes, SbdSymbol);

            Assert.True(pk.Data.SequenceEqual(user.Account.Active.KeyAuths[0].Key.Data));
        }
        public DefaultSignProvider(string privateKey)
        {
            var privKeyBytes = CryptoHelper.GetPrivateKeyBytesWithoutCheckSum(privateKey);
            var pubKey       = CryptoHelper.PubKeyBytesToString(Secp256K1Manager.GetPublicKey(privKeyBytes, true));

            Keys.Add(pubKey, privKeyBytes);
        }
Exemple #3
0
        public static string GetVakaPublicKey(string privatekey)
        {
            var privateBytes = WifUtility.DecodePrivateWif(privatekey);
            var publicKey    = Secp256K1Manager.GetPublicKey(privateBytes, true);

            return(WifUtility.GetPublicWif(publicKey, "VAKA"));
        }
Exemple #4
0
        public void GenerateKeys()
        {
            WriteLine("password key:");
            var privateKey = Secp256K1Manager.GenerateRandomKey();
            var privateWif = "P" + Base58.EncodePrivateWif(privateKey);

            WriteLine(privateWif);
            WriteLine(Hex.ToString(privateKey));

            var publicKey       = Secp256K1Manager.GetPublicKey(privateKey, true);
            var encodePublicWif = Base58.EncodePublicWif(publicKey, "STM");

            WriteLine(encodePublicWif);
            WriteLine(Hex.ToString(publicKey));

            var name = "userlogin";

            string[] roles = { "posting", "active", "owner", "memo" };

            foreach (var role in roles)
            {
                WriteLine(role);
                var subWif = Base58.GetSubWif(name, privateWif, role);
                WriteLine(subWif);
                var pk = Base58.DecodePrivateWif(subWif);
                WriteLine(Hex.ToString(pk));
                var subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);
                var publicWif    = Base58.EncodePublicWif(subPublicKey, "STM");
                WriteLine(publicWif);
                WriteLine(Hex.ToString(subPublicKey));
            }
        }
Exemple #5
0
        private (byte[], byte[]) GenerateKeyPair()
        {
            var privateKey = Secp256K1Manager.GenerateRandomKey();
            var publicKey  = Secp256K1Manager.GetPublicKey(privateKey, compressed: true);

            return(publicKey, privateKey);
        }
Exemple #6
0
        public void GetPublicKeyTest(string privateKey, string expectedPubKey)
        {
            var pk        = Base58.DecodePrivateWif(privateKey);
            var publicKey = Secp256K1Manager.GetPublicKey(pk, true);
            var pWif      = Base58.EncodePublicWif(publicKey, "STM");

            Assert.Equal(expectedPubKey, pWif);
        }
Exemple #7
0
        public static byte[] GetPublicKey(byte[] privateKey, bool compressed)
        {
            var originalkeyBytes = Secp256K1Manager.GetPublicKey(privateKey, compressed);
            var validKeyBytes    = new byte[originalkeyBytes.Length - 1];

            Buffer.BlockCopy(originalkeyBytes, 1, validKeyBytes, 0, validKeyBytes.Length);

            return(validKeyBytes);
        }
 public static bool IsValid(
     // ReSharper disable once ParameterTypeCanBeEnumerable.Global
     byte[] publicKey,
     byte[] privateKey)
 {
     return(Secp256K1Manager
            .GetPublicKey(privateKey, true)
            .SequenceEqual(publicKey));
 }
Exemple #9
0
 public void Sign(byte[] privKey, byte[] pubKey)
 {
     byte[] pub = pubKey;
     if (pubKey == null)
     {
         pub = Secp256K1Manager.GetPublicKey(privKey, true);
     }
     this.GtxObject.Sign(privKey, pub);
 }
Exemple #10
0
        /// <summary>
        /// Gets the public key.
        /// </summary>
        /// <returns>The public key.</returns>
        public PublicKey GetPublicKey()
        {
            var pubKeyByes = Secp256K1Manager.GetPublicKey(this.D.ToByteArrayUnsigned(true), true);

            return(new PublicKey()
            {
                Data = pubKeyByes
            });
        }
Exemple #11
0
        public static KeyPair GenerateKeyPair()
        {
            KeyPair keyPair = new KeyPair();

            byte[] privateKey = Secp256K1Manager.GenerateRandomKey();
            keyPair.PrivateKey = WifUtility.GetPrivateWif(privateKey);
            byte[] publicKey = Secp256K1Manager.GetPublicKey(privateKey, true);
            keyPair.PublicKey = WifUtility.GetPublicWif(publicKey, "EOS");
            return(keyPair);
        }
Exemple #12
0
        public string GetPublicKey()
        {
            var publicKeyBytes  = Secp256K1Manager.GetPublicKey(Key, true);
            var checksum        = GetChecksum(publicKeyBytes);
            var keyWithChecksum = new[] { publicKeyBytes, checksum };

            var publicKey = Base58.Encode(keyWithChecksum.Flattern());

            return($"EOS{publicKey}");
        }
Exemple #13
0
        public static KeyPair GenerateKeyPair(string keyType = null)
        {
            var key    = Secp256K1Manager.GenerateRandomKey();
            var pubKey = Secp256K1Manager.GetPublicKey(key, true);

            return(new KeyPair()
            {
                PrivateKey = KeyToString(key, keyType, keyType == "R1" ? "PVT_R1_" : null),
                PublicKey = KeyToString(pubKey, keyType, keyType == "R1" ? "PUB_R1_" : "EOS")
            });
        }
Exemple #14
0
        public async Task AccountCreateOperationTest()
        {
            var name = $"ditch-test-user-{DateTime.Now.Ticks}";

            WriteLine($"name: {name}");
            var key  = Secp256K1Manager.GenerateRandomKey();
            var pwif = "P" + Base58.EncodePrivateWif(key);

            WriteLine($"pwif: {pwif}");

            var user   = User;
            var userId = user.Account.Id;

            var op = new AccountCreateOperation
            {
                Fee = new Asset
                {
                    Amount  = 500000,
                    AssetId = new AssetIdType(1, 3, 0)
                },
                Registrar       = userId,
                Referrer        = userId,
                ReferrerPercent = 10000,
                Name            = name,
                Options         = new AccountOptions
                {
                    VotingAccount = new AccountIdType(1, 2, 5)
                }
            };

            var subWif = Base58.GetSubWif(name, pwif, "owner");

            WriteLine($"owner: {subWif}");
            var pk           = Base58.DecodePrivateWif(subWif);
            var subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);

            op.Owner = new Authority(new PublicKeyType(subPublicKey, SbdSymbol));

            subWif = Base58.GetSubWif(name, pwif, "active");
            WriteLine($"active: {subWif}");
            pk           = Base58.DecodePrivateWif(subWif);
            subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);
            op.Active    = new Authority(new PublicKeyType(subPublicKey, SbdSymbol));

            subWif = Base58.GetSubWif(name, pwif, "memo");
            WriteLine($"memo: {subWif}");
            pk                 = Base58.DecodePrivateWif(subWif);
            subPublicKey       = Secp256K1Manager.GetPublicKey(pk, true);
            op.Options.MemoKey = new PublicKeyType(subPublicKey, SbdSymbol);


            await Post(user.ActiveKeys, false, op).ConfigureAwait(false);
        }
Exemple #15
0
        public async Task WalletImportKeyTest()
        {
            var user = new UserInfo
            {
                Login = $"testname{DateTime.Now:yyyyMMddhhmmss}"
            };

            WriteLine(user.Login);
            var resp = await Api.WalletCreate(user.Login, CancellationToken.None);

            WriteLine(resp);
            Assert.IsFalse(resp.IsError);
            user.Password = resp.Result;

            var key1 = Secp256K1Manager.GenerateRandomKey();

            user.PrivateOwnerWif = Core.Base58.EncodePrivateWif(key1);
            var pKey1 = Secp256K1Manager.GetPublicKey(key1, true);

            user.PublicOwnerWif = Core.Base58.EncodePublicWif(pKey1, "EOS");

            var key2 = Secp256K1Manager.GenerateRandomKey();

            user.PrivateActiveWif = Core.Base58.EncodePrivateWif(key2);
            var pKey2 = Secp256K1Manager.GetPublicKey(key2, true);

            user.PublicActiveWif = Core.Base58.EncodePublicWif(pKey2, "EOS");

            WriteLine(user);

            var addOwner = await Api.WalletImportKey(user.Login, user.PrivateOwnerWif, CancellationToken.None);

            WriteLine(addOwner);
            Assert.IsFalse(resp.IsError);

            var addActive = await Api.WalletImportKey(user.Login, user.PrivateActiveWif, CancellationToken.None);

            WriteLine(addActive);
            Assert.IsFalse(resp.IsError);

            var unlock = await Api.WalletUnlock(user.Login, user.Password, CancellationToken);

            Assert.IsFalse(unlock.IsError);

            var walletGetPublicKeysResult = await Api.WalletGetPublicKeys(CancellationToken.None);

            WriteLine(walletGetPublicKeysResult);
            Assert.IsFalse(walletGetPublicKeysResult.IsError);
        }
Exemple #16
0
        /// <summary>
        /// Generate a new key pair based on a key type
        /// </summary>
        /// <param name="keyType">Optional key type. (sha256x2, R1)</param>
        /// <returns>key pair</returns>
        public static KeyPair GenerateKeyPair(string keyType = "sha256x2")
        {
            var key    = Secp256K1Manager.GenerateRandomKey();
            var pubKey = Secp256K1Manager.GetPublicKey(key, true);

            if (keyType != "sha256x2" && keyType != "R1")
            {
                throw new Exception("invalid key type.");
            }

            return(new KeyPair()
            {
                PrivateKey = KeyToString(key, keyType, keyType == "R1" ? "PVT_R1_" : null),
                PublicKey = KeyToString(pubKey, keyType != "sha256x2" ? keyType : null, keyType == "R1" ? "PUB_R1_" : "EOS")
            });
        }
 public static bool ValidatePrivateKey(byte[] privateKey, byte[][] publicKeys)
 {
     try
     {
         var pubKey = Secp256K1Manager.GetPublicKey(privateKey, true);
         foreach (var publicPostingKey in publicKeys)
         {
             if (pubKey.SequenceEqual(publicPostingKey))
             {
                 return(true);
             }
         }
     }
     catch
     {
         //todo nothing
     }
     return(false);
 }
Exemple #18
0
        public async Task AccountCreateOperationTestAsync()
        {
            var name = "userlogin";
            var key  = Secp256K1Manager.GenerateRandomKey();
            var pwif = "P" + Base58.EncodePrivateWif(key);

            var user = User.Account.Id;

            var op = new AccountCreateOperation
            {
                Fee = new Asset
                {
                    Amount  = 100000,
                    AssetId = new AssetIdType(1, 3, 0)
                },
                Registrar       = user,
                Referrer        = user,
                ReferrerPercent = 10000,
                Name            = name,
                Options         = new AccountOptions
                {
                    VotingAccount = new AccountIdType(1, 2, 5)
                }
            };

            var subWif       = Base58.GetSubWif(name, pwif, "owner");
            var pk           = Base58.DecodePrivateWif(subWif);
            var subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);

            op.Owner = new Authority(new PublicKeyType(subPublicKey, "TEST"));

            subWif       = Base58.GetSubWif(name, pwif, "active");
            pk           = Base58.DecodePrivateWif(subWif);
            subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);
            op.Active    = new Authority(new PublicKeyType(subPublicKey, "TEST"));

            subWif             = Base58.GetSubWif(name, pwif, "memo");
            pk                 = Base58.DecodePrivateWif(subWif);
            subPublicKey       = Secp256K1Manager.GetPublicKey(pk, true);
            op.Options.MemoKey = new PublicKeyType(subPublicKey, "TEST");

            await Post(User.ActiveKeys, false, op);
        }
 public static bool ValidatePrivateKey(string pass, byte[][] publicKeys)
 {
     try
     {
         var privateKey = Ditch.Core.Base58.DecodePrivateWif(pass);
         var pubKey     = Secp256K1Manager.GetPublicKey(privateKey, true);
         foreach (var publicPostingKey in publicKeys)
         {
             if (pubKey.SequenceEqual(publicPostingKey))
             {
                 return(true);
             }
         }
     }
     catch
     {
         //todo nothing
     }
     return(false);
 }
Exemple #20
0
        public async Task AccountCreateTest()
        {
            var name = "userlogin";

            var op = new AccountCreateOperation
            {
                Fee            = new Asset(3000, Config.SteemAssetNumSteem),
                Creator        = User.Login,
                NewAccountName = User.Login,
                JsonMetadata   = "",
            };

            var privateKey = Secp256K1Manager.GenerateRandomKey();
            var privateWif = "P" + Base58.EncodePrivateWif(privateKey);

            var subWif       = Base58.GetSubWif(name, privateWif, "owner");
            var pk           = Base58.DecodePrivateWif(subWif);
            var subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);

            op.Owner.KeyAuths.Add(new KeyValuePair <PublicKeyType, ushort>(new PublicKeyType(subPublicKey), 1));

            subWif       = Base58.GetSubWif(name, privateWif, "active");
            pk           = Base58.DecodePrivateWif(subWif);
            subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);
            op.Active.KeyAuths.Add(new KeyValuePair <PublicKeyType, ushort>(new PublicKeyType(subPublicKey), 1));

            subWif       = Base58.GetSubWif(name, privateWif, "posting");
            pk           = Base58.DecodePrivateWif(subWif);
            subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);
            op.Posting.KeyAuths.Add(new KeyValuePair <PublicKeyType, ushort>(new PublicKeyType(subPublicKey), 1));

            subWif       = Base58.GetSubWif(name, privateWif, "memo");
            pk           = Base58.DecodePrivateWif(subWif);
            subPublicKey = Secp256K1Manager.GetPublicKey(pk, true);
            op.MemoKey   = new PublicKeyType(subPublicKey);

            var response = Post(User.ActiveKeys, false, op);

            Assert.IsFalse(response.IsError, response.GetErrorMessage());
        }
Exemple #21
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
            });
        }
Exemple #22
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));
        }
Exemple #23
0
 public static byte[] PrivateKeyToPublicKey(byte[] privateKey, bool compressed)
 {
     return(Secp256K1Manager.GetPublicKey(privateKey, compressed));
 }