Esempio n. 1
0
        public void ShouldRecoverUsingShortcutHashes()
        {
            var signature = "0x0976a177078198a261faf206287b8bb93ebb233347ab09a57c8691733f5772f67f398084b30fc6379ffee2cc72d510fd0f8a7ac2ee0162b95dc5d61146b40ffa1c";
            var text      = "test";
            var signer    = new EthereumMessageSigner();
            var account   = signer.HashAndEcRecover(text, signature);

            Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c", account.EnsureHexPrefix().ToLower());

            signature = signer.HashAndSign(text,
                                           "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7");

            account = signer.HashAndEcRecover(text, signature);

            Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c".ToLower(), account.EnsureHexPrefix().ToLower());
        }
Esempio n. 2
0
        public async void SigningTestSimple()
        {
            // The message itself, this is what is being signed
            var msg = "test message 1234567890";
            //var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var web3 = new Web3(new Account(_privateKey), _blockchainUrl);

            // SIGN. Doesnt need web3, done offline.
            // In:  Message, Signer Private Key
            // Out: Signature (65 bytes)
            Log("--- SIGN with Nethereum ---");
            var signer    = new EthereumMessageSigner();
            var signature = signer.HashAndSign(msg, _privateKey);

            Log($"signature: {signature} is for message: {msg}");
            Log("");


            // RECOVER with Nethereum
            // In: Message, Signature
            // Out: Signer Address
            Log("--- RECOVER with Nethereum ---");
            var signerAddressRecoveredNethereum = signer.HashAndEcRecover(msg, signature);

            Log($"Actual Signer address using C# HashAndEcRecover: {signerAddressRecoveredNethereum}");
            Log($"Expected Signer address: {web3.TransactionManager.Account.Address}");  // since web3 created using same private key as was used to sign
            Log("");


            // RECOVER with Solidity
            // In: Message, Signature
            // Out: Signer Address
            Log("--- RECOVER with Solidity ---");
            Log($"Deploying contract SignatureChecker...");
            var signatureCheckerDeployment = new SignatureCheckerDeployment();
            var signatureCheckerService    = await SignatureCheckerService.DeployContractAndGetServiceAsync(web3, signatureCheckerDeployment);

            Log($"SignatureChecker contract address is: {signatureCheckerService.ContractHandler.ContractAddress}");
            var bytesForSignature = signature.HexToByteArray();
            var signerAddressRecoveredSolidity = await signatureCheckerService.GetSignerAddressFromMessageAndSignatureQueryAsync(msg, bytesForSignature);

            Log($"Actual Signer Address Recovered using Solidity: {signerAddressRecoveredSolidity}");

            signerAddressRecoveredSolidity.Should().Be(signerAddressRecoveredNethereum.ToLowerInvariant());

            // See Solidity code Channels\Channels.Contracts\Contracts\SignatureChecker.sol:
            //   // this recreates the message that was signed on the client
            //   bytes32 messageAsClient = prefixed(keccak256(abi.encodePacked(message)));
        }
        /*
         * When receiving a signature that has been made with a hashed
         * file it’s necessary to start by hashing the file we want to verify and then recover the address that signed it.
         */
        public void HashMsgRecover(EthereumMessageSigner LSigner)
        {
            if (LSigner != null)
            {
                //that code, will be refactor..  mmm
                string FprivateKey = FCryptoCurrencyAdvocacy.GetPrivateKey(1);
                var    LSignature  = LSigner.HashAndSign(Fmsg, FprivateKey);

                //recover message
                var addressRec2 = LSigner.HashAndEcRecover(Fmsg, LSignature);
            }
            else
            {
                throw new ArgumentNullException("Lsigner");
            }
        }
        public static string DetermineStatusByValidatingSignature(string psEOA, string psMsg, string psSignature, string psDummyValue)
        {
            string sStatusCd = BlazorAppWonkaExtensions.CONST_STATUS_CD_INACTIVE;

            if (!String.IsNullOrEmpty(psEOA) && !String.IsNullOrEmpty(psMsg) && !String.IsNullOrEmpty(psSignature))
            {
                var signer = new EthereumMessageSigner();

                var addressRecovered = signer.HashAndEcRecover(psMsg, psSignature);

                if (addressRecovered == psEOA)
                {
                    sStatusCd = BlazorAppWonkaExtensions.CONST_STATUS_CD_ACTIVE;
                }
            }

            return(sStatusCd);
        }
Esempio n. 5
0
 public static bool Verify(User user)
 {
     return(MessageSigner.HashAndEcRecover(user.Proof.Nonce, user.Proof.Signature).Equals(user.Proof.Address, StringComparison.OrdinalIgnoreCase));
 }