Пример #1
0
        private static bool IsPayToScriptHash(ScriptPubKey scriptPubKey)
        {
            if (scriptPubKey.length == (int)PubKeySize.Hash + 3 &&
                scriptPubKey[0] == (byte)OpCode.OP_HASH160 &&
                scriptPubKey[1] == 0X14 &&
                scriptPubKey[22] == (byte)OpCode.OP_EQUAL)
            {
                return(true);
            }

            return(false);
        }
Пример #2
0
 public virtual HashVersion GetHashVersion()
 {
     if (ScriptPubKey.IsScriptType(ScriptType.Taproot))
     {
         return(HashVersion.Taproot);
     }
     if (PayToWitTemplate.Instance.CheckScriptPubKey(ScriptPubKey))
     {
         return(HashVersion.WitnessV0);
     }
     return(HashVersion.Original);
 }
Пример #3
0
        public Address(ScriptPubKey scriptPubKey, TxnType txnType)
        {
            byte[] pushData = Script.FromScriptPubKey(scriptPubKey, txnType);

            List <byte> hash;
            byte        prefix = 0x00;

            switch (txnType)
            {
            case TxnType.P2SH:
                hash   = new List <byte>(pushData);
                prefix = (byte)Prefix.TestnetP2SH;
                break;

            case TxnType.P2WPKH:
                hash = new List <byte>(pushData);
                break;

            case TxnType.P2WSH:
                hash = new List <byte>(pushData);
                break;

            case TxnType.P2PKH:
                hash   = new List <byte>(pushData);
                prefix = (byte)Prefix.TestnetP2PKH;
                break;

            case TxnType.P2PK:
                hash   = Crypto.DoubleHash(pushData);
                prefix = (byte)Prefix.TestnetP2PKH;
                break;

            default:
                hash = null;
                break;
            }

            if (hash != null)
            {
                if (txnType == TxnType.P2WPKH || txnType == TxnType.P2WSH)
                {
                    address = Bech32.Encode(0, hash.ToArray(), false);
                }
                else
                {
                    hash.Insert(0, prefix);
                    Crypto.AddChecksum(ref hash);
                    address = Base58Check.Encode(hash.ToArray());
                }
            }
        }
Пример #4
0
        private static bool MatchPayToPubKeyHash(ScriptPubKey scriptPubKey)
        {
            if (scriptPubKey.length == (int)PubKeySize.Hash + 5 &&
                scriptPubKey[0] == (byte)OpCode.OP_DUP &&
                scriptPubKey[1] == (byte)OpCode.OP_HASH160 &&
                scriptPubKey[2] == (int)PubKeySize.Hash &&
                scriptPubKey[23] == (byte)OpCode.OP_EQUALVERIFY &&
                scriptPubKey[24] == (byte)OpCode.OP_CHECKSIG)
            {
                return(true);
            }

            return(false);
        }
Пример #5
0
            internal JObject ToJson()
            {
                var obj = new JObject();

                obj.Add("ScriptPubKey", Encoders.Hex.EncodeData(ScriptPubKey.ToBytes(true)));
                obj.Add("IsInternal", IsInternal);
                if (RedeemScript != null)
                {
                    obj.Add("RedeemScript", Encoders.Hex.EncodeData(RedeemScript.ToBytes(true)));
                }
                obj.Add("AddedDate", AddedDate);
                obj.Add("Filter", Filter);
                obj.Add("Wallet", Wallet);
                return(obj);
            }
Пример #6
0
        private static bool MatchPayToPubKey(ScriptPubKey scriptPubKey)
        {
            if (scriptPubKey.length == (int)PubKeySize.Uncompressed + 2 &&
                scriptPubKey[0] == (int)PubKeySize.Uncompressed &&
                scriptPubKey[scriptPubKey.length - 1] == (byte)OpCode.OP_CHECKSIG)
            {
                return(true);
            }

            if (scriptPubKey.length == (int)PubKeySize.Compressed + 2 &&
                scriptPubKey[0] == (int)PubKeySize.Compressed &&
                scriptPubKey[scriptPubKey.length - 1] == (byte)OpCode.OP_CHECKSIG)
            {
                return(true);
            }

            return(false);
        }
Пример #7
0
        internal DynamicTableEntity ToEntity()
        {
            DynamicTableEntity entity = new DynamicTableEntity();

            entity.ETag         = "*";
            entity.PartitionKey = PartitionKey;

            var locator = CreateBalanceLocator();

            entity.RowKey = BalanceId + "-" + locator.ToString(true);

            entity.Properties.Add("s", new EntityProperty(SeenUtc));
            Helper.SetEntityProperty(entity, "ss", Helper.SerializeList(SpentIndices.Select(e => new IntCompactVarInt(e))));

            Helper.SetEntityProperty(entity, "a", Helper.SerializeList(SpentOutpoints));
            if (SpentCoins != null)
            {
                Helper.SetEntityProperty(entity, "b", Helper.SerializeList(SpentCoins.Select(c => new Spendable(c.Outpoint, c.TxOut))));
            }
            Helper.SetEntityProperty(entity, "c", Helper.SerializeList(ReceivedCoins.Select(e => new IntCompactVarInt(e.Outpoint.N))));
            Helper.SetEntityProperty(entity, "d", Helper.SerializeList(ReceivedCoins.Select(e => e.TxOut)));
            var flags = (HasOpReturn ? "o" : "n") + (IsCoinbase ? "o" : "n");

            entity.Properties.AddOrReplace("e", new EntityProperty(flags));
            entity.Properties.AddOrReplace("f", new EntityProperty(Helper.Serialize(MatchedRules)));
            if (ColoredTransaction != null)
            {
                entity.Properties.AddOrReplace("g", new EntityProperty(ColoredTransaction.ToBytes()));
            }
            if (ScriptPubKey != null && !BalanceId.ContainsScript)
            {
                var bytes = ScriptPubKey.ToBytes(true);
                if (bytes.Length < 63000)
                {
                    entity.Properties.Add("h", new EntityProperty(bytes));
                }
            }
            if (CustomData != null)
            {
                Helper.SetEntityProperty(entity, "cu", Encoding.UTF8.GetBytes(CustomData));
            }
            return(entity);
        }
Пример #8
0
        private static bool IsUnspendable(ScriptPubKey scriptPubKey)
        {
            if (scriptPubKey.length >= 1 &&
                scriptPubKey[0] == (byte)OpCode.OP_RETURN)
            {
                if (scriptPubKey.length == 1)
                {
                    return(true);
                }

                byte pushValue = scriptPubKey[1];
                if (scriptPubKey.length == pushValue + 2)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #9
0
        public static TxMessage Create(
            string previousTransactionIdHex,
            string senderBitcoinAddress,
            string senderPrivateKeyHex,
            string recipientBitcoinAddress,
            double transactionAmountInBtc)
        {
            var txMessage = new TxMessage();

            var senderPrivateKey = StringHelper.HexStringToByteArray(senderPrivateKeyHex);

            var scriptPubKeyPreviousTransaction = new ScriptPubKey(
                BitcoinHelper.Create160BitPublicKeyFromBitcoinAddress(senderBitcoinAddress)
                );

            var scriptPubKeyCurrentTransaction = new ScriptPubKey(
                BitcoinHelper.Create160BitPublicKeyFromBitcoinAddress(recipientBitcoinAddress)
                );

            var txInOutPoint = new OutPoint(
                StringHelper.HexStringToByteArray(previousTransactionIdHex), 0
                );

            txMessage.TxInsCount = new VarInt(1);
            txMessage.TxIns      = new List <TxIn>
            {
                new TxIn(txInOutPoint, scriptPubKeyPreviousTransaction.ToBytes())
            };

            txMessage.TxOutsCount = new VarInt(1);
            txMessage.TxOuts      = new List <TxOut>
            {
                new TxOut(
                    Convert.ToUInt64(transactionAmountInBtc * 100000000),
                    scriptPubKeyCurrentTransaction
                    )
            };

            txMessage.Sign(senderPrivateKey);

            return(txMessage);
        }
Пример #10
0
        public static byte[] FromScriptPubKey(ScriptPubKey scriptPubKey, TxnType txnType)
        {
            byte[] ret;

            switch (txnType)
            {
            case TxnType.P2SH:
                ret = new byte[scriptPubKey[1]];
                Array.Copy(scriptPubKey.scriptPubKey, 2, ret, 0, scriptPubKey[1]);
                break;

            case TxnType.P2WPKH:
                ret = new byte[scriptPubKey[1]];
                Array.Copy(scriptPubKey.scriptPubKey, 2, ret, 0, scriptPubKey[1]);
                break;

            case TxnType.P2WSH:
                ret = new byte[scriptPubKey[1]];
                Array.Copy(scriptPubKey.scriptPubKey, 2, ret, 0, scriptPubKey[1]);
                break;

            case TxnType.P2PKH:
                ret = new byte[scriptPubKey[2]];
                Array.Copy(scriptPubKey.scriptPubKey, 3, ret, 0, scriptPubKey[2]);
                break;

            case TxnType.P2PK:
                ret = new byte[scriptPubKey[0]];
                Array.Copy(scriptPubKey.scriptPubKey, 1, ret, 0, scriptPubKey[0]);
                break;

            default:
                ret = null;
                break;
            }

            return(ret);
        }
Пример #11
0
        private static bool MatchRawMultiSig(ScriptPubKey scriptPubKey)
        {
            if (scriptPubKey.length < 1 ||
                scriptPubKey[scriptPubKey.length - 1] != (byte)OpCode.OP_CHECKMULTISIG)
            {
                return(false);
            }

            if (scriptPubKey[0] < (byte)OpCode.OP_1 || scriptPubKey[0] > (byte)OpCode.OP_16)
            {
                return(false);
            }

            byte nRequiredKeys = DecodeOpN((byte)scriptPubKey[0]);
            byte nTotalKeys    = DecodeOpN((byte)scriptPubKey[scriptPubKey.length - 1]);

            if (nRequiredKeys > nTotalKeys)
            {
                return(false);
            }

            int pushByteIndex = 1;
            int pushSize;

            for (int i = 0; i < nTotalKeys; ++i)
            {
                pushSize = scriptPubKey[pushByteIndex];
                if (pushSize != (int)PubKeySize.Compressed ||
                    pushSize != (int)PubKeySize.Uncompressed)
                {
                    return(false);
                }
                pushByteIndex += pushSize + 1;
            }

            return(true);
        }
Пример #12
0
        private static bool IsWitnessProgram(ScriptPubKey scriptPubKey,
                                             ref int witnessVersion, ref byte[] witnessProgram)
        {
            if (scriptPubKey.length < 4 || scriptPubKey.length > 42)
            {
                return(false);
            }

            if (scriptPubKey[0] != (byte)OpCode.OP_0 &&
                (scriptPubKey[0] < (byte)OpCode.OP_1) || scriptPubKey[0] > (byte)OpCode.OP_16)
            {
                return(false);
            }

            if (scriptPubKey.length == scriptPubKey[1] + 2)
            {
                witnessVersion = DecodeOpN(scriptPubKey[0]);
                witnessProgram = new byte[scriptPubKey[1]];
                Array.Copy(scriptPubKey.scriptPubKey, 2, witnessProgram, 0, scriptPubKey[1]);
                return(true);
            }

            return(false);
        }
Пример #13
0
 public void ReadStream(BinaryReader br)
 {
     value = br.ReadUInt64();
     VarInt.Decode(br, out firstVarIntByteScriptLength, out outputScriptLength);
     scriptPubKey = new ScriptPubKey(br, outputScriptLength);
 }
Пример #14
0
        public Transaction(JObject json, List <Vin> _vin)//summary
        {
            txid = json["result"]["txid"].ToString();
            hash = json["result"]["hash"].ToString();

            int     br            = json["result"]["vout"].Count();
            decimal ukupnoIzlaztr = 0;

            vout = new List <Vout>();

            foreach (int i in Enumerable.Range(0, br))
            {
                var     s     = json["result"]["vout"][i]["value"].ToString();
                decimal value = Decimal.Round(decimal.Parse(json["result"]["vout"][i]["value"].ToString(), System.Globalization.NumberStyles.Any), 8, MidpointRounding.AwayFromZero);



                if (value > 0)
                {
                    ukupnoIzlaztr += value;

                    int           bradresa = json["result"]["vout"][i]["scriptPubKey"]["addresses"].Count();
                    List <string> a        = new List <string>();
                    foreach (int j in Enumerable.Range(0, bradresa))
                    {
                        string address = json["result"]["vout"][i]["scriptPubKey"]["addresses"][j].ToString();
                        a.Add(address);
                    }


                    Vout v = new Vout();
                    v.value = value;
                    ScriptPubKey spk = new ScriptPubKey();
                    spk.addresses = new List <string>();
                    foreach (string add in a)
                    {
                        spk.addresses.Add(add);
                    }
                    v.scriptPubKey = spk;

                    vout.Add(v);
                }
            }
            ukupnoIzlaz = ukupnoIzlaztr;


            decimal ukupnoUlaztr = 0;

            vin = _vin;
            int br2 = _vin.Count();

            foreach (int i in Enumerable.Range(0, br2))
            {
                ukupnoUlaztr += _vin[i].value;
            }

            ukupnoUlaz = ukupnoUlaztr;

            if (_vin[0].coinbase == true)
            {
                fee = 0;
            }
            else
            {
                fee = ukupnoUlaz - ukupnoIzlaz;
            }
        }
Пример #15
0
        public Transaction(JObject json, Block block, List <Vin> _vin) //details
        {
            txid   = json["result"]["txid"].ToString();
            hash   = json["result"]["hash"].ToString();
            size   = Int32.Parse(json["result"]["size"].ToString());
            weight = Int32.Parse(json["result"]["weight"].ToString());


            if (block != null)
            {
                time            = block.time;
                status          = "Confirmed";
                includedInBlock = "Yes";
                blockh          = block.height;
                conf            = block.confirmations;
            }
            else
            {
                status          = "Unconfirmed";
                includedInBlock = "No";
                conf            = 0;
            }
            int     br            = json["result"]["vout"].Count();
            decimal ukupnoIzlaztr = 0;

            vout = new List <Vout>();

            foreach (int i in Enumerable.Range(0, br))
            {
                var     s     = json["result"]["vout"][i]["value"].ToString();
                decimal value = Decimal.Round(decimal.Parse(json["result"]["vout"][i]["value"].ToString(), System.Globalization.NumberStyles.Any), 8, MidpointRounding.AwayFromZero);



                if (value > 0)
                {
                    ukupnoIzlaztr += value;

                    int           bradresa = json["result"]["vout"][i]["scriptPubKey"]["addresses"].Count();
                    List <string> a        = new List <string>();
                    foreach (int j in Enumerable.Range(0, bradresa))
                    {
                        string address = json["result"]["vout"][i]["scriptPubKey"]["addresses"][j].ToString();
                        a.Add(address);
                    }

                    Vout v = new Vout();
                    v.value = value;
                    ScriptPubKey spk = new ScriptPubKey();
                    spk.addresses = new List <string>();
                    foreach (string add in a)
                    {
                        spk.addresses.Add(add);
                    }
                    v.scriptPubKey = spk;
                    v.n            = Int32.Parse(json["result"]["vout"][i]["n"].ToString());

                    vout.Add(v);
                }
            }
            ukupnoIzlaz = ukupnoIzlaztr;


            //vin
            decimal ukupnoUlaztr = 0;

            vin = _vin;
            int br2 = _vin.Count();

            foreach (int i in Enumerable.Range(0, br2))
            {
                ukupnoUlaztr += _vin[i].value;
            }

            ukupnoUlaz = ukupnoUlaztr;

            if (_vin[0].coinbase == true)
            {
                fee = 0;
            }
            else
            {
                fee = ukupnoUlaz - ukupnoIzlaz;
            }
        }
 public override string ToString()
 {
     return($"{Value.Satoshi}{Separator}{ScriptPubKey.ToHex()}{Separator}{PubkeyType}");
 }
Пример #17
0
 private bool IsTo(KeyId keyId)
 {
     return(ScriptPubKey.GetDestination() == keyId);
 }
Пример #18
0
 public Coin ToCoin()
 {
     return(new Coin(new OutPoint(uint256.Parse(TransactionHash), N), new TxOut(Amount, ScriptPubKey.ToScript())));
 }
Пример #19
0
 public virtual bool CanGetScriptCode(Network network)
 {
     return(_OverrideScriptCode != null || !ScriptPubKey.IsPayToScriptHash(network) && !PayToWitScriptHashTemplate.Instance.CheckScriptPubKey(network, ScriptPubKey));
 }
Пример #20
0
        public override string ToString()
        {
            String address = Address?.ToString() ?? ScriptPubKey.ToHex();

            return($"{address} received a transaction");
        }
Пример #21
0
 public IEnumerable <byte[]> GetTrackedData()
 {
     return(ScriptPubKey.ToOps().Select(o => o.PushData).Where(o => o != null));
 }