Exemple #1
0
 public ArraySegment <CfdKeyData> GetKeyListAll()
 {
     if (KeyData.KeyType == CfdDescriptorKeyType.Null)
     {
         return(MultisigKeyList);
     }
     CfdKeyData[] keyList = new CfdKeyData[1];
     keyList[0] = KeyData;
     return(new ArraySegment <CfdKeyData>(keyList));
 }
Exemple #2
0
 public CfdKeyData[] GetMultisigKeyList()
 {
     if ((HasKeyHash()) || (rootData.MultisigRequireNum == 0))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalStateError,
                              "Failed to script hash or multisig key.");
     }
     CfdKeyData[] keyList = new CfdKeyData[rootData.MultisigKeyList.Count];
     rootData.MultisigKeyList.CopyTo(keyList);
     return(keyList);
 }
Exemple #3
0
 public CfdDescriptorScriptData(CfdDescriptorScriptType scriptType, uint depth,
                                CfdHashType hashType, Address address, Script redeemScript)
 {
     ScriptType         = scriptType;
     Depth              = depth;
     HashType           = hashType;
     Address            = address;
     RedeemScript       = redeemScript;
     KeyData            = new CfdKeyData();
     MultisigKeyList    = new ArraySegment <CfdKeyData>();
     MultisigRequireNum = 0;
 }
Exemple #4
0
 public CfdDescriptorScriptData(CfdDescriptorScriptType scriptType, uint depth,
                                CfdHashType hashType, Address address, CfdKeyData keyData)
 {
     ScriptType         = scriptType;
     Depth              = depth;
     HashType           = hashType;
     Address            = address;
     RedeemScript       = new Script();
     KeyData            = keyData;
     MultisigKeyList    = new ArraySegment <CfdKeyData>();
     MultisigRequireNum = 0;
     ScriptTree         = new TapBranch();
 }
Exemple #5
0
 public CfdDescriptorScriptData(CfdDescriptorScriptType scriptType, uint depth,
                                Script redeemScript)
 {
     ScriptType         = scriptType;
     Depth              = depth;
     HashType           = CfdHashType.P2sh;
     Address            = new Address();
     RedeemScript       = redeemScript;
     KeyData            = new CfdKeyData();
     MultisigKeyList    = new ArraySegment <CfdKeyData>();
     MultisigRequireNum = 0;
     ScriptTree         = new TaprootScriptTree();
 }
Exemple #6
0
 public CfdDescriptorScriptData(CfdDescriptorScriptType scriptType, uint depth,
                                CfdHashType hashType, Address address, Script redeemScript,
                                CfdKeyData keyData, CfdKeyData[] multisigKeyList, uint multisigRequireNum)
 {
     ScriptType         = scriptType;
     Depth              = depth;
     HashType           = hashType;
     Address            = address;
     RedeemScript       = redeemScript;
     KeyData            = keyData;
     MultisigKeyList    = new ArraySegment <CfdKeyData>(multisigKeyList);
     MultisigRequireNum = multisigRequireNum;
 }
Exemple #7
0
        private static CfdDescriptorScriptData[] ParseDescriptor(
            ErrorHandle handle, string descriptorString,
            string derivePath, CfdNetworkType network, out CfdDescriptorScriptData rootData)
        {
            var ret = NativeMethods.CfdParseDescriptor(
                handle.GetHandle(), descriptorString, (int)network, derivePath,
                out IntPtr descriptorHandle, out uint maxIndex);

            if (ret != CfdErrorCode.Success)
            {
                handle.ThrowError(ret);
            }
            try
            {
                bool isMultisig = false;
                uint maxKeyNum  = 0;
                uint requireNum = 0;
                {
                    ret = NativeMethods.CfdGetDescriptorRootData(
                        handle.GetHandle(), descriptorHandle,
                        out int scriptType, out IntPtr lockingScript, out IntPtr address,
                        out int hashType, out IntPtr redeemScript,
                        out int keyType, out IntPtr pubkey, out IntPtr extPubkey,
                        out IntPtr extPrivkey, out IntPtr schnorrPubkey, out IntPtr treeString,
                        out isMultisig, out maxKeyNum, out requireNum);
                    if (ret != CfdErrorCode.Success)
                    {
                        handle.ThrowError(ret);
                    }
                    CCommon.ConvertToString(lockingScript);
                    string tempAddress        = CCommon.ConvertToString(address);
                    string tempRedeemScript   = CCommon.ConvertToString(redeemScript);
                    string tempPubkey         = CCommon.ConvertToString(pubkey);
                    string tempExtPubkey      = CCommon.ConvertToString(extPubkey);
                    string tempExtPrivkey     = CCommon.ConvertToString(extPrivkey);
                    string tempSchnorr        = CCommon.ConvertToString(schnorrPubkey);
                    string tempTreeStr        = CCommon.ConvertToString(treeString);
                    CfdDescriptorKeyType type = (CfdDescriptorKeyType)keyType;
                    CfdKeyData           keyData;
                    TapBranch            scriptTree = new TapBranch();
                    if (type == CfdDescriptorKeyType.Bip32)
                    {
                        keyData = new CfdKeyData(new ExtPubkey(tempExtPubkey));
                    }
                    else if (type == CfdDescriptorKeyType.Bip32Priv)
                    {
                        keyData = new CfdKeyData(new ExtPrivkey(tempExtPrivkey));
                    }
                    else if (type == CfdDescriptorKeyType.SchnorrPubkey)
                    {
                        keyData = new CfdKeyData(new SchnorrPubkey(tempSchnorr));
                    }
                    else if (type == CfdDescriptorKeyType.Public)
                    {
                        keyData = new CfdKeyData(new Pubkey(tempPubkey));
                    }
                    else
                    {
                        keyData = new CfdKeyData();
                    }
                    Address addr = new Address();
                    if (tempAddress.Length > 0)
                    {
                        addr = new Address(tempAddress);
                    }
                    if (tempTreeStr.Length > 0)
                    {
                        scriptTree = new TapBranch(tempTreeStr);
                    }
                    rootData = new CfdDescriptorScriptData((CfdDescriptorScriptType)scriptType, 0,
                                                           (CfdHashType)hashType, addr, new Script(tempRedeemScript),
                                                           scriptTree, keyData, Array.Empty <CfdKeyData>(), 0);
                }
                CfdDescriptorScriptData[] list = new CfdDescriptorScriptData[maxIndex + 1];
                for (uint index = 0; index <= maxIndex; ++index)
                {
                    // force initialized because guard illegal memory access.
                    IntPtr lockingScript = IntPtr.Zero;
                    IntPtr redeemScript  = IntPtr.Zero;
                    IntPtr address       = IntPtr.Zero;
                    IntPtr pubkey        = IntPtr.Zero;
                    IntPtr extPubkey     = IntPtr.Zero;
                    IntPtr extPrivkey    = IntPtr.Zero;
                    ret = NativeMethods.CfdGetDescriptorData(
                        handle.GetHandle(), descriptorHandle, index, out _, out uint depth,
                        out int scriptType, out lockingScript, out address,
                        out int hashType, out redeemScript,
                        out int keyType, out pubkey, out extPubkey, out extPrivkey,
                        out isMultisig, out maxKeyNum, out requireNum);
                    if (ret != CfdErrorCode.Success)
                    {
                        handle.ThrowError(ret);
                    }
                    CCommon.ConvertToString(lockingScript);
                    string tempAddress      = CCommon.ConvertToString(address);
                    string tempRedeemScript = CCommon.ConvertToString(redeemScript);
                    string tempPubkey       = CCommon.ConvertToString(pubkey);
                    string tempExtPubkey    = CCommon.ConvertToString(extPubkey);
                    string tempExtPrivkey   = CCommon.ConvertToString(extPrivkey);
                    CfdDescriptorScriptData data;
                    CfdKeyData           keyData;
                    CfdDescriptorKeyType type;
                    switch ((CfdDescriptorScriptType)scriptType)
                    {
                    case CfdDescriptorScriptType.Combo:
                    case CfdDescriptorScriptType.Pk:
                    case CfdDescriptorScriptType.Pkh:
                    case CfdDescriptorScriptType.Wpkh:
                    case CfdDescriptorScriptType.Taproot:
                        type = (CfdDescriptorKeyType)keyType;
                        if (type == CfdDescriptorKeyType.Bip32)
                        {
                            keyData = new CfdKeyData(new ExtPubkey(tempExtPubkey));
                        }
                        else if (type == CfdDescriptorKeyType.Bip32Priv)
                        {
                            keyData = new CfdKeyData(new ExtPrivkey(tempExtPrivkey));
                        }
                        else if (type == CfdDescriptorKeyType.SchnorrPubkey)
                        {
                            keyData = new CfdKeyData(new SchnorrPubkey(tempPubkey));
                        }
                        else
                        {
                            keyData = new CfdKeyData(new Pubkey(tempPubkey));
                        }
                        data = new CfdDescriptorScriptData(
                            (CfdDescriptorScriptType)scriptType,
                            depth, (CfdHashType)hashType, new Address(tempAddress), keyData);
                        break;

                    case CfdDescriptorScriptType.Sh:
                    case CfdDescriptorScriptType.Wsh:
                    case CfdDescriptorScriptType.Multi:
                    case CfdDescriptorScriptType.SortedMulti:
                        if (isMultisig)
                        {
                            CfdKeyData[] keyList = new CfdKeyData[maxKeyNum];
                            for (uint multisigIndex = 0; multisigIndex < maxKeyNum; ++multisigIndex)
                            {
                                IntPtr multisigPubkey     = IntPtr.Zero;
                                IntPtr multisigExtPubkey  = IntPtr.Zero;
                                IntPtr multisigExtPrivkey = IntPtr.Zero;
                                ret = NativeMethods.CfdGetDescriptorMultisigKey(handle.GetHandle(), descriptorHandle,
                                                                                multisigIndex, out int multisigKeyType, out multisigPubkey,
                                                                                out multisigExtPubkey, out multisigExtPrivkey);
                                if (ret != CfdErrorCode.Success)
                                {
                                    handle.ThrowError(ret);
                                }
                                tempPubkey     = CCommon.ConvertToString(multisigPubkey);
                                tempExtPubkey  = CCommon.ConvertToString(multisigExtPubkey);
                                tempExtPrivkey = CCommon.ConvertToString(multisigExtPrivkey);
                                type           = (CfdDescriptorKeyType)multisigKeyType;
                                if (type == CfdDescriptorKeyType.Bip32)
                                {
                                    keyList[multisigIndex] = new CfdKeyData(new ExtPubkey(tempExtPubkey));
                                }
                                else if (type == CfdDescriptorKeyType.Bip32Priv)
                                {
                                    keyList[multisigIndex] = new CfdKeyData(new ExtPrivkey(tempExtPrivkey));
                                }
                                else
                                {
                                    keyList[multisigIndex] = new CfdKeyData(new Pubkey(tempPubkey));
                                }
                            }
                            data = new CfdDescriptorScriptData(
                                (CfdDescriptorScriptType)scriptType,
                                depth, (CfdHashType)hashType, new Address(tempAddress),
                                new Script(tempRedeemScript), keyList, requireNum);
                            rootData = new CfdDescriptorScriptData(rootData.ScriptType, 0,
                                                                   rootData.HashType, rootData.Address, rootData.RedeemScript,
                                                                   rootData.KeyData, keyList, requireNum);
                        }
                        else
                        {
                            data = new CfdDescriptorScriptData(
                                (CfdDescriptorScriptType)scriptType,
                                depth, (CfdHashType)hashType, new Address(tempAddress),
                                new Script(tempRedeemScript));
                        }
                        break;

                    case CfdDescriptorScriptType.Raw:
                        data = new CfdDescriptorScriptData(
                            (CfdDescriptorScriptType)scriptType,
                            depth, new Script(tempRedeemScript));
                        break;

                    case CfdDescriptorScriptType.Addr:
                        data = new CfdDescriptorScriptData(
                            (CfdDescriptorScriptType)scriptType,
                            depth, (CfdHashType)hashType, new Address(tempAddress));
                        break;

                    default:
                        data = new CfdDescriptorScriptData();
                        break;
                    }
                    list[index] = data;

                    if (scriptType == (int)CfdDescriptorScriptType.Combo)
                    {
                        // TODO: combo data is top only.
                        CfdDescriptorScriptData[] newList = { list[0] };
                        list = newList;
                        break;
                    }
                }
                return(list);
            }
            finally
            {
                NativeMethods.CfdFreeDescriptorHandle(
                    handle.GetHandle(), descriptorHandle);
            }
        }