Exemplo n.º 1
0
        public override bool CheckScriptPubKey(Script scriptPubKey)
        {
            var ops = scriptPubKey.ToOps().ToArray();
            if(ops.Length < 3)
                return false;

            var sigCount = ops[0];
            if(!sigCount.IsSmallUInt)
                return false;

            var expectedKeyCount = 0;
            var keyCountIndex = 0;
            for(int i = 1 ; i < ops.Length ; i++)
            {
                if(ops[i].PushData == null)
                    return false;
                if(!PubKey.IsValidSize(ops[i].PushData.Length))
                {
                    keyCountIndex = i;
                    break;
                }
                expectedKeyCount++;
            }
            if(!ops[keyCountIndex].IsSmallUInt)
                return false;
            if(ops[keyCountIndex].GetValue() != expectedKeyCount)
                return false;
            return ops[keyCountIndex + 1].Code == OpcodeType.OP_CHECKMULTISIG &&
                  keyCountIndex + 1 == ops.Length - 1;
        }
Exemplo n.º 2
0
		public byte[][] ExtractScriptPubKeyParameters(Script scriptPubKey)
		{
			if(!FastCheckScriptPubKey(scriptPubKey))
				return null;
			var ops = scriptPubKey.ToOps().ToArray();
			if(!CheckScriptPubKeyCore(scriptPubKey, ops))
				return null;
			return ops.Skip(1).Select(o=>o.PushData).ToArray();
		}
		public byte[] ExtractScriptPubKeyParameters(Script scriptPubKey)
		{
			if(!FastCheckScriptPubKey(scriptPubKey))
				return null;
			var ops = scriptPubKey.ToOps().ToArray();
			if(ops.Length != 2)
				return null;
			if(ops[1].PushData == null || ops[1].PushData.Length > MAX_OPRETURN_SIZE)
				return null;
			return ops[1].PushData;
		}
Exemplo n.º 4
0
        public override bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
        {
            if(!scriptSig.IsPushOnly)
                return false;
            if(!CheckScriptPubKey(scriptPubKey))
                return false;

            var sigCountExpected = scriptPubKey.ToOps().First().GetValue();
            var sigOps = scriptSig.ToOps().ToArray();
            return sigOps[0].Code == OpcodeType.OP_0 ?
                                    sigCountExpected == sigOps.Length - 1 : sigCountExpected == sigOps.Length;
        }
Exemplo n.º 5
0
        public PubKey ExtractScriptPubKeyParameters(Script script)
        {
            var ops = script.ToOps().ToArray();

            if (!CheckScriptPubKeyCore(script, ops))
            {
                return(null);
            }
            try
            {
                return(new PubKey(ops[0].PushData));
            }
            catch (FormatException)
            {
                return(null);
            }
        }
 public WitProgramParameters ExtractScriptPubKeyParameters2(Network network, Script scriptPubKey)
 {
     if (!CheckScriptPubKey(scriptPubKey))
     {
         return(null);
     }
     Op[] ops = scriptPubKey.ToOps().ToArray();
     if (ops.Length != 2 || ops[1].PushData == null)
     {
         return(null);
     }
     return(new WitProgramParameters()
     {
         Version = ops[0].Code,
         Program = ops[1].PushData
     });
 }
Exemplo n.º 7
0
        public byte[][] ExtractScriptPubKeyParameters(Script scriptPubKey)
        {
            bool needMoreCheck;

            if (!FastCheckScriptPubKey(scriptPubKey, out needMoreCheck))
            {
                return(null);
            }

            Op[] ops = scriptPubKey.ToOps().ToArray();
            if (!CheckScriptPubKeyCore(scriptPubKey, ops))
            {
                return(null);
            }

            return(ops.Skip(1).Select(o => o.PushData).ToArray());
        }
Exemplo n.º 8
0
        public TransactionSignature[] ExtractScriptSigParameters(Script scriptSig)
        {
            var ops = scriptSig.ToOps().ToArray();

            if (!CheckScriptSigCore(scriptSig, ops, null, null))
            {
                return(null);
            }
            try
            {
                return(ops.Skip(1).Select(i => new TransactionSignature(i.PushData)).ToArray());
            }
            catch (FormatException)
            {
                return(null);
            }
        }
Exemplo n.º 9
0
        public virtual bool CheckScriptPubKey(Script scriptPubKey)
        {
            if (scriptPubKey == null)
            {
                throw new ArgumentNullException("scriptPubKey");
            }

            bool needMoreCheck;
            bool result = FastCheckScriptPubKey(scriptPubKey, out needMoreCheck);

            if (needMoreCheck)
            {
                result &= CheckScriptPubKeyCore(scriptPubKey, scriptPubKey.ToOps().ToArray());
            }

            return(result);
        }
        public PayToMultiSigTemplateParameters ExtractScriptPubKeyParameters(Script scriptPubKey)
        {
            bool needMoreCheck;

            if (!FastCheckScriptPubKey(scriptPubKey, out needMoreCheck))
            {
                return(null);
            }
            Op[] ops = scriptPubKey.ToOps().ToArray();
            if (!CheckScriptPubKeyCore(scriptPubKey, ops))
            {
                return(null);
            }

            //already checked in CheckScriptPubKeyCore
            int sigCount    = ops[0].GetInt().Value;
            int keyCount    = ops[ops.Length - 2].GetInt().Value;
            var keys        = new List <PubKey>();
            var invalidKeys = new List <byte[]>();

            for (int i = 1; i < keyCount + 1; i++)
            {
                if (!PubKey.Check(ops[i].PushData, false))
                {
                    invalidKeys.Add(ops[i].PushData);
                }
                else
                {
                    try
                    {
                        keys.Add(new PubKey(ops[i].PushData));
                    }
                    catch (FormatException)
                    {
                        invalidKeys.Add(ops[i].PushData);
                    }
                }
            }

            return(new PayToMultiSigTemplateParameters()
            {
                SignatureCount = sigCount,
                PubKeys = keys.ToArray(),
                InvalidPubKeys = invalidKeys.ToArray()
            });
        }
Exemplo n.º 11
0
        public PayToMultiSigTemplateParameters ExtractScriptPubKeyParameters(Script scriptPubKey)
        {
            if (!FastCheckScriptPubKey(scriptPubKey))
            {
                return(null);
            }
            var ops = scriptPubKey.ToOps().ToArray();

            if (!CheckScriptPubKeyCore(scriptPubKey, ops))
            {
                return(null);
            }

            var sigCount = (int)ops[0].GetValue();
            var keyCount = (int)ops[ops.Length - 2].GetValue();

            List <PubKey> keys        = new List <PubKey>();
            List <byte[]> invalidKeys = new List <byte[]>();

            for (int i = 1; i < keyCount + 1; i++)
            {
                if (!PubKey.Check(ops[i].PushData, false))
                {
                    invalidKeys.Add(ops[i].PushData);
                }
                else
                {
                    try
                    {
                        keys.Add(new PubKey(ops[i].PushData));
                    }
                    catch (FormatException)
                    {
                        invalidKeys.Add(ops[i].PushData);
                    }
                }
            }

            return(new PayToMultiSigTemplateParameters()
            {
                SignatureCount = sigCount,
                PubKeys = keys.ToArray(),
                InvalidPubKeys = invalidKeys.ToArray()
            });
        }
Exemplo n.º 12
0
        public override bool CheckScriptPubKey(Script scriptPubKey)
        {
            var ops = scriptPubKey.ToOps().ToList();

            if (ops.Count < 1)
            {
                return(false);
            }
            if (ops[0].Code != OpcodeType.OP_RETURN)
            {
                return(false);
            }
            if (ops.Count == 2)
            {
                return(ops[1].PushData != null && ops[1].PushData.Length <= 40);
            }
            return(true);
        }
Exemplo n.º 13
0
		public PayToPubkeyHashScriptSigParameters ExtractScriptSigParameters(Script scriptSig)
		{
			var ops = scriptSig.ToOps().ToArray();
			if(!CheckScriptSigCore(scriptSig, ops, null, null))
				return null;
			try
			{
				return new PayToPubkeyHashScriptSigParameters()
				{
					TransactionSignature = ops[0].Code == OpcodeType.OP_0 ? null : new TransactionSignature(ops[0].PushData),
					PublicKey = new PubKey(ops[1].PushData, true),
				};
			}
			catch(FormatException)
			{
				return null;
			}
		}
Exemplo n.º 14
0
		public TransactionSignature ExtractScriptSigParameters(Script scriptSig)
		{
			var ops = scriptSig.ToOps().ToArray();
			if(!CheckScriptSigCore(scriptSig, ops, null, null))
				return null;

			var data = ops[0].PushData;
			if(!TransactionSignature.ValidLength(data.Length))
				return null;
			try
			{
				return new TransactionSignature(data);
			}
			catch(FormatException)
			{
				return null;
			}
		}
Exemplo n.º 15
0
        public PayToPubkeyHashScriptSigParameters ExtractScriptSigParameters(Script scriptSig)
        {
            var ops = scriptSig.ToOps().ToArray();

            if (!CheckScriptSigCore(scriptSig, ops, null, null))
            {
                return(null);
            }
            if (PubKey.TryCreatePubKey(ops[1].PushData, out var pk))
            {
                return(new PayToPubkeyHashScriptSigParameters()
                {
                    TransactionSignature = ops[0].Code == OpcodeType.OP_0 ? null : new TransactionSignature(ops[0].PushData),
                    PublicKey = pk,
                });
            }
            return(null);
        }
Exemplo n.º 16
0
        public byte[] ExtractScriptPubKeyParameters(Script scriptPubKey)
        {
            if (!FastCheckScriptPubKey(scriptPubKey))
            {
                return(null);
            }
            var ops = scriptPubKey.ToOps().ToArray();

            if (ops.Length != 2)
            {
                return(null);
            }
            if (ops[1].PushData == null || ops[1].PushData.Length > MAX_OPRETURN_SIZE)
            {
                return(null);
            }
            return(ops[1].PushData);
        }
Exemplo n.º 17
0
        public override bool CheckScriptPubKey(Script scriptPubKey)
        {
            var ops = scriptPubKey.ToOps().ToArray();

            if (ops.Length < 3)
            {
                return(false);
            }

            var sigCount = ops[0];

            if (!sigCount.IsSmallUInt)
            {
                return(false);
            }

            var expectedKeyCount = 0;
            var keyCountIndex    = 0;

            for (int i = 1; i < ops.Length; i++)
            {
                if (ops[i].PushData == null)
                {
                    return(false);
                }
                if (!PubKey.IsValidSize(ops[i].PushData.Length))
                {
                    keyCountIndex = i;
                    break;
                }
                expectedKeyCount++;
            }
            if (!ops[keyCountIndex].IsSmallUInt)
            {
                return(false);
            }
            if (ops[keyCountIndex].GetValue() != expectedKeyCount)
            {
                return(false);
            }
            return(ops[keyCountIndex + 1].Code == OpcodeType.OP_CHECKMULTISIG &&
                   keyCountIndex + 1 == ops.Length - 1);
        }
Exemplo n.º 18
0
        public override bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
        {
            var ops = scriptSig.ToOps().ToArray();

            if (ops.Length == 0)
            {
                return(false);
            }
            if (!scriptSig.IsPushOnly)
            {
                return(false);
            }
            if (!VerifyRedeemScript)
            {
                return(true);
            }
            var redeemScript = new Script(ops.Last().PushData);
            var template     = StandardScripts.GetTemplateFromScriptPubKey(redeemScript);

            return(template != null && template.Type != TxOutType.TX_SCRIPTHASH);
        }
        public PayToMultiSigTemplateParameters ExtractScriptPubKeyParameters(Script scriptPubKey, Network network)
        {
            bool needMoreCheck;

            if (!FastCheckScriptPubKey(scriptPubKey, out needMoreCheck))
            {
                return(null);
            }
            Op[] ops = scriptPubKey.ToOps().ToArray();
            if (!CheckScriptPubKeyCore(scriptPubKey, ops))
            {
                return(null);
            }

            byte[] federationId = ops[0].PushData;
            (PubKey[] pubKeys, int signatureCount) = network.Federations.GetFederation(federationId).GetFederationDetails();
            return(new PayToMultiSigTemplateParameters()
            {
                PubKeys = pubKeys, SignatureCount = signatureCount
            });
        }
Exemplo n.º 20
0
        public PayToPubkeyHashScriptSigParameters ExtractScriptSigParameters(Script scriptSig)
        {
            var ops = scriptSig.ToOps().ToArray();

            if (!CheckScriptSigCore(scriptSig, ops, null, null))
            {
                return(null);
            }
            try
            {
                return(new PayToPubkeyHashScriptSigParameters()
                {
                    TransactionSignature = new TransactionSignature(ops[0].PushData),
                    PublicKey = new PubKey(ops[1].PushData),
                });
            }
            catch (FormatException)
            {
                return(null);
            }
        }
Exemplo n.º 21
0
        public uint GetSigOpCount(Script scriptSig)
        {
            if (!IsPayToScriptHash)
            {
                return(GetSigOpCount(true));
            }
            // This is a pay-to-script-hash scriptPubKey;
            // get the last item that the scriptSig
            // pushes onto the stack:
            var validSig = new PayToScriptHashTemplate()
            {
                VerifyRedeemScript = false
            }.CheckScriptSig(scriptSig, null);

            if (!validSig)
            {
                return(0);
            }
            /// ... and return its opcount:
            return(new Script(scriptSig.ToOps().Last().PushData).GetSigOpCount(true));
        }
Exemplo n.º 22
0
        public PayToMultiSigTemplateParameters ExtractScriptPubKeyParameters(Script scriptPubKey)
        {
            bool needMoreCheck;

            if (!FastCheckScriptPubKey(scriptPubKey, out needMoreCheck))
            {
                return(null);
            }
            var ops = scriptPubKey.ToOps().ToArray();

            if (!CheckScriptPubKeyCore(scriptPubKey, ops))
            {
                return(null);
            }

            //already checked in CheckScriptPubKeyCore
            var           sigCount    = ops[0].GetInt().Value;
            var           keyCount    = ops[ops.Length - 2].GetInt().Value;
            List <PubKey> keys        = new List <PubKey>();
            List <byte[]> invalidKeys = new List <byte[]>();

            for (int i = 1; i < keyCount + 1; i++)
            {
                if (PubKey.TryCreatePubKey(ops[i].PushData, out var pk))
                {
                    keys.Add(pk);
                }
                else
                {
                    invalidKeys.Add(ops[i].PushData);
                }
            }

            return(new PayToMultiSigTemplateParameters()
            {
                SignatureCount = sigCount,
                PubKeys = keys.ToArray(),
                InvalidPubKeys = invalidKeys.ToArray()
            });
        }
        public TransactionSignature[] ExtractScriptSigParameters(Network network, Script scriptSig)
        {
            bool needMoreCheck;

            if (!FastCheckScriptSig(scriptSig, null, out needMoreCheck))
            {
                return(null);
            }
            Op[] ops = scriptSig.ToOps().ToArray();
            if (!CheckScriptSigCore(network, scriptSig, ops, null, null))
            {
                return(null);
            }
            try
            {
                return(ops.Skip(1).Select(i => i.Code == OpcodeType.OP_0 ? null : new TransactionSignature(i.PushData)).ToArray());
            }
            catch (FormatException)
            {
                return(null);
            }
        }
        public TransactionSignature ExtractScriptSigParameters(Network network, Script scriptSig)
        {
            Op[] ops = scriptSig.ToOps().ToArray();
            if (!CheckScriptSigCore(network, scriptSig, ops, null, null))
            {
                return(null);
            }

            byte[] data = ops[0].PushData;
            if (!TransactionSignature.ValidLength(data.Length))
            {
                return(null);
            }
            try
            {
                return(new TransactionSignature(data));
            }
            catch (FormatException)
            {
                return(null);
            }
        }
Exemplo n.º 25
0
        /// <summary>
        /// Adds a scriptSig to the list of elements that will be used for building the filter.
        /// </summary>
        /// <param name="scriptSig">The scriptSig.</param>
        /// <returns>The updated filter builder instance.</returns>
        public GolombRiceFilterBuilder AddScriptSig(Script scriptSig)
        {
            if (scriptSig == null)
            {
                throw new ArgumentNullException(nameof(scriptSig));
            }

            var data = new List <byte[]>();

            foreach (var op in scriptSig.ToOps())
            {
                if (op.PushData != null)
                {
                    data.Add(op.PushData);
                }
                else if (op.Code == OpcodeType.OP_0)
                {
                    data.Add(new byte[0]);
                }
            }
            AddEntries(data);
            return(this);
        }
Exemplo n.º 26
0
        public PayToScriptHashSigParameters ExtractScriptSigParameters(Script scriptSig)
        {
            var ops = scriptSig.ToOps().ToArray();

            if (!CheckScriptSigCore(scriptSig, ops, null, null))
            {
                return(null);
            }
            try
            {
                PayToScriptHashSigParameters result = new PayToScriptHashSigParameters();
                result.Signatures =
                    ops
                    .Take(ops.Length - 1)
                    .Select(o => new TransactionSignature(o.PushData))
                    .ToArray();
                result.RedeemScript = new Script(ops[ops.Length - 1].PushData);
                return(result);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemplo n.º 27
0
		public PayToScriptHashSigParameters ExtractScriptSigParameters(Script scriptSig, Script scriptPubKey)
		{
			var ops = scriptSig.ToOps().ToArray();
			var ops2 = scriptPubKey == null ? null : scriptPubKey.ToOps().ToArray();
			if(!CheckScriptSigCore(scriptSig, ops, scriptPubKey, ops2))
				return null;
			try
			{
				var multiSig = ops.Length > 0 && ops[0].Code == OpcodeType.OP_0;
				PayToScriptHashSigParameters result = new PayToScriptHashSigParameters();
				result.Signatures =
					ops
					.Skip(multiSig ? 1 : 0)
					.Take(ops.Length - 1 - (multiSig ? 1 : 0))
					.Select(o => o.Code == OpcodeType.OP_0 ? null : new TransactionSignature(o.PushData))
					.ToArray();
				result.RedeemScript = Script.FromBytesUnsafe(ops[ops.Length - 1].PushData);
				return result;
			}
			catch(Exception)
			{
				return null;
			}
		}
Exemplo n.º 28
0
        public override bool CheckScriptPubKey(Script scriptPubKey)
        {
            if (scriptPubKey == null)
            {
                throw new ArgumentNullException("scriptPubKey");
            }
            var bytes = scriptPubKey.ToBytes(true);

            if (bytes.Length < 2)
            {
                return(false);
            }
            var version = bytes[0];

            if (!ValidSegwitVersion(version))
            {
                return(false);
            }
            var pushSize = bytes[1];

            if (pushSize < 0x02)
            {
                return(false);
            }
            if (pushSize <= 0x4b)
            {
                return((1 + 1 + pushSize) == bytes.Length);
            }
            var ops = scriptPubKey.ToOps().ToArray();

            if (ops.Length != 2)
            {
                return(false);
            }
            return(ops.All(o => o.PushData != null));
        }
 public TxDestination ExtractScriptPubKeyParameters(Network network, Script scriptPubKey)
 {
     if (!CheckScriptPubKey(scriptPubKey))
     {
         return(null);
     }
     Op[] ops = scriptPubKey.ToOps().ToArray();
     if (ops.Length != 2 || ops[1].PushData == null)
     {
         return(null);
     }
     if (ops[0].Code == OpcodeType.OP_0)
     {
         if (ops[1].PushData.Length == 20)
         {
             return(new WitKeyId(ops[1].PushData));
         }
         if (ops[1].PushData.Length == 32)
         {
             return(new WitScriptId(ops[1].PushData));
         }
     }
     return(null);
 }
Exemplo n.º 30
0
        private string FindDestination(JObject transaction)
        {
            foreach (JObject output in transaction["outputs"])
            {
                ByteString script = ByteString.Parse((string)output["script"]);
                Script parsedScript = new Script(script.ToByteArray());

                Op firstOpCode = parsedScript.ToOps().FirstOrDefault();

                if (parsedScript.ToOps() != null && firstOpCode.Code == OpcodeType.OP_RETURN)
                {
                    foreach (Op opCode in parsedScript.ToOps())
                    {
                        if (opCode.PushData != null && opCode.PushData.Length >= 2 && !opCode.IsInvalid)
                        {
                            if (opCode.PushData[0] == 'O' && opCode.PushData[1] == 'G')
                                return Encoding.UTF8.GetString(opCode.PushData, 2, opCode.PushData.Length - 2);
                        }
                    }
                }
            }

            return null;
        }
Exemplo n.º 31
0
        public virtual bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
        {
            if (scriptSig == null)
            {
                throw new ArgumentNullException("scriptSig");
            }

            if (!FastCheckScriptSig(scriptSig, scriptPubKey))
            {
                return(false);
            }
            return(CheckScriptSigCore(scriptSig, scriptSig.ToOps().ToArray(), scriptPubKey, scriptPubKey == null ? null : scriptPubKey.ToOps().ToArray()));
        }
Exemplo n.º 32
0
        public TransactionSignature ExtractScriptSigParameters(Script scriptSig)
        {
            if(!CheckScriptSig(scriptSig, null))
                return null;

            var data = scriptSig.ToOps().First().PushData;
            if(!TransactionSignature.ValidLength(data.Length))
                return null;
            try
            {
                return new TransactionSignature(data);
            }
            catch(FormatException)
            {
                return null;
            }
        }
Exemplo n.º 33
0
		public TxDestination ExtractScriptPubKeyParameters(Script scriptPubKey)
		{
			if(!CheckScriptPubKey(scriptPubKey))
				return null;
			var ops = scriptPubKey.ToOps().ToArray();
			if(ops.Length != 2 || ops[1].PushData == null)
				return null;
			if(ops[0].Code == OpcodeType.OP_0)
			{
				if(ops[1].PushData.Length == 20)
					return new WitKeyId(ops[1].PushData);
				if(ops[1].PushData.Length == 32)
					return new WitScriptId(ops[1].PushData);
			}
			return null;
		}
Exemplo n.º 34
0
 private static bool Fill(StealthMetadata output, Script metadata)
 {
     var ops = metadata.ToOps().ToArray();
     if(ops.Length != 2 || ops[0].Code != OpcodeType.OP_RETURN)
         return false;
     var data = ops[1].PushData;
     if(data == null || data.Length != 1 + 4 + 33)
         return false;
     MemoryStream ms = new MemoryStream(data);
     output.Version = ms.ReadByte();
     if(output.Version != 6)
         return false;
     output.Nonce = ms.ReadBytes(4);
     output.EphemKey = new PubKey(ms.ReadBytes(33));
     output.Script = metadata;
     output.Hash = Hashes.Hash256(data);
     var msprefix = new MemoryStream(output.Hash.ToBytes(false));
     output.BitField = Utils.ToUInt32(msprefix.ReadBytes(4), true);
     return true;
 }
Exemplo n.º 35
0
		public TransactionSignature ExtractScriptSigParameters(Script scriptSig)
		{
			var ops = scriptSig.ToOps().ToArray();
			if(!CheckScriptSigCore(scriptSig, ops, null, null))
				return null;

			var data = ops[0].PushData;
			if(!TransactionSignature.ValidLength(data.Length))
				return null;
			try
			{
				return new TransactionSignature(data);
			}
			catch(FormatException)
			{
				return null;
			}
		}
Exemplo n.º 36
0
		public virtual bool CheckScriptPubKey(Script scriptPubKey)
		{
			if(scriptPubKey == null)
				throw new ArgumentNullException("scriptPubKey");
			bool needMoreCheck;
			bool result = FastCheckScriptPubKey(scriptPubKey, out needMoreCheck);
			if(needMoreCheck)
			{
				result &= CheckScriptPubKeyCore(scriptPubKey, scriptPubKey.ToOps().ToArray());
			}
			return result;
		}
Exemplo n.º 37
0
 public override bool CheckScriptPubKey(Script scriptPubKey)
 {
     var ops = scriptPubKey.ToOps().ToList();
     if(ops.Count < 1)
         return false;
     if(ops[0].Code != OpcodeType.OP_RETURN)
         return false;
     if(ops.Count == 2)
     {
         return ops[1].PushData != null && ops[1].PushData.Length <= 40;
     }
     return true;
 }
Exemplo n.º 38
0
		public TransactionSignature[] ExtractScriptSigParameters(Script scriptSig)
		{
			bool needMoreCheck;
			if(!FastCheckScriptSig(scriptSig, null, out needMoreCheck))
				return null;
			var ops = scriptSig.ToOps().ToArray();
			if(!CheckScriptSigCore(scriptSig, ops, null, null))
				return null;
			try
			{
				return ops.Skip(1).Select(i => i.Code == OpcodeType.OP_0 ? null : new TransactionSignature(i.PushData)).ToArray();
			}
			catch(FormatException)
			{
				return null;
			}
		}
Exemplo n.º 39
0
 public ScriptId ExtractScriptPubKeyParameters(Script scriptPubKey)
 {
     if(!this.CheckScriptPubKey(scriptPubKey))
         return null;
     return new ScriptId(scriptPubKey.ToOps().Skip(1).First().PushData);
 }
Exemplo n.º 40
0
 public PayToScriptHashSigParameters ExtractScriptSigParameters(Script scriptSig)
 {
     var ops = scriptSig.ToOps().ToArray();
     if(!ops.All(o => o.PushData != null))
         return null;
     try
     {
         PayToScriptHashSigParameters result = new PayToScriptHashSigParameters();
         result.Signatures =
             ops
             .Take(ops.Length - 1)
             .Select(o => new TransactionSignature(o.PushData))
             .ToArray();
         result.Script = new Script(ops[ops.Length - 1].PushData);
         return result;
     }
     catch(Exception)
     {
         return null;
     }
 }
Exemplo n.º 41
0
 public override bool CheckScriptPubKey(Script scriptPubKey)
 {
     var ops = scriptPubKey.ToOps().ToList();
     if(ops.Count != 2)
         return false;
     return ops[0].PushData != null && PubKey.IsValidSize(ops[0].PushData.Length) &&
            ops[1].Code == OpcodeType.OP_CHECKSIG;
 }
Exemplo n.º 42
0
 public override bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
 {
     var ops = scriptSig.ToOps().ToArray();
     if(ops.Length == 0)
         return false;
     if(!scriptSig.IsPushOnly)
         return false;
     if(!VerifyRedeemScript)
         return true;
     var redeemScript = new Script(ops.Last().PushData);
     var template = StandardScripts.GetTemplateFromScriptPubKey(redeemScript);
     return template != null && template.Type != TxOutType.TX_SCRIPTHASH;
 }
Exemplo n.º 43
0
 public override bool CheckScriptPubKey(Script scriptPubKey)
 {
     var ops = scriptPubKey.ToOps().ToArray();
     if(ops.Length != 3)
         return false;
     return ops[0].Code == OpcodeType.OP_HASH160 &&
            ops[1].Code == (OpcodeType)0x14 &&
            ops[2].Code == OpcodeType.OP_EQUAL;
 }
Exemplo n.º 44
0
		public uint GetSigOpCount(Script scriptSig)
		{
			if(!IsPayToScriptHash)
				return GetSigOpCount(true);
			// This is a pay-to-script-hash scriptPubKey;
			// get the last item that the scriptSig
			// pushes onto the stack:
			var validSig = new PayToScriptHashTemplate().CheckScriptSig(scriptSig, this);
			return !validSig ? 0 : new Script(scriptSig.ToOps().Last().PushData).GetSigOpCount(true);
			// ... and return its opcount:
		}
Exemplo n.º 45
0
 public PayToPubkeyHashScriptSigParameters ExtractScriptSigParameters(Script scriptSig)
 {
     if(!CheckScriptSig(scriptSig))
         return null;
     var ops = scriptSig.ToOps().ToArray();
     return new PayToPubkeyHashScriptSigParameters()
     {
         TransactionSignature = new TransactionSignature(ops[0].PushData),
         PublicKey = new PubKey(ops[1].PushData),
     };
 }
Exemplo n.º 46
0
		public PayToMultiSigTemplateParameters ExtractScriptPubKeyParameters(Script scriptPubKey)
		{
			bool needMoreCheck;
			if(!FastCheckScriptPubKey(scriptPubKey, out needMoreCheck))
				return null;
			var ops = scriptPubKey.ToOps().ToArray();
			if(!CheckScriptPubKeyCore(scriptPubKey, ops))
				return null;

			var sigCount = (int)ops[0].GetValue();
			var keyCount = (int)ops[ops.Length - 2].GetValue();

			List<PubKey> keys = new List<PubKey>();
			List<byte[]> invalidKeys = new List<byte[]>();
			for(int i = 1; i < keyCount + 1; i++)
			{
				if(!PubKey.Check(ops[i].PushData, false))
					invalidKeys.Add(ops[i].PushData);
				else
				{
					try
					{
						keys.Add(new PubKey(ops[i].PushData));
					}
					catch(FormatException)
					{
						invalidKeys.Add(ops[i].PushData);
					}
				}
			}

			return new PayToMultiSigTemplateParameters()
			{
				SignatureCount = sigCount,
				PubKeys = keys.ToArray(),
				InvalidPubKeys = invalidKeys.ToArray()
			};
		}
Exemplo n.º 47
0
        public PayToMultiSigTemplateParameters ExtractScriptPubKeyParameters(Script scriptPubKey)
        {
            if(!CheckScriptPubKey(scriptPubKey))
                return null;

            var ops = scriptPubKey.ToOps().ToArray();
            var sigCount = (byte)ops[0].PushData[0];
            List<PubKey> keys = new List<PubKey>();
            for(int i = 1 ; i < ops.Length ; i++)
            {
                if(!PubKey.IsValidSize(ops[i].PushData.Length))
                    break;
                keys.Add(new PubKey(ops[i].PushData));
            }

            return new PayToMultiSigTemplateParameters()
            {
                SignatureCount = sigCount,
                PubKeys = keys.ToArray()
            };
        }
Exemplo n.º 48
0
		public PayToScriptHashSigParameters ExtractScriptSigParameters(Script scriptSig, Script scriptPubKey)
		{
			var ops = scriptSig.ToOps().ToArray();
			var ops2 = scriptPubKey == null ? null : scriptPubKey.ToOps().ToArray();
			if(!CheckScriptSigCore(scriptSig, ops, scriptPubKey, ops2))
				return null;

			PayToScriptHashSigParameters result = new PayToScriptHashSigParameters();
			result.RedeemScript = Script.FromBytesUnsafe(ops[ops.Length - 1].PushData);
			result.Pushes = ops.Take(ops.Length - 1).Select(o => o.PushData).ToArray();
			return result;
		}
Exemplo n.º 49
0
        public virtual bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
        {
            if (scriptSig == null)
            {
                throw new ArgumentNullException("scriptSig");
            }
            bool needMoreCheck;
            var  result = FastCheckScriptSig(scriptSig, scriptPubKey, out needMoreCheck);

            if (needMoreCheck)
            {
                result &= CheckScriptSigCore(scriptSig, scriptSig.ToOps().ToArray(), scriptPubKey, scriptPubKey == null ? null : scriptPubKey.ToOps().ToArray());
            }
            return(result);
        }
Exemplo n.º 50
0
		public PayToPubkeyHashScriptSigParameters ExtractScriptSigParameters(Script scriptSig)
		{
			var ops = scriptSig.ToOps().ToArray();
			if(!CheckScriptSigCore(scriptSig, ops, null, null))
				return null;
			try
			{
				return new PayToPubkeyHashScriptSigParameters()
				{
					TransactionSignature = ops[0].Code == OpcodeType.OP_0 ? null : new TransactionSignature(ops[0].PushData),
					PublicKey = new PubKey(ops[1].PushData, true),
				};
			}
			catch(FormatException)
			{
				return null;
			}
		}
Exemplo n.º 51
0
 public KeyId ExtractScriptPubKeyParameters(Script script)
 {
     if(!CheckScriptPubKey(script))
         return null;
     return new KeyId(script.ToOps().Skip(2).First().PushData);
 }
Exemplo n.º 52
0
		public virtual bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
		{
			if(scriptSig == null)
				throw new ArgumentNullException("scriptSig");
			bool needMoreCheck;
			var result = FastCheckScriptSig(scriptSig, scriptPubKey, out needMoreCheck);
			if(needMoreCheck)
			{
				result &= CheckScriptSigCore(scriptSig, scriptSig.ToOps().ToArray(), scriptPubKey, scriptPubKey == null ? null : scriptPubKey.ToOps().ToArray());
			}
			return result;
		}
Exemplo n.º 53
0
 public override bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
 {
     var ops = scriptSig.ToOps().ToArray();
     if(ops.Length != 2)
         return false;
     return ops[0].PushData != null &&
            ops[1].PushData != null && PubKey.IsValidSize(ops[1].PushData.Length);
 }
Exemplo n.º 54
0
		public WitProgramParameters ExtractScriptPubKeyParameters2(Script scriptPubKey)
		{
			if(!CheckScriptPubKey(scriptPubKey))
				return null;
			var ops = scriptPubKey.ToOps().ToArray();
			if(ops.Length != 2 || ops[1].PushData == null)
				return null;
			return new WitProgramParameters()
			{
				Version = ops[0].Code,
				Program = ops[1].PushData
			};
		}
Exemplo n.º 55
0
 public override bool CheckScriptPubKey(Script scriptPubKey)
 {
     var ops = scriptPubKey.ToOps().ToArray();
     if(ops.Length != 5)
         return false;
     return ops[0].Code == OpcodeType.OP_DUP &&
            ops[1].Code == OpcodeType.OP_HASH160 &&
            ops[2].PushData != null && ops[2].PushData.Length == 0x14 &&
            ops[3].Code == OpcodeType.OP_EQUALVERIFY &&
            ops[4].Code == OpcodeType.OP_CHECKSIG;
 }
        public WantedSystemMessage GetWantedSystemMessage(Script scriptPubKey, RsaPrivateKey privateKey = null)
        {
            var msg = new WantedSystemMessage();

            if (scriptPubKey.Length < 13)
            {
                throw new Exception("This ScriptPubKey is not a valid Wanted System message.");
            }

            Op[] scriptPubKeyOps = scriptPubKey.ToOps().ToArray();
            if ((scriptPubKeyOps[0].Code != OpcodeType.OP_NOP) || (scriptPubKeyOps[1].Code != OpcodeType.OP_NOP) || (scriptPubKeyOps[2].Code != OpcodeType.OP_RETURN))
            {
                throw new Exception("This ScriptPubKey is not a valid Wanted System message.");
            }

            byte[] pd = scriptPubKeyOps[3].PushData;

            byte[] header = pd.Take <byte>(3).ToArray();
            msg.Version = pd[3];
            if (msg.Version != 1)
            {
                throw new Exception($"Wanted System message vesion {msg.Version} is not supported.");
            }

            msg.Compression = (MessageCompression)pd[4];
            if (msg.Compression != MessageCompression.GZip)
            {
                throw new Exception($"Wanted System message compression {msg.Compression} is not supported.");
            }

            msg.ChecksumType = (MessageChecksum)pd[5];
            if (msg.ChecksumType != MessageChecksum.None)
            {
                throw new Exception($"Wanted System message checksum {msg.ChecksumType} is not supported.");
            }

            msg.Encryption = (MessageEncryption)pd[6];
            if (msg.Encryption != MessageEncryption.RSA4096AES256)
            {
                throw new Exception($"Wanted System message encryption {msg.Encryption} is not supported.");
            }

            ushort encryptionKeyLength = BitConverter.ToUInt16(pd, 7);
            ushort metadataLength      = BitConverter.ToUInt16(pd, 9);
            ushort messageLength       = BitConverter.ToUInt16(pd, 11);

            byte[] encryptionKey      = new byte[encryptionKeyLength];
            byte[] compressedMetadata = new byte[metadataLength];
            byte[] compressedMessage  = new byte[messageLength];

            Array.Copy(pd, 13, encryptionKey, 0, encryptionKeyLength);
            Array.Copy(pd, 13 + encryptionKeyLength, compressedMetadata, 0, metadataLength);
            Array.Copy(pd, 13 + encryptionKeyLength + metadataLength, compressedMessage, 0, messageLength);

            byte[] uncompressedMetadata = GZIPDecompressByteArray(compressedMetadata);
            byte[] uncompressedMessage  = GZIPDecompressByteArray(compressedMessage);

            // process metadata using json serializer
            string metadata = System.Text.Encoding.UTF8.GetString(uncompressedMetadata);

            msg.Metadata = JsonConvert.DeserializeObject <WantedSystemMessageMetadata>(metadata);

            // Decrypt the message if needed
            if (msg.Encryption == MessageEncryption.RSA4096AES256)
            {
                if (privateKey == null)
                {
                    throw new Exception("The message is encrypted but the decryption key was not provided.");
                }

                byte[] aesKey = null;
                try
                {
                    aesKey = RSADecryptByteArray(encryptionKey, privateKey);
                }
                catch
                {
                    throw new Exception("The private key you provided isn't a match for the public key the message was encrypted with.");
                }

                uncompressedMessage = AESDecryptByteArray(uncompressedMessage, aesKey);
            }
            msg.Text = System.Text.Encoding.UTF8.GetString(uncompressedMessage);

            return(msg);
        }
Exemplo n.º 57
0
		public bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
		{
			if(scriptSig == null)
				throw new ArgumentNullException("scriptSig");

			if(!FastCheckScriptSig(scriptSig, scriptPubKey))
				return false;
			return CheckScriptSigCore(scriptSig, scriptSig.ToOps().ToArray(), scriptPubKey, scriptPubKey == null ? null : scriptPubKey.ToOps().ToArray());
		}
Exemplo n.º 58
0
        public override bool CheckScriptSig(Script scriptSig, Script scriptPubKey)
        {
            var ops = scriptSig.ToOps().ToList();
            if(ops.Count != 1)
                return false;

            return ops[0].PushData != null;
        }
Exemplo n.º 59
0
 public PubKey ExtractScriptPubKeyParameters(Script script)
 {
     if(!CheckScriptPubKey(script))
         return null;
     return new PubKey(script.ToOps().ToArray()[0].PushData);
 }