internal static ExportKeysResponse ExportKeys(ExportKeysRequest exportKeysRequest, IReadOnlyCollection <ISegWitAddress> addresses) { var header = new StringBuilder(); header.AppendLine($"Starting export from wallet {exportKeysRequest.WalletName}, network {C.Network.Name} on {DateTime.UtcNow} UTC."); var errors = new StringBuilder(); errors.AppendLine("Errors"); var success = new StringBuilder(); success.AppendLine("Exported Private Key (Hex); Unix Time UTC; IsChange; Address; Label:"); int errorCount = 0; int successCount = 0; try { header.AppendLine($"{addresses.Count} found in wallet."); var enc = new Bech32Encoder($"{C.Network.CoinTicker.ToLowerInvariant()}key"); foreach (var a in addresses) { try { var decryptedKey = VCL.DecryptWithPassphrase(exportKeysRequest.WalletPassphrase, a.GetEncryptedPrivateKey()); if (decryptedKey == null) { errorCount++; header.AppendLine( $"Address '{a.Address}' could not be decrpted with this passphrase."); } else { var privateKey = enc.Encode(0, decryptedKey); success.AppendLine($"{privateKey};{a.Address}"); successCount++; } } catch (Exception e) { header.AppendLine($"Exception processing Address '{a.Address}': {e.Message}"); } } header.AppendLine($"{errorCount} errors occured."); header.AppendLine($"{successCount} addresses with private keys successfully exported."); } catch (Exception e) { errors.AppendLine(e.Message); return(new ExportKeysResponse { Message = $"Export failed because an exception occured: {e.Message}" }); } return(new ExportKeysResponse { Message = $"{header}{Environment.NewLine}{success}{Environment.NewLine}{errors}{Environment.NewLine}" }); }
public void ValidAddress() { foreach (string[] address in VALID_ADDRESS) { byte witVer; byte[] witProg; Bech32Encoder encoder = bech32; try { witProg = bech32.Decode(address[0], out witVer); encoder = bech32; } catch { witProg = tbech32.Decode(address[0], out witVer); encoder = tbech32; } byte[] scriptPubkey = Scriptpubkey(witVer, witProg); string hex = string.Join("", scriptPubkey.Select(x => x.ToString("x2"))); Assert.Equal(hex, address[1]); string addr = encoder.Encode(witVer, witProg); Assert.Equal(address[0].ToLowerInvariant(), addr); } }
internal static string CreateBech32(Bech32Type type, byte[] bytes, byte witnessVersion, Network network) { if (network == null) throw new ArgumentNullException("network"); if (bytes == null) throw new ArgumentNullException("bytes"); Bech32Encoder encoder = network.GetBech32Encoder(type, true); return encoder.Encode(witnessVersion, bytes); }
public void ValidAddress() { foreach (var address in VALID_ADDRESS) { Bech32Encoder encoder = Bech32Encoder.ExtractEncoderFromString(address[0]); var witProg = encoder.Decode(address[0], out var witVer); var scriptPubkey = Scriptpubkey(witVer, witProg); var hex = string.Join("", scriptPubkey.Select(x => x.ToString("x2"))); Assert.Equal(hex, address[1]); var addr = encoder.Encode(witVer, witProg); Assert.Equal(address[0].ToLowerInvariant(), addr); } }
public static T ToNetwork <T>(this T obj, Network network) where T : IBitcoinString { if (network == null) { throw new ArgumentNullException("network"); } if (obj == null) { throw new ArgumentNullException("obj"); } if (obj.Network == network) { return(obj); } if (obj is IBase58Data) { var b58 = (IBase58Data)obj; if (b58.Type != Base58Type.COLORED_ADDRESS) { byte[] version = network.GetVersionBytes(b58.Type, true); byte[] inner = Encoders.Base58Check.DecodeData(b58.ToString()).Skip(version.Length).ToArray(); string newBase58 = Encoders.Base58Check.EncodeData(version.Concat(inner).ToArray()); return(Network.Parse <T>(newBase58, network)); } else { string colored = BitcoinColoredAddress.GetWrappedBase58(obj.ToString(), obj.Network); BitcoinAddress address = Network.Parse <BitcoinAddress>(colored, obj.Network).ToNetwork(network); return((T)(object)address.ToColoredAddress()); } } else if (obj is IBech32Data) { var b32 = (IBech32Data)obj; Bech32Encoder encoder = b32.Network.GetBech32Encoder(b32.Type, true); byte wit; byte[] data = encoder.Decode(b32.ToString(), out wit); encoder = network.GetBech32Encoder(b32.Type, true); string str = encoder.Encode(wit, data); return((T)(object)Network.Parse <T>(str, network)); } else { throw new NotSupportedException(); } }
public XDSPubKeyAddress CreateHdAddress(byte[] walletSeed, int accountIndex, int changePath, int addressIndex) { var keyPath = new KeyPath($"m/44'/{XDSPrincipal.XDSCoinType}'/{accountIndex}'/{changePath}/{addressIndex}"); var seedExtKey = new ExtKey(walletSeed); var derivedKey = seedExtKey.Derive(keyPath); CompressedPubKey compressedPubKey = derivedKey.PrivateKey.CompressedPubKey; var hash = compressedPubKey.GetHash160(); var bech = new Bech32Encoder("xds"); var address = bech.Encode(0, hash); return(new XDSPubKeyAddress { PrivateKey = derivedKey.PrivateKey.RawBytes, PublicKey = derivedKey.PrivateKey.CompressedPubKey.ToBytes(), Hash = hash, KeyPath = keyPath.ToString(), Address = address, ScriptPubKey = ByteArrays.Concatenate(new byte[1], hash) }); }
public static string ToScriptAddress(this byte[] hash256) { KeyHelper.CheckBytes(hash256, 32); return(ScriptAddressEncoder.Encode(0, hash256)); }
public static string ToPubKeyHashAddress(this byte[] hash160) { KeyHelper.CheckBytes(hash160, 20); return(PubKeyAddressEncoder.Encode(0, hash160)); }
public static string ToScriptAddress(this byte[] hash256) { CryptoRandom.CheckBytes(hash256, 32); return(ScriptAddressEncoder.Encode(0, hash256)); }