/// <summary> /// constructor for pubkey. /// </summary> /// <param name="pubkey">public key</param> /// <param name="type">address type</param> /// <param name="network">network type</param> public Address(Pubkey 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); addressType = type; } }
/// <summary> /// Get pegin address. /// </summary> /// <param name="fedpegScript">fedpeg script</param> /// <param name="pubkey">pubkey</param> /// <param name="hashType">hash type</param> /// <param name="network">network type</param> /// <returns>pegin address data</returns> public static PeginData GetPeginAddress(Script fedpegScript, Pubkey pubkey, CfdHashType hashType, CfdNetworkType network) { if (fedpegScript is null) { throw new ArgumentNullException(nameof(fedpegScript)); } if (pubkey is null) { throw new ArgumentNullException(nameof(pubkey)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdGetPeginAddress( handle.GetHandle(), (int)network, fedpegScript.ToHexString(), (int)hashType, pubkey.ToHexString(), "", out IntPtr outputPeginAddress, out IntPtr outputClaimScript, out IntPtr outputFedpegScript); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string peginAddress = CCommon.ConvertToString(outputPeginAddress); string claimScript = CCommon.ConvertToString(outputClaimScript); string tweakedFedpegScript = CCommon.ConvertToString(outputFedpegScript); return(new PeginData(new Address(peginAddress), new Script(claimScript), new Script(tweakedFedpegScript))); } }
public ExtPubkey(CfdNetworkType networkType, Pubkey parentPubkey, Pubkey pubkey, ByteData chainCode, uint depth, uint childNumber) { if (parentPubkey is null) { throw new ArgumentNullException(nameof(parentPubkey)); } 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, parentPubkey.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.pubkey = pubkey; this.chainCode = chainCode; this.depth = depth; this.childNumber = childNumber; GetExtkeyInformation(handle, extkey, out version, out fingerprint, out _, out _, out _, out _); } }
/// <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> /// 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> /// Get Schnorr public key from public key. /// </summary> /// <param name="pubkey">public key</param> /// <param name="parity">parity flag</param> /// <returns>schnorr pubkey</returns> public static SchnorrPubkey GetPubkeyFromPubkey(Pubkey pubkey, out bool parity) { if (pubkey is null) { throw new ArgumentNullException(nameof(pubkey)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdGetSchnorrPubkeyFromPubkey( handle.GetHandle(), pubkey.ToHexString(), out IntPtr schnorrPubkey, out parity); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string data = CCommon.ConvertToString(schnorrPubkey); return(new SchnorrPubkey(data)); } }
/// <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> /// constructor. /// </summary> /// <param name="address">address</param> /// <param name="confidentialKey">confidential key</param> public ConfidentialAddress(Address address, Pubkey confidentialKey) { if (address is null) { throw new ArgumentNullException(nameof(address)); } if (confidentialKey is null) { throw new ArgumentNullException(nameof(confidentialKey)); } unconfidenialAddress = address; key = confidentialKey; using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateConfidentialAddress( handle.GetHandle(), address.ToAddressString(), confidentialKey.ToHexString(), out IntPtr outputConfidentialAddr); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } confidentialAddress = CCommon.ConvertToString(outputConfidentialAddr); } }