public void ValidKeyLitecoin(string wif, byte[] bytes, bool isPrivateKey, bool isTestnet, string addrType, bool isCompressed) { var network = isTestnet ? Network.LitecoinTest : Network.LitecoinMain; if (isPrivateKey) { var key = new Key(bytes, isCompressed); Assert.AreEqual(wif, key.ToString(network)); } else { Address addr = null; if (addrType == "pubkey") { addr = new PubKeyHashAddress(network, bytes); } else if (addrType == "script") { addr = new ScriptHashAddress(network, bytes); } else { Assert.Fail($"unknown address type: '{addrType}' for '{wif}'"); } Assert.AreEqual(wif, addr.ToString()); Assert.AreEqual(addr.Destination.ToByteArray(), bytes); } }
public CreateReceiveAddressResponse CreateReceiveAddress(CreateReceiveAddressRequest createReceiveAddressRequest) { Guard.NotNull(createReceiveAddressRequest, nameof(createReceiveAddressRequest)); using var context = GetWalletContext(); { PubKeyHashAddress pubKeyHashAddress = context.WalletManager.CreateReceiveAddress(createReceiveAddressRequest.Label, createReceiveAddressRequest.Passphrase); return(new CreateReceiveAddressResponse { PubKeyHashAddress = pubKeyHashAddress }); } }
static PubKeyHashAddress GeneratePubKeyHashAddress(byte[] decryptedSeed, string passphrase, int coinType, int isChange, int index) { var keyMaterial = KeyHelper.CreateHdKeyMaterial(decryptedSeed, passphrase, coinType, AddressType.PubKeyHash, isChange, index); var privateKey = keyMaterial.GetKey(passphrase); var scriptPubKey = privateKey.PubKey.Compress().WitHash.ScriptPubKey; var address = new PubKeyHashAddress { KeyMaterial = keyMaterial, AddressType = AddressType.PubKeyHash, Label = null, ScriptPubKeyHex = scriptPubKey.ToHex() }; address.Address = scriptPubKey.GetAddressFromScriptPubKey(); Debug.Assert(address.Address.Length == C.PubKeyHashAddressLength); return(address); }
Dictionary <string, PubKeyHashAddress> ParseDump(string dump) { if (dump == null) { throw new ArgumentNullException(nameof(dump)); } string[] lines = dump.Split( new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries ); var lookup = new Dictionary <string, PubKeyHashAddress>(); foreach (var line in lines) { if (line.StartsWith("#")) { continue; } if (line.StartsWith("00")) // script=1 { continue; } string[] lineItems = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (lineItems.Length < 2) { continue; } BitcoinSecret bitcoinSecret; string wif = lineItems[0]; try { bitcoinSecret = new BitcoinSecret(wif, C.Network); // x1 main has same SECRET_KEY prefix as BTC main if (!bitcoinSecret.PubKey.IsCompressed) { throw new InvalidOperationException("Expected compressed pubkey!"); } } catch (Exception e) { this.logger.LogWarning($"Invalid WIF '{wif}': {e.Message}, skipping."); continue; } string address = null; for (var i = 1; i < lineItems.Length; i++) { var item = lineItems[i]; if (item.StartsWith("addr=")) { // addr=bech32 - one address if (item.StartsWith("addr=x11")) { address = item.Replace("addr=", "").Trim(); continue; } // addr=1...,3...,x11... - also legacy formats string[] severalAdr = item.Split(new[] { "," }, StringSplitOptions.None); for (var j = 0; j < severalAdr.Length; j++) { if (severalAdr[j].StartsWith("x11")) { address = severalAdr[j].Trim(); } } } } if (!string.IsNullOrWhiteSpace(address)) { if (address.Length == C.PubKeyHashAddressLength) { var scriptPubKey = bitcoinSecret.PrivateKey.PubKey.Compress().WitHash.ScriptPubKey; var pubKeyHashAddress = new PubKeyHashAddress { KeyMaterial = new KeyMaterial { PlaintextBytes = bitcoinSecret.PrivateKey.ToBytes().Take(32).ToArray(), KeyType = KeyType.Imported }, AddressType = AddressType.PubKeyHash, ScriptPubKeyHex = scriptPubKey.ToHex(), Address = scriptPubKey.GetAddressFromScriptPubKey() }; Debug.Assert(scriptPubKey == new Key(pubKeyHashAddress.KeyMaterial.PlaintextBytes).PubKey.WitHash.ScriptPubKey); if (address == pubKeyHashAddress.Address) { if (!lookup.ContainsKey(address)) { lookup.Add(address, pubKeyHashAddress); } else { this.logger.LogWarning($"Lookup already contains address {address}."); } } else { this.logger.LogWarning($"Address {address} cannot be created from WIF {wif}, skipping."); } } else if (address.Length == C.ScriptAddressLength) { this.logger.LogWarning($"Address {address} looks like a script address, skipping."); } else { this.logger.LogWarning($"Address '{address}' has an invalid length!"); } } } return(lookup); }