defuse_crypto/curve/
secp256k1.rs1use crate::{CryptoHash, Curve};
2
3pub struct Secp256k1;
4
5impl Curve for Secp256k1 {
6 type PublicKey = [u8; 64];
7
8 type Signature = [u8; 65];
17
18 type Message = CryptoHash;
20
21 type VerifyingKey = ();
23}
24
25#[cfg(feature = "near-contract")]
26impl crate::VerifiableCurve for Secp256k1 {
27 #[inline]
28 fn verify(
29 [signature @ .., v]: &Self::Signature,
30 hash: &Self::Message,
31 _verifying_key: &(),
32 ) -> Option<Self::PublicKey> {
33 near_sdk::env::ecrecover(
34 hash, signature, *v,
35 true,
38 )
39 }
40}
41
42impl crate::TypedCurve for Secp256k1 {
43 const CURVE_TYPE: crate::CurveType = crate::CurveType::Secp256k1;
44}
45
46#[cfg_attr(any(feature = "arbitrary", test), derive(arbitrary::Arbitrary))]
47#[cfg_attr(
48 feature = "borsh",
49 derive(::borsh::BorshSerialize, ::borsh::BorshDeserialize),
50 cfg_attr(feature = "abi", derive(::borsh::BorshSchema))
51)]
52#[cfg_attr(
53 feature = "serde",
54 derive(::serde_with::SerializeDisplay, ::serde_with::DeserializeFromStr),
55 cfg_attr(feature = "abi", derive(::schemars::JsonSchema))
56)]
57#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
58#[repr(transparent)]
59pub struct Secp256k1PublicKey(
60 #[cfg_attr(all(feature = "abi", feature = "serde"), schemars(with = "String"))]
62 pub <Secp256k1 as Curve>::PublicKey,
63);
64
65#[cfg(feature = "parse")]
66const _: () = {
67 use crate::{ParseCurveError, TypedCurve};
68 use core::fmt::{self, Debug, Display};
69 use std::str::FromStr;
70
71 impl Debug for Secp256k1PublicKey {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 Display::fmt(self, f)
74 }
75 }
76
77 impl Display for Secp256k1PublicKey {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 f.write_str(&<Secp256k1 as TypedCurve>::to_base58(self.0))
80 }
81 }
82
83 impl FromStr for Secp256k1PublicKey {
84 type Err = ParseCurveError;
85
86 fn from_str(s: &str) -> Result<Self, Self::Err> {
87 Secp256k1::parse_base58(s).map(Self)
88 }
89 }
90};
91
92#[cfg_attr(any(feature = "arbitrary", test), derive(arbitrary::Arbitrary))]
93#[cfg_attr(
94 feature = "borsh",
95 derive(::borsh::BorshSerialize, ::borsh::BorshDeserialize),
96 cfg_attr(feature = "abi", derive(::borsh::BorshSchema))
97)]
98#[cfg_attr(
99 feature = "serde",
100 derive(::serde_with::SerializeDisplay, ::serde_with::DeserializeFromStr),
101 cfg_attr(feature = "abi", derive(::schemars::JsonSchema))
102)]
103#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
104#[repr(transparent)]
105pub struct Secp256k1Signature(
106 #[cfg_attr(all(feature = "abi", feature = "serde"), schemars(with = "String"))]
108 pub <Secp256k1 as Curve>::Signature,
109);
110
111#[cfg(feature = "parse")]
112const _: () = {
113 use crate::{ParseCurveError, TypedCurve};
114 use core::fmt::{self, Debug, Display};
115 use std::str::FromStr;
116
117 impl Debug for Secp256k1Signature {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 Display::fmt(self, f)
120 }
121 }
122
123 impl Display for Secp256k1Signature {
124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125 f.write_str(&<Secp256k1 as TypedCurve>::to_base58(self.0))
126 }
127 }
128
129 impl FromStr for Secp256k1Signature {
130 type Err = ParseCurveError;
131
132 fn from_str(s: &str) -> Result<Self, Self::Err> {
133 Secp256k1::parse_base58(s).map(Self)
134 }
135 }
136};