public virtual string HashAndSign(byte[] plainMessage, CfxECKey key)
        {
            var hash      = Hash(plainMessage);
            var signature = key.SignAndCalculateV(hash);

            return(CreateStringSignature(signature));
        }
Esempio n. 2
0
        public byte[] CalculateCommonSecret(CfxECKey publicKey)
        {
            var agreement = new ECDHBasicAgreement();

            agreement.Init(_ecKey.PrivateKey);
            var z = agreement.CalculateAgreement(publicKey._ecKey.GetPublicKeyParameters());

            return(BigIntegers.AsUnsignedByteArray(agreement.GetFieldSize(), z));
        }
Esempio n. 3
0
        public async Task <EthECDSASignature> SignAsync(byte[] rawBytes)
        {
            var signature = await SignExternallyAsync(rawBytes);

            if (CalculatesV)
            {
                return(new EthECDSASignature(signature));
            }

            var publicKey = await GetPublicKeyAsync();

            var recId = CfxECKey.CalculateRecId(signature, rawBytes, publicKey);

            signature.V = new[] { (byte)(recId + 27) };
            return(new EthECDSASignature(signature));
        }
Esempio n. 4
0
        public async Task <EthECDSASignature> SignAsync(byte[] rawBytes, BigInteger chainId)
        {
            var signature = await SignExternallyAsync(rawBytes);

            if (CalculatesV)
            {
                return(new EthECDSASignature(signature));
            }

            var publicKey = await GetPublicKeyAsync();

            var recId  = CfxECKey.CalculateRecId(signature, rawBytes, publicKey);
            var vChain = CfxECKey.CalculateV(chainId, recId);

            signature.V = vChain.ToBytesForRLPEncoding();
            return(new EthECDSASignature(signature));
        }
Esempio n. 5
0
        public static SignedTransactionBase CreateTransaction(string to, BigInteger gas, BigInteger gasPrice, BigInteger amount, string data, BigInteger nonce, string r, string s, string v)
        {
            var rBytes = r.HexToByteArray();
            var sBytes = s.HexToByteArray();
            var vBytes = v.HexToByteArray();

            var signature = EthECDSASignatureFactory.FromComponents(rBytes, sBytes, vBytes);

            if (signature.IsVSignedForChain())
            {
                var vBigInteger = vBytes.ToBigIntegerFromRLPDecoded();
                var chainId     = CfxECKey.GetChainFromVChain(vBigInteger);
                return(new TransactionChainId(nonce.ToBytesForRLPEncoding(), gasPrice.ToBytesForRLPEncoding(), gas.ToBytesForRLPEncoding(),
                                              to.HexToByteArray(), amount.ToBytesForRLPEncoding(), data.HexToByteArray(), chainId.ToBytesForRLPEncoding(), rBytes, sBytes, vBytes));
            }
            else
            {
                return(new Transaction(nonce.ToBytesForRLPEncoding(), gasPrice.ToBytesForRLPEncoding(), gas.ToBytesForRLPEncoding(),
                                       to.HexToByteArray(), amount.ToBytesForRLPEncoding(), data.HexToByteArray(), rBytes, sBytes, vBytes[0]));
            }
        }
Esempio n. 6
0
        public string sign(byte[] privateKey, dynamic chainId)
        {
            this.storageLimit = BitConverter.GetBytes(100000).Clear();

            this.chianId = BitConverter.GetBytes((int)chainId).Clear();
            //this.Gas = BitConverter.GetBytes(2000000).Clear();
            List <byte[]> raw = new List <byte[]> {
                this._nonce, this.GasPrice, this.Gas, this.to,
                this.Value, this.storageLimit, this._epochHeight, this.chianId, this.Data
            };

            var x1 = rlpEncode(raw).ToHex();
            var x2 = sha3Keccack.CalculateHash(rlpEncode(raw));//sha3
            var k  = new CfxECKey("0x" + privateKey.ToHex());
            var x3 = k.SignAndCalculateV(x2);
            //x3.V = new byte[] { 0x01 };

            List <object> rawWithRSV = new List <object> {
                raw, x3.V, x3.R, x3.S
            };
            var x4 = rlpEncode(rawWithRSV).ToHex();

            return("0x" + x4);
        }
Esempio n. 7
0
 public void Sign(CfxECKey key, BigInteger chainId)
 {
     Signature        = key.SignAndCalculateV(RawHash, chainId);
     rlpSignedEncoded = null;
 }
Esempio n. 8
0
 public void Sign(CfxECKey key)
 {
     Signature        = key.SignAndCalculateV(RawHash);
     rlpSignedEncoded = null;
 }
        public virtual string EcRecover(byte[] hashMessage, string signature)
        {
            var ecdaSignature = ExtractEcdsaSignature(signature);

            return(CfxECKey.RecoverFromSignature(ecdaSignature, hashMessage).GetPublicAddress());
        }
        public virtual string Sign(byte[] message, CfxECKey key)
        {
            var signature = key.SignAndCalculateV(message);

            return(CreateStringSignature(signature));
        }
 public override string Sign(byte[] message, CfxECKey key)
 {
     return(base.Sign(HashPrefixedMessage(message), key));
 }
Esempio n. 12
0
 public virtual void Sign(CfxECKey key)
 {
     SimpleRlpSigner.Sign(key);
 }
 public string EncodeUTF8AndSign(string message, CfxECKey key)
 {
     return(base.Sign(HashPrefixedMessage(Encoding.UTF8.GetBytes(message)), key));
 }
 public override string HashAndSign(byte[] plainMessage, CfxECKey key)
 {
     return(base.Sign(HashAndHashPrefixedMessage(plainMessage), key));
 }
 public override void Sign(CfxECKey key)
 {
     SimpleRlpSigner.Sign(key, GetChainIdAsBigInteger());
 }
Esempio n. 16
0
        public static string GetPublicAddress(string privateKey)
        {
            var key = new CfxECKey(privateKey.HexToByteArray(), true);

            return(key.GetPublicAddress());
        }
 private BigInteger GetChainFromVChain()
 {
     return(CfxECKey.GetChainFromVChain(Signature.V.ToBigIntegerFromRLPDecoded()));
 }