Esempio n. 1
0
        public static bool TryDecode(string encodedAddress, out Address address)
        {
            byte[] base58DecodedAddress;
            if (!Base58.TryDecode(encodedAddress, out base58DecodedAddress))
            {
                address = null;
                return(false);
            }

            // TODO: This has to be updated to parse pubkey addresses, which have different
            // base58 -decoded lengths than the p2pkh and p2sh addresses.

            if (base58DecodedAddress.Length != 2 + Ripemd160Hash.Length + Checksum.SumLength)
            {
                address = null;
                return(false);
            }

            if (!Checksum.Verify(base58DecodedAddress))
            {
                address = null;
                return(false);
            }

            // All p2pkh and p2sh addresses store the pubkey hash in the same location.
            var hash160 = new byte[Ripemd160Hash.Length];

            Array.Copy(base58DecodedAddress, 2, hash160, 0, Ripemd160Hash.Length);

            var prefix = new AddressPrefix(base58DecodedAddress[0], base58DecodedAddress[1]);
            BlockChainIdentity intendedBlockChain;

            if (AddressPrefix.IdentityForSecp256k1PubkeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecp256k1PubKeyHash(intendedBlockChain, hash160);
                return(true);
            }
            else if (AddressPrefix.IdentityForEd25519PubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToEd25519PubKeyHash(intendedBlockChain, hash160);
                return(true);
            }
            else if (AddressPrefix.IdentityForSecSchnorrPubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecSchnorrPubKeyHash(intendedBlockChain, hash160);
                return(true);
            }
            else if (AddressPrefix.IdentityForScriptHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToScriptHash(intendedBlockChain, hash160);
                return(true);
            }
            else
            {
                address = null;
                return(false);
            }
        }
Esempio n. 2
0
        public static bool TryFromOutputScript(OutputScript pkScript, BlockChainIdentity intendedBlockChain, out Address address)
        {
            var payToPubKeyHashScript = pkScript as OutputScript.Secp256k1PubKeyHash;

            if (payToPubKeyHashScript != null)
            {
                address = new PayToSecp256k1PubKeyHash(intendedBlockChain, payToPubKeyHashScript.Hash160);
                return(true);
            }

            var payToEd25519PubKeyHashScript = pkScript as OutputScript.Ed25519PubKeyHash;

            if (payToEd25519PubKeyHashScript != null)
            {
                address = new PayToEd25519PubKeyHash(intendedBlockChain, payToEd25519PubKeyHashScript.Hash160);
                return(true);
            }

            var payToSecSchnorrPubKeyHash = pkScript as OutputScript.SecSchnorrPubKeyHash;

            if (payToSecSchnorrPubKeyHash != null)
            {
                address = new PayToSecSchnorrPubKeyHash(intendedBlockChain, payToSecSchnorrPubKeyHash.Hash160);
                return(true);
            }

            var payToScriptHashScript = pkScript as OutputScript.ScriptHash;

            if (payToScriptHashScript != null)
            {
                address = new PayToScriptHash(intendedBlockChain, payToScriptHashScript.Hash160);
                return(true);
            }

            address = null;
            return(false);
        }
Esempio n. 3
0
        public static bool TryFromOutputScript(OutputScript pkScript, BlockChainIdentity intendedBlockChain, out Address address)
        {
            var payToPubKeyHashScript = pkScript as OutputScript.Secp256k1PubKeyHash;
            if (payToPubKeyHashScript != null)
            {
                address = new PayToSecp256k1PubKeyHash(intendedBlockChain, payToPubKeyHashScript.Hash160);
                return true;
            }

            var payToEd25519PubKeyHashScript = pkScript as OutputScript.Ed25519PubKeyHash;
            if (payToEd25519PubKeyHashScript != null)
            {
                address = new PayToEd25519PubKeyHash(intendedBlockChain, payToEd25519PubKeyHashScript.Hash160);
                return true;
            }

            var payToSecSchnorrPubKeyHash = pkScript as OutputScript.SecSchnorrPubKeyHash;
            if (payToSecSchnorrPubKeyHash != null)
            {
                address = new PayToSecSchnorrPubKeyHash(intendedBlockChain, payToSecSchnorrPubKeyHash.Hash160);
                return true;
            }

            var payToScriptHashScript = pkScript as OutputScript.ScriptHash;
            if (payToScriptHashScript != null)
            {
                address = new PayToScriptHash(intendedBlockChain, payToScriptHashScript.Hash160);
                return true;
            }

            address = null;
            return false;
        }
Esempio n. 4
0
        public static bool TryDecode(string encodedAddress, out Address address)
        {
            byte[] base58DecodedAddress;
            if (!Base58.TryDecode(encodedAddress, out base58DecodedAddress))
            {
                address = null;
                return false;
            }

            // TODO: This has to be updated to parse pubkey addresses, which have different
            // base58 -decoded lengths than the p2pkh and p2sh addresses.

            if (base58DecodedAddress.Length != 2 + Ripemd160Hash.Length + Checksum.SumLength)
            {
                address = null;
                return false;
            }

            if (!Checksum.Verify(base58DecodedAddress))
            {
                address = null;
                return false;
            }

            // All p2pkh and p2sh addresses store the pubkey hash in the same location.
            var hash160 = new byte[Ripemd160Hash.Length];
            Array.Copy(base58DecodedAddress, 2, hash160, 0, Ripemd160Hash.Length);

            var prefix = new AddressPrefix(base58DecodedAddress[0], base58DecodedAddress[1]);
            BlockChainIdentity intendedBlockChain;
            if (AddressPrefix.IdentityForSecp256k1PubkeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecp256k1PubKeyHash(intendedBlockChain, hash160);
                return true;
            }
            else if (AddressPrefix.IdentityForEd25519PubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToEd25519PubKeyHash(intendedBlockChain, hash160);
                return true;
            }
            else if (AddressPrefix.IdentityForSecSchnorrPubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecSchnorrPubKeyHash(intendedBlockChain, hash160);
                return true;
            }
            else if (AddressPrefix.IdentityForScriptHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToScriptHash(intendedBlockChain, hash160);
                return true;
            }
            else
            {
                address = null;
                return false;
            }
        }