Esempio n. 1
0
        public IBitcoinBasedTransaction CreatePaymentTx(BitcoinBasedCurrency currency)
        {
            var       initTx = BitcoinBasedCommon.CreateFakeTx(currency, Common.Alice.PubKey, 1_0000_0000, 1_0000_0000);
            const int amount = 1_0000_0000;
            const int fee    = 1_0000;
            const int change = 9999_0000;

            var tx = currency.CreatePaymentTx(
                unspentOutputs: initTx.Outputs,
                destinationAddress: Common.Bob.PubKey.GetAddress(currency),
                changeAddress: Common.Alice.PubKey.GetAddress(currency),
                amount: amount,
                fee: fee,
                lockTime: DateTimeOffset.MinValue);

            Assert.NotNull(tx);
            Assert.True(tx.Check());
            Assert.NotNull(tx.Outputs.FirstOrDefault(o => o.Value == amount));
            Assert.NotNull(tx.Outputs.FirstOrDefault(o => o.Value == change));
            Assert.Equal(tx.Outputs.First(o => o.Value == amount).DestinationAddress(currency), Common.BobAddress(currency));
            Assert.Equal(tx.Outputs.First(o => o.Value == change).DestinationAddress(currency), Common.AliceAddress(currency));
            Assert.Equal(initTx.TotalOut - fee, tx.TotalOut);
            Assert.Equal(fee, tx.GetFee(initTx.Outputs));

            return(tx);
        }
Esempio n. 2
0
        public IBitcoinBasedTransaction CreateSegwitPaymentTx(BitcoinBasedCurrency currency)
        {
            var       initTx = BitcoinBasedCommon.CreateFakeTx(currency, Common.Alice.PubKey, 1_0000_0000, 1_0000_0000);
            const int amount = 1_0000_0000;
            const int fee    = 1_0000;
            const int change = 9999_0000;

            var tx = BitcoinBasedCommon.CreateSegwitPaymentTx(
                currency: currency,
                outputs: initTx.Outputs,
                from: Common.Alice.PubKey,
                to: Common.Bob.PubKey,
                amount: amount,
                fee: fee);

            Assert.NotNull(tx);
            Assert.True(tx.Check());
            Assert.NotNull(tx.Outputs.FirstOrDefault(o => o.Value == amount));
            Assert.NotNull(tx.Outputs.FirstOrDefault(o => o.Value == change));
            Assert.Equal(tx.Outputs.First(o => o.Value == amount).DestinationAddress(currency), Common.BobSegwitAddress(currency));
            Assert.Equal(initTx.TotalOut - fee, tx.TotalOut);
            Assert.Equal(fee, tx.GetFee(initTx.Outputs));

            return(tx);
        }
Esempio n. 3
0
        public (IBitcoinBasedTransaction, byte[]) CreateHtlcP2PkhScriptSwapPaymentTx(BitcoinBasedCurrency currency)
        {
            var       initTx = BitcoinBasedCommon.CreateFakeTx(currency, Common.Alice.PubKey, 1_0000_0000, 1_0000_0000);
            const int amount = 1_0000_0000;
            const int fee    = 1_0000;
            //const int change = 9999_0000;

            var tx = currency.CreateHtlcP2PkhScriptSwapPaymentTx(
                unspentOutputs: initTx.Outputs,
                aliceRefundAddress: Common.Alice.PubKey.GetAddress(currency),
                bobAddress: Common.Bob.PubKey.GetAddress(currency),
                lockTime: DateTimeOffset.UtcNow.AddHours(1),
                secretHash: Common.SecretHash,
                secretSize: Common.Secret.Length,
                amount: amount,
                fee: fee,
                redeemScript: out var redeemScript);

            Assert.NotNull(tx);
            Assert.NotNull(redeemScript);
            Assert.True(tx.Check());
            Assert.Equal(initTx.TotalOut - fee, tx.TotalOut);
            Assert.Equal(fee, tx.GetFee(initTx.Outputs));

            return(tx, redeemScript);
        }
Esempio n. 4
0
        public IBitcoinBasedTransaction SignHtlcP2PkhSwapPaymentTx(BitcoinBasedCurrency currency)
        {
            var initTx = BitcoinBasedCommon.CreateFakeTx(currency, Common.Alice.PubKey, 1_0000_0000, 1_0000_0000);
            var tx     = CreateHtlcP2PkhSwapPaymentTx(currency);

            tx.Sign(Common.Alice, initTx.Outputs);

            Assert.True(tx.Verify(initTx.Outputs, checkScriptPubKey: false));

            return(tx);
        }
Esempio n. 5
0
        public IBitcoinBasedTransaction SignSegwitPaymentTx(BitcoinBasedCurrency currency)
        {
            var initTx = BitcoinBasedCommon.CreateFakeTx(currency, Common.Alice.PubKey, 1_0000_0000, 1_0000_0000);
            var tx     = CreateSegwitPaymentTx(currency);

            tx.Sign(Common.Alice, initTx.Outputs);

            Assert.True(tx.Verify(initTx.Outputs));

            return(tx);
        }
Esempio n. 6
0
        public (IBitcoinBasedTransaction, byte[]) SignHtlcP2PkhScriptSwapPaymentTx(BitcoinBasedCurrency currency)
        {
            var initTx = BitcoinBasedCommon.CreateFakeTx(currency, Common.Alice.PubKey, 1_0000_0000, 1_0000_0000);

            var(tx, redeemScript) = CreateHtlcP2PkhScriptSwapPaymentTx(currency);

            tx.Sign(Common.Alice, initTx.Outputs);

            Assert.True(tx.Verify(initTx.Outputs));

            return(tx, redeemScript);
        }
        public void TryVerifyPartyPaymentTxTest(
            ICurrencies currencies,
            string currency,
            string symbol,
            Side side)
        {
            var currencyConfig = currencies.Get <BitcoinBasedCurrency>(currency);

            const int initAmount = 1_0000_0000;
            var       initTx     = BitcoinBasedCommon.CreateFakeTx(currencyConfig, Common.Alice.PubKey, initAmount);

            var refundLockTimeInSec = 60 * 60;
            var utcNow   = DateTimeOffset.UtcNow;
            var lockTime = utcNow.AddSeconds(refundLockTimeInSec);

            var tx = currencyConfig.CreateHtlcP2PkhScriptSwapPaymentTx(
                unspentOutputs: initTx.Outputs,
                aliceRefundAddress: Common.Alice.PubKey.GetAddress(currencyConfig),
                bobAddress: Common.Bob.PubKey.GetAddress(currencyConfig),
                lockTime: lockTime,
                secretHash: Common.SecretHash,
                secretSize: Common.Secret.Length,
                amount: 9999_0000,
                fee: 1_0000,
                redeemScript: out var redeemScript);

            tx.Sign(Common.Alice, initTx.Outputs);

            var swap = new Swap
            {
                PartyRedeemScript = Convert.ToBase64String(redeemScript),
                ToAddress         = Common.Bob.PubKey.GetAddress(currencyConfig),
                TimeStamp         = utcNow.UtcDateTime,
                Symbol            = symbol,
                Side  = side,
                Price = 1,
                Qty   = 0.9999m
            };

            var result = BitcoinBasedTransactionVerifier.TryVerifyPartyPaymentTx(
                tx: tx,
                swap: swap,
                currencies: currencies,
                secretHash: Common.SecretHash,
                refundLockTime: refundLockTimeInSec,
                error: out var errors);

            Assert.True(result);
            Assert.Null(errors);
        }
Esempio n. 8
0
        public IBitcoinBasedTransaction CreateP2PkSwapPaymentTx(BitcoinBasedCurrency currency)
        {
            var       initTx = BitcoinBasedCommon.CreateFakeTx(currency, Common.Alice.PubKey, 1_0000_0000, 1_0000_0000);
            const int amount = 1_0000_0000;
            const int fee    = 1_0000;
            //const int change = 9999_0000;

            var tx = currency.CreateP2PkSwapPaymentTx(
                unspentOutputs: initTx.Outputs,
                aliceRefundPubKey: Common.Alice.PubKey.ToBytes(),
                bobRefundPubKey: Common.Bob.PubKey.ToBytes(),
                bobDestinationPubKey: Common.Bob.PubKey.ToBytes(),
                secretHash: Common.SecretHash,
                amount: amount,
                fee: fee);

            Assert.NotNull(tx);
            Assert.True(tx.Check());
            Assert.Equal(initTx.TotalOut - fee, tx.TotalOut);
            Assert.Equal(fee, tx.GetFee(initTx.Outputs));

            return(tx);
        }