public void CanUseSegwitAddress() { var address = (BitcoinWitPubKeyAddress)BitcoinAddress.Create("p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm"); Assert.Equal("0014010966776006953d5567439e5e39f86a0d273bee", address.ScriptPubKey.ToHex()); Assert.Equal("0014010966776006953d5567439e5e39f86a0d273bee", address.Hash.ScriptPubKey.ToHex()); Assert.Equal("3R1ZpeYRXx5oFtJWNoUwLFoACixRQ7sDQa", address.GetScriptAddress().ToString()); //Example of the BIP var pubkey = new PubKey("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"); Assert.Equal(new Script("OP_0 010966776006953D5567439E5E39F86A0D273BEE"), pubkey.GetSegwitAddress(Network.Main).ScriptPubKey); Assert.Equal("p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm", pubkey.GetSegwitAddress(Network.Main).ToString()); Assert.Equal(pubkey.GetSegwitAddress(Network.TestNet).ToString(), pubkey.GetSegwitAddress(Network.SegNet).ToString()); Assert.NotEqual(pubkey.GetSegwitAddress(Network.Main).ToString(), pubkey.GetSegwitAddress(Network.SegNet).ToString()); }
/// <summary> /// Creates a number of additional addresses in the current account. /// </summary> /// <remarks> /// The name given to the account is of the form "account (i)" by default, where (i) is an incremental index starting at 0. /// According to BIP44, an account at index (i) can only be created when the account at index (i - 1) contains at least one transaction. /// </remarks> /// <param name="network">The network these addresses will be for.</param> /// <param name="addressesQuantity">The number of addresses to create.</param> /// <param name="isChange">Whether the addresses added are change (internal) addresses or receiving (external) addresses.</param> /// <returns>The created addresses.</returns> public IEnumerable <HdAddress> CreateAddresses(Network network, int addressesQuantity, bool isChange = false) { ICollection <HdAddress> addresses = isChange ? this.InternalAddresses : this.ExternalAddresses; // Get the index of the last address. int firstNewAddressIndex = 0; if (addresses.Any()) { firstNewAddressIndex = addresses.Max(add => add.Index) + 1; } var addressesCreated = new List <HdAddress>(); for (int i = firstNewAddressIndex; i < firstNewAddressIndex + addressesQuantity; i++) { // Retrieve the pubkey associated with the private key of this address index. PubKey pubkey = HdOperations.GeneratePublicKey(this.ExtendedPubKey, i, isChange); // Generate the P2PKH address corresponding to the pubkey. BitcoinPubKeyAddress address = pubkey.GetAddress(network); BitcoinWitPubKeyAddress witAddress = pubkey.GetSegwitAddress(network); // Add the new address details to the list of addresses. var newAddress = new HdAddress { Index = i, HdPath = HdOperations.CreateHdPath((int)this.GetCoinType(), this.Index, isChange, i), ScriptPubKey = address.ScriptPubKey, Pubkey = pubkey.ScriptPubKey, Bech32Address = witAddress.ToString(), Address = address.ToString(), }; addresses.Add(newAddress); addressesCreated.Add(newAddress); } if (isChange) { this.InternalAddresses = addresses; } else { this.ExternalAddresses = addresses; } return(addressesCreated); }
public static IBitcoinBasedTransaction CreateSegwitPaymentTx( BitcoinBasedCurrency currency, IEnumerable <ITxOutput> outputs, PubKey from, PubKey to, int amount, int fee) { return(currency.CreateP2WPkhTx( unspentOutputs: outputs, destinationAddress: to.GetSegwitAddress(currency.Network).ToString(), changeAddress: from.GetSegwitAddress(currency.Network).ToString(), amount: amount, fee: fee)); }
public void CanUseSegwitAddress() { var address = (BitcoinWitPubKeyAddress)BitcoinAddress.Create("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"); var pubkey = new PubKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"); Assert.Equal(pubkey.WitHash.ScriptPubKey.ToHex(), address.ScriptPubKey.ToHex()); Assert.Equal("0014751e76e8199196d454941c45d1b3a323f1433bd6", address.Hash.ScriptPubKey.ToHex()); Assert.Equal("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", address.ScriptPubKey.GetDestinationAddress(address.Network).ToString()); Assert.Equal("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", address.Hash.GetAddress(address.Network).ToString()); Assert.Equal(Network.Main, address.Network); Assert.Equal("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", address.ScriptPubKey.GetDestinationAddress(address.Network).ToNetwork(Network.TestNet).ToString()); address = (BitcoinWitPubKeyAddress)BitcoinAddress.Create("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx"); pubkey = new PubKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"); Assert.Equal(pubkey.WitHash.ScriptPubKey.ToHex(), address.ScriptPubKey.ToHex()); Assert.Equal("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", address.ScriptPubKey.GetDestinationAddress(address.Network).ToString()); Assert.Equal("0014751e76e8199196d454941c45d1b3a323f1433bd6", address.Hash.ScriptPubKey.ToHex()); Assert.Equal("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", address.Hash.GetAddress(address.Network).ToString()); Assert.Equal(Network.TestNet, address.Network); Assert.Throws <FormatException>(() => BitcoinAddress.Create("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", Network.Main)); var addressScript = (BitcoinWitScriptAddress)BitcoinAddress.Create("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3"); Assert.Equal(pubkey.ScriptPubKey.WitHash.ScriptPubKey.ToHex(), addressScript.ScriptPubKey.ToHex()); Assert.Equal("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", addressScript.Hash.GetAddress(addressScript.Network).ToString()); Assert.Equal("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", addressScript.ScriptPubKey.GetDestinationAddress(addressScript.Network).ToString()); //Example of the BIP pubkey = new PubKey("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"); Assert.Equal(new Script("OP_0 010966776006953D5567439E5E39F86A0D273BEE"), pubkey.GetSegwitAddress(Network.Main).ScriptPubKey); //Test .ToNetwork() var addr = pubkey.GetAddress(Network.Main); Assert.Equal("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", addr.ToString()); Assert.Equal("mfcSEPR8EkJrpX91YkTJ9iscdAzppJrG9j", addr.ToNetwork(Network.TestNet).ToString()); Assert.Throws <FormatException>(() => Network.Parse <IBase58Data>("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main)); Network.Parse <IBitcoinString>("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main); Assert.Throws <Bech32FormatException>(() => Network.Parse <IBitcoinString>("bc1qrp33g0q5c3txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main)); Assert.Throws <Bech32FormatException>(() => new BitcoinWitScriptAddress("bc1qrp33g0q5c3txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main)); Assert.Throws <Bech32FormatException>(() => new BitcoinWitPubKeyAddress("bc1qw507d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", Network.Main)); }
public BitcoinWitPubKeyAddress GetP2wpkhAddress(Network network) => PubKey.GetSegwitAddress(network);
public static string GetSegwitAddress( this PubKey key, BitcoinBasedCurrency currency) { return(key.GetSegwitAddress(currency.Network).ToString()); }
public void CanUseSegwitAddress() { var address = (BitcoinWitPubKeyAddress)BitcoinAddress.Create("p2xuirpcDHjyETjKozsw1S19Hs23q6wJxvvX"); Assert.Equal("00140db804a04a249f53c53e1b3619a1411b4c41e2e2", address.ScriptPubKey.ToHex()); Assert.Equal("00140db804a04a249f53c53e1b3619a1411b4c41e2e2", address.Hash.ScriptPubKey.ToHex()); Assert.Equal("MUGbdic7BDYtn2K7TVyk47aArXKGk9XC1j", address.GetScriptAddress().ToString()); Assert.Equal("a914df70747a4fce42b9f8dc62318da432d067669eff87", address.GetScriptAddress().ScriptPubKey.ToHex()); //Example of the BIP var pubkey = new PubKey("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"); Assert.Equal(new Script("OP_0 010966776006953D5567439E5E39F86A0D273BEE"), pubkey.GetSegwitAddress(Network.Main).ScriptPubKey); Assert.Equal("YMeG5MzsnmVKk86fzdjzhfJ5FXmexT8gvwi5", pubkey.GetSegwitAddress(Network.Main).ToString()); }
public void CanUseSegwitAddress() { var address = (BitcoinWitPubKeyAddress)BitcoinAddress.Create("p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm"); Assert.Equal("0014010966776006953d5567439e5e39f86a0d273bee", address.ScriptPubKey.ToHex()); Assert.Equal("0014010966776006953d5567439e5e39f86a0d273bee", address.Hash.ScriptPubKey.ToHex()); Assert.Equal("3R1ZpeYRXx5oFtJWNoUwLFoACixRQ7sDQa", address.GetScriptAddress().ToString()); //Example of the BIP var pubkey = new PubKey("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"); Assert.Equal(new Script("OP_0 010966776006953D5567439E5E39F86A0D273BEE"), pubkey.GetSegwitAddress(Network.Main).ScriptPubKey); Assert.Equal("p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm", pubkey.GetSegwitAddress(Network.Main).ToString()); }