WalletTestBase.cs 2.48 KB
Newer Older
Maxim Bogdanov's avatar
Maxim Bogdanov committed
1 2 3
using System;
using System.Collections.Generic;
using NBitcoin;
4
using Stratis.Bitcoin.Tests.Common;
Maxim Bogdanov's avatar
Maxim Bogdanov committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

namespace Stratis.Bitcoin.Features.Wallet.Tests
{
    public class WalletTestBase
    {
        public static AccountRoot CreateAccountRoot(CoinType coinType)
        {
            return new AccountRoot()
            {
                Accounts = new List<HdAccount>(),
                CoinType = coinType
            };
        }

        public static AccountRoot CreateAccountRootWithHdAccountHavingAddresses(string accountName, CoinType coinType)
        {
            return new AccountRoot()
            {
                Accounts = new List<HdAccount> {
                    new HdAccount {
                        Name = accountName,
                        InternalAddresses = new List<HdAddress>
                        {
                            CreateAddress(false),
                        },
                        ExternalAddresses = new List<HdAddress>
                        {
                            CreateAddress(false),
                        }
                    }
                },
                CoinType = coinType
            };
        }

        public static HdAccount CreateAccount(string name)
        {
            return new HdAccount
            {
                Name = name,
                HdPath = "1/2/3/4/5",
            };
        }

        public static TransactionData CreateTransaction(uint256 id, Money amount, int? blockHeight, SpendingDetails spendingDetails = null, DateTimeOffset? creationTime = null)
        {
            if (creationTime == null)
            {
                creationTime = new DateTimeOffset(new DateTime(2017, 6, 23, 1, 2, 3));
            }

            return new TransactionData
            {
                Amount = amount,
                Id = id,
                CreationTime = creationTime.Value,
                BlockHeight = blockHeight,
                SpendingDetails = spendingDetails
            };
        }

        public static HdAddress CreateAddress(bool changeAddress = false)
        {
68
            string hdPath = "1/2/3/4/5";
Maxim Bogdanov's avatar
Maxim Bogdanov committed
69 70 71 72 73 74 75
            if (changeAddress)
            {
                hdPath = "1/2/3/4/1";
            }
            var key = new Key();
            var address = new HdAddress
            {
76
                Address = key.PubKey.GetAddress(KnownNetworks.Main).ToString(),
Maxim Bogdanov's avatar
Maxim Bogdanov committed
77 78 79 80 81 82 83 84
                HdPath = hdPath,
                ScriptPubKey = key.ScriptPubKey
            };

            return address;
        }
    }
}