chik_sdk_bindings/
secp.rs

1use binky::Result;
2use chik_protocol::{Bytes32, BytesImpl};
3use chik_secp::{
4    K1PublicKey as K1PublicKeyRs, K1SecretKey as K1SecretKeyRs, K1Signature as K1SignatureRs,
5    R1PublicKey as R1PublicKeyRs, R1SecretKey as R1SecretKeyRs, R1Signature as R1SignatureRs,
6};
7
8#[derive(Clone)]
9pub struct K1SecretKey(pub K1SecretKeyRs);
10
11impl K1SecretKey {
12    pub fn from_bytes(bytes: Bytes32) -> Result<Self> {
13        Ok(Self(K1SecretKeyRs::from_bytes(&bytes.to_bytes())?))
14    }
15
16    pub fn to_bytes(&self) -> Result<Bytes32> {
17        Ok(Bytes32::new(self.0.to_bytes()))
18    }
19
20    pub fn public_key(&self) -> Result<K1PublicKey> {
21        Ok(K1PublicKey(self.0.public_key()))
22    }
23
24    pub fn sign_prehashed(&self, prehashed: Bytes32) -> Result<K1Signature> {
25        Ok(K1Signature(self.0.sign_prehashed(&prehashed.to_bytes())?))
26    }
27}
28
29#[derive(Clone, Copy)]
30pub struct K1PublicKey(pub K1PublicKeyRs);
31
32impl K1PublicKey {
33    pub fn from_bytes(bytes: BytesImpl<33>) -> Result<Self> {
34        Ok(Self(K1PublicKeyRs::from_bytes(&bytes.to_bytes())?))
35    }
36
37    pub fn to_bytes(&self) -> Result<BytesImpl<33>> {
38        Ok(BytesImpl::new(self.0.to_bytes()))
39    }
40
41    pub fn fingerprint(&self) -> Result<u32> {
42        Ok(self.0.fingerprint())
43    }
44
45    pub fn verify_prehashed(&self, prehashed: Bytes32, signature: K1Signature) -> Result<bool> {
46        Ok(self.0.verify_prehashed(&prehashed.to_bytes(), &signature.0))
47    }
48}
49
50#[derive(Clone, Copy)]
51pub struct K1Signature(pub K1SignatureRs);
52
53impl K1Signature {
54    pub fn from_bytes(bytes: BytesImpl<64>) -> Result<Self> {
55        Ok(Self(K1SignatureRs::from_bytes(&bytes.to_bytes())?))
56    }
57
58    pub fn to_bytes(&self) -> Result<BytesImpl<64>> {
59        Ok(BytesImpl::new(self.0.to_bytes()))
60    }
61}
62
63#[derive(Clone)]
64pub struct R1SecretKey(pub R1SecretKeyRs);
65
66impl R1SecretKey {
67    pub fn from_bytes(bytes: Bytes32) -> Result<Self> {
68        Ok(Self(R1SecretKeyRs::from_bytes(&bytes.to_bytes())?))
69    }
70
71    pub fn to_bytes(&self) -> Result<Bytes32> {
72        Ok(Bytes32::new(self.0.to_bytes()))
73    }
74
75    pub fn public_key(&self) -> Result<R1PublicKey> {
76        Ok(R1PublicKey(self.0.public_key()))
77    }
78
79    pub fn sign_prehashed(&self, prehashed: Bytes32) -> Result<R1Signature> {
80        Ok(R1Signature(self.0.sign_prehashed(&prehashed.to_bytes())?))
81    }
82}
83
84#[derive(Clone, Copy)]
85pub struct R1PublicKey(pub R1PublicKeyRs);
86
87impl R1PublicKey {
88    pub fn from_bytes(bytes: BytesImpl<33>) -> Result<Self> {
89        Ok(Self(R1PublicKeyRs::from_bytes(&bytes.to_bytes())?))
90    }
91
92    pub fn to_bytes(&self) -> Result<BytesImpl<33>> {
93        Ok(BytesImpl::new(self.0.to_bytes()))
94    }
95
96    pub fn fingerprint(&self) -> Result<u32> {
97        Ok(self.0.fingerprint())
98    }
99
100    pub fn verify_prehashed(&self, prehashed: Bytes32, signature: R1Signature) -> Result<bool> {
101        Ok(self.0.verify_prehashed(&prehashed.to_bytes(), &signature.0))
102    }
103}
104
105#[derive(Clone, Copy)]
106pub struct R1Signature(pub R1SignatureRs);
107
108impl R1Signature {
109    pub fn from_bytes(bytes: BytesImpl<64>) -> Result<Self> {
110        Ok(Self(R1SignatureRs::from_bytes(&bytes.to_bytes())?))
111    }
112
113    pub fn to_bytes(&self) -> Result<BytesImpl<64>> {
114        Ok(BytesImpl::new(self.0.to_bytes()))
115    }
116}