Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public IBitcoinBasedTransaction SignP2PkSwapRefundTx(BitcoinBasedCurrency currency)
        {
            const int paymentQty = 1_0000_0000;

            var paymentTx        = CreateP2PkSwapPaymentTx(currency);
            var paymentTxOutputs = paymentTx.Outputs.Where(o => o.Value == paymentQty).ToArray();

            var       lockTime = DateTimeOffset.Now.AddHours(12);
            const int amount   = 9999_0000;
            const int fee      = 1_0000;
            // change = 0;

            var refundTx = BitcoinBasedCommon.CreatePaymentTx(
                currency: Common.BtcTestNet,
                outputs: paymentTxOutputs,
                from: Common.Alice.PubKey,
                to: Common.Alice.PubKey,
                amount: amount,
                fee: fee,
                lockTime: lockTime
                );

            var sigHash = new uint256(refundTx.GetSignatureHash(paymentTxOutputs.First()));

            var aliceSign    = Common.Alice.Sign(sigHash, SigHash.All);
            var bobSign      = Common.Bob.Sign(sigHash, SigHash.All);
            var refundScript = BitcoinBasedSwapTemplate.GenerateSwapRefund(aliceSign, bobSign);

            refundTx.NonStandardSign(refundScript, paymentTxOutputs.First());

            Assert.True(refundTx.Verify(paymentTxOutputs));

            return(refundTx);
        }
Beispiel #4
0
        public IBitcoinBasedTransaction CreateP2PkSwapRefundTx(BitcoinBasedCurrency currency)
        {
            const int paymentQty = 1_0000_0000;

            var paymentTx        = CreateP2PkSwapPaymentTx(currency);
            var paymentTxOutputs = paymentTx.Outputs
                                   .Where(o => o.Value == paymentQty)
                                   .ToArray();
            var paymentTxTotal = paymentTxOutputs.Aggregate(0L, (s, output) => s + output.Value);

            var       lockTime = DateTimeOffset.Now.AddHours(12);
            const int amount   = 9999_0000;
            const int fee      = 1_0000;
            // change = 0;

            var tx = BitcoinBasedCommon.CreatePaymentTx(
                currency: Common.BtcTestNet,
                outputs: paymentTxOutputs,
                from: Common.Alice.PubKey,
                to: Common.Alice.PubKey,
                amount: amount,
                fee: fee,
                lockTime: lockTime
                );

            Assert.True(tx.Check());
            Assert.Equal(paymentTxTotal - fee, tx.TotalOut);
            Assert.Equal(fee, tx.GetFee(paymentTxOutputs));

            return(tx);
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }
Beispiel #10
0
        public IBitcoinBasedTransaction SignHtlcP2PkhScriptSwapRefundTx(BitcoinBasedConfig currency)
        {
            const int paymentQty = 1_0000_0000;

            var(paymentTx, redeemScriptBytes) = CreateHtlcP2PkhScriptSwapPaymentTx(currency);
            var paymentTxOutputs = paymentTx.Outputs.Where(o => o.Value == paymentQty).ToArray();

            var       lockTime = DateTimeOffset.UtcNow.AddHours(1);
            const int amount   = 9999_0000;
            const int fee      = 1_0000;
            // change = 0;

            var redeemScript = new Script(redeemScriptBytes);

            var refundTx = BitcoinBasedCommon.CreatePaymentTx(
                currency: Common.BtcTestNet,
                outputs: paymentTxOutputs,
                from: Common.Alice.PubKey,
                to: Common.Alice.PubKey,
                amount: amount,
                fee: fee,
                lockTime: lockTime,
                knownRedeems: redeemScript
                );

            var sigHash = new uint256(refundTx.GetSignatureHash(redeemScript, paymentTxOutputs.First()));

            var aliceSign = Common.Alice.Sign(sigHash, SigHash.All);

            var refundScript = BitcoinBasedSwapTemplate.GenerateHtlcSwapRefundForP2Sh(
                aliceRefundSig: aliceSign.ToBytes(),
                aliceRefundPubKey: Common.Alice.PubKey.ToBytes(),
                redeemScript: redeemScriptBytes);

            refundTx.NonStandardSign(refundScript, paymentTxOutputs.First());

            Assert.True(refundTx.Verify(paymentTxOutputs, currency));

            return(refundTx);
        }
Beispiel #11
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);
        }