Exemple #1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="hex">hex string</param>
 public SchnorrPubkey(string hex)
 {
     if ((hex == null) || (hex.Length != Size * 2))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to pubkey size.");
     }
     data = hex;
 }
Exemple #2
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="bytes">byte array</param>
 public SchnorrPubkey(byte[] bytes)
 {
     if ((bytes == null) || (bytes.Length != Size))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to pubkey size.");
     }
     data = StringUtil.FromBytes(bytes);
 }
Exemple #3
0
 public ByteData ToDerEncode()
 {
     if (!isSetDerEncode)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalStateError, "Failed to unset der encode flag.");
     }
     return(ToDerEncode(signatureHashType));
 }
 /// <summary>
 /// get bytes from array.
 /// </summary>
 /// <param name="index">array index</param>
 /// <returns>byte array</returns>
 public byte[] GetBytes(uint index)
 {
     if (index >= hexArray.Length)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to index.");
     }
     return(StringUtil.ToBytes(hexArray[index]));
 }
 /// <summary>
 /// get hex string from array.
 /// </summary>
 /// <param name="index">array index</param>
 /// <returns>hex string</returns>
 public string ToHexString(uint index)
 {
     if (index >= hexArray.Length)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to index.");
     }
     return(hexArray[index]);
 }
 /// <summary>
 /// Get tapscript if existing tapscript on branch root.
 /// </summary>
 /// <returns></returns>
 public Script GetTapScript()
 {
     if (tapscript.IsEmpty())
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalStateError, "tapscript not found.");
     }
     return(tapscript);
 }
Exemple #7
0
 public Txid(string txid)
 {
     if ((txid == null) || (txid.Length != Size * 2))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to txid size.");
     }
     this.txid = txid;
 }
Exemple #8
0
 public TapBranch GetScriptTree()
 {
     if (!HasTapScript())
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalStateError,
                              "Failed to unused script tree.");
     }
     return(rootData.ScriptTree);
 }
Exemple #9
0
 public CfdKeyData GetKeyData()
 {
     if (rootData.KeyData.KeyType == CfdDescriptorKeyType.Null)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalStateError,
                              "Failed to script hash or multisig key.");
     }
     return(rootData.KeyData);
 }
 /// <summary>
 /// get asset bytes.
 /// </summary>
 /// <returns>asset bytes. blinded is 33 bytes, else is 32 bytes.</returns>
 public byte[] ToBytes()
 {
     if (commitment.Length == (Size * 2))
     {
         var assetBytes = StringUtil.ToBytes(commitment);
         return(CfdCommon.ReverseBytes(assetBytes));
     }
     return(StringUtil.ToBytes(commitment));
 }
Exemple #11
0
        public Txid(byte[] bytes)
        {
            if ((bytes == null) || (bytes.Length != Size))
            {
                CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to txid size.");
            }
            var txidBytes = CfdCommon.ReverseBytes(bytes);

            txid = StringUtil.FromBytes(txidBytes);
        }
Exemple #12
0
 /// <summary>
 /// byte array.
 /// </summary>
 /// <returns>byte array</returns>
 public byte[] ToBytes()
 {
     if (hasReverse)
     {
         return(CfdCommon.ReverseBytes(StringUtil.ToBytes(data)));
     }
     else
     {
         return(StringUtil.ToBytes(data));
     }
 }
Exemple #13
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="bytes">byte data array</param>
 public SignParameter(byte[] bytes)
 {
     if (bytes == null)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to txid size.");
     }
     data = StringUtil.FromBytes(bytes);
     signatureHashType = new SignatureHashType(CfdSighashType.All, false);
     pubkey            = new Pubkey();
     isSetDerEncode    = false;
 }
Exemple #14
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="hex">byte data hex</param>
 public SignParameter(string hex)
 {
     if (hex == null)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to txid size.");
     }
     data = hex;
     signatureHashType = new SignatureHashType(CfdSighashType.All, false);
     pubkey            = new Pubkey();
     isSetDerEncode    = false;
 }
Exemple #15
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 #16
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="bytes">privkey byte array</param>
 public Privkey(byte[] bytes)
 {
     if ((bytes == null) || (bytes.Length != Size))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to privkey size.");
     }
     privkey      = StringUtil.FromBytes(bytes);
     networkType  = CfdNetworkType.Mainnet;
     privkeyWif   = "";
     isCompressed = true;
 }
Exemple #17
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="hex">hex string</param>
 public SchnorrSignature(string hex)
 {
     if ((hex == null) || (hex.Length != Size * 2))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to signature size.");
     }
     data = hex;
     string[] list = Verify(data);
     nonce = new SchnorrPubkey(list[0]);
     key   = new Privkey(list[1]);
 }
Exemple #18
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="bytes">byte array</param>
 public SchnorrSignature(byte[] bytes)
 {
     if ((bytes == null) || (bytes.Length != Size))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to signature size.");
     }
     data = StringUtil.FromBytes(bytes);
     string[] list = Verify(data);
     nonce = new SchnorrPubkey(list[0]);
     key   = new Privkey(list[1]);
 }
 /// <summary>
 /// constructor.
 /// </summary>
 /// <param name="asset">asset string.</param>
 public ConfidentialAsset(string asset)
 {
     if (asset is null)
     {
         throw new ArgumentNullException(nameof(asset));
     }
     if ((asset.Length != Size * 2) && (asset.Length != CommitmentSize * 2))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to asset size.");
     }
     commitment = asset;
 }
 /// <summary>
 /// Add branch.
 /// </summary>
 /// <param name="hash">branch hash.</param>
 public void AddBranch(ByteData hash)
 {
     if (hash is null)
     {
         throw new ArgumentNullException(nameof(hash));
     }
     if (hash.GetSize() != 32)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Invalid branch hash size.");
     }
     AddBranch(hash.ToHexString());
 }
Exemple #21
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="bytes">pubkey byte array</param>
 public Pubkey(byte[] bytes)
 {
     if (bytes is null)
     {
         throw new ArgumentNullException(nameof(bytes));
     }
     else if (bytes.Length > UncompressLength)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to pubkey size.");
     }
     pubkey = StringUtil.FromBytes(bytes);
     // check format
     Validate(pubkey);
 }
Exemple #22
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="pubkeyHex">pubkey hex string</param>
 public Pubkey(string pubkeyHex)
 {
     if (pubkeyHex is null)
     {
         throw new ArgumentNullException(nameof(pubkeyHex));
     }
     else if (pubkeyHex.Length > UncompressLength * 2)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to pubkey size.");
     }
     pubkey = pubkeyHex;
     // check format
     Validate(pubkey);
 }
Exemple #23
0
        public Script GetRedeemScript()
        {
            switch (rootData.HashType)
            {
            case CfdHashType.P2sh:
            case CfdHashType.P2wsh:
            case CfdHashType.P2shP2wsh:
                return(rootData.RedeemScript);

            default:
                CfdCommon.ThrowError(CfdErrorCode.IllegalStateError,
                                     "Failed to unused script descriptor.");
                return(null);
            }
        }
Exemple #24
0
 /// <summary>
 /// constructor.
 /// </summary>
 /// <param name="bytes">script byte array.</param>
 public Script(byte[] bytes)
 {
     if (bytes is null)
     {
         throw new ArgumentNullException(nameof(bytes));
     }
     if (bytes.Length > MaxSize)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to script size.");
     }
     script = StringUtil.FromBytes(bytes);
     using (var handle = new ErrorHandle())
     {
         scriptItems = ParseScript(handle, script);
     }
 }
Exemple #25
0
 /// <summary>
 /// constructor.
 /// </summary>
 /// <param name="scriptHex">script hex string</param>
 public Script(string scriptHex)
 {
     if (scriptHex is null)
     {
         throw new ArgumentNullException(nameof(scriptHex));
     }
     if (scriptHex.Length > MaxSize * 2)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to script size.");
     }
     script = scriptHex;
     using (var handle = new ErrorHandle())
     {
         scriptItems = ParseScript(handle, scriptHex);
     }
 }
 /// <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;
 }
Exemple #27
0
        /// <summary>
        /// combine pubkeys.
        /// </summary>
        /// <param name="pubkeyList">pubkey list</param>
        /// <returns>combined pubkey</returns>
        public static Pubkey Combine(Pubkey[] pubkeyList)
        {
            if (pubkeyList is null)
            {
                throw new ArgumentNullException(nameof(pubkeyList));
            }
            else if (pubkeyList.Length < 2)
            {
                CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError,
                                     "Failed to pubkey list length. minimum length is 2.");
            }
            using (var handle = new ErrorHandle())
            {
                var ret = NativeMethods.CfdInitializeCombinePubkey(
                    handle.GetHandle(),
                    out IntPtr combineHandle);
                if (ret != CfdErrorCode.Success)
                {
                    handle.ThrowError(ret);
                }
                try
                {
                    foreach (Pubkey pubkeyItem in pubkeyList)
                    {
                        ret = NativeMethods.CfdAddCombinePubkey(
                            handle.GetHandle(), combineHandle, pubkeyItem.ToHexString());
                        if (ret != CfdErrorCode.Success)
                        {
                            handle.ThrowError(ret);
                        }
                    }

                    ret = NativeMethods.CfdFinalizeCombinePubkey(
                        handle.GetHandle(), combineHandle, out IntPtr combinedKey);
                    if (ret != CfdErrorCode.Success)
                    {
                        handle.ThrowError(ret);
                    }
                    return(new Pubkey(CCommon.ConvertToString(combinedKey)));
                }
                finally
                {
                    NativeMethods.CfdFreeCombinePubkeyHandle(handle.GetHandle(), combineHandle);
                }
            }
        }
Exemple #28
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="hex">hex string</param>
 /// <param name="hasReverse">reversed data</param>
 public ByteData256(string hex, bool hasReverse)
 {
     if (hex is null)
     {
         throw new ArgumentNullException(nameof(hex));
     }
     if (hex.Length == 0)
     {
         data = emptyString;
     }
     else if (hex.Length != Size * 2)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to ByteData256 size.");
     }
     else
     {
         data = hex;
     }
     this.hasReverse = hasReverse;
 }
Exemple #29
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="bytes">byte array</param>
 public ByteData256(byte[] bytes)
 {
     if (bytes is null)
     {
         throw new ArgumentNullException(nameof(bytes));
     }
     if (bytes.Length == 0)
     {
         data = emptyString;
     }
     else if (bytes.Length != Size)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to ByteData256 size.");
     }
     else
     {
         data = StringUtil.FromBytes(bytes);
     }
     hasReverse = false;
 }
 /// <summary>
 /// constructor.
 /// </summary>
 /// <param name="asset">asset bytes.</param>
 public ConfidentialAsset(byte[] asset)
 {
     if (asset is null)
     {
         throw new ArgumentNullException(nameof(asset));
     }
     if ((asset.Length != Size) && (asset.Length != CommitmentSize))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to asset size.");
     }
     if (asset.Length == Size)
     {
         var assetBytes = CfdCommon.ReverseBytes(asset);
         commitment = StringUtil.FromBytes(assetBytes);
     }
     else
     {
         commitment = StringUtil.FromBytes(asset);
     }
 }