Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Address network prefix to be overriden for printing #233

Merged
merged 8 commits into from
Feb 23, 2020
8 changes: 4 additions & 4 deletions blockchain/blocks/src/tipset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ mod tests {
.parents(TipSetKeys {
cids: vec![cids[3].clone()],
})
.miner_address(Address::new_secp256k1(ticket_p.clone()).unwrap())
.miner_address(Address::new_secp256k1(&ticket_p).unwrap())
.timestamp(timestamp)
.ticket(Ticket {
vrfproof: VRFResult::new(ticket_p),
Expand All @@ -274,9 +274,9 @@ mod tests {
let data2: Vec<u8> = vec![1, 4, 3, 6, 1, 1, 2, 2, 4, 5, 3, 12, 2];
let cids = key_setup();
return vec![
template_header(data0.clone(), cids[0].clone(), 1),
template_header(data1.clone(), cids[1].clone(), 2),
template_header(data2.clone(), cids[2].clone(), 3),
template_header(data0, cids[0].clone(), 1),
template_header(data1, cids[1].clone(), 2),
template_header(data2, cids[2].clone(), 3),
];
}

Expand Down
2 changes: 1 addition & 1 deletion crypto/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn ecrecover(hash: &[u8; 32], signature: &[u8; 65]) -> Result<Address, Error> {

let key = recover(&message, &sig, &rec_id)?;
let ret = key.serialize();
let addr = Address::new_secp256k1(ret.to_vec())?;
let addr = Address::new_secp256k1(&ret)?;
Ok(addr)
}

Expand Down
25 changes: 21 additions & 4 deletions vm/address/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl Address {
payload,
})
}

/// Creates address from encoded bytes
pub fn from_bytes(bz: Vec<u8>) -> Result<Self, Error> {
if bz.len() < 2 {
Expand All @@ -84,14 +85,17 @@ impl Address {
pub fn new_id(id: u64) -> Result<Self, Error> {
Address::new(NETWORK_DEFAULT, Protocol::ID, to_leb_bytes(id)?)
}

/// Generates new address using Secp256k1 pubkey
pub fn new_secp256k1(pubkey: Vec<u8>) -> Result<Self, Error> {
Address::new(NETWORK_DEFAULT, Protocol::Secp256k1, address_hash(&pubkey))
pub fn new_secp256k1(pubkey: &[u8]) -> Result<Self, Error> {
Address::new(NETWORK_DEFAULT, Protocol::Secp256k1, address_hash(pubkey))
}

/// Generates new address using the Actor protocol
pub fn new_actor(data: Vec<u8>) -> Result<Self, Error> {
Address::new(NETWORK_DEFAULT, Protocol::Actor, address_hash(&data))
pub fn new_actor(data: &[u8]) -> Result<Self, Error> {
Address::new(NETWORK_DEFAULT, Protocol::Actor, address_hash(data))
}

/// Generates new address using BLS pubkey
pub fn new_bls(pubkey: Vec<u8>) -> Result<Self, Error> {
Address::new(NETWORK_DEFAULT, Protocol::BLS, pubkey)
Expand All @@ -101,10 +105,23 @@ impl Address {
pub fn protocol(&self) -> Protocol {
self.protocol
}

/// Returns data payload of Address
pub fn payload(&self) -> &[u8] {
&self.payload
}

/// Returns network configuration of Address
pub fn network(&self) -> Network {
self.network
}

/// Sets the network for the address and returns a mutable reference to it
pub fn set_network(&mut self, network: Network) -> &mut Self {
self.network = network;
self
}

/// Returns encoded bytes of Address
pub fn to_bytes(&self) -> Vec<u8> {
let mut bz: Vec<u8> = self.payload().to_vec();
Expand Down
Loading