Exemplo n.º 1
0
        public void ConvertP2wpkhTest()
        {
            IOperation[] ops = new IOperation[2] {
                new PushDataOp(OP._0), new PushDataOp(Helper.GetBytes(20))
            };
            ScriptSerializer ser = new ScriptSerializer();

            byte[] actual   = ser.ConvertP2wpkh(ops);
            byte[] expected = Helper.HexToBytes($"76a914{Helper.GetBytesHex(20)}88ac");

            Assert.Equal(expected, actual);
        }
Exemplo n.º 2
0
        private bool VerifyP2wpkh(ITransaction tx, int index, PushDataOp sigPush, PushDataOp pubPush,
                                  ReadOnlySpan <byte> pubScrData, ulong amount, out string error)
        {
            if (sigPush.data == null || pubPush.data == null)
            {
                error = "Invalid data pushes in P2WPKH witness.";
                return(false);
            }

            var actualHash = hash160.ComputeHash(pubPush.data);

            if (!pubScrData.Slice(2, 20).SequenceEqual(actualHash))
            {
                error = "Invalid hash.";
                return(false);
            }

            if (!Signature.TryReadStrict(sigPush.data, out Signature sig, out error))
            {
                error = $"Invalid signature ({error})";
                return(false);
            }

            if (!PublicKey.TryRead(pubPush.data, out PublicKey pubK))
            {
                error = "Invalid public key";
                return(false);
            }

            byte[] toSign = tx.SerializeForSigningSegWit(scrSer.ConvertP2wpkh(actualHash), index, amount, sig.SigHash);
            if (calc.Verify(toSign, sig, pubK, ForceLowS))
            {
                error = null;
                return(true);
            }
            else
            {
                error = "Invalid signature";
                return(false);
            }
        }