Exemplo n.º 1
0
    public void SetUsedLabelIncludePrivateFunds()
    {
        var selection = new LabelSelectionViewModel(Money.Parse("1.5"));

        var pockets = new List <Pocket>();

        pockets.AddPocket(1.0M, out _, "Dan");

        var privateCoins = new[]
        {
            BitcoinFactory.CreateSmartCoin(LabelTestExtensions.NewKey(anonymitySet: 999), 0.5m),
            BitcoinFactory.CreateSmartCoin(LabelTestExtensions.NewKey(anonymitySet: 999), 0.5m),
        };
        var coinsView = new CoinsView(privateCoins.ToArray());
        var pocket    = new Pocket((SmartLabel.Empty, coinsView));

        pockets.Add(pocket);

        selection.Reset(pockets.ToArray());

        var output = selection.AutoSelectPockets("Dan");

        selection.SetUsedLabel(output.SelectMany(x => x.Coins), privateThreshold: 10);

        Assert.True(selection.EnoughSelected);
    }
Exemplo n.º 2
0
        public void test_Get()
        {
            //CBasicKeyStore keystore;
            //CCoinsView coinsDummy;
            CoinsView coins = new CoinsView();                         //(coinsDummy);

            Transaction[] dummyTransactions = SetupDummyInputs(coins); //(keystore, coins);

            Transaction t1 = new Transaction();

            t1.Inputs.AddRange(Enumerable.Range(0, 3).Select(_ => new TxIn()));
            t1.Inputs[0].PrevOut.Hash = dummyTransactions[0].GetHash();
            t1.Inputs[0].PrevOut.N    = 1;
            t1.Inputs[0].ScriptSig   += new byte[65];
            t1.Inputs[1].PrevOut.Hash = dummyTransactions[1].GetHash();
            t1.Inputs[1].PrevOut.N    = 0;
            t1.Inputs[1].ScriptSig    = t1.Inputs[1].ScriptSig + new byte[65] + Enumerable.Range(0, 33).Select(_ => (byte)4);
            t1.Inputs[2].PrevOut.Hash = dummyTransactions[1].GetHash();
            t1.Inputs[2].PrevOut.N    = 1;
            t1.Inputs[2].ScriptSig    = t1.Inputs[2].ScriptSig + new byte[65] + Enumerable.Range(0, 33).Select(_ => (byte)4);
            t1.Outputs.AddRange(Enumerable.Range(0, 2).Select(_ => new TxOut()));
            t1.Outputs[0].Value         = 90 * Money.CENT;
            t1.Outputs[0].ScriptPubKey += OpcodeType.OP_1;

            Assert.True(StandardScripts.AreInputsStandard(t1, coins));
            //Assert.Equal(coins.GetValueIn(t1), (50+21+22)*Money.CENT);

            //// Adding extra junk to the scriptSig should make it non-standard:
            t1.Inputs[0].ScriptSig += OpcodeType.OP_11;
            Assert.True(!StandardScripts.AreInputsStandard(t1, coins));

            //// ... as should not having enough:
            t1.Inputs[0].ScriptSig = new Script();
            Assert.True(!StandardScripts.AreInputsStandard(t1, coins));
        }
Exemplo n.º 3
0
        private Transaction[] SetupDummyInputs(CoinsView coinsRet)
        {
            Transaction[] dummyTransactions = Enumerable.Range(0, 2).Select(_ => new Transaction()).ToArray();

            // Add some keys to the keystore:
            Key[] key = Enumerable.Range(0, 4).Select((_, i) => new Key(i % 2 != 0)).ToArray();


            // Create some dummy input transactions
            dummyTransactions[0].Outputs.AddRange(Enumerable.Range(0, 2).Select(_ => new TxOut()));
            dummyTransactions[0].Outputs[0].Value        = 11 * Money.CENT;
            dummyTransactions[0].Outputs[0].ScriptPubKey = dummyTransactions[0].Outputs[0].ScriptPubKey + key[0].PubKey.ToBytes() + OpcodeType.OP_CHECKSIG;
            dummyTransactions[0].Outputs[1].Value        = 50 * Money.CENT;
            dummyTransactions[0].Outputs[1].ScriptPubKey = dummyTransactions[0].Outputs[1].ScriptPubKey + key[1].PubKey.ToBytes() + OpcodeType.OP_CHECKSIG;
            coinsRet.AddTransaction(dummyTransactions[0], 0);


            dummyTransactions[1].Outputs.AddRange(Enumerable.Range(0, 2).Select(_ => new TxOut()));
            dummyTransactions[1].Outputs[0].Value        = 21 * Money.CENT;
            dummyTransactions[1].Outputs[0].ScriptPubKey = StandardScripts.PayToAddress(key[2].PubKey.GetAddress(Network.Main));
            dummyTransactions[1].Outputs[1].Value        = 22 * Money.CENT;
            dummyTransactions[1].Outputs[1].ScriptPubKey = StandardScripts.PayToAddress(key[3].PubKey.GetAddress(Network.Main));
            coinsRet.AddTransaction(dummyTransactions[1], 0);


            return(dummyTransactions);
        }
Exemplo n.º 4
0
        public void test_Get()
        {
            //CBasicKeyStore keystore;
            //CCoinsView coinsDummy;
            CoinsView coins = new CoinsView();//(coinsDummy);
            Transaction[] dummyTransactions = SetupDummyInputs(coins);//(keystore, coins);

            Transaction t1 = new Transaction();
            t1.Inputs.AddRange(Enumerable.Range(0, 3).Select(_ => new TxIn()));
            t1.Inputs[0].PrevOut.Hash = dummyTransactions[0].GetHash();
            t1.Inputs[0].PrevOut.N = 1;
            t1.Inputs[0].ScriptSig += new byte[65];
            t1.Inputs[1].PrevOut.Hash = dummyTransactions[1].GetHash();
            t1.Inputs[1].PrevOut.N = 0;
            t1.Inputs[1].ScriptSig = t1.Inputs[1].ScriptSig + new byte[65] + Enumerable.Range(0, 33).Select(_ => (byte)4);
            t1.Inputs[2].PrevOut.Hash = dummyTransactions[1].GetHash();
            t1.Inputs[2].PrevOut.N = 1;
            t1.Inputs[2].ScriptSig = t1.Inputs[2].ScriptSig + new byte[65] + Enumerable.Range(0, 33).Select(_ => (byte)4);
            t1.Outputs.AddRange(Enumerable.Range(0, 2).Select(_ => new TxOut()));
            t1.Outputs[0].Value = 90 * Money.CENT;
            t1.Outputs[0].ScriptPubKey += OpcodeType.OP_1;

            Assert.True(StandardScripts.AreInputsStandard(t1, coins));
            //Assert.Equal(coins.GetValueIn(t1), (50+21+22)*Money.CENT);

            //// Adding extra junk to the scriptSig should make it non-standard:
            t1.Inputs[0].ScriptSig += OpcodeType.OP_11;
            Assert.True(!StandardScripts.AreInputsStandard(t1, coins));

            //// ... as should not having enough:
            t1.Inputs[0].ScriptSig = new Script();
            Assert.True(!StandardScripts.AreInputsStandard(t1, coins));
        }
Exemplo n.º 5
0
        public void test_IsStandard()
        {
            var coins = new CoinsView();

            Transaction[] dummyTransactions = SetupDummyInputs(coins);

            Transaction t = new Transaction();

            t.Inputs.Add(new TxIn());
            t.Inputs[0].PrevOut.Hash = dummyTransactions[0].GetHash();
            t.Inputs[0].PrevOut.N    = 1;
            t.Inputs[0].ScriptSig    = new Script(Op.GetPushOp(new byte[65]));
            t.Outputs.Add(new TxOut());
            t.Outputs[0].Value = 90 * Money.CENT;
            Key key       = new Key(true);
            var payToHash = new PayToPubkeyHashTemplate();

            t.Outputs[0].ScriptPubKey = payToHash.GenerateScriptPubKey(key.PubKey.ID);

            Assert.True(StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].Value = 501;             //dust
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].Value = 601;             // not dust
            Assert.True(StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_1;
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            // 40-byte TX_NULL_DATA (standard)
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            Assert.True(StandardScripts.IsStandardTransaction(t));

            // 41-byte TX_NULL_DATA (non-standard)
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            // TX_NULL_DATA w/o PUSHDATA
            t.Outputs.Clear();
            t.Outputs.Add(new TxOut());
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            Assert.True(StandardScripts.IsStandardTransaction(t));

            // Only one TX_NULL_DATA permitted in all cases
            t.Outputs.Add(new TxOut());
            t.Outputs.Add(new TxOut());
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            t.Outputs[1].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            t.Outputs[1].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            t.Outputs[1].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            Assert.True(!StandardScripts.IsStandardTransaction(t));
        }
Exemplo n.º 6
0
    public static void AddPocket(this List <Pocket> pockets, decimal amount, out Pocket pocket, params string[] labels)
    {
        var label     = new SmartLabel(labels);
        var coinsView = new CoinsView(new[] { BitcoinFactory.CreateSmartCoin(NewKey(label), amount) });

        pocket = new Pocket((label, coinsView));
        pockets.Add(pocket);
    }
 private void HideAllItemViews()
 {
     BoxView.Hide();
     //BrawlerView.Hide();
     BrawlerPowerView.Hide();
     CoinsView.Hide();
     GemsView.Hide();
     TicketsView.Hide();
 }
Exemplo n.º 8
0
    public static Pocket CreateSingleCoinPocket(decimal amount, string label = "", int anonSet = 0)
    {
        var coins = new[]
        {
            CreateCoin(amount, label, anonSet)
        };

        var coinsView = new CoinsView(coins.ToArray());
        var pocket    = new Pocket((label, coinsView));

        return(pocket);
    }
Exemplo n.º 9
0
		//
		// Check transaction inputs, and make sure any
		// pay-to-script-hash transactions are evaluating IsStandard scripts
		//
		// Why bother? To avoid denial-of-service attacks; an attacker
		// can submit a standard HASH... OP_EQUAL transaction,
		// which will get accepted into blocks. The redemption
		// script can be anything; an attacker could use a very
		// expensive-to-check-upon-redemption script like:
		//   DUP CHECKSIG DROP ... repeated 100 times... OP_1
		//
		public static bool AreInputsStandard(Transaction tx, CoinsView coinsView)
		{
			if(tx.IsCoinBase)
				return true; // Coinbases don't use vin normally

			for(int i = 0 ; i < tx.Inputs.Count ; i++)
			{
				TxOut prev = coinsView.GetOutputFor(tx.Inputs[i]);
				if(prev == null)
					return false;
				if(!IsStandardScriptSig(tx.Inputs[i].ScriptSig, prev.ScriptPubKey))
					return false;
			}

			return true;
		}
        public void CanCompressMessageAsScript()
        {
            var coinsView = new CoinsView(this.network);

            Transaction[] dummyTransactions = SetupDummyInputs(coinsView);

            string metadata = "{\"compression\": \"gzip\", \"encryption\": \"none\", \"rewardAddress\": \"\", signatureType: \"ECDSA\", \"messageHash\": \"\", \"messageSignature\": \"\"}";

            byte[] uncompressedMetadata = System.Text.Encoding.UTF8.GetBytes(metadata);
            byte[] compressedMetadata   = CompressByteArray(uncompressedMetadata);

            string message = "Bitcoin is a worldwide cryptocurrency and payment system which is the first decentralized digital currency, as the system works without a central repository or single administrator. It was invented by an unknown person or group of people under the name Satoshi Nakamoto and released as open-source software in 2009. The system is peer-to-peer, and transactions take place between users directly, without an intermediary. These transactions are verified by network nodes and recorded in a public distributed ledger called a blockchain. Bitcoins are created as a reward for a process known as mining.They can be exchanged for other currencies, products, and services.As of February 2015, over 100, 000 merchants and vendors accepted bitcoin as payment. Bitcoin can also be held as an investment.According to research produced by Cambridge University in 2017, there are 2.9 to 5.8 million unique users using a cryptocurrency wallet, most of them using bitcoin." + OpcodeType.OP_RETURN;

            byte[] uncompressedMessage = System.Text.Encoding.UTF8.GetBytes(message);
            byte[] compressedMessage   = CompressByteArray(uncompressedMessage);

            byte[] header         = System.Text.Encoding.UTF8.GetBytes("TWS");
            byte   version        = 1;
            byte   compression    = 1;
            byte   checksumType   = 0;
            ushort metadataLength = (ushort)compressedMetadata.Length;
            ushort messageLength  = (ushort)compressedMessage.Length;

            List <byte> pushData = new List <byte>();

            pushData.AddRange(header);
            pushData.Add(version);
            pushData.Add(compression);
            pushData.Add(checksumType);
            pushData.AddRange(BitConverter.GetBytes(metadataLength));
            pushData.AddRange(BitConverter.GetBytes(messageLength));
            pushData.AddRange(compressedMetadata);
            pushData.AddRange(compressedMessage);
            var script = new Script(pushData);

            if (script.Length > 16505)
            {
                throw new Exception("Push data can't be bigger than 16505 bytes.");
            }

            Transaction t = BuildMessageTransaction(script.ToBytes(), dummyTransactions[0].GetHash());

            coinsView.AddTransaction(this.network.Consensus, dummyTransactions[1], 0);

            AssertCompressed(script, 696);
        }
Exemplo n.º 11
0
        public static bool AreInputsStandard(Transaction tx, CoinsView coinsView)
        {
            if (tx.IsCoinBase)
            {
                return(true);                // Coinbases don't use vin normally
            }
            foreach (var input in tx.Inputs)
            {
                TxOut prev = coinsView.GetOutputFor(input);
                if (prev == null)
                {
                    return(false);
                }
                if (!IsStandardScriptSig(input.ScriptSig, prev.ScriptPubKey))
                {
                    return(false);
                }
            }

            return(true);
        }
 public override bool AreInputsStandard(Network network, Transaction tx, CoinsView coinsView)
 {
     return(base.AreInputsStandard(network, tx, coinsView));
 }
Exemplo n.º 13
0
 public virtual bool AreInputsStandard(Network network, Transaction tx, CoinsView coinsView)
 {
     return(StandardScripts.AreInputsStandard(network, tx, coinsView));
 }
Exemplo n.º 14
0
        private Transaction[] SetupDummyInputs(CoinsView coinsRet)
        {
            Transaction[] dummyTransactions = Enumerable.Range(0, 2).Select(_ => new Transaction()).ToArray();

            // Add some keys to the keystore:
            Key[] key = Enumerable.Range(0, 4).Select((_, i) => new Key(i % 2 != 0)).ToArray();

            // Create some dummy input transactions
            dummyTransactions[0].Outputs.AddRange(Enumerable.Range(0, 2).Select(_ => new TxOut()));
            dummyTransactions[0].Outputs[0].Value = 11 * Money.CENT;
            dummyTransactions[0].Outputs[0].ScriptPubKey = dummyTransactions[0].Outputs[0].ScriptPubKey + key[0].PubKey.ToBytes() + OpcodeType.OP_CHECKSIG;
            dummyTransactions[0].Outputs[1].Value = 50 * Money.CENT;
            dummyTransactions[0].Outputs[1].ScriptPubKey = dummyTransactions[0].Outputs[1].ScriptPubKey + key[1].PubKey.ToBytes() + OpcodeType.OP_CHECKSIG;
            coinsRet.AddTransaction(dummyTransactions[0],0);

            dummyTransactions[1].Outputs.AddRange(Enumerable.Range(0, 2).Select(_ => new TxOut()));
            dummyTransactions[1].Outputs[0].Value = 21 * Money.CENT;
            dummyTransactions[1].Outputs[0].ScriptPubKey = StandardScripts.PayToAddress(key[2].PubKey.GetAddress(Network.Main));
            dummyTransactions[1].Outputs[1].Value = 22 * Money.CENT;
            dummyTransactions[1].Outputs[1].ScriptPubKey = StandardScripts.PayToAddress(key[3].PubKey.GetAddress(Network.Main));
            coinsRet.AddTransaction(dummyTransactions[1],0);

            return dummyTransactions;
        }
Exemplo n.º 15
0
        public void test_IsStandard()
        {
            var coins = new CoinsView();
            Transaction[] dummyTransactions = SetupDummyInputs(coins);

            Transaction t = new Transaction();
            t.Inputs.Add(new TxIn());
            t.Inputs[0].PrevOut.Hash = dummyTransactions[0].GetHash();
            t.Inputs[0].PrevOut.N = 1;
            t.Inputs[0].ScriptSig = new Script(Op.GetPushOp(new byte[65]));
            t.Outputs.Add(new TxOut());
            t.Outputs[0].Value = 90 * Money.CENT;
            Key key = new Key(true);
            var payToHash = new PayToPubkeyHashTemplate();
            t.Outputs[0].ScriptPubKey = payToHash.GenerateScriptPubKey(key.PubKey.ID);

            Assert.True(StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].Value = 501; //dust
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].Value = 601; // not dust
            Assert.True(StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_1;
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            // 40-byte TX_NULL_DATA (standard)
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            Assert.True(StandardScripts.IsStandardTransaction(t));

            // 41-byte TX_NULL_DATA (non-standard)
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            // TX_NULL_DATA w/o PUSHDATA
            t.Outputs.Clear();
            t.Outputs.Add(new TxOut());
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            Assert.True(StandardScripts.IsStandardTransaction(t));

            // Only one TX_NULL_DATA permitted in all cases
            t.Outputs.Add(new TxOut());
            t.Outputs.Add(new TxOut());
            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            t.Outputs[1].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN + ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
            t.Outputs[1].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            Assert.True(!StandardScripts.IsStandardTransaction(t));

            t.Outputs[0].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            t.Outputs[1].ScriptPubKey = new Script() + OpcodeType.OP_RETURN;
            Assert.True(!StandardScripts.IsStandardTransaction(t));
        }