Skip to content

Commit 5b0f724

Browse files
committed
chore: address: Cleanup BLS address handling
1 parent b3ae786 commit 5b0f724

File tree

3 files changed

+13
-55
lines changed

3 files changed

+13
-55
lines changed

shared/src/address/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
1717

1818
pub use self::errors::Error;
1919
pub use self::network::Network;
20-
pub use self::payload::{BLSPublicKey, Payload};
20+
pub use self::payload::Payload;
2121
pub use self::protocol::Protocol;
2222
use crate::encoding::{blake2b_variable, serde_bytes, Cbor};
2323
use crate::ActorID;
@@ -41,7 +41,7 @@ pub const BLS_PUB_LEN: usize = 48;
4141
pub const FIRST_NON_SINGLETON_ADDR: ActorID = 100;
4242

4343
lazy_static::lazy_static! {
44-
static ref BLS_ZERO_ADDR_BYTES: BLSPublicKey = {
44+
static ref BLS_ZERO_ADDR_BYTES: [u8; BLS_PUB_LEN] = {
4545
let bz_addr = Address::from_str("f3yaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaby2smx7a");
4646
if let Ok(Address {payload: Payload::BLS(pubkey), ..}) = bz_addr {
4747
pubkey
@@ -127,7 +127,7 @@ impl Address {
127127
key.copy_from_slice(pubkey);
128128
Ok(Self {
129129
network: NETWORK_DEFAULT,
130-
payload: Payload::BLS(key.into()),
130+
payload: Payload::BLS(key),
131131
})
132132
}
133133

shared/src/address/payload.rs

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,11 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

44
use std::convert::TryInto;
5-
use std::hash::{Hash, Hasher};
6-
use std::ops::Deref;
7-
use std::{fmt, u64};
5+
use std::hash::Hash;
6+
use std::u64;
87

98
use super::{from_leb_bytes, to_leb_bytes, Error, Protocol, BLS_PUB_LEN, PAYLOAD_HASH_LEN};
109

11-
/// Public key struct used as BLS Address data.
12-
/// This type is only needed to be able to implement traits on it due to limitations on
13-
/// arrays within Rust that are greater than 32 length. Can be dereferenced into `[u8; 48]`.
14-
#[derive(Copy, Clone)]
15-
pub struct BLSPublicKey(pub [u8; BLS_PUB_LEN]);
16-
17-
impl Hash for BLSPublicKey {
18-
fn hash<H: Hasher>(&self, state: &mut H) {
19-
state.write(&self.0);
20-
}
21-
}
22-
23-
impl Eq for BLSPublicKey {}
24-
impl PartialEq for BLSPublicKey {
25-
fn eq(&self, other: &Self) -> bool {
26-
self.0[..].eq(&other.0[..])
27-
}
28-
}
29-
30-
impl fmt::Debug for BLSPublicKey {
31-
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
32-
self.0[..].fmt(formatter)
33-
}
34-
}
35-
36-
impl From<[u8; BLS_PUB_LEN]> for BLSPublicKey {
37-
fn from(pk: [u8; BLS_PUB_LEN]) -> Self {
38-
BLSPublicKey(pk)
39-
}
40-
}
41-
42-
impl Deref for BLSPublicKey {
43-
type Target = [u8; BLS_PUB_LEN];
44-
fn deref(&self) -> &Self::Target {
45-
&self.0
46-
}
47-
}
48-
4910
/// Payload is the data of the Address. Variants are the supported Address protocols.
5011
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
5112
pub enum Payload {
@@ -56,7 +17,7 @@ pub enum Payload {
5617
/// Actor protocol address, 20 byte hash of actor data
5718
Actor([u8; PAYLOAD_HASH_LEN]),
5819
/// BLS key address, full 48 byte public key
59-
BLS(BLSPublicKey),
20+
BLS([u8; BLS_PUB_LEN]),
6021
}
6122

6223
impl Payload {
@@ -99,14 +60,11 @@ impl Payload {
9960
.try_into()
10061
.map_err(|_| Error::InvalidPayloadLength(payload.len()))?,
10162
),
102-
Protocol::BLS => {
103-
if payload.len() != BLS_PUB_LEN {
104-
return Err(Error::InvalidBLSLength(payload.len()));
105-
}
106-
let mut pk = [0u8; BLS_PUB_LEN];
107-
pk.copy_from_slice(payload);
108-
Self::BLS(pk.into())
109-
}
63+
Protocol::BLS => Self::BLS(
64+
payload
65+
.try_into()
66+
.map_err(|_| Error::InvalidPayloadLength(payload.len()))?,
67+
),
11068
};
11169
Ok(payload)
11270
}

shared/tests/address_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,11 @@ fn invalid_byte_addresses() {
392392
// BLS Protocol
393393
StringAddrVec {
394394
input: bls_l,
395-
expected: Error::InvalidBLSLength(49),
395+
expected: Error::InvalidPayloadLength(49),
396396
},
397397
StringAddrVec {
398398
input: bls_s,
399-
expected: Error::InvalidBLSLength(47),
399+
expected: Error::InvalidPayloadLength(47),
400400
},
401401
];
402402

0 commit comments

Comments
 (0)