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)); } }
public void SignCompressedCompactTest() { var key = Secp256K1Manager.GenerateRandomKey(); var sw1 = new Stopwatch(); var rand = new RNGCryptoServiceProvider(); byte[] msg; for (int i = 1; i < 1000; i++) { msg = new byte[i]; rand.GetBytes(msg); var hash = Sha256Manager.GetHash(msg); sw1.Start(); var signature1 = Secp256K1Manager.SignCompressedCompact(hash, key); sw1.Stop(); Assert.True(signature1.Length == 65); Assert.True(Secp256K1Manager.IsCanonical(signature1, 1)); if (!Secp256K1Manager.IsCanonical(signature1, 1)) { WriteLine($"signature1 not canonical - skip [{i}]"); } } WriteLine($"Secp256K1Manager time {sw1.ElapsedTicks}"); }
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)); }
private (byte[], byte[]) GenerateKeyPair() { var privateKey = Secp256K1Manager.GenerateRandomKey(); var publicKey = Secp256K1Manager.GetPublicKey(privateKey, compressed: true); return(publicKey, privateKey); }
private (PrivateKey PrivateKey, PublicKey PublicKey, Address Address) Create(bool compress = true) { var privateKeyBytes = Secp256K1Manager.GenerateRandomKey(); var publicKeyBytes = Secp256K1Manager.GetPublicKey(privateKeyBytes, compress).Skip(1).ToArray(); var publicKeyHash = Sha256Manager.GetHash(publicKeyBytes); var addressBytes = publicKeyHash.Skip(12).ToArray(); return(new PrivateKey(privateKeyBytes), new PublicKey(publicKeyBytes, compress), new Address(addressBytes)); }
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); }
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") }); }
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); }
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); }
/// <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 async Task AccountCreateTest() { const string name = "userlogin"; var op = new AccountCreateOperation { Fee = new Asset(3000, 3, "GBG"), Creator = User.Login, NewAccountName = User.Login, JsonMetadata = "" }; var privateKey = Secp256K1Manager.GenerateRandomKey(); var privateWif = "12345678"; //"P" + Base58.EncodePrivateWif(privateKey); var subWif = Base58.GetSubWif(name, privateWif, "owner"); Console.WriteLine(subWif); var pk = Base58.DecodePrivateWif(subWif); var subPublicKey = Secp256K1Manager.GetPublicKey(pk, true); Console.WriteLine(Base58.EncodePublicWif(subPublicKey, Config.KeyPrefix)); op.Owner.KeyAuths.Add(new KeyValuePair <PublicKeyType, ushort>(new PublicKeyType(subPublicKey), 1)); subWif = Base58.GetSubWif(name, privateWif, "active"); Console.WriteLine(subWif); pk = Base58.DecodePrivateWif(subWif); subPublicKey = Secp256K1Manager.GetPublicKey(pk, true); Console.WriteLine(Base58.EncodePublicWif(subPublicKey, Config.KeyPrefix)); op.Active.KeyAuths.Add(new KeyValuePair <PublicKeyType, ushort>(new PublicKeyType(subPublicKey), 1)); subWif = Base58.GetSubWif(name, privateWif, "posting"); Console.WriteLine(subWif); pk = Base58.DecodePrivateWif(subWif); subPublicKey = Secp256K1Manager.GetPublicKey(pk, true); Console.WriteLine(Base58.EncodePublicWif(subPublicKey, Config.KeyPrefix)); op.Posting.KeyAuths.Add(new KeyValuePair <PublicKeyType, ushort>(new PublicKeyType(subPublicKey), 1)); subWif = Base58.GetSubWif(name, privateWif, "memo"); Console.WriteLine(subWif); pk = Base58.DecodePrivateWif(subWif); subPublicKey = Secp256K1Manager.GetPublicKey(pk, true); Console.WriteLine(Base58.EncodePublicWif(subPublicKey, Config.KeyPrefix)); op.MemoKey = new PublicKeyType(subPublicKey); await Post(User.ActiveKeys, false, op).ConfigureAwait(false); }
public void GenerateKeys() { var pk = Secp256K1Manager.GenerateRandomKey(); var privateWif = Base58.EncodePrivateWif(pk); Console.WriteLine($"private owner: {privateWif}"); var pubKey = Secp256K1Manager.GetPublicKey(pk, true); var publicWif = Base58.EncodePublicWif(pubKey, "EOS"); Console.WriteLine($"public owner: {publicWif}"); pk = Secp256K1Manager.GenerateRandomKey(); privateWif = Base58.EncodePrivateWif(pk); Console.WriteLine($"private active: {privateWif}"); pubKey = Secp256K1Manager.GetPublicKey(pk, true); publicWif = Base58.EncodePublicWif(pubKey, "EOS"); Console.WriteLine($"public active: {publicWif}"); }
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 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()); }
public async Task AccountCreateTest() { string name = User.Login + DateTime.Now.Ticks; 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); await Post(User.ActiveKeys, false, op).ConfigureAwait(false); }
private static string GetRandomPassBase() { ++PassPhraseGen.counter; if (PassPhraseGen.counter % 5000 == 0) { int num1 = Hex.ToString(Sha256Manager.GetHash(Secp256K1Manager.GenerateRandomKey())).GetHashCode() + DateTime.UtcNow.Millisecond; DateTime utcNow = DateTime.UtcNow; int second = utcNow.Second; int num2 = num1 + second; utcNow = DateTime.UtcNow; int minute = utcNow.Minute; PassPhraseGen.rand = new Random(num2 + minute); } if (PassPhraseGen.rand.Next(1000) > 970 && PassPhraseGen.config != PassPhraseGen.Config.AllBots) { return(PassPhraseGen.GetUnderscoringPass()); } if (PassPhraseGen.rand.Next(1000) > 990 && PassPhraseGen.config != PassPhraseGen.Config.AllBots) { return(PassPhraseGen.GetRepetitionPass()); } if (PassPhraseGen.rand.Next(1000) > 975 && PassPhraseGen.config != PassPhraseGen.Config.AllBots) { return(PassPhraseGen.GetPhonePass()); } if (PassPhraseGen.rand.Next(1000) > 985 && PassPhraseGen.config != PassPhraseGen.Config.AllBots) { return(PassPhraseGen.GetAlphaNumPass()); } if (PassPhraseGen.rand.Next(1000) > 950 && PassPhraseGen.config != PassPhraseGen.Config.AllBots) { if (PassPhraseGen.rand.Next(1000) > 900) { return(new string("!@#$%^&*()_=+-[]\\/{},.<>~`'\"abcdefghijlkmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"[PassPhraseGen.rand.Next("!@#$%^&*()_=+-[]\\/{},.<>~`'\"abcdefghijlkmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890".Length)], PassPhraseGen.rand.Next(1, 100))); } return(string.Concat((object)PassPhraseGen.rand.Next(20183131))); } if (PassPhraseGen.rand.Next(1000) > 980 && PassPhraseGen.config != PassPhraseGen.Config.AllBots) { return(PassPhraseGen.GetProlongationPass()); } if ((PassPhraseGen.config == PassPhraseGen.Config.Classic || PassPhraseGen.config == PassPhraseGen.Config.AllBots) && PassPhraseGen.rand.Next(1000) > 980) { return(PassPhraseGen.GetCulturePass()); } if ((PassPhraseGen.config == PassPhraseGen.Config.Classic || PassPhraseGen.config == PassPhraseGen.Config.UniquePhrases) && PassPhraseGen.rand.Next(1000) > 800) { return(PassPhraseGen.GetNamePass()); } int length = PassPhraseGen.rand.Next(1, 60) / 5; if (length == 0) { length = 1; } if (PassPhraseGen.rand.Next(100) > 20 && length > 2) { length = 2; } if (PassPhraseGen.rand.Next(100) > 20 && length > 3) { length = 3; } if (PassPhraseGen.rand.Next(100) > 80) { length = 1; } if (PassPhraseGen.config != PassPhraseGen.Config.AllBots && PassPhraseGen.config != PassPhraseGen.Config.Classic && length < 2) { length = 2; } if (PassPhraseGen.config == PassPhraseGen.Config.AllBots) { length = 1; } bool flag1 = PassPhraseGen.rand.Next(100) > 50; bool flag2 = PassPhraseGen.rand.Next(100) > 95; bool flag3 = PassPhraseGen.rand.Next(100) > 75; bool flag4 = PassPhraseGen.rand.Next(100) > 90; bool flag5 = PassPhraseGen.rand.Next(100) > 50; bool flag6 = PassPhraseGen.rand.Next(100) > 50; bool flag7 = PassPhraseGen.rand.Next(100) > 95; string[] strArray = new string[length]; for (int index = 0; index < length; ++index) { strArray[index] = PassPhraseGen.rand.Next(100) <= 85 || PassPhraseGen.config == PassPhraseGen.Config.AllBots ? PassPhraseGen.words[PassPhraseGen.rand.Next(PassPhraseGen.words.Length)] : PassPhraseGen.GetGibberishPass(); if (flag7) { strArray[index] = strArray[index].ToLower(); strArray[index] = strArray[index][0].ToString().ToUpper() + strArray[index].Substring(1); } } string s = string.Join(flag1 ? "" : " ", strArray); if (!flag7 & flag3) { s = s.ToLower(); } if (!flag7 & flag2) { s = s.ToUpper(); } if ((uint)PassPhraseGen.config > 0U & flag4) { s = !flag5 ? (!flag6 ? Hex.ToString(Encoding.UTF8.GetBytes(s)) : Hex.ToString(Sha256Manager.GetHash(Sha256Manager.GetHash(Encoding.UTF8.GetBytes(s))))) : Hex.ToString(Sha256Manager.GetHash(Encoding.UTF8.GetBytes(s))); } else if (length <= 2 && PassPhraseGen.config != PassPhraseGen.Config.AllBots && PassPhraseGen.rand.Next(100) > 95) { int num = PassPhraseGen.rand.Next(PassPhraseGen.rand.Next(100) > 75 ? 2020 : 99); s += (string)(object)num; } return(s); }