Пример #1
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));
        }
Пример #2
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));
        }
Пример #3
0
        public EthECDSASignature Sign(string contract, string makerAddress, string makerToken,
                                      string takerToken, string makerAmount, string takerAmount, string expires, string nonce)
        {
            var plainData = new Object[]
            {
                contract,
                takerToken,
                BigInteger.Parse(takerAmount),
                makerToken,
                BigInteger.Parse(makerAmount),
                BigInteger.Parse(expires),
                BigInteger.Parse(nonce)
            };

            var prms = new[] {
                new Parameter("address", 1),
                new Parameter("address", 1),
                new Parameter("uint256", 1),
                new Parameter("address", 1),
                new Parameter("uint256", 1),
                new Parameter("uint256", 1),
                new Parameter("uint256", 1)
            };


            var data = SolidityPack(plainData, prms);



            var keystoreCrypto = new KeyStoreCrypto();
            //for etherDelta its SHA256, for IDEX just change with SHA3
            var hashed = keystoreCrypto.CalculateSha256Hash(data);


            var signer  = new EthereumMessageSigner();
            var newHash = signer.HashPrefixedMessage(hashed);


            var signatureRaw = signer.SignAndCalculateV(newHash, _userDataProvider.PrivateKey);
            var signature    = EthECDSASignature.CreateStringSignature(signatureRaw);
            var probe        = signer.EcRecover(hashed, signature);


            var ethEcdsa = MessageSigner.ExtractEcdsaSignature(signature);

            if (probe == makerAddress)
            {
                return(ethEcdsa);
            }
            //depending on usage, but it would be better to throw exc here if fails
            return(null);

            //throw new Exception("Signing failed");
        }
Пример #4
0
        /*
         * deposit
         * van
         * crouch
         * super
         * viable
         * electric
         * bamboo
         * nephew
         * hold
         * whip
         * nation
         * ankle
         * wonder
         * bottom
         * win
         * bomb
         * dog
         * search
         * globe
         * shrug
         * primary
         * spy
         * limb
         * knock
         */
        public static async Task TestMessageSigning()
        {
            using (var trezorManager = await TrezorFactory.GetWindowsConnectedLedgerManagerAsync(GetPin))
            {
                await trezorManager.InitializeAsync();

                var signer  = new TrezorExternalSigner(trezorManager, 0);
                var address = await signer.GetAddressAsync();

                var messageSignature = await signer.SignAsync(Encoding.UTF8.GetBytes("Hello"));

                var nethereumMessageSigner    = new Nethereum.Signer.EthereumMessageSigner();
                var nethereumMessageSignature = nethereumMessageSigner.EncodeUTF8AndSign("Hello", new EthECKey(
                                                                                             "0x2e14c29aaecd1b7c681154d41f50c4bb8b6e4299a431960ed9e860e39cae6d29"));
                System.Console.WriteLine("Trezor: " + EthECDSASignature.CreateStringSignature(messageSignature));
                System.Console.WriteLine("Nethereum: " + nethereumMessageSignature);
            }
        }
Пример #5
0
        public static string GetSignature(byte[] output)
        {
            using (var memory = new MemoryStream(output))
            {
                using (var reader = new BinaryReader(memory))
                {
                    byte[] sigV = reader.ReadBytes(1);
                    byte[] sigR = reader.ReadBytes(32);
                    byte[] sigS = reader.ReadBytes(32);

                    var signature = new EthECDSASignature(
                        new Org.BouncyCastle.Math.BigInteger(sigR),
                        new Org.BouncyCastle.Math.BigInteger(sigS),
                        sigV);
                    return(EthECDSASignature.CreateStringSignature(signature));
                }
            }
        }