Exemplo n.º 1
0
        private IPubkeyScript BuildPubkeyScript(string address)
        {
            Address addr = new Address();

            if (!addr.TryGetType(address, out PubkeyScriptType scrType, out byte[] hash))
            {
                throw new FormatException();
            }

            PubkeyScript scr = new PubkeyScript();

            switch (scrType)
            {
            case PubkeyScriptType.P2PKH:
                scr.SetToP2PKH(hash);
                break;

            case PubkeyScriptType.P2SH:
                scr.SetToP2SH(hash);
                break;

            case PubkeyScriptType.P2WPKH:
                scr.SetToP2WPKH(hash);
                break;

            case PubkeyScriptType.P2WSH:
                scr.SetToP2WSH(hash);
                break;

            default:
                throw new NotImplementedException();
            }

            return(scr);
        }
Exemplo n.º 2
0
        public void SetToP2WSH_FromAddressTest()
        {
            PubkeyScript scr = new PubkeyScript();

            scr.SetToP2WSH("bc1qr8rpjl3pgzuaqd8myzu6c7ah2wjpyv7278sa4ld8x94fnnh5zstq6q0csc");
            byte[] expected = Helper.HexToBytes("002019c6197e2140b9d034fb20b9ac7bb753a41233caf1e1dafda7316a99cef41416");
            Assert.Equal(expected, scr.Data);
        }
Exemplo n.º 3
0
        public void SetToP2WSH_FromBytesTest()
        {
            PubkeyScript scr = new PubkeyScript();

            scr.SetToP2WSH(Helper.GetBytes(32));
            byte[] expected = Helper.HexToBytes($"0020{Helper.GetBytesHex(32)}");
            Assert.Equal(expected, scr.Data);
        }
Exemplo n.º 4
0
        public void SetToP2WSH_FromScriptTest()
        {
            PubkeyScript scr  = new PubkeyScript();
            var          mock = new MockSerializableScript(new byte[] { 1, 2, 3 }, 255);

            scr.SetToP2WSH(mock);
            byte[] expected = Helper.HexToBytes("0020039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81");
            Assert.Equal(expected, scr.Data);
        }
Exemplo n.º 5
0
        public void SetToP2WSH_ExceptionTest()
        {
            PubkeyScript scr = new PubkeyScript();

            byte[]  nba   = null;
            IScript nscr  = null;
            string  naddr = null;

            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WSH(nba));
            Assert.Throws <ArgumentOutOfRangeException>(() => scr.SetToP2WSH(new byte[20]));
            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WSH(nscr));
            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WSH(naddr));
            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WSH(""));
            Assert.Throws <FormatException>(() => scr.SetToP2WSH(KeyHelper.Pub1CompAddr));
        }
        public override bool SetOperations()
        {
            Errors = null;

            if (string.IsNullOrEmpty(Address))
            {
                Errors = "Address can not be empty.";
                return(false);
            }
            else if (addrManager.TryGetType(Address, out PubkeyScriptType scrType, out byte[] hash))
            {
                PubkeyScript scr = new PubkeyScript();
                switch (scrType)
                {
                case PubkeyScriptType.P2PKH:
                    scr.SetToP2PKH(hash);
                    break;

                case PubkeyScriptType.P2SH:
                    scr.SetToP2SH(hash);
                    break;

                case PubkeyScriptType.P2WPKH:
                    scr.SetToP2WPKH(hash);
                    break;

                case PubkeyScriptType.P2WSH:
                    scr.SetToP2WSH(hash);
                    break;

                default:
                    Errors = "Undefined script type.";     // This should never happen.
                    return(false);
                }

                OpsToAdd = scr.OperationList;
                return(true);
            }