/// <summary> /// Sign ECDSA-adaptor. /// </summary> /// <param name="msg">32-byte msg</param> /// <param name="secretKey">secret key</param> /// <param name="adaptor">adaptor pubkey</param> /// <returns>ECDSA-adaptor pair</returns> public static AdaptorPair Sign(ByteData msg, Privkey secretKey, Pubkey adaptor) { if (msg is null) { throw new ArgumentNullException(nameof(msg)); } if (secretKey is null) { throw new ArgumentNullException(nameof(secretKey)); } if (adaptor is null) { throw new ArgumentNullException(nameof(adaptor)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdSignEcdsaAdaptor( handle.GetHandle(), msg.ToHexString(), secretKey.ToHexString(), adaptor.ToHexString(), out IntPtr signature, out IntPtr proof); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string tempSig = CCommon.ConvertToString(signature); string tempProof = CCommon.ConvertToString(proof); return(new AdaptorPair(new ByteData(tempSig), new ByteData(tempProof))); } }
/// <summary> /// Sign schnorr with nonce. /// </summary> /// <param name="msg">32-byte msg</param> /// <param name="secretKey">secret key</param> /// <param name="nonce">32-byte nonce</param> /// <returns>schnorr signature</returns> public static SchnorrSignature SignWithNonce(ByteData msg, Privkey secretKey, ByteData nonce) { if (msg is null) { throw new ArgumentNullException(nameof(msg)); } if (secretKey is null) { throw new ArgumentNullException(nameof(secretKey)); } if (nonce is null) { throw new ArgumentNullException(nameof(nonce)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdSignSchnorrWithNonce( handle.GetHandle(), msg.ToHexString(), secretKey.ToHexString(), nonce.ToHexString(), out IntPtr signature); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string tempSig = CCommon.ConvertToString(signature); return(new SchnorrSignature(tempSig)); } }
/// <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); }
/// <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)); } }
/// <summary> /// Extract ECDSA-adaptor secret key. /// </summary> /// <param name="adaptorSignature">adaptor signature</param> /// <param name="signature">signature</param> /// <param name="adaptor">adaptor pubkey</param> /// <returns>secret key</returns> public static Privkey ExtractSecret(ByteData adaptorSignature, ByteData signature, Pubkey adaptor) { if (adaptorSignature is null) { throw new ArgumentNullException(nameof(adaptorSignature)); } if (signature is null) { throw new ArgumentNullException(nameof(signature)); } if (adaptor is null) { throw new ArgumentNullException(nameof(adaptor)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdExtractEcdsaAdaptorSecret( handle.GetHandle(), adaptorSignature.ToHexString(), signature.ToHexString(), adaptor.ToHexString(), out IntPtr secret); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string sk = CCommon.ConvertToString(secret); return(new Privkey(sk)); } }
/// <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); } }
/// <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; }
/// <summary> /// convert hex from ByteData object. /// </summary> /// <param name="bytes">ByteData object</param> /// <returns>hex string</returns> public static string FromBytes(ByteData bytes) { if (bytes is null) { throw new ArgumentNullException(nameof(bytes)); } return(FromBytes(bytes.ToBytes())); }
/// <summary> /// constructor from seed. /// </summary> /// <param name="seed">seed bytes</param> public HDWallet(ByteData seed) { if (seed is null) { throw new ArgumentNullException(nameof(seed)); } this.seed = seed; }
/// <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()); }
protected static string ConvertTapscriptToString(Script tapscript, byte leafVersion) { if (tapscript is null) { throw new ArgumentNullException(nameof(tapscript)); } string treeStr = "tl(" + tapscript.ToHexString(); if (leafVersion != tapscriptLeafVersion) { ByteData data = new ByteData(new byte[] { leafVersion }); treeStr += "," + data.ToHexString(); } treeStr += ")"; return(treeStr); }
/// <summary> /// normalize signature. /// </summary> /// <param name="signature">signature</param> /// <returns>normalized signature</returns> public static ByteData NormalizeSignature(ByteData signature) { if (signature is null) { throw new ArgumentNullException(nameof(signature)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdNormalizeSignature( handle.GetHandle(), signature.ToHexString(), out IntPtr normalizedSignature); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(new ByteData(CCommon.ConvertToString(normalizedSignature))); } }
/// <summary> /// pubkey tweak mul. /// </summary> /// <param name="tweak">tweak bytes</param> /// <returns>tweaked pubkey</returns> public Pubkey TweakMul(ByteData tweak) { if (tweak is null) { throw new ArgumentNullException(nameof(tweak)); } using (var handle = new ErrorHandle()) { CfdErrorCode ret; ret = NativeMethods.CfdPubkeyTweakMul( handle.GetHandle(), pubkey, tweak.ToHexString(), out IntPtr tweakedKey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(new Pubkey(CCommon.ConvertToString(tweakedKey))); } }
/// <summary> /// Get tweaked Schnorr public key. /// </summary> /// <param name="tweak">tweak</param> /// <param name="parity">parity flag</param> /// <returns>schnorr pubkey</returns> public SchnorrPubkey TweakAdd(ByteData tweak, out bool parity) { if (tweak is null) { throw new ArgumentNullException(nameof(tweak)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdSchnorrPubkeyTweakAdd( handle.GetHandle(), data, tweak.ToHexString(), out IntPtr tempPubkey, out parity); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } var pk = CCommon.ConvertToString(tempPubkey); return(new SchnorrPubkey(pk)); } }
/// <summary> /// Verify ECDSA-adaptor signature. /// </summary> /// <param name="adaptorSignature">adaptor signature</param> /// <param name="adaptorProof">adaptor proof</param> /// <param name="adaptor">adaptor pubkey</param> /// <param name="msg">32-byte msg</param> /// <param name="pubkey">pubkey</param> /// <returns>verify result</returns> public static bool Verify(ByteData adaptorSignature, ByteData adaptorProof, Pubkey adaptor, ByteData msg, Pubkey pubkey) { if (adaptorSignature is null) { throw new ArgumentNullException(nameof(adaptorSignature)); } if (adaptorProof is null) { throw new ArgumentNullException(nameof(adaptorProof)); } if (adaptor is null) { throw new ArgumentNullException(nameof(adaptor)); } if (msg is null) { throw new ArgumentNullException(nameof(msg)); } if (pubkey is null) { throw new ArgumentNullException(nameof(pubkey)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdVerifyEcdsaAdaptor( handle.GetHandle(), adaptorSignature.ToHexString(), adaptorProof.ToHexString(), adaptor.ToHexString(), msg.ToHexString(), pubkey.ToHexString()); if (ret == CfdErrorCode.Success) { return(true); } else if (ret != CfdErrorCode.SignVerificationError) { handle.ThrowError(ret); } } return(false); }
/// <summary> /// encode by DER. /// </summary> /// <param name="signature">signature</param> /// <param name="sighashType">sighash type</param> /// <returns>DER encoded data</returns> public static ByteData EncodeToDer(ByteData signature, SignatureHashType sighashType) { if (signature is null) { throw new ArgumentNullException(nameof(signature)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdEncodeSignatureByDer( handle.GetHandle(), signature.ToHexString(), sighashType.GetValue(), sighashType.IsSighashAnyoneCanPay, out IntPtr derSignature); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(new ByteData(CCommon.ConvertToString(derSignature))); } }
/// <summary> /// Calculate ec-signature. /// </summary> /// <param name="sighash">signature hash.</param> /// <param name="hasGrindR">use grind-R.</param> /// <returns></returns> public SignParameter CalculateEcSignature(ByteData sighash, bool hasGrindR) { if (sighash is null) { throw new ArgumentNullException(nameof(sighash)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCalculateEcSignature( handle.GetHandle(), sighash.ToHexString(), privkey, privkeyWif, (int)networkType, hasGrindR, out IntPtr signatureHex); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } SignParameter signature = new SignParameter(CCommon.ConvertToString(signatureHex)); SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false); signature.SetDerEncode(sighashType); signature.SetRelatedPubkey(GetPubkey()); return(signature); } }
/// <summary> /// constructor. /// </summary> /// <param name="seed">seed bytes</param> /// <param name="networkType">network type</param> public ExtPrivkey(ByteData seed, CfdNetworkType networkType) { if (seed is null) { throw new ArgumentNullException(nameof(seed)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateExtkeyFromSeed( handle.GetHandle(), seed.ToHexString(), (int)networkType, (int)CfdExtKeyType.Privkey, out IntPtr tempExtkey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } extkey = CCommon.ConvertToString(tempExtkey); GetExtkeyInformation(handle, extkey, out version, out fingerprint, out chainCode, out depth, out childNumber, out _); privkey = GetPrivkeyFromExtKey(handle, extkey, networkType); this.networkType = networkType; } }
private static void GetExtkeyInformation(ErrorHandle handle, string extkey, out ByteData version, out ByteData fingerprint, out ByteData chainCode, out uint depth, out uint childNumber, out CfdNetworkType networkType) { var ret = NativeMethods.CfdGetExtkeyInformation( handle.GetHandle(), extkey, out IntPtr tempVersion, out IntPtr tempFingerprint, out IntPtr tempChainCode, out depth, out childNumber); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string workVersion = CCommon.ConvertToString(tempVersion); string workFingerprint = CCommon.ConvertToString(tempFingerprint); string workChainCode = CCommon.ConvertToString(tempChainCode); version = new ByteData(workVersion); fingerprint = new ByteData(workFingerprint); chainCode = new ByteData(workChainCode); if (VersionMainnet == version.ToHexString()) { networkType = CfdNetworkType.Mainnet; } else { networkType = CfdNetworkType.Testnet; if (VersionTestnet != version.ToHexString()) { CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Failed to version format."); } } }
/// <summary> /// Adapt ECDSA-adaptor signature. /// </summary> /// <param name="adaptorSignature">adaptor signature</param> /// <param name="adaptorSecret">adaptor secret key</param> /// <returns>ecdsa signature in compact format</returns> public static ByteData Adapt(ByteData adaptorSignature, Privkey adaptorSecret) { if (adaptorSignature is null) { throw new ArgumentNullException(nameof(adaptorSignature)); } if (adaptorSecret is null) { throw new ArgumentNullException(nameof(adaptorSecret)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdAdaptEcdsaAdaptor( handle.GetHandle(), adaptorSignature.ToHexString(), adaptorSecret.ToHexString(), out IntPtr signature); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string sig = CCommon.ConvertToString(signature); return(new ByteData(sig)); } }
public ExtPubkey(CfdNetworkType networkType, ByteData fingerprint, Pubkey pubkey, ByteData chainCode, uint depth, uint childNumber) { if (fingerprint is null) { throw new ArgumentNullException(nameof(fingerprint)); } if (pubkey is null) { throw new ArgumentNullException(nameof(pubkey)); } if (chainCode is null) { throw new ArgumentNullException(nameof(chainCode)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateExtkey( handle.GetHandle(), (int)networkType, (int)CfdExtKeyType.Pubkey, "", fingerprint.ToHexString(), pubkey.ToHexString(), chainCode.ToHexString(), (byte)depth, childNumber, out IntPtr tempExtkey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } extkey = CCommon.ConvertToString(tempExtkey); this.networkType = networkType; this.fingerprint = fingerprint; this.pubkey = pubkey; this.chainCode = chainCode; this.depth = depth; this.childNumber = childNumber; GetExtkeyInformation(handle, extkey, out version, out _, out _, out _, out _, out _); } }
/// <summary> /// convert entropy to mnemonic. /// </summary> /// <param name="entropy">entropy</param> /// <param name="language">language</param> /// <returns>mnemonic words</returns> public static string[] ConvertEntropyToMnemonic( ByteData entropy, string language) { if (entropy is null) { throw new ArgumentNullException(nameof(entropy)); } if (language is null) { throw new ArgumentNullException(nameof(language)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdConvertEntropyToMnemonic(handle.GetHandle(), entropy.ToHexString(), language, out IntPtr tempMnemonic); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string mnemonic = CCommon.ConvertToString(tempMnemonic); return(mnemonic.Split(' ')); } }
/// <summary> /// decode from DER. /// </summary> /// <param name="derSignature">DER encoded data</param> /// <returns>signature (SignParameter object)</returns> public static SignParameter DecodeFromDer(ByteData derSignature) { if (derSignature is null) { throw new ArgumentNullException(nameof(derSignature)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdDecodeSignatureFromDer( handle.GetHandle(), derSignature.ToHexString(), out IntPtr signature, out int signatureHashType, out bool _); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string signatureStr = CCommon.ConvertToString(signature); SignatureHashType sighashType = new SignatureHashType(signatureHashType); SignParameter signParam = new SignParameter(signatureStr); signParam.SetDerEncode(sighashType); return(signParam); } }
/// <summary> /// Constructor. /// </summary> /// <param name="signature">adaptor signature</param> /// <param name="proof">adaptor proof</param> public AdaptorPair(ByteData signature, ByteData proof) { Signature = signature; Proof = proof; }
/// <summary> /// Get tree from control block. /// </summary> /// <param name="controlBlock">control block</param> /// <param name="tapscript">tapscript</param> /// <returns>Taproot script tree</returns> public static TaprootScriptTree GetFromControlBlock( ByteData controlBlock, Script tapscript) { return(new TaprootScriptTree(controlBlock, tapscript)); }
/// <summary> /// Verify schnorr signature. /// </summary> /// <param name="signature">schnorr signature</param> /// <param name="msg">32-byte msg</param> /// <returns>verify result</returns> public bool Verify(SchnorrSignature signature, ByteData msg) { return(SchnorrUtil.Verify(signature, msg, this)); }
/// <summary> /// Calculate ec-signature. /// </summary> /// <param name="sighash">signature hash.</param> /// <returns></returns> public SignParameter CalculateEcSignature(ByteData sighash) { return(CalculateEcSignature(sighash, true)); }
/// <summary> /// Sign schnorr. /// </summary> /// <param name="msg">32-byte msg</param> /// <param name="secretKey">secret key</param> /// <returns>schnorr signature</returns> public static SchnorrSignature Sign(ByteData msg, Privkey secretKey) { return(Sign(msg, secretKey, new ByteData())); }