public void CanDetectBase58WithoutAmbiguity() { var address = new { Base58 = "bWyXRVD4J3Y8bG8VQ8aQmnnztdMNzExRdaw", ExpectedType = typeof(BitcoinColoredAddress), Network = KnownNetworks.RegTest }; IBitcoinString result = Network.Parse(address.Base58, address.Network); Assert.IsType <BitcoinColoredAddress>(result); Assert.True(result.Network == KnownNetworks.RegTest); address = new { Base58 = new ExtKey().Neuter().ToString(KnownNetworks.RegTest), ExpectedType = typeof(BitcoinExtPubKey), Network = KnownNetworks.RegTest }; result = Network.Parse(address.Base58, address.Network); Assert.IsType <BitcoinExtPubKey>(result); Assert.True(result.Network == KnownNetworks.RegTest); result = Network.Parse(address.Base58, null); Assert.Contains(result.Network, new[] { KnownNetworks.RegTest, KnownNetworks.TestNet }); }
public void CanDetectBase58WithoutAmbiguity() { var address = new { Base58 = "bWyXRVD4J3Y8bG8VQ8aQmnnztdMNzExRdaw", ExpectedType = typeof(BitcoinColoredAddress), Network = Network.RegTest }; IBitcoinString result = Network.Parse(address.Base58, address.Network); Assert.IsType <BitcoinColoredAddress>(result); Assert.True(result.Network == Network.RegTest); address = new { Base58 = new ExtKey().Neuter().ToString(Network.RegTest), ExpectedType = typeof(BitcoinExtPubKey), Network = Network.RegTest }; result = Network.Parse(address.Base58, address.Network); Assert.IsType <BitcoinExtPubKey>(result); Assert.True(result.Network == Network.RegTest); result = Network.Parse(address.Base58, null); Assert.True(result.Network == Network.TestNet); string str = Serializer.ToString(new DummyClass() { ExtPubKey = new ExtKey().Neuter().GetWif(Network.RegTest) }, Network.RegTest); Assert.NotNull(Serializer.ToObject <DummyClass>(str, Network.RegTest)); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } try { IBitcoinString result = Network.Parse(reader.Value.ToString()); if (result == null) { throw new JsonObjectException("Invalid BitcoinString network", reader); } if (!objectType.GetTypeInfo().IsAssignableFrom(result.GetType().GetTypeInfo())) { throw new JsonObjectException("Invalid BitcoinString type expected " + objectType.Name + ", actual " + result.GetType().Name, reader); } return(result); } catch (FormatException ex) { throw new JsonObjectException(ex.Message, reader); } }
/// <inheritdoc /> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { IBitcoinString base58 = value as IBitcoinString; if (base58 != null) { writer.WriteValue(value.ToString()); } }
public override bool TryParse(string str, Network network, Type targetType, out IBitcoinString result) { if (typeof(BitcoinAddress).GetTypeInfo().IsAssignableFrom(targetType.GetTypeInfo())) { try { result = new BitcoinBlindedAddress(str, network); return(true); } catch { } } return(base.TryParse(str, network, targetType, out result)); }
public WhatIsBase58(IBitcoinString data) { Data = data.ToString(); var b58 = data as IBase58Data; if (b58 != null) { Type = ToString(b58.Type); StringType = "Base58"; } var b32 = data as IBech32Data; if (b32 != null) { Type = ToString(b32.Type); StringType = "Bech32"; } Network = data.Network; }
//https://en.bitcoin.it/wiki/List_of_address_prefixes public void CanDetectBase58NetworkAndType() { var tests = new[] { new { Base58 = "bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", ExpectedType = typeof(BitcoinWitScriptAddress), Network = this.networkMain }, new { Base58 = "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7", ExpectedType = typeof(BitcoinWitScriptAddress), Network = this.networkTestNet }, new { Base58 = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", ExpectedType = typeof(BitcoinWitPubKeyAddress), Network = this.networkMain }, new { Base58 = "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", ExpectedType = typeof(BitcoinWitPubKeyAddress), Network = this.networkTestNet }, new { Base58 = "bWqaKUZETiECYgmJNbNZUoanBxnAzoVjCNx", ExpectedType = typeof(BitcoinColoredAddress), Network = this.networkTestNet }, new { Base58 = "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem", ExpectedType = typeof(BitcoinPubKeyAddress), Network = this.networkMain }, new { Base58 = "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem", ExpectedType = typeof(BitcoinPubKeyAddress), Network = this.networkMain }, new { Base58 = "3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX", ExpectedType = typeof(BitcoinScriptAddress), Network = this.networkMain }, new { Base58 = "mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn", ExpectedType = typeof(BitcoinPubKeyAddress), Network = this.networkTestNet }, new { Base58 = "5Hwgr3u458GLafKBgxtssHSPqJnYoGrSzgQsPwLFhLNYskDPyyA", ExpectedType = typeof(BitcoinSecret), Network = this.networkMain }, new { Base58 = "92Pg46rUhgTT7romnV7iGW6W1gbGdeezqdbJCzShkCsYNzyyNcc", ExpectedType = typeof(BitcoinSecret), Network = this.networkTestNet }, new { Base58 = "3qdi7TXgRo1qR", ExpectedType = (Type)null, Network = (Network)null }, new { Base58 = "6PYLtMnXvfG3oJde97zRyLYFZCYizPU5T3LwgdYJz1fRhh16bU7u6PPmY7", ExpectedType = typeof(BitcoinEncryptedSecretNoEC), Network = (Network)null }, new { Base58 = "6PfQu77ygVyJLZjfvMLyhLMQbYnu5uguoJJ4kMCLqWwPEdfpwANVS76gTX", ExpectedType = typeof(BitcoinEncryptedSecretEC), Network = (Network)null }, new { Base58 = "passphrasepxFy57B9v8HtUsszJYKReoNDV6VHjUSGt8EVJmux9n1J3Ltf1gRxyDGXqnf9qm", ExpectedType = typeof(BitcoinPassphraseCode), Network = (Network)null }, new { Base58 = "cfrm38V8aXBn7JWA1ESmFMUn6erxeBGZGAxJPY4e36S9QWkzZKtaVqLNMgnifETYw7BPwWC9aPD", ExpectedType = typeof(BitcoinConfirmationCode), Network = (Network)null }, new { Base58 = "xprv9s21ZrQH143K3Gx1VAAD1ueDmwoPQUApekxWYSJ1f4W4m1nUPpRGdV5sTVhixZJT5cP2NqtEMZ2mrwHdW5RWpohCwspWidCpcLALvioXDyz", ExpectedType = typeof(BitcoinExtKey), Network = this.networkMain }, new { Base58 = "xpub661MyMwAqRbcEhHavVcryjNF2uA5woK6JCNRNJB8Z3dxPU8VNBd9E8GP7fusw2bhgYe7BXt6izr5iUaYo483919jjdtfEpG8j97djnEgJqo", ExpectedType = typeof(BitcoinExtPubKey), Network = this.networkMain }, new { Base58 = "akB4NBW9UuCmHuepksob6yfZs6naHtRCPNy", ExpectedType = typeof(BitcoinColoredAddress), Network = this.networkMain } }; foreach (var test in tests) { if (test.ExpectedType == null) { Assert.Throws <FormatException>(() => Network.Parse(test.Base58, null)); } else { IBitcoinString result = Network.Parse(test.Base58, null); Assert.True(test.ExpectedType == result.GetType()); if (test.Network != null) { if (test.Network.Name.ToLowerInvariant().Contains("test")) { Assert.Contains(result.Network, new[] { KnownNetworks.RegTest, KnownNetworks.TestNet }); } else { Assert.Equal(test.Network, result.Network); } } Network.Parse(test.Base58, test.Network); if (test.Network == null) { continue; } foreach (Network network in NetworkRegistration.GetNetworks()) { if (test.Network.Name.ToLowerInvariant().Contains("test")) { Assert.Contains(result.Network, new[] { KnownNetworks.RegTest, KnownNetworks.TestNet }); break; } else { if (network == test.Network) { break; } } Assert.Throws <FormatException>(() => Network.Parse(test.Base58, network)); } } } }
public override bool TryParse(string str, Network network, Type targetType, out IBitcoinString result) { if (str.StartsWith("ttpv", StringComparison.OrdinalIgnoreCase) && targetType.GetTypeInfo().IsAssignableFrom(typeof(BitcoinExtKey).GetTypeInfo())) { try { var decoded = Encoders.Base58Check.DecodeData(str); decoded[0] = 0x04; decoded[1] = 0x35; decoded[2] = 0x83; decoded[3] = 0x94; result = new BitcoinExtKey(Encoders.Base58Check.EncodeData(decoded), network); return(true); } catch { } } if (str.StartsWith("ttub", StringComparison.OrdinalIgnoreCase) && targetType.GetTypeInfo().IsAssignableFrom(typeof(BitcoinExtPubKey).GetTypeInfo())) { try { var decoded = Encoders.Base58Check.DecodeData(str); decoded[0] = 0x04; decoded[1] = 0x35; decoded[2] = 0x87; decoded[3] = 0xCF; result = new BitcoinExtPubKey(Encoders.Base58Check.EncodeData(decoded), network); return(true); } catch { } } return(base.TryParse(str, network, targetType, out result)); }
public override bool TryParse(string str, Network network, Type targetType, out IBitcoinString result) { if (str.StartsWith("xprv", StringComparison.OrdinalIgnoreCase) && targetType.GetTypeInfo().IsAssignableFrom(typeof(BitcoinExtKey).GetTypeInfo())) { try { result = new BitcoinExtKey(str, network); return(true); } catch { } } if (str.StartsWith("xpub", StringComparison.OrdinalIgnoreCase) && targetType.GetTypeInfo().IsAssignableFrom(typeof(BitcoinExtPubKey).GetTypeInfo())) { try { result = new BitcoinExtPubKey(str, network); return(true); } catch { } } return(base.TryParse(str, network, targetType, out result)); }
public virtual bool TryParse(string str, Network network, Type targetType, out IBitcoinString result) { result = null; return(false); }