Skip to content

Commit 2560beb

Browse files
committed
Adds serialization methods to primitives. Updates bytes inputs according to #1
1 parent 90f0789 commit 2560beb

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

src/primitives.rs

+42-34
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use bitcoin::blockdata::script::Script;
88
use bitcoin::blockdata::transaction::Transaction;
99
use bitcoin::consensus::encode::{deserialize, serialize, serialize_hex};
1010
use bitcoin::hash_types::Txid;
11-
use bitcoin::secp256k1::constants::{
12-
PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, UNCOMPRESSED_PUBLIC_KEY_SIZE,
13-
};
11+
use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, UNCOMPRESSED_PUBLIC_KEY_SIZE};
1412
use bitcoin::secp256k1::key::{PublicKey, SecretKey};
1513

1614
use lightning::chain::transaction::OutPoint;
@@ -24,19 +22,16 @@ pub struct PySecretKey {
2422
#[pymethods]
2523
impl PySecretKey {
2624
#[new]
27-
pub fn new(data: &[u8]) -> PyResult<Self> {
28-
if data.len() != SECRET_KEY_SIZE {
29-
Err(exceptions::PyValueError::new_err(format!(
30-
"Data must be {}-byte long",
31-
SECRET_KEY_SIZE
32-
)))
33-
} else {
34-
let sk = match SecretKey::from_slice(data) {
35-
Ok(x) => Ok(PySecretKey { inner: x }),
36-
Err(error) => Err(exceptions::PyValueError::new_err(format!("{}", error))),
37-
};
38-
sk
39-
}
25+
pub fn new(data: [u8; 32]) -> PyResult<Self> {
26+
let sk = match SecretKey::from_slice(&data) {
27+
Ok(x) => Ok(PySecretKey { inner: x }),
28+
Err(error) => Err(exceptions::PyValueError::new_err(format!("{}", error))),
29+
};
30+
sk
31+
}
32+
33+
fn serialize(&self, py: Python) -> Py<PyBytes> {
34+
PyBytes::new(py, &self.inner[..]).into()
4035
}
4136
}
4237

@@ -48,6 +43,7 @@ impl PyObjectProtocol for PySecretKey {
4843
}
4944

5045
#[pyclass(name=PublicKey)]
46+
#[derive(Clone)]
5147
pub struct PyPublicKey {
5248
pub inner: PublicKey,
5349
}
@@ -69,6 +65,15 @@ impl PyPublicKey {
6965
pk
7066
}
7167
}
68+
69+
#[args(compressed = "true")]
70+
fn serialize(&self, py: Python, compressed: bool) -> Py<PyBytes> {
71+
if compressed {
72+
PyBytes::new(py, &self.inner.serialize()).into()
73+
} else {
74+
PyBytes::new(py, &self.inner.serialize_uncompressed()).into()
75+
}
76+
}
7277
}
7378

7479
#[pyproto]
@@ -129,6 +134,10 @@ impl PyBlockHeader {
129134
fn nonce(&self) -> u32 {
130135
self.inner.nonce
131136
}
137+
138+
fn serialize(&self, py: Python) -> Py<PyBytes> {
139+
PyBytes::new(py, &serialize(&self.inner)).into()
140+
}
132141
}
133142

134143
#[pyproto]
@@ -152,8 +161,8 @@ impl PyScript {
152161
}
153162
}
154163

155-
fn as_bytes(&self) -> &[u8] {
156-
self.inner.as_bytes()
164+
fn serialize(&self, py: Python) -> Py<PyBytes> {
165+
PyBytes::new(py, self.inner.as_bytes()).into()
157166
}
158167
}
159168

@@ -173,17 +182,15 @@ pub struct PyTxId {
173182
#[pymethods]
174183
impl PyTxId {
175184
#[new]
176-
pub fn new(data: Vec<u8>) -> PyResult<Self> {
177-
if data.len() != 32 {
178-
Err(exceptions::PyValueError::new_err(format!(
179-
"Data must be 32-byte long"
180-
)))
181-
} else {
182-
Ok(PyTxId {
183-
inner: deserialize(&data).unwrap(),
184-
})
185+
pub fn new(data: [u8; 32]) -> Self {
186+
PyTxId {
187+
inner: deserialize(&data).unwrap(),
185188
}
186189
}
190+
191+
fn serialize(&self, py: Python) -> Py<PyBytes> {
192+
PyBytes::new(py, &serialize(&self.inner)).into()
193+
}
187194
}
188195

189196
#[pyproto]
@@ -211,11 +218,8 @@ impl PyOutPoint {
211218
}
212219

213220
#[staticmethod]
214-
pub fn from_bytes(txid: Vec<u8>, index: u16) -> PyResult<Self> {
215-
match PyTxId::new(txid) {
216-
Ok(x) => Ok(PyOutPoint::new(x, index)),
217-
Err(e) => Err(e),
218-
}
221+
pub fn from_bytes(txid: [u8; 32], index: u16) -> Self {
222+
PyOutPoint::new(PyTxId::new(txid), index)
219223
}
220224

221225
#[getter]
@@ -231,6 +235,10 @@ impl PyOutPoint {
231235
pub fn to_channel_id(&self, py: Python) -> Py<PyBytes> {
232236
PyBytes::new(py, &serialize(&self.inner.to_channel_id())).into()
233237
}
238+
239+
fn serialize(&self, py: Python) -> Py<PyBytes> {
240+
PyBytes::new(py, &serialize(&self.inner.into_bitcoin_outpoint())).into()
241+
}
234242
}
235243

236244
#[pyproto]
@@ -249,9 +257,9 @@ pub struct PyTransaction {
249257
#[pymethods]
250258
impl PyTransaction {
251259
#[new]
252-
pub fn new(data: Vec<u8>) -> Self {
260+
pub fn new(data: &[u8]) -> Self {
253261
PyTransaction {
254-
inner: deserialize(&data).unwrap(),
262+
inner: deserialize(data).unwrap(),
255263
}
256264
}
257265
}

0 commit comments

Comments
 (0)