Exemple #1
0
        public static Script GetScriptPubKey(this string bech32Address)
        {
            if (bech32Address == null)
            {
                throw new ArgumentNullException(nameof(bech32Address));
            }

            if (bech32Address.Length == C.PubKeyHashAddressLength && bech32Address.StartsWith(PubKeyAddressPrefix))
            {
                var hash160 = PubKeyAddressEncoder.Decode(bech32Address, out var witnessVersion);
                CryptoRandom.CheckBytes(hash160, 20);

                if (witnessVersion != 0)
                {
                    InvalidAddress(bech32Address);
                }

                return(new Script(OpcodeType.OP_0, Op.GetPushOp(hash160)));
            }

            if (bech32Address.Length == C.ScriptAddressLength && bech32Address.StartsWith(ScriptAddressPrefix))
            {
                var hash256 = PubKeyAddressEncoder.Decode(bech32Address, out var witnessVersion);
                CryptoRandom.CheckBytes(hash256, 32);

                if (witnessVersion != 0)
                {
                    InvalidAddress(bech32Address);
                }

                return(new Script(OpcodeType.OP_0, Op.GetPushOp(hash256)));
            }

            throw InvalidAddress(bech32Address);
        }
Exemple #2
0
        public static string ToScriptAddress(this byte[] hash256)
        {
            CryptoRandom.CheckBytes(hash256, 32);

            return(ScriptAddressEncoder.Encode(0, hash256));
        }
Exemple #3
0
        public static string ToPubKeyHashAddress(this byte[] hash160)
        {
            CryptoRandom.CheckBytes(hash160, 20);

            return(PubKeyAddressEncoder.Encode(0, hash160));
        }