public static string DoSignVTHOTx(List <string[]> transactions, string privateKey, bool isSend, int?gasLimit) { byte chainTag = 0; byte[] blockRef = null; var clauses = new List <ToClause>(); foreach (var transaction in transactions) { var amount = Amount.VTHO(); amount.SetDecimalAmount(transaction[1]); clauses.Add( ERC20Contract.BuildTranferToClause(ERC20Token.VTHO, Address.FromHexString(transaction[0]), amount)); chainTag = ByteUtils.ToByteArray(transaction[2])[0]; blockRef = transaction[3] == null?BlockchainClient.GetBlockRef(null).ToByteArray() : ByteUtils.ToByteArray(transaction[3]); } int gas = clauses.Count * gasLimit ?? 80000; var rawTransaction = RawTransactionFactory.Instance.CreateRawTransaction(chainTag, blockRef, 720, gas, (byte)0x0, CryptoUtils.GenerateTxNonce(), clauses.ToArray()); if (isSend) { var result = TransactionClient.SignThenTransfer(rawTransaction, ECKeyPair.Create(privateKey)); return(JsonConvert.SerializeObject(result)); } else { var result = TransactionClient.Sign(rawTransaction, ECKeyPair.Create(privateKey)); return(ByteUtils.ToHexString(result.Encode(), Prefix.ZeroLowerX)); } }
public void DomainNamePropertyTest() { ECKeyPair pair; pair = ECKeyPair.Create(ECDomainNames.secp192r1); Assert.AreEqual(ECDomainNames.secp192r1, pair.DomainName); }
public static ECKeyPair Decrypt(String password, WalletFile walletFile) { Validate(walletFile); var crypto = walletFile.Crypto; var mac = ByteUtils.ToByteArray(crypto.Mac); var iv = ByteUtils.ToByteArray(crypto.Cipherparams.IV); var cipherText = ByteUtils.ToByteArray(crypto.Ciphertext); byte[] derivedKey; var kdfParams = crypto.Kdfparams; if (kdfParams is ScryptKdfParams) { var scryptKdfParams = (ScryptKdfParams)crypto.Kdfparams; int dklen = scryptKdfParams.Dklen; int n = scryptKdfParams.N; int p = scryptKdfParams.P; int r = scryptKdfParams.R; var salt = ByteUtils.ToByteArray(scryptKdfParams.Salt); derivedKey = GenerateDerivedScryptKey(Encoding.UTF8.GetBytes(password), salt, n, r, p, dklen); } else if (kdfParams is Aes128CtrKdfParams) { var aes128CtrKdfParams = (Aes128CtrKdfParams)crypto.Kdfparams; int c = aes128CtrKdfParams.C; String prf = aes128CtrKdfParams.Prf; var salt = ByteUtils.ToByteArray(aes128CtrKdfParams.Salt); derivedKey = GenerateAes128CtrDerivedKey(Encoding.UTF8.GetBytes(password), salt, c, prf); } else { throw new CipherException("Unable to deserialize params: " + crypto.Kdf); } var derivedMac = GenerateMac(derivedKey, cipherText); if (!Arrays.Equals(derivedMac, mac)) { throw new CipherException("Invalid password provided"); } var encryptKey = Arrays.CopyOfRange(derivedKey, 0, 16); var privateKey = PerformCipherOperation(false, iv, encryptKey, cipherText); return(ECKeyPair.Create(privateKey)); }
/// <summary> /// Create wallet from password. /// </summary> /// <param name="passphases">password to encrypt the private key. </param> /// <returns> <seealso cref="WalletInfo"/> </returns> public static WalletInfo CreateWallet(string passphases) { if (string.IsNullOrWhiteSpace(passphases)) { return(null); } ECKeyPair keyPair = ECKeyPair.Create(); WalletFile walletFile = null; try { walletFile = Wallet.CreateStandard(passphases, keyPair); } catch (CipherException e) { System.Console.WriteLine(e.StackTrace); System.Console.WriteLine(Environment.NewLine); } return(new WalletInfo(walletFile, keyPair)); }
public static string BuildSignature(byte[] cert, byte[] txRawHash, string privateKey) { var txRawBytes = CryptoUtils.Blake2b(txRawHash); var cerHexBytes = CryptoUtils.Sha256(cert); var message = new byte[txRawBytes.Length + cerHexBytes.Length]; Array.Copy(cerHexBytes, 0, message, 0, cerHexBytes.Length); Array.Copy(txRawBytes, 0, message, cerHexBytes.Length, txRawBytes.Length); var key = ECKeyPair.Create(ByteUtils.ToByteArray(privateKey)); var signature = ECDSASign.SignMessage(CryptoUtils.Sha256(message), key, false); var signBytes = signature.ToByteArray(); _logger.Info("signature: {} {}", ByteUtils.ToHexString(signBytes, null), ByteUtils.CleanHexPrefix(ByteUtils.ToHexString(signature.R, Prefix.ZeroLowerX)) + ByteUtils.CleanHexPrefix(ByteUtils.ToHexString(signature.S, Prefix.ZeroLowerX)) + "0" + signature.V); return(ByteUtils.ToHexString(signBytes, null)); }
public void RlpEncodeTransaction() { // based on https://github.com/vechain/thor/blob/d9f618b4974733e04949f7b9424001f5bd572baa/tx/transaction_test.go#L20 string to = "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed"; var txn = new Transaction( (Network)1, 0x00000000aabbccdd, 32, new[] { new VetClause(to, 10000m, "0x000000606060"), new VetClause(to, 20000m, "0x000000606060") }, 12345678, 128, 21000, null ); var rlpSigData = txn.RlpDataForSignature; var expectedSigRlpHex = "0xf8540184aabbccdd20f840df947567d83b7b8d80addcb281a71d54fc7b3364ffed82271086000000606060df947567d83b7b8d80addcb281a71d54fc7b3364ffed824e208600000060606081808252088083bc614ec0"; var actualSigRlpHex = rlpSigData.ToHex(true); Assert.Equal(expectedSigRlpHex, actualSigRlpHex); var expectedRlpHex = "0xf8550184aabbccdd20f840df947567d83b7b8d80addcb281a71d54fc7b3364ffed82271086000000606060df947567d83b7b8d80addcb281a71d54fc7b3364ffed824e208600000060606081808252088083bc614ec080"; var actualRlpHex = txn.RLPData.ToHex(true); Assert.Equal(expectedRlpHex, actualRlpHex); var signingHashHex = Hash.Blake2B(rlpSigData).ToHex(true); Assert.Equal("0x2a1c25ce0d66f45276a5f308b99bf410e2fc7d5b6ea37a49f2ab9f1da9446478", signingHashHex); var ec = ECKeyPair.Create("0x7582be841ca040aa940fff6c05773129e135623e41acce3e0b8ba520dc1ae26a"); txn.Sign(ec); var expectedSignatureHex = "0xf76f3c91a834165872aa9464fc55b03a13f46ea8d3b858e528fcceaf371ad6884193c3f313ff8effbb57fe4d1adc13dceb933bedbf9dbb528d2936203d5511df00"; Assert.Equal(expectedSignatureHex, txn.signature); // signer: 0xd989829d88b0ed1b06edf5c50174ecfa64f14a64 // id: 0xda90eaea52980bc4bb8d40cb2ff84d78433b3b4a6e7d50b75736c5e3e77b71ec var expectedSignedRlpHex = "0xf8970184aabbccdd20f840df947567d83b7b8d80addcb281a71d54fc7b3364ffed82271086000000606060df947567d83b7b8d80addcb281a71d54fc7b3364ffed824e208600000060606081808252088083bc614ec0b841f76f3c91a834165872aa9464fc55b03a13f46ea8d3b858e528fcceaf371ad6884193c3f313ff8effbb57fe4d1adc13dceb933bedbf9dbb528d2936203d5511df00"; var actualSignedRlpHex = txn.RLPData.ToHex(true); Assert.Equal(expectedSignedRlpHex, actualSignedRlpHex); var jsonBytes = JsonSerializer.Serialize(txn, VeChainClient.JsonFormatterResolver); var json = JsonSerializer.PrettyPrint(jsonBytes); Assert.Equal(@"{ ""chainTag"": 1, ""blockRef"": ""0x00000000aabbccdd"", ""expiration"": 32, ""clauses"": [ { ""to"": ""0x7567d83b7b8d80addcb281a71d54fc7b3364ffed"", ""value"": ""0x2710"", ""data"": ""0x000000606060"" }, { ""to"": ""0x7567d83b7b8d80addcb281a71d54fc7b3364ffed"", ""value"": ""0x4e20"", ""data"": ""0x000000606060"" } ], ""gasPriceCoef"": 128, ""gas"": 21000, ""nonce"": ""0xbc614e"", ""signature"": ""0xf76f3c91a834165872aa9464fc55b03a13f46ea8d3b858e528fcceaf371ad6884193c3f313ff8effbb57fe4d1adc13dceb933bedbf9dbb528d2936203d5511df00"" }", json); }