コード例 #1
0
        public Signature Sign(Model model)
        {
            var signer = new EthereumMessageSigner();
            var hash   = SoliditySHA3(model);

            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.LogDebug("signing model {0} , {1} bytes", model, hash.ToHex().EnsureHexPrefix());
            }

            return(Signature.Of(signer.HashAndSign(hash, new EthECKey(privateKey))));
        }
コード例 #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)));
        }
コード例 #3
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());
        }
コード例 #4
0
        /*
         * 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");
            }
        }
コード例 #5
0
        /*hashing message and then signing it.
         * debo llamar esto desde el controlador, cierto luis?
         */
        public void HashAndSign()
        {
            if (Fmsg != null)
            {
                string FprivateKey = FCryptoCurrencyAdvocacy.GetPrivateKey(1);

                var LSigner    = new EthereumMessageSigner();
                var LSignature = LSigner.HashAndSign(Fmsg, FprivateKey);
                //example "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7");
                HashMsgRecover(LSigner);
            }
            else
            {
                throw new ArgumentNullException("Fmsg");
            }
        }
コード例 #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));
        }
コード例 #7
0
 public static string Sign(string message, string privateKey)
 {
     return(MessageSigner.HashAndSign(message, privateKey));
 }
コード例 #8
0
 public string HashAndSign(string privateKey, string message)
 {
     return(_signer.HashAndSign(message, privateKey));
 }
コード例 #9
0
ファイル: EthSignService.cs プロジェクト: SP8DE/protocol
        public string SignMessage(byte[] message, string privateKey)
        {
            var signer = new EthereumMessageSigner();

            return(signer.HashAndSign(message, new EthECKey(privateKey)));
        }
コード例 #10
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));
        }
コード例 #11
0
        public async void SigningTestPo()
        {
            var web3 = new Web3(new Account(_privateKey), _blockchainUrl);

            var po2 = new Po()
            {
                BuyerAddress       = "0x40eD4f49EC2C7BDCCe8631B1A7b54ed5d4Aa9610",
                PoNumber           = 1,
                ApproverAddress    = "0x40eD4f49EC2C7BDCCe8631B1A7b54ed5d4Aa9610",
                ReceiverAddress    = "0x40eD4f49EC2C7BDCCe8631B1A7b54ed5d4Aa9610",
                BuyerWalletAddress = "0x40eD4f49EC2C7BDCCe8631B1A7b54ed5d4Aa9610",
                CurrencyAddress    = "0x40eD4f49EC2C7BDCCe8631B1A7b54ed5d4Aa9610",
                CurrencySymbol     = "DAI",
                PoType             = PoType.Cash,
                SellerId           = "Nethereum.eShop",
                PoCreateDate       = 100,
                PoItemCount        = 2,
                PoItems            = new List <PoItem>()
                {
                    new PoItem()
                    {
                        PoNumber                 = 1,
                        PoItemNumber             = 10,
                        SoNumber                 = "so1",
                        SoItemNumber             = "100",
                        ProductId                = "gtin1111",
                        Quantity                 = 1,
                        Unit                     = "EA",
                        QuantitySymbol           = "NA",
                        QuantityAddress          = "0x40eD4f49EC2C7BDCCe8631B1A7b54ed5d4Aa9610",
                        CurrencyValue            = 11,
                        Status                   = PoItemStatus.Created,
                        GoodsIssuedDate          = 100,
                        GoodsReceivedDate        = 0,
                        PlannedEscrowReleaseDate = 100,
                        ActualEscrowReleaseDate  = 110,
                        IsEscrowReleased         = false,
                        CancelStatus             = PoItemCancelStatus.Initial
                    }
                }
            };
            var encoded     = new ABIEncode().GetABIEncoded(new ABIValue(new TupleType(), po2));
            var hashEncoded = new ABIEncode().GetSha3ABIEncoded(new ABIValue(new TupleType(), po2));
            var signed      = new EthereumMessageSigner().Sign(hashEncoded, _privateKey);
            var account2    = new Account(_privateKey);

            // Prepare new PO
            var po = CreatePo(1, "123", 1);

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

            Log($"signature: {signature} is for message: [byte array not shown here yet]");
            Log("");


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

            // See Solidity code Channels\Channels.Contracts\Contracts\SignatureChecker.sol:
            var bytesForSignature = signature.HexToByteArray();
            var signerAddressRecoveredSolidity = await signatureCheckerService.GetSignerAddressFromPoAndSignatureQueryAsync(
                po, bytesForSignature);

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

            signerAddressRecoveredSolidity.Should().Be(web3.TransactionManager.Account.Address);
        }