コード例 #1
0
        /// <summary>
        /// Encodes the given decoded address into cashaddr format
        /// </summary>
        /// <param name="decoded"></param>
        /// <returns></returns>
        public static string EncodeAsCashaddr(BchAddrData decoded)
        {
            var prefix = GetCashaddrkPrefix(decoded);
            var type   = decoded.Type == CashType.P2PKH ? "P2PKH" : "P2SH";
            var hash   = decoded.Hash;

            return(CashAddr.Encode(prefix, type, hash));
        }
コード例 #2
0
        /// <summary>
        /// Attempts to decode the given address assuming it is a cashaddr address with explicit prefix
        /// </summary>
        /// <param name="address">A valid Bitcoin Cash address in any format</param>
        /// <returns></returns>
        public static BchAddrData DecodeCashAddressWithPrefix(string address)
        {
            var decoded = CashAddr.Decode(address);
            var type    = decoded.Type == "P2PKH" ? CashType.P2PKH : CashType.P2SH;

            switch (decoded.Prefix)
            {
            case "bitcoincash":
                return(BchAddrData.Create(CashFormat.Cashaddr, CashNetwork.Mainnet, type, decoded.Hash));

            case "devault":
                return(BchAddrData.Create(CashFormat.Cashaddr, CashNetwork.DevaultMainnet, type, decoded.Hash));

            case "bchtest":
                return(BchAddrData.Create(CashFormat.Cashaddr, CashNetwork.Testnet, type, decoded.Hash));

            case "regtest":
                return(BchAddrData.Create(CashFormat.Cashaddr, CashNetwork.RegTest, type, decoded.Hash));
            }
            throw new Validation.ValidationError($"Invalid address {address}");
        }