Ejemplo n.º 1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="pubkey">schnorr pubkey</param>
 /// <param name="controlBlock">control block</param>
 /// <param name="tapLeafHash">tapleaf hash</param>
 /// <param name="tapScript">tapscript</param>
 public TaprootScriptData(SchnorrPubkey pubkey, ByteData controlBlock,
                          ByteData256 tapLeafHash, Script tapScript)
 {
     Pubkey       = pubkey;
     ControlBlock = controlBlock;
     TapLeafHash  = tapLeafHash;
     TapScript    = tapScript;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructor. (default)
 /// </summary>
 public TapBranch()
 {
     tapscript   = new Script();
     leafVersion = 0;
     branches    = Array.Empty <TapBranch>();
     topHash     = new ByteData256();
     treeString  = "";
     targetNodes = Array.Empty <ByteData256>();
 }
Ejemplo n.º 3
0
 internal void Initialize(string treeString, Script tapscript, ByteData256[] targetNodes)
 {
     this.treeString  = treeString;
     this.targetNodes = targetNodes;
     branches         = Array.Empty <TapBranch>();
     topHash          = new ByteData256();
     this.tapscript   = tapscript;
     leafVersion      = tapscriptLeafVersion;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// constructor
 /// </summary>
 /// <param name="hash">branch hash</param>
 public TapBranch(ByteData256 hash)
 {
     if (hash is null)
     {
         throw new ArgumentNullException(nameof(hash));
     }
     if (hash.GetSize() != 32)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Invalid branch hash size.");
     }
     treeString  = hash.ToHexString();
     targetNodes = Array.Empty <ByteData256>();
     branches    = Array.Empty <TapBranch>();
     topHash     = hash;
     tapscript   = new Script();
     leafVersion = 0;
 }
Ejemplo n.º 5
0
        void UpdateAllData(ErrorHandle handle, TreeHandle treeHandle)
        {
            var ret = NativeMethods.CfdGetTapBranchCount(
                handle.GetHandle(), treeHandle.GetHandle(), out uint count);

            if (ret != CfdErrorCode.Success)
            {
                handle.ThrowError(ret);
            }
            TapBranch[] branchList = new TapBranch[count];
            var         offset     = branches.Length;

            for (var index = offset; index < count; ++index)
            {
                ret = NativeMethods.CfdGetTapBranchHandle(
                    handle.GetHandle(), treeHandle.GetHandle(), (byte)index,
                    out IntPtr branchHash, out IntPtr branchTreeHandle);
                if (ret != CfdErrorCode.Success)
                {
                    handle.ThrowError(ret);
                }
                CCommon.ConvertToString(branchHash);
                using (var branchHandle = new TreeHandle(handle, branchTreeHandle))
                {
                    TapBranch branch = new TapBranch();
                    branch.GetAllData(handle, branchHandle);
                    branchList[index] = branch;
                }
            }
            if (count != 0)
            {
                var newTargetNodes = new ByteData256[count];
                for (var index = 0; index < offset; ++index)
                {
                    branchList[index]     = branches[index];
                    newTargetNodes[index] = targetNodes[index];
                }
                for (var index = offset; index < count; ++index)
                {
                    newTargetNodes[index] = branchList[index].topHash;
                }
                branches    = branchList;
                targetNodes = newTargetNodes;
            }
            UpdateBranchData(handle, treeHandle);
        }
Ejemplo n.º 6
0
        void UpdateBranchData(ErrorHandle handle, TreeHandle treeHandle)
        {
            if (handle is null)
            {
                throw new ArgumentNullException(nameof(handle));
            }
            if (treeHandle is null)
            {
                throw new ArgumentNullException(nameof(treeHandle));
            }

            var ret = NativeMethods.CfdGetTaprootScriptTreeSrting(handle.GetHandle(),
                                                                  treeHandle.GetHandle(), out IntPtr treeString);

            if (ret != CfdErrorCode.Success)
            {
                handle.ThrowError(ret);
            }
            this.treeString = CCommon.ConvertToString(treeString);
            if (branches.Length != 0)
            {
                var index = branches.Length - 1;
                ret = NativeMethods.CfdGetTapBranchData(handle.GetHandle(),
                                                        treeHandle.GetHandle(), (byte)index, true, out IntPtr branchHash,
                                                        out _, out IntPtr tempScript, out byte _);
                if (ret != CfdErrorCode.Success)
                {
                    handle.ThrowError(ret);
                }
                CCommon.ConvertToString(tempScript);
                topHash = new ByteData256(CCommon.ConvertToString(branchHash));
            }
            else if (topHash.IsEmpty())
            {
                ret = NativeMethods.CfdGetBaseTapLeaf(handle.GetHandle(),
                                                      treeHandle.GetHandle(), out byte _,
                                                      out IntPtr tapscript, out IntPtr tapLeafHash);
                if (ret != CfdErrorCode.Success)
                {
                    handle.ThrowError(ret);
                }
                CCommon.ConvertToString(tapscript);
                string tapLeafHashStr = CCommon.ConvertToString(tapLeafHash);
                topHash = new ByteData256(tapLeafHashStr);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="hash">branch hash</param>
        public TapBranch(string treeStr)
        {
            if (treeStr is null)
            {
                throw new ArgumentNullException(nameof(treeStr));
            }
            treeString  = treeStr;
            targetNodes = Array.Empty <ByteData256>();
            branches    = Array.Empty <TapBranch>();
            topHash     = new ByteData256();
            tapscript   = new Script();
            leafVersion = 0;

            using (var handle = new ErrorHandle())
                using (var treeHandle = new TreeHandle(handle))
                {
                    Load(handle, treeHandle);
                    GetAllData(handle, treeHandle);
                }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="tapscript">tapscript</param>
        /// <param name="leafVersion">leaf version</param>
        public TapBranch(Script tapscript, byte leafVersion)
        {
            if (tapscript is null)
            {
                throw new ArgumentNullException(nameof(tapscript));
            }
            this.tapscript   = tapscript;
            this.leafVersion = leafVersion;
            treeString       = ConvertTapscriptToString(tapscript, leafVersion);
            targetNodes      = Array.Empty <ByteData256>();
            branches         = Array.Empty <TapBranch>();
            topHash          = new ByteData256();

            using (var handle = new ErrorHandle())
                using (var treeHandle = new TreeHandle(handle))
                {
                    Load(handle, treeHandle);
                    UpdateBranchData(handle, treeHandle);
                }
        }
Ejemplo n.º 9
0
        internal void GetAllData(ErrorHandle handle, TreeHandle treeHandle)
        {
            var tree  = GetBranchData(handle, treeHandle);
            var count = tree.branches.Length;

            TapBranch[]   branchList = new TapBranch[count];
            ByteData256[] nodeList   = new ByteData256[count];
            for (var index = 0; index < count; ++index)
            {
                var ret = NativeMethods.CfdGetTapBranchHandle(
                    handle.GetHandle(), treeHandle.GetHandle(), (byte)index,
                    out IntPtr branchHash, out IntPtr branchTreeHandle);
                if (ret != CfdErrorCode.Success)
                {
                    handle.ThrowError(ret);
                }
                CCommon.ConvertToString(branchHash);
                using (var branchHandle = new TreeHandle(handle, branchTreeHandle))
                {
                    TapBranch branch = new TapBranch();
                    branch.GetAllData(handle, branchHandle);
                    branchList[index] = branch;
                }
            }
            if (count != 0)
            {
                branches = branchList;
            }
            topHash    = tree.topHash;
            treeString = tree.treeString;
            if (!tree.tapscript.IsEmpty())
            {
                tapscript   = tree.tapscript;
                leafVersion = tree.leafVersion;
                for (var index = 0; index < count; ++index)
                {
                    nodeList[index] = branchList[index].topHash;
                }
                targetNodes = nodeList;
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="treeStr">tree string</param>
        /// <param name="tapscript">tapscript</param>
        /// <param name="targetNodes">target node list</param>
        public TapBranch(string treeStr, Script tapscript, ByteData256[] targetNodes)
        {
            if (treeStr is null)
            {
                throw new ArgumentNullException(nameof(treeStr));
            }
            if (tapscript is null)
            {
                throw new ArgumentNullException(nameof(tapscript));
            }
            ByteData256[] nodes = Array.Empty <ByteData256>();
            if (!(targetNodes is null))
            {
                foreach (var node in targetNodes)
                {
                    if (node.GetSize() != 32)
                    {
                        CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Invalid branch hash size.");
                    }
                }
                nodes = targetNodes;
            }
            treeString       = treeStr;
            this.targetNodes = nodes;
            branches         = Array.Empty <TapBranch>();
            topHash          = new ByteData256();
            this.tapscript   = tapscript;
            leafVersion      = tapscriptLeafVersion;

            using (var handle = new ErrorHandle())
                using (var treeHandle = new TreeHandle(handle))
                {
                    Load(handle, treeHandle);
                    GetAllData(handle, treeHandle);
                }
        }