/// <inheritdoc /> public HdAddress GetUnusedAddress(WalletAccountReference accountReference) { Guard.NotNull(accountReference, nameof(accountReference)); Wallet wallet = this.GetWalletByName(accountReference.WalletName); // get the account HdAccount account = wallet.GetAccountByCoinType(accountReference.AccountName, this.coinType); // validate address creation if (account.ExternalAddresses.Any()) { // check last created address contains transactions. var firstUnusedExternalAddress = account.GetFirstUnusedReceivingAddress(); if (firstUnusedExternalAddress != null) { return(firstUnusedExternalAddress); } } // creates an address account.CreateAddresses(this.network, 1); // persists the address to the wallet file this.SaveToFile(wallet); // adds the address to the list of tracked addresses this.LoadKeysLookup(); return(account.GetFirstUnusedReceivingAddress()); }
/// <inheritdoc /> public Mnemonic CreateWallet(string password, string name, string passphrase = null, string mnemonicList = null) { Guard.NotEmpty(password, nameof(password)); Guard.NotEmpty(name, nameof(name)); // for now the passphrase is set to be the password by default. if (passphrase == null) { passphrase = password; } // generate the root seed used to generate keys from a mnemonic picked at random // and a passphrase optionally provided by the user Mnemonic mnemonic = string.IsNullOrEmpty(mnemonicList) ? new Mnemonic(Wordlist.English, WordCount.Twelve) : new Mnemonic(mnemonicList); ExtKey extendedKey = HdOperations.GetHdPrivateKey(mnemonic, passphrase); // create a wallet file Wallet wallet = this.GenerateWalletFile(password, name, extendedKey); // generate multiple accounts and addresses from the get-go for (int i = 0; i < WalletCreationAccountsCount; i++) { HdAccount account = wallet.AddNewAccount(password, this.coinType); account.CreateAddresses(this.network, UnusedAddressesBuffer); account.CreateAddresses(this.network, UnusedAddressesBuffer, true); } // update the height of the we start syncing from this.UpdateLastBlockSyncedHeight(wallet, this.chain.Tip); // save the changes to the file and add addresses to be tracked this.SaveToFile(wallet); this.Load(wallet); this.LoadKeysLookup(); return(mnemonic); }
/// <inheritdoc /> public Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase = null) { Guard.NotEmpty(password, nameof(password)); Guard.NotEmpty(name, nameof(name)); Guard.NotEmpty(mnemonic, nameof(mnemonic)); // for now the passphrase is set to be the password by default. if (passphrase == null) { passphrase = password; } // generate the root seed used to generate keys ExtKey extendedKey = HdOperations.GetHdPrivateKey(mnemonic, passphrase); // create a wallet file Wallet wallet = this.GenerateWalletFile(password, name, extendedKey, creationTime); // generate multiple accounts and addresses from the get-go for (int i = 0; i < WalletRecoveryAccountsCount; i++) { HdAccount account = wallet.AddNewAccount(password, this.coinType); account.CreateAddresses(this.network, UnusedAddressesBuffer); account.CreateAddresses(this.network, UnusedAddressesBuffer, true); } int blockSyncStart = this.chain.GetHeightAtTime(creationTime); this.UpdateLastBlockSyncedHeight(wallet, this.chain.GetBlock(blockSyncStart)); // save the changes to the file and add addresses to be tracked this.SaveToFile(wallet); this.Load(wallet); this.LoadKeysLookup(); return(wallet); }
/// <inheritdoc /> public HdAddress GetOrCreateChangeAddress(HdAccount account) { // get address to send the change to var changeAddress = account.GetFirstUnusedChangeAddress(); // no more change addresses left. create a new one. if (changeAddress == null) { var accountAddress = account.CreateAddresses(this.network, 1, isChange: true).Single(); changeAddress = account.InternalAddresses.First(a => a.Address == accountAddress); // persists the address to the wallet file this.SaveToFile(); // adds the address to the list of tracked addresses this.LoadKeysLookup(); } return(changeAddress); }