예제 #1
0
        public static Types.Script ParseAddress(string address, string prefix)
        {
            (string hrp, int[] data) = ConvertAddress.Decode(address);

            if (hrp != prefix)
            {
                throw new Exception($"Invalid prefix! Expected: {prefix}, actual: {hrp}");
            }
            if (data[0] == 1)
            {
                if (data.Length < 2)
                {
                    throw new Exception("Invalid payload length!");
                }
                string codeHash;
                string hashType;
                if (data[1] == SecpShortId)
                {
                    codeHash = SecpCodeHash;
                    hashType = SecpHashType;
                }
                else if (data[1] == MultisigShortId)
                {
                    codeHash = MultisigCodeHash;
                    hashType = MultisigHashType;
                }
                else
                {
                    throw new Exception("Short address format error!");
                }

                return(new Types.Script()
                {
                    CodeHash = codeHash,
                    HashType = hashType,
                    Args = IntsToHex(data.Skip(2).ToArray())
                });
            }
            else if (data[0] == 2 || data[0] == 4)
            {
                if (data.Length < 33)
                {
                    throw new Exception("Invalid payload length!");
                }

                return(new Types.Script()
                {
                    CodeHash = IntsToHex(data.Skip(1).Take(32).ToArray()),
                    HashType = data[0] == 2 ? "data" : "type",
                    Args = IntsToHex(data.Skip(33).ToArray())
                });
            }
            throw new Exception($"Invalid payload format type: {data[0]}");
        }
예제 #2
0
        public static string GenerateAddress(Types.Script script, string prefix)
        {
            List <int> data    = new List <int>();
            int?       shortId = null;

            if (script.CodeHash == SecpCodeHash && script.HashType == SecpHashType)
            {
                shortId = SecpShortId;
            }
            else if (script.CodeHash == MultisigCodeHash && script.HashType == MultisigHashType)
            {
                shortId = MultisigShortId;
            }
            if (shortId != null)
            {
                data.Add(1);
                data.Add((int)shortId);
                foreach (byte c in Types.Convert.HexStringToBytes(script.Args))
                {
                    data.Add(Convert.ToInt32(c));
                }
            }
            else
            {
                data.Add(script.HashType == "type" ? 4 : 2);
                foreach (byte c in Types.Convert.HexStringToBytes(script.CodeHash))
                {
                    data.Add(Convert.ToInt32(c));
                }
                foreach (byte c in Types.Convert.HexStringToBytes(script.Args))
                {
                    data.Add(Convert.ToInt32(c));
                }
            }
            string addr = ConvertAddress.Encode(prefix, data.ToArray());

            return(addr);
        }