Exemplo n.º 1
0
        public void TestRecoverAddressTest()
        {
            var priKey     = "0xdce1443bd2ef0c2631adc1c67e5c93f13dc23a41c18b536effbbdcbcdb96fb65".ToBytes();
            var vechainKey = new SimpleWallet(priKey);

            var msgHash = Keccack256.CalculateHash("hello world");

            var signature = Secp256k1.Sign(msgHash, priKey);

            var recoverAddress = SimpleWallet.RecoverAddress(msgHash, signature);

            Assert.True(recoverAddress == "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed");
        }
Exemplo n.º 2
0
        public void TestSecp256k1()
        {
            var priKey = Secp256k1.GeneratePrivateKey();
            var pubKey = Secp256k1.DerivePublicKey(priKey);

            var msgHash = Keccack256.CalculateHash("hello world");

            var signature = Secp256k1.Sign(msgHash, priKey);

            var recoveredPubKey = Secp256k1.RecoverPublickey(Keccack256.CalculateHash("hello world"), signature);

            Assert.True(pubKey.SequenceEqual(recoveredPubKey));
        }
Exemplo n.º 3
0
        private byte[][] Encode(Dictionary <string, dynamic> args)
        {
            List <byte[]> topics = new List <byte[]>();

            if (!this._definition.Anonymous)
            {
                topics.Add(this._definition.Sha3Signature);
            }

            foreach (var input in this._definition.inputs)
            {
                if (!input.Indexed)
                {
                    continue;
                }
                if (args.ContainsKey(input.Name) && args[input.Name] == null)
                {
                    topics.Add(null);
                }
                else
                {
                    var arg = args[input.Name];
                    if (this.IsDynamicType(input.ABIType))
                    {
                        if (input.ABIType == "string")
                        {
                            byte[] topic = Keccack256.CalculateHash(arg.ToString());
                            topics.Add(topic);
                        }
                        else
                        {
                            if ((arg is string) && (arg as string).IsHexString())
                            {
                                byte[] topic = Keccack256.CalculateHash(arg);
                                topics.Add(topic);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("invalid {0} value", input.ABIType));
                            }
                        }
                    }
                    else
                    {
                        byte[] topic = AbiParameterCoder.EncodeParame(new AbiInputParameter(input, arg));
                        topics.Add(topic);
                    }
                }
            }
            return(topics.ToArray());
        }
Exemplo n.º 4
0
        private byte[][] Encode(Dictionary <string, dynamic> args)
        {
            List <byte[]> topics = new List <byte[]>();

            if (!this._definition.Anonymous)
            {
                topics.Add(this._definition.Sha3Signature);
            }

            foreach (var input in this._definition.inputs)
            {
                if (!input.Indexed)
                {
                    continue;
                }
                if (args.ContainsKey(input.Name) && args[input.Name] == null)
                {
                    topics.Add(null);
                }
                else
                {
                    var arg = args[input.Name];
                    if (IsDynamicType(input.ABIType))
                    {
                        if (input.ABIType == "string")
                        {
                            byte[] topic = Keccack256.CalculateHash(arg.ToString());
                            topics.Add(topic);
                        }
                        else
                        {
                            if (arg is string stringArgs && stringArgs.IsHexString())
                            {
                                byte[] topic = Keccack256.CalculateHash(arg);
                                topics.Add(topic);
                            }
                            else
                            {
                                throw new ArgumentException($"invalid {input.ABIType} value");
                            }
                        }
                    }