public EcdsaKeyPair GetRandomKey() { var crypto = CryptoProvider.GetCrypto(); var privateKey = crypto.GeneratePrivateKey().ToPrivateKey(); return(new EcdsaKeyPair(privateKey)); }
public void Test_External_Signature2() { var crypto = CryptoProvider.GetCrypto(); var rawTx = "0xf86d808504a817c800832dc6c0948e7b7262e0fa4616566591d51f998f16a79fb547880de0b6b3a76400008025a0115105d96a43f41a5ea562bb3e591cbfa431a8cdae9c3030457adca2cb854f78a012fb41922c53c73473563003667ed8e783359c91d95b42301e1955d530b1ca33"; var ethTx = new TransactionChainId(rawTx.HexToBytes()); Console.WriteLine("ETH RLP: " + ethTx.GetRLPEncodedRaw().ToHex()); var nonce = ethTx.Nonce.ToHex(); Console.WriteLine("Nonce " + nonce); Console.WriteLine("ChainId " + Convert.ToUInt64(ethTx.ChainId.ToHex(), 16)); var tx = new Transaction { To = ethTx.ReceiveAddress.ToUInt160(), Value = ethTx.Value.ToUInt256(true), Nonce = Convert.ToUInt64(ethTx.Nonce.ToHex(), 16), GasPrice = Convert.ToUInt64(ethTx.GasPrice.ToHex(), 16), GasLimit = Convert.ToUInt64(ethTx.GasLimit.ToHex(), 16) }; Console.WriteLine("RLP: " + tx.Rlp(true).ToHex()); var address = ethTx.Key.GetPublicAddress().HexToBytes(); var from = ethTx.Key.GetPublicAddress().HexToBytes().ToUInt160(); Console.WriteLine(address.ToHex()); var r = "0x115105d96a43f41a5ea562bb3e591cbfa431a8cdae9c3030457adca2cb854f78".HexToBytes(); var s = "0x12fb41922c53c73473563003667ed8e783359c91d95b42301e1955d530b1ca33".HexToBytes(); var v = "0x25".HexToBytes(); var signature = r.Concat(s).Concat(v).ToArray(); Console.WriteLine(signature.ToHex()); var message = "0xed808504a817c800832dc6c0948e7b7262e0fa4616566591d51f998f16a79fb547880de0b6b3a764000080018080" .HexToBytes().KeccakBytes(); var recoveredPubkey = crypto.RecoverSignatureHashed(message, signature, false); Console.WriteLine(recoveredPubkey.ToHex()); var addr = crypto.ComputeAddress(recoveredPubkey); var same = addr.SequenceEqual(from.ToBytes()); Console.WriteLine(addr.ToHex()); Console.WriteLine(same); }
public ShadowsocksClient(string method, string password) { var param = CryptoProvider.GetCrypto(method); if (param.IsAead) { shadow = new AeadClient(param, password); } else { shadow = new UnsafeClient(param, password); } }
private static void DecryptWallet(DecryptOptions options) { string path = options.WalletPath; path = Path.IsPathRooted(path) || path.StartsWith("~/") ? path : Path.Join(Path.GetDirectoryName(Path.GetFullPath(path)), path); var encryptedContent = File.ReadAllBytes(path); var key = Encoding.UTF8.GetBytes(options.WalletPassword).KeccakBytes(); var crypto = CryptoProvider.GetCrypto(); var decryptedContent = Encoding.UTF8.GetString(crypto.AesGcmDecrypt(key, encryptedContent)); System.Console.WriteLine(decryptedContent); }
private static void EncryptWallet(EncryptOptions options) { string path = options.WalletPath; path = Path.IsPathRooted(path) || path.StartsWith("~/") ? path : Path.Join(Path.GetDirectoryName(Path.GetFullPath(path)), path); var content = File.ReadAllBytes(path) ?? throw new Exception($"Cannot read file {path}"); var key = Encoding.UTF8.GetBytes(options.WalletPassword).KeccakBytes(); var crypto = CryptoProvider.GetCrypto(); var encryptedContent = crypto.AesGcmEncrypt(key, content); using var stream = System.Console.OpenStandardOutput(); stream.Write(encryptedContent, 0, encryptedContent.Length); }
private static void GenerateNewWallet(string path, string password) { var config = new JsonWallet( CryptoProvider.GetCrypto().GenerateRandomBytes(32).ToHex(false), GenerateHubKey().PrivateKey, new Dictionary <ulong, string> { }, new Dictionary <ulong, string> { } ); var json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(config)); var passwordHash = Encoding.UTF8.GetBytes(password).KeccakBytes(); var crypto = CryptoProvider.GetCrypto(); File.WriteAllBytes(path, crypto.AesGcmEncrypt(passwordHash, json)); }
private static void GenWallet(string path, string ecdsaKey, string hubKey, string tpkeKey, string tsKey, string password) { var config = new JsonWallet( ecdsaKey, hubKey, new Dictionary <ulong, string> { { 0, tpkeKey } }, new Dictionary <ulong, string> { { 0, tsKey } } ); var json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(config)); var passwordHash = Encoding.UTF8.GetBytes(password).KeccakBytes(); var crypto = CryptoProvider.GetCrypto(); File.WriteAllBytes(path, crypto.AesGcmEncrypt(passwordHash, json)); }
public static void CloudKeygen(int n, int f, IEnumerable <string> ips, ushort basePort, ushort target, ulong chainId, ulong cycleDuration, ulong validatorsCount, string networkName, string feedAddress, string feedBalance, string stakeAmount, IEnumerable <ulong> hardforks) { if (n <= 3 * f) { throw new Exception("N must be >= 3 * F + 1"); } var tpkeKeyGen = new Crypto.TPKE.TrustedKeyGen(n, f); var tpkePubKey = tpkeKeyGen.GetPubKey(); var sigKeyGen = new Crypto.ThresholdSignature.TrustedKeyGen(n, f); var privShares = sigKeyGen.GetPrivateShares().ToArray(); var pubShares = sigKeyGen.GetPrivateShares() .Select(s => s.GetPublicKeyShare()) .Select(s => s.ToHex()) .ToArray(); var ecdsaPrivateKeys = new string[n]; var ecdsaPublicKeys = new string[n]; var addresses = new string[n]; var crypto = CryptoProvider.GetCrypto(); for (var i = 0; i < n; ++i) { ecdsaPrivateKeys[i] = crypto.GenerateRandomBytes(32).ToHex(false); ecdsaPublicKeys[i] = crypto.ComputePublicKey(ecdsaPrivateKeys[i].HexToBytes(), true).ToHex(false); addresses[i] = ecdsaPrivateKeys[i].HexToBytes().ToPrivateKey().GetPublicKey().GetAddress().ToHex(); } var hubPublicKeys = new string[n]; var serializedHubPrivateKeys = new string[n]; for (var i = 0; i < n; ++i) { (serializedHubPrivateKeys[i], hubPublicKeys[i]) = PrivateWallet.GenerateHubKey(); } var bootstraps = ips .Zip(hubPublicKeys, (ip, id) => $"{id}@{ip}") .Select((x, i) => $"{x}:{41011 + i}") .ToArray(); var peers = ecdsaPublicKeys.ToArray(); for (var i = 0; i < n; ++i) { var net = new NetworkConfig { Peers = peers, MaxPeers = 100, ForceIPv6 = false, BootstrapAddresses = bootstraps, HubLogLevel = "Trace", HubMetricsPort = basePort + 2, NetworkName = networkName, ChainId = (int)chainId, CycleDuration = cycleDuration, ValidatorsCount = validatorsCount, }; var genesis = new GenesisConfig(tpkePubKey.ToHex(), "5.000000000000000000", "0.000000100000000000") { Balances = new Dictionary <string, string> { { feedAddress, feedBalance } }, Validators = Enumerable.Range(0, n).Select(j => new ValidatorInfo( ecdsaPublicKeys[j], pubShares[j], feedAddress, stakeAmount )).ToList() }; for (var j = 0; j < n; ++j) { genesis.Balances[addresses[j]] = "100"; } var secp256K1 = new Secp256k1(); var privateKey = new byte[32]; var rnd = System.Security.Cryptography.RandomNumberGenerator.Create(); do { rnd.GetBytes(privateKey); }while (!secp256K1.SecretKeyVerify(privateKey)); var privateKeyHex = privateKey.ToHex(); var publicKey = new byte[64]; Debug.Assert(secp256K1.PublicKeyCreate(publicKey, privateKey)); var publicKeyHex = publicKey.ToHex(); System.Console.WriteLine($"Loop {i + 1:D2}: private key [{privateKeyHex}] associated with public key [{publicKeyHex}]"); var rpc = new RpcConfig { Hosts = new[] { "+" }, Port = basePort, MetricsPort = (ushort)(basePort + 1), // ApiKey = "0x2e917846fe7487a4ea3a765473a3fc9b2d9227a4d312bc77fb9de357cf73d7e52b771d537394336e9eb2cb4838138f668f4bd7d8cf7e04d9242a42c71b99f166", ApiKey = publicKeyHex }; var walletPath = "wallet.json"; var vault = new VaultConfig { Path = walletPath, Password = getRandomPassword() }; var storage = new StorageConfig { Path = "ChainLachain", Provider = "RocksDB", }; var blockchain = new BlockchainConfig { TargetBlockTime = target, }; List <ulong> hardforkHeights = hardforks.ToList(); var hardfork = new HardforkConfig { Hardfork_1 = hardforkHeights[0], Hardfork_2 = hardforkHeights[1], Hardfork_3 = hardforkHeights[2], Hardfork_4 = hardforkHeights[3], }; var version = new VersionConfig { Version = 2 }; var blockHeight = new CacheOptions { SizeLimit = 100 }; var cache = new CacheConfig { BlockHeight = blockHeight }; var config = new Config(net, genesis, rpc, vault, storage, blockchain, hardfork, version, cache); File.WriteAllText($"config{i + 1:D2}.json", JsonConvert.SerializeObject(config, Formatting.Indented)); GenWallet( $"wallet{i + 1:D2}.json", ecdsaPrivateKeys[i], serializedHubPrivateKeys[i], tpkeKeyGen.GetPrivKey(i).ToHex(), privShares[i].ToHex(), vault.Password ); } var tpkePrivKeys = string.Join( ", ", Enumerable.Range(0, n) .Select(idx => tpkeKeyGen.GetPrivKey(idx)) .Select(x => $"\"{x.ToHex()}\"")); var tsKeys = string.Join( ", ", sigKeyGen.GetPrivateShares() .Select(x => $"(\"{x.GetPublicKeyShare().ToHex()}\", \"{x.ToHex()}\")") ); System.Console.WriteLine( $"{n}: " + "{" + " \"tpke\": (" + $" \"{tpkePubKey.ToHex()}\"," + $" [{tpkePrivKeys}]" + " )," + $" \"ts\": [{tsKeys}]," + "}"); System.Console.WriteLine( string.Join(", ", ecdsaPrivateKeys.Zip(ecdsaPublicKeys).Zip(addresses) .Select(t => $"(\"{t.First.Second}\", \"{t.First.First}\", \"{t.Second}\")")) ); }
private static string getRandomPassword() { var crypto = CryptoProvider.GetCrypto(); return(crypto.GenerateRandomBytes(20).ToHex(false)); }
public ShadowsocksClient(string method, byte[] key) { var param = CryptoProvider.GetCrypto(method); shadow = new AeadClient(param, key); }