Exemplo n.º 1
0
        /// <summary>
        /// Signs using a predefined typed data schema and converts and encodes the provide the message value
        /// </summary>
        public string SignTypedDataV4 <T, TDomain>(T message, TypedData <TDomain> typedData, EthECKey key) where TDomain : IDomain
        {
            var encodedData = EncodeTypedData(message, typedData);
            var signature   = key.SignAndCalculateV(Sha3Keccack.Current.CalculateHash(encodedData));

            return(EthECDSASignature.CreateStringSignature(signature));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encodes data according to EIP-712, hashes it and signs with <paramref name="key"/>.
        /// Matches the signature produced by eth_signTypedData_v4
        /// </summary>
        public string SignTypedDataV4(TypedData typedData, EthECKey key)
        {
            var encodedData = EncodeTypedData(typedData);
            var signature   = key.SignAndCalculateV(Sha3Keccack.Current.CalculateHash(encodedData));

            return(EthECDSASignature.CreateStringSignature(signature));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Encodes data according to EIP-712.
        /// </summary>
        public byte[] EncodeTypedData(TypedData typedData)
        {
            using (var memoryStream = new MemoryStream())
                using (var writer = new BinaryWriter(memoryStream))
                {
                    writer.Write("1901".HexToByteArray());
                    writer.Write(HashStruct(typedData.Types, "EIP712Domain", ParseDomain(typedData.Domain).Select(x => x.MemberValue)));
                    writer.Write(HashStruct(typedData.Types, typedData.PrimaryType, typedData.Message));

                    writer.Flush();
                    var result = memoryStream.ToArray();
                    return(result);
                }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Encodes data according to EIP-712.
        /// </summary>
        public byte[] EncodeTypedData <TDomain>(TypedData <TDomain> typedData) where TDomain : IDomain
        {
            using (var memoryStream = new MemoryStream())
                using (var writer = new BinaryWriter(memoryStream))
                {
                    writer.Write("1901".HexToByteArray());
                    writer.Write(HashStruct(typedData.Types, "EIP712Domain", MemberValueFactory.CreateFromMessage(typedData.Domain)));
                    writer.Write(HashStruct(typedData.Types, typedData.PrimaryType, typedData.Message));

                    writer.Flush();
                    var result = memoryStream.ToArray();
                    return(result);
                }
        }
Exemplo n.º 5
0
        private TypedData GenerateTypedData <T>(T data, Domain domain, string primaryTypeName)
        {
            var parameters = _parametersEncoder.GetParameterAttributeValues(typeof(T), data).OrderBy(x => x.ParameterAttribute.Order);

            var typeMembers = new List <MemberDescription>();
            var typeValues  = new List <MemberValue>();

            foreach (var parameterAttributeValue in parameters)
            {
                typeMembers.Add(new MemberDescription
                {
                    Type = parameterAttributeValue.ParameterAttribute.Type,
                    Name = parameterAttributeValue.ParameterAttribute.Name
                });

                typeValues.Add(new MemberValue
                {
                    TypeName = parameterAttributeValue.ParameterAttribute.Type,
                    Value    = parameterAttributeValue.Value
                });
            }

            var result = new TypedData
            {
                PrimaryType = primaryTypeName,
                Types       = new Dictionary <string, MemberDescription[]>
                {
                    [primaryTypeName] = typeMembers.ToArray(),
                    ["EIP712Domain"]  = ParseDomain(domain).Select(x => x.MemberDescription).ToArray()
                },
                Message = typeValues.ToArray(),
                Domain  = domain
            };

            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Encodes data according to EIP-712, hashes it and signs with <paramref name="key"/>.
        /// </summary>
        public string SignTypedData(TypedData typedData, EthECKey key)
        {
            var encodedData = EncodeTypedData(typedData);

            return(_signer.HashAndSign(encodedData, key));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Encodes data according to EIP-712, hashes it and signs with <paramref name="key"/>.
        /// </summary>
        public string SignTypedData <TDomain>(TypedData <TDomain> typedData, EthECKey key) where TDomain : IDomain
        {
            var encodedData = EncodeTypedData(typedData);

            return(_signer.HashAndSign(encodedData, key));
        }
Exemplo n.º 8
0
        public byte[] EncodeAndHashTypedData <T, TDomain>(T message, TypedData <TDomain> typedData) where TDomain : IDomain
        {
            var encodedData = EncodeTypedData(message, typedData);

            return(Sha3Keccack.Current.CalculateHash(encodedData));
        }
Exemplo n.º 9
0
 /// <summary>
 /// Encodes data according to EIP-712, it uses a predefined typed data schema and converts and encodes the provide the message value
 /// </summary>
 public byte[] EncodeTypedData <T, TDomain>(T message, TypedData <TDomain> typedData) where TDomain : IDomain
 {
     typedData.Message = MemberValueFactory.CreateFromMessage(message);
     return(EncodeTypedData(typedData));
 }
Exemplo n.º 10
0
        public string RecoverFromSignatureV4 <TDomain>(TypedData <TDomain> typedData, string signature) where TDomain : IDomain
        {
            var encodedData = EncodeTypedData(typedData);

            return(new MessageSigner().EcRecover(Sha3Keccack.Current.CalculateHash(encodedData), signature));
        }