Ejemplo n.º 1
0
        /// <summary>
        /// constructor for schnorr pubkey.
        /// </summary>
        /// <param name="pubkey">schnorr public key</param>
        /// <param name="type">address type</param>
        /// <param name="network">network type</param>
        public Address(SchnorrPubkey pubkey, CfdAddressType type, CfdNetworkType network)
        {
            if (pubkey is null)
            {
                throw new ArgumentNullException(nameof(pubkey));
            }
            using (var handle = new ErrorHandle())
            {
                var ret = NativeMethods.CfdCreateAddress(
                    handle.GetHandle(),
                    (int)type,
                    pubkey.ToHexString(),
                    "",
                    (int)network,
                    out IntPtr outputAddress,
                    out IntPtr outputLockingScript,
                    out IntPtr outputP2shSegwitLockingScript);
                if (ret != CfdErrorCode.Success)
                {
                    handle.ThrowError(ret);
                }
                address                 = CCommon.ConvertToString(outputAddress);
                lockingScript           = CCommon.ConvertToString(outputLockingScript);
                p2shSegwitLockingScript = CCommon.ConvertToString(outputP2shSegwitLockingScript);

                Initialize(handle, address, out network,
                           out witnessVersion, out string tempLockingScript,
                           out hash);

                this.network = network;
                addressType  = type;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get taproot data.
        /// </summary>
        /// <param name="internalPubkey">internal pubkey</param>
        /// <returns>taproot data</returns>
        public TaprootScriptData GetTaprootData(SchnorrPubkey internalPubkey)
        {
            if (internalPubkey is null)
            {
                throw new ArgumentNullException(nameof(internalPubkey));
            }
            using (var handle = new ErrorHandle())
                using (var treeHandle = new TreeHandle(handle))
                {
                    Load(handle, treeHandle);
                    var ret = NativeMethods.CfdGetTaprootScriptTreeHash(
                        handle.GetHandle(), treeHandle.GetHandle(), internalPubkey.ToHexString(),
                        out IntPtr witnessProgram, out IntPtr tapLeafHashPtr, out IntPtr controlBlockStr);
                    if (ret != CfdErrorCode.Success)
                    {
                        handle.ThrowError(ret);
                    }
                    var witnessProgramStr = CCommon.ConvertToString(witnessProgram);
                    var tapLeafHash       = CCommon.ConvertToString(tapLeafHashPtr);
                    var controlBlock      = CCommon.ConvertToString(controlBlockStr);

                    return(new TaprootScriptData(
                               new SchnorrPubkey(witnessProgramStr), new ByteData(controlBlock),
                               new ByteData256(tapLeafHash), GetTapScript()));
                }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="controlBlock">control block</param>
        /// <param name="tapscript">tapscript</param>
        public TaprootScriptTree(ByteData controlBlock, Script tapscript)
        {
            if (controlBlock is null)
            {
                throw new ArgumentNullException(nameof(controlBlock));
            }
            if (tapscript is null)
            {
                throw new ArgumentNullException(nameof(tapscript));
            }

            using (var handle = new ErrorHandle())
                using (var treeHandle = new TreeHandle(handle))
                {
                    var ret = NativeMethods.CfdSetTapScriptByWitnessStack(handle.GetHandle(),
                                                                          treeHandle.GetHandle(), controlBlock.ToHexString(), tapscript.ToHexString(),
                                                                          out IntPtr internalPubkeyPtr);
                    if (ret != CfdErrorCode.Success)
                    {
                        handle.ThrowError(ret);
                    }
                    internalPubkey = new SchnorrPubkey(CCommon.ConvertToString(internalPubkeyPtr));
                    GetAllData(handle, treeHandle);
                }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor. (empty)
 /// </summary>
 public SchnorrSignature()
 {
     data        = "";
     nonce       = new SchnorrPubkey();
     key         = new Privkey();
     sighashType = new SignatureHashType(CfdSighashType.Default, false);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Verify schnorr signature.
 /// </summary>
 /// <param name="signature">schnorr signature</param>
 /// <param name="msg">32-byte msg</param>
 /// <param name="schnorrPubkey">pubkey</param>
 /// <returns>verify result</returns>
 public static bool Verify(SchnorrSignature signature, ByteData msg, SchnorrPubkey schnorrPubkey)
 {
     if (signature is null)
     {
         throw new ArgumentNullException(nameof(signature));
     }
     if (msg is null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (schnorrPubkey is null)
     {
         throw new ArgumentNullException(nameof(schnorrPubkey));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdVerifySchnorr(
             handle.GetHandle(), signature.ToHexString(),
             msg.ToHexString(),
             schnorrPubkey.ToHexString());
         if (ret == CfdErrorCode.Success)
         {
             return(true);
         }
         else if (ret != CfdErrorCode.SignVerificationError)
         {
             handle.ThrowError(ret);
         }
     }
     return(false);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Compute signature point.
 /// </summary>
 /// <param name="msg">32-byte msg</param>
 /// <param name="nonce">schnorr nonce</param>
 /// <param name="schnorrPubkey">pubkey</param>
 /// <returns>signature point</returns>
 public static Pubkey ComputeSigPoint(ByteData msg, SchnorrPubkey nonce, SchnorrPubkey schnorrPubkey)
 {
     if (msg is null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (nonce is null)
     {
         throw new ArgumentNullException(nameof(nonce));
     }
     if (schnorrPubkey is null)
     {
         throw new ArgumentNullException(nameof(schnorrPubkey));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdComputeSchnorrSigPoint(
             handle.GetHandle(), msg.ToHexString(),
             nonce.ToHexString(), schnorrPubkey.ToHexString(),
             out IntPtr sigPoint);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string point = CCommon.ConvertToString(sigPoint);
         return(new Pubkey(point));
     }
 }
Ejemplo n.º 7
0
 public CfdKeyData(Pubkey pubkey)
 {
     KeyType       = CfdDescriptorKeyType.Public;
     Pubkey        = pubkey;
     ExtPubkey     = new ExtPubkey();
     ExtPrivkey    = new ExtPrivkey();
     SchnorrPubkey = new SchnorrPubkey();
 }
Ejemplo n.º 8
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.º 9
0
 public CfdKeyData(SchnorrPubkey schnorrPubkey)
 {
     KeyType       = CfdDescriptorKeyType.SchnorrPubkey;
     Pubkey        = new Pubkey();
     ExtPubkey     = new ExtPubkey();
     ExtPrivkey    = new ExtPrivkey();
     SchnorrPubkey = schnorrPubkey;
 }
Ejemplo n.º 10
0
 public CfdKeyData(ExtPrivkey extPrivkey)
 {
     KeyType       = CfdDescriptorKeyType.Bip32Priv;
     Pubkey        = new Pubkey();
     ExtPubkey     = new ExtPubkey();
     ExtPrivkey    = extPrivkey;
     SchnorrPubkey = new SchnorrPubkey();
 }
Ejemplo n.º 11
0
 public CfdKeyData(CfdDescriptorKeyType keyType, Pubkey pubkey,
                   ExtPubkey extPubkey, ExtPrivkey extPrivkey)
 {
     KeyType       = keyType;
     Pubkey        = pubkey;
     ExtPubkey     = extPubkey;
     ExtPrivkey    = extPrivkey;
     SchnorrPubkey = new SchnorrPubkey();
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Set default internal pubkey.
 /// </summary>
 /// <param name="pubkey">internal pubkey</param>
 public void SetInternalPubkey(SchnorrPubkey pubkey)
 {
     if (pubkey is null)
     {
         throw new ArgumentNullException(nameof(pubkey));
     }
     else if (pubkey.IsValid())
     {
         internalPubkey = pubkey;
     }
 }
Ejemplo n.º 13
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]);
 }
Ejemplo n.º 14
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]);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="origin">source branch.</param>
        public TaprootScriptTree(TapBranch origin)
        {
            if (origin is null)
            {
                throw new ArgumentNullException(nameof(origin));
            }
            var script = new Script();

            if (origin.HasTapScript())
            {
                script = origin.GetTapScript();
            }
            Initialize(origin.ToString(), script, origin.GetTargetNodes());
            internalPubkey = new SchnorrPubkey();

            using (var handle = new ErrorHandle())
                using (var treeHandle = new TreeHandle(handle))
                {
                    Load(handle, treeHandle);
                    GetAllData(handle, treeHandle);
                }
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="hex">hex string</param>
 public SchnorrSignature(string hex)
 {
     if (hex is null)
     {
         throw new ArgumentNullException(nameof(hex));
     }
     if ((hex.Length != Size * 2) && (hex.Length != AddedSigHashTypeSize * 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]);
     if (hex.Length == AddedSigHashTypeSize * 2)
     {
         sighashType = CollectSigHashType(data);
     }
     else
     {
         sighashType = new SignatureHashType(CfdSighashType.Default, false);
     }
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Constructor. (default)
 /// </summary>
 public TaprootScriptTree()
 {
     internalPubkey = new SchnorrPubkey();
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Constructor. (empty)
 /// </summary>
 public SchnorrSignature()
 {
     data  = "";
     nonce = new SchnorrPubkey();
     key   = new Privkey();
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="tapscript">tapscript</param>
 /// <param name="leafVersion">leaf version</param>
 public TaprootScriptTree(Script tapscript, byte leafVersion) : base(tapscript, leafVersion)
 {
     internalPubkey = new SchnorrPubkey();
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="treeStr">script tree string.</param>
 /// <param name="tapscript">tapscript</param>
 public TaprootScriptTree(string treeStr, Script tapscript) : base(treeStr, tapscript)
 {
     internalPubkey = new SchnorrPubkey();
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="treeStr">script tree string.</param>
 /// <param name="tapscript">tapscript</param>
 /// <param name="targetNodes">branch hash list.</param>
 public TaprootScriptTree(string treeStr, Script tapscript, ByteData256[] targetNodes) : base(treeStr, tapscript, targetNodes)
 {
     internalPubkey = new SchnorrPubkey();
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="tapscript">tapscript</param>
 public TaprootScriptTree(Script tapscript) : base(tapscript)
 {
     internalPubkey = new SchnorrPubkey();
 }