@@ -8,9 +8,7 @@ use bitcoin::blockdata::script::Script;
8
8
use bitcoin:: blockdata:: transaction:: Transaction ;
9
9
use bitcoin:: consensus:: encode:: { deserialize, serialize, serialize_hex} ;
10
10
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 } ;
14
12
use bitcoin:: secp256k1:: key:: { PublicKey , SecretKey } ;
15
13
16
14
use lightning:: chain:: transaction:: OutPoint ;
@@ -24,19 +22,16 @@ pub struct PySecretKey {
24
22
#[ pymethods]
25
23
impl PySecretKey {
26
24
#[ 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 ( )
40
35
}
41
36
}
42
37
@@ -48,6 +43,7 @@ impl PyObjectProtocol for PySecretKey {
48
43
}
49
44
50
45
#[ pyclass( name=PublicKey ) ]
46
+ #[ derive( Clone ) ]
51
47
pub struct PyPublicKey {
52
48
pub inner : PublicKey ,
53
49
}
@@ -69,6 +65,15 @@ impl PyPublicKey {
69
65
pk
70
66
}
71
67
}
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
+ }
72
77
}
73
78
74
79
#[ pyproto]
@@ -129,6 +134,10 @@ impl PyBlockHeader {
129
134
fn nonce ( & self ) -> u32 {
130
135
self . inner . nonce
131
136
}
137
+
138
+ fn serialize ( & self , py : Python ) -> Py < PyBytes > {
139
+ PyBytes :: new ( py, & serialize ( & self . inner ) ) . into ( )
140
+ }
132
141
}
133
142
134
143
#[ pyproto]
@@ -152,8 +161,8 @@ impl PyScript {
152
161
}
153
162
}
154
163
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 ( )
157
166
}
158
167
}
159
168
@@ -173,17 +182,15 @@ pub struct PyTxId {
173
182
#[ pymethods]
174
183
impl PyTxId {
175
184
#[ 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 ( ) ,
185
188
}
186
189
}
190
+
191
+ fn serialize ( & self , py : Python ) -> Py < PyBytes > {
192
+ PyBytes :: new ( py, & serialize ( & self . inner ) ) . into ( )
193
+ }
187
194
}
188
195
189
196
#[ pyproto]
@@ -211,11 +218,8 @@ impl PyOutPoint {
211
218
}
212
219
213
220
#[ 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)
219
223
}
220
224
221
225
#[ getter]
@@ -231,6 +235,10 @@ impl PyOutPoint {
231
235
pub fn to_channel_id ( & self , py : Python ) -> Py < PyBytes > {
232
236
PyBytes :: new ( py, & serialize ( & self . inner . to_channel_id ( ) ) ) . into ( )
233
237
}
238
+
239
+ fn serialize ( & self , py : Python ) -> Py < PyBytes > {
240
+ PyBytes :: new ( py, & serialize ( & self . inner . into_bitcoin_outpoint ( ) ) ) . into ( )
241
+ }
234
242
}
235
243
236
244
#[ pyproto]
@@ -249,9 +257,9 @@ pub struct PyTransaction {
249
257
#[ pymethods]
250
258
impl PyTransaction {
251
259
#[ new]
252
- pub fn new ( data : Vec < u8 > ) -> Self {
260
+ pub fn new ( data : & [ u8 ] ) -> Self {
253
261
PyTransaction {
254
- inner : deserialize ( & data) . unwrap ( ) ,
262
+ inner : deserialize ( data) . unwrap ( ) ,
255
263
}
256
264
}
257
265
}
0 commit comments