Exemplo n.º 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;
 }
Exemplo n.º 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);
 }
Exemplo n.º 3
0
 public ByteData ToDerEncode()
 {
     if (!isSetDerEncode)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalStateError, "Failed to unset der encode flag.");
     }
     return(ToDerEncode(signatureHashType));
 }
Exemplo n.º 4
0
 /// <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]));
 }
Exemplo n.º 5
0
 /// <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]);
 }
Exemplo n.º 6
0
 /// <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);
 }
Exemplo n.º 7
0
 public Txid(string txid)
 {
     if ((txid == null) || (txid.Length != Size * 2))
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to txid size.");
     }
     this.txid = txid;
 }
Exemplo n.º 8
0
 public TapBranch GetScriptTree()
 {
     if (!HasTapScript())
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalStateError,
                              "Failed to unused script tree.");
     }
     return(rootData.ScriptTree);
 }
Exemplo n.º 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);
 }
Exemplo n.º 10
0
 /// <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));
 }
Exemplo n.º 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);
        }
Exemplo n.º 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));
     }
 }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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);
 }
Exemplo n.º 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;
 }
Exemplo n.º 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]);
 }
Exemplo n.º 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]);
 }
Exemplo n.º 19
0
 /// <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;
 }
Exemplo n.º 20
0
 /// <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());
 }
Exemplo n.º 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);
 }
Exemplo n.º 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);
 }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
     }
 }
Exemplo n.º 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);
     }
 }
Exemplo n.º 26
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;
 }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 30
0
 /// <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);
     }
 }