private static string Validate(string base58, ref Network expectedNetwork)
        {
            PubKey        p = null;
            TxDestination h = null;

            return(Validate(base58, ref expectedNetwork, ref p, ref h));
        }
예제 #2
0
파일: KeyId.cs 프로젝트: zzms/NBitcoin
        public override bool Equals(object obj)
        {
            TxDestination item = obj as TxDestination;

            if (item == null)
            {
                return(false);
            }
            return(Utils.ArrayEqual(_DestBytes, item._DestBytes) && item.GetType() == this.GetType());
        }
예제 #3
0
        public override bool Equals(object obj)
        {
            TxDestination item = obj as TxDestination;

            if (item == null)
            {
                return(false);
            }
            return(_Dest.Equals(item._Dest));
        }
 public BitcoinBlindedAddress(PubKey blindingKey, TxDestination keyId, Network network)
     : base(NotNull(keyId, nameof(keyId)) ??
            NotNull(blindingKey, nameof(blindingKey)) ??
            Network.CreateBase58(Base58Type.BLINDED_ADDRESS,
                                 network.GetVersionBytes(((IBase58Data)keyId.GetAddress(network)).Type, true)
                                 .Concat(blindingKey.ToBytes())
                                 .Concat(keyId.ToBytes()), network), network)
 {
     _BlindingKey = blindingKey;
     _Hash        = keyId;
 }
예제 #5
0
 /// <summary>
 /// Create scriptPubKey from destination id
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Script CreateFromDestination(TxDestination id)
 {
     if (id is ScriptId)
     {
         return(PayToScriptHashTemplate.Instance.GenerateScriptPubKey((ScriptId)id));
     }
     if (id is KeyId)
     {
         return(PayToPubkeyHashTemplate.Instance.GenerateScriptPubKey((KeyId)id));
     }
     throw new NotSupportedException();
 }
        public BitcoinBlindedAddress(string base58, Network expectedNetwork = null)
            : base(Validate(base58, ref expectedNetwork), expectedNetwork)
        {
            var data = Encoders.Base58Check.DecodeData(base58);

            PubKey        p = null;
            TxDestination h = null;

            Validate(base58, ref expectedNetwork, ref p, ref h);
            _Hash        = h;
            _BlindingKey = p;
        }
예제 #7
0
 public BitcoinAddress CreateBitcoinAddress(TxDestination dest)
 {
     if (dest == null)
     {
         throw new ArgumentNullException("dest");
     }
     if (dest is ScriptId)
     {
         return(CreateBitcoinScriptAddress((ScriptId)dest));
     }
     if (dest is KeyId)
     {
         return(new BitcoinAddress((KeyId)dest, this));
     }
     throw new ArgumentException("Invalid dest type", "dest");
 }
예제 #8
0
        /// <summary>
        /// Create scriptPubKey from destination id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Script CreateFromDestination(TxDestination id)
        {
            var scriptId = id as ScriptId;

            if (scriptId != null)
            {
                return(PayToScriptHashTemplate.Instance.GenerateScriptPubKey(scriptId));
            }

            var pubkeyHash = id as KeyId;

            if (pubkeyHash != null)
            {
                return(PayToPubkeyHashTemplate.Instance.GenerateScriptPubKey(pubkeyHash));
            }

            throw new NotSupportedException();
        }
예제 #9
0
        public ScriptCoin AssertCoherent(Network network)
        {
            if (Redeem == null)
            {
                throw new ArgumentException("redeem cannot be null", "redeem");
            }

            TxDestination expectedDestination = GetRedeemHash(network, TxOut.ScriptPubKey);

            if (expectedDestination == null)
            {
                throw new ArgumentException("the provided scriptPubKey is not P2SH or P2WSH");
            }
            if (expectedDestination is ScriptId)
            {
                if (PayToWitScriptHashTemplate.Instance.CheckScriptPubKey(network, Redeem))
                {
                    throw new ArgumentException("The redeem script provided must be the witness one, not the P2SH one");
                }

                if (expectedDestination.ScriptPubKey != Redeem.Hash.ScriptPubKey)
                {
                    if (Redeem.WitHash.ScriptPubKey.Hash.ScriptPubKey != expectedDestination.ScriptPubKey)
                    {
                        throw new ArgumentException("The redeem provided does not match the scriptPubKey of the coin");
                    }
                }
            }
            else if (expectedDestination is WitScriptId)
            {
                if (expectedDestination.ScriptPubKey != Redeem.WitHash.ScriptPubKey)
                {
                    throw new ArgumentException("The redeem provided does not match the scriptPubKey of the coin");
                }
            }
            else
            {
                throw new NotSupportedException("Not supported redeemed scriptPubkey");
            }

            return(this);
        }
예제 #10
0
        public static BitcoinAddress Create(TxDestination id, Network network)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }
            if (id is KeyId)
            {
                return(new BitcoinAddress(id, network));
            }
            if (id is ScriptId)
            {
                return(new BitcoinScriptAddress((ScriptId)id, network));
            }

            throw new NotSupportedException();
        }
예제 #11
0
 protected BitcoinAddress(TxDestination dest, Network network)
     : base(dest.ToBytes(), network)
 {
 }
예제 #12
0
		public static BitcoinAddress Create(TxDestination id, Network network)
		{
			if(id == null)
				throw new ArgumentNullException("id");
			if(network == null)
				throw new ArgumentNullException("network");
			if(id is KeyId)
				return new BitcoinAddress(id, network);
			if(id is ScriptId)
				return new BitcoinScriptAddress((ScriptId)id, network);
			
			throw new NotSupportedException();
		}
예제 #13
0
		/// <summary>
		/// Create scriptPubKey from destination id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public static Script CreateFromDestination(TxDestination id)
		{
			var scriptId = id as ScriptId;
			if(scriptId != null)
				return PayToScriptHashTemplate.Instance.GenerateScriptPubKey(scriptId);

			var pubkeyHash = id as KeyId;
			if(pubkeyHash != null)
				return PayToPubkeyHashTemplate.Instance.GenerateScriptPubKey(pubkeyHash);

			throw new NotSupportedException();
		}
        private static string Validate(string base58, ref Network expectedNetwork, ref PubKey blinding, ref TxDestination hash)
        {
            if (base58 == null)
            {
                throw new ArgumentNullException("base58");
            }
            var networks = expectedNetwork == null?Network.GetNetworks() : new[] { expectedNetwork };
            var data     = Encoders.Base58Check.DecodeData(base58);

            foreach (var network in networks)
            {
                bool isP2SH       = false;
                var  versionBytes = network.GetVersionBytes(Base58Type.BLINDED_ADDRESS, false);
                if (versionBytes == null || !data.StartWith(versionBytes))
                {
                    continue;
                }
                var innerData     = data.Skip(versionBytes.Length).ToArray();
                var versionBytes2 = network.GetVersionBytes(Base58Type.PUBKEY_ADDRESS, false);
                if (!innerData.StartWith(versionBytes2))
                {
                    versionBytes2 = network.GetVersionBytes(Base58Type.SCRIPT_ADDRESS, false);
                    if (!innerData.StartWith(versionBytes2))
                    {
                        continue;
                    }
                    isP2SH = true;
                }

                if (innerData.Length != versionBytes2.Length + 33 + 20)
                {
                    continue;
                }
                try
                {
                    blinding = new PubKey(innerData.SafeSubarray(versionBytes2.Length, 33));
                    var h = innerData.SafeSubarray(versionBytes2.Length + 33, 20);
                    hash = isP2SH ? (TxDestination) new ScriptId(h) : new KeyId(h);
                }
                catch (FormatException) { continue; }
                expectedNetwork = network;
                return(base58);
            }
            throw new FormatException("Invalid BitcoinBlindedAddress");
        }
예제 #15
0
		public BitcoinAddress(TxDestination id, Network network)
			: base(id.ToBytes(), network)
		{
		}
예제 #16
0
 private void SetDestination(TxDestination destination)
 {
     ScriptPubKey = destination.CreateScriptPubKey();
 }
예제 #17
0
 private Key FindKey(TransactionSigningContext ctx, TxDestination id)
 {
     return(_Keys
            .Concat(ctx.AdditionalKeys)
            .FirstOrDefault(k => k.PubKey.Hash == id));
 }
예제 #18
0
 public BitcoinAddress(TxDestination id, Network network)
     : base(id.ToBytes(), network)
 {
 }
예제 #19
0
 private void SetDestination(TxDestination destination)
 {
     ScriptPubKey = destination.CreateScriptPubKey();
 }
예제 #20
0
 protected BitcoinAddress(TxDestination dest, Network network)
     : base(dest.ToBytes(), network)
 {
 }