public static Transaction SetupValidTransaction(Features.Wallet.Types.Wallet wallet, string password, HdAddress spendingAddress, Script destinationScript, HdAddress changeAddress, Money amount, Money fee) { TransactionOutputData spendingTransaction = wallet.walletStore.GetForAddress(spendingAddress.Address).ElementAt(0); spendingTransaction.Address = spendingAddress.Address; var coin = new Coin(spendingTransaction.Id, (uint)spendingTransaction.Index, spendingTransaction.Amount, spendingTransaction.ScriptPubKey); Key privateKey = Key.Parse(wallet.EncryptedSeed, password, wallet.Network); var builder = new TransactionBuilder(wallet.Network); Transaction tx = builder .AddCoins(new List <Coin> { coin }) .AddKeys(new ExtKey(privateKey, wallet.ChainCode).Derive(new KeyPath(spendingAddress.HdPath)).GetWif(wallet.Network)) .Send(destinationScript, amount) .SetChange(changeAddress.ScriptPubKey) .SendFees(fee) .BuildTransaction(true); if (!builder.Verify(tx)) { throw new WalletException("Could not build transaction, please make sure you entered the correct data."); } return(tx); }
public static (Features.Wallet.Types.Wallet wallet, ExtKey key) GenerateBlankWalletWithExtKey(string name, string password) { var mnemonic = new Mnemonic("grass industry beef stereo soap employ million leader frequent salmon crumble banana"); ExtKey extendedKey = mnemonic.DeriveExtKey(password); var walletFile = new Features.Wallet.Types.Wallet { Name = name, EncryptedSeed = extendedKey.PrivateKey.GetEncryptedBitcoinSecret(password, KnownNetworks.Main).ToWif(), ChainCode = extendedKey.ChainCode, CreationTime = DateTimeOffset.Now, Network = KnownNetworks.Main, walletStore = new WalletMemoryStore(), BlockLocator = new List <uint256>() { KnownNetworks.Main.GenesisHash }, AccountsRoot = new List <AccountRoot> { new AccountRoot() { Accounts = new List <HdAccount>(), CoinType = KnownNetworks.Main.Consensus.CoinType, LastBlockSyncedHash = KnownNetworks.Main.GenesisHash, LastBlockSyncedHeight = 0 } }, }; WalletData data = walletFile.walletStore.GetData(); data.BlockLocator = walletFile.BlockLocator; data.WalletName = walletFile.Name; data.WalletTip = new Utilities.HashHeightPair(KnownNetworks.Main.GenesisHash, 0); walletFile.walletStore.SetData(data); return(walletFile, extendedKey); }
public static (ExtKey ExtKey, string ExtPubKey) GenerateAccountKeys(Features.Wallet.Types.Wallet wallet, string password, string keyPath) { var accountExtKey = new ExtKey(Key.Parse(wallet.EncryptedSeed, password, wallet.Network), wallet.ChainCode); string accountExtendedPubKey = accountExtKey.Derive(new KeyPath(keyPath)).Neuter().ToString(wallet.Network); return(accountExtKey, accountExtendedPubKey); }
/// <summary> /// Creates a new wallet. /// </summary> /// <remarks> /// If it's the first time this wallet is created within this class, it is added to a collection for use by other tests. /// If the same parameters have already been used to create a wallet, the wallet will be retrieved from the internal collection and a copy of this wallet will be returned. /// </remarks> /// <param name="name">The name.</param> /// <param name="password">The password.</param> /// <returns>The generated wallet.</returns> public Features.Wallet.Types.Wallet GenerateBlankWallet(string name, string password) { if (this.walletsGenerated.TryGetValue((name, password), out Features.Wallet.Types.Wallet existingWallet)) { string serializedExistingWallet = JsonConvert.SerializeObject(existingWallet, Formatting.None); return(JsonConvert.DeserializeObject <Features.Wallet.Types.Wallet>(serializedExistingWallet)); } Features.Wallet.Types.Wallet newWallet = WalletTestsHelpers.GenerateBlankWallet(name, password); this.walletsGenerated.Add((name, password), newWallet); string serializedNewWallet = JsonConvert.SerializeObject(newWallet, Formatting.None); return(JsonConvert.DeserializeObject <Features.Wallet.Types.Wallet>(serializedNewWallet)); }
public static (Features.Wallet.Types.Wallet wallet, ExtKey key) GenerateBlankWalletWithExtKey(string name, string password) { var mnemonic = new Mnemonic("grass industry beef stereo soap employ million leader frequent salmon crumble banana"); ExtKey extendedKey = mnemonic.DeriveExtKey(password); var walletFile = new Features.Wallet.Types.Wallet { Name = name, EncryptedSeed = extendedKey.PrivateKey.GetEncryptedBitcoinSecret(password, KnownNetworks.Main).ToWif(), ChainCode = extendedKey.ChainCode, CreationTime = DateTimeOffset.Now, Network = KnownNetworks.Main, AccountsRoot = new List <AccountRoot> { new AccountRoot() { Accounts = new List <HdAccount>(), CoinType = KnownNetworks.Main.Consensus.CoinType } }, }; return(walletFile, extendedKey); }
/// <summary> /// Creates a new wallet. /// </summary> /// <remarks> /// If it's the first time this wallet is created within this class, it is added to a collection for use by other tests. /// If the same parameters have already been used to create a wallet, the wallet will be retrieved from the internal collection and a copy of this wallet will be returned. /// </remarks> /// <param name="name">The name.</param> /// <param name="password">The password.</param> /// <returns>The generated wallet.</returns> public Features.Wallet.Types.Wallet GenerateBlankWallet(string name, string password) { if (this.walletsGenerated.TryGetValue((name, password), out Features.Wallet.Types.Wallet existingWallet)) { string serializedExistingWallet = JsonConvert.SerializeObject(existingWallet, Formatting.None); var wal1 = JsonConvert.DeserializeObject <Features.Wallet.Types.Wallet>(serializedExistingWallet); wal1.BlockLocator = existingWallet.BlockLocator; wal1.AccountsRoot.Single().LastBlockSyncedHash = existingWallet.AccountsRoot.Single().LastBlockSyncedHash; wal1.AccountsRoot.Single().LastBlockSyncedHeight = existingWallet.AccountsRoot.Single().LastBlockSyncedHeight; wal1.walletStore = new WalletMemoryStore(); var data1 = wal1.walletStore.GetData(); data1.BlockLocator = existingWallet.BlockLocator; data1.WalletName = existingWallet.Name; data1.WalletTip = new Utilities.HashHeightPair(existingWallet.AccountsRoot.Single().LastBlockSyncedHash, existingWallet.AccountsRoot.Single().LastBlockSyncedHeight.Value); wal1.walletStore.SetData(data1); return(wal1); } Features.Wallet.Types.Wallet newWallet = WalletTestsHelpers.GenerateBlankWallet(name, password); this.walletsGenerated.Add((name, password), newWallet); string serializedNewWallet = JsonConvert.SerializeObject(newWallet, Formatting.None); var wal = JsonConvert.DeserializeObject <Features.Wallet.Types.Wallet>(serializedNewWallet); wal.walletStore = new WalletMemoryStore(); wal.BlockLocator = newWallet.BlockLocator; wal.AccountsRoot.Single().LastBlockSyncedHash = newWallet.AccountsRoot.Single().LastBlockSyncedHash; wal.AccountsRoot.Single().LastBlockSyncedHeight = newWallet.AccountsRoot.Single().LastBlockSyncedHeight; var data = wal.walletStore.GetData(); data.BlockLocator = wal.BlockLocator; data.WalletName = wal.Name; data.WalletTip = new Utilities.HashHeightPair(wal.AccountsRoot.Single().LastBlockSyncedHash, wal.AccountsRoot.Single().LastBlockSyncedHeight.Value); wal.walletStore.SetData(data); return(wal); }
public WalletFixture() { this.walletsGenerated = new Dictionary <(string, string), Features.Wallet.Types.Wallet>(); }
public static (PubKey PubKey, BitcoinPubKeyAddress Address) GenerateAddressKeys(Features.Wallet.Types.Wallet wallet, string accountExtendedPubKey, string keyPath) { PubKey addressPubKey = ExtPubKey.Parse(accountExtendedPubKey).Derive(new KeyPath(keyPath)).PubKey; BitcoinPubKeyAddress address = addressPubKey.GetAddress(wallet.Network); return(addressPubKey, address); }
public static Transaction SetupValidTransaction(Features.Wallet.Types.Wallet wallet, string password, HdAddress spendingAddress, PubKey destinationPubKey, HdAddress changeAddress, Money amount, Money fee) { return(SetupValidTransaction(wallet, password, spendingAddress, destinationPubKey.ScriptPubKey, changeAddress, amount, fee)); }
public void CreateWalletData() { string walltName = "wallet-with-funds"; string path = Path.Combine(WalletOutputDataPath + @"\txdb", walltName + ".db"); if (File.Exists(path)) { File.Delete(path); } var network = new BitcoinRegTest(); var folder = new DataFolder(WalletOutputDataPath); var wallet = new Features.Wallet.Types.Wallet() { Name = walltName }; var walletStore = new WalletStore(network, folder, wallet); string dataPath = Path.Combine(DataPath, "wallet-data-table.json"); var dataTable = JsonConvert.DeserializeObject <WalletDataItem[]>(File.ReadAllText(dataPath))[0]; WalletData walletData = new WalletData { Key = dataTable.Id, WalletName = dataTable.WalletName, EncryptedSeed = dataTable.EncryptedSeed, WalletTip = new HashHeightPair(uint256.Parse(dataTable.WalletTip.Split("-")[1]), int.Parse(dataTable.WalletTip.Split("-")[0])), BlockLocator = dataTable.BlockLocator.Select(uint256.Parse).ToList() }; walletStore.SetData(walletData); dataPath = Path.Combine(DataPath, "wallet-transactions-table.json"); var transactionsTable = JsonConvert.DeserializeObject <TransactionDataItem[]>(File.ReadAllText(dataPath)); foreach (var item in transactionsTable) { var trx = new TransactionOutputData { OutPoint = new OutPoint(uint256.Parse(item.OutPoint.Split("-")[0]), int.Parse(item.OutPoint.Split("-")[1])), Address = item.Address, Id = uint256.Parse(item.Id), Amount = new Money(item.Amount), Index = item.Index, BlockHeight = item.BlockHeight, BlockHash = item.BlockHash != null?uint256.Parse(item.BlockHash) : null, CreationTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(item.CreationTime)), ScriptPubKey = new Script(Encoders.Hex.DecodeData(item.ScriptPubKey)), IsPropagated = item.IsPropagated, AccountIndex = item.AccountIndex, IsCoinStake = item.IsCoinStake, IsCoinBase = item.IsCoinBase, IsColdCoinStake = item.IsColdCoinStake, }; if (item.SpendingDetails != null) { trx.SpendingDetails = new Features.Wallet.Database.SpendingDetails { BlockHeight = item.SpendingDetails.BlockHeight, IsCoinStake = item.SpendingDetails.IsCoinStake, CreationTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(item.SpendingDetails.CreationTime)), TransactionId = uint256.Parse(item.SpendingDetails.TransactionId) }; if (item.SpendingDetails.Payments != null) { foreach (PaymentDetails spendingDetailsPayment in item.SpendingDetails.Payments) { trx.SpendingDetails.Payments.Add(new Features.Wallet.Database.PaymentDetails { Amount = new Money(spendingDetailsPayment.Amount), DestinationAddress = spendingDetailsPayment.DestinationAddress, DestinationScriptPubKey = new Script(Encoders.Hex.DecodeData(spendingDetailsPayment.DestinationScriptPubKey)), OutputIndex = spendingDetailsPayment.OutputIndex, PayToSelf = spendingDetailsPayment.PayToSelf }); } } } walletStore.InsertOrUpdate(trx); } }