pub struct ExtendedAddr {
    pub addr: [u8; 28],
    pub attributes: Attributes,
    pub addr_type: AddrType,
}
Expand description

A valid cardano address deconstructed

Fields§

§addr: [u8; 28]§attributes: Attributes§addr_type: AddrType

Implementations§

Examples found in repository?
src/legacy_address/address.rs (line 203)
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    pub fn identical_with_pubkey(&self, xpub: &XPub) -> AddressMatchXPub {
        let ea = self.deconstruct();
        let newea = ExtendedAddr::new(xpub, ea.attributes);
        if self == &newea.to_address() {
            AddressMatchXPub::Yes
        } else {
            AddressMatchXPub::No
        }
    }

    /// mostly helper of the previous function, so not to have to expose the xpub construction
    pub fn identical_with_pubkey_raw(&self, xpub: &[u8]) -> AddressMatchXPub {
        match XPub::from_slice(xpub) {
            Ok(xpub) => self.identical_with_pubkey(&xpub),
            _ => AddressMatchXPub::No,
        }
    }
}

impl AsRef<[u8]> for Addr {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl TryFrom<&[u8]> for Addr {
    type Error = cbor_event::Error;

    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        let mut v = Vec::new();
        // TODO we only want validation of slice here, but we don't have api to do that yet.
        {
            let mut raw = Deserializer::from(std::io::Cursor::new(&slice));
            let _: ExtendedAddr = cbor_event::de::Deserialize::deserialize(&mut raw)?;
        }
        v.extend_from_slice(slice);
        Ok(Addr(v))
    }
}

impl ::std::str::FromStr for Addr {
    type Err = ParseExtendedAddrError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = base58::decode(s).map_err(ParseExtendedAddrError::Base58Error)?;
        Self::try_from(&bytes[..]).map_err(ParseExtendedAddrError::EncodingError)
    }
}

impl From<ExtendedAddr> for Addr {
    fn from(ea: ExtendedAddr) -> Self {
        ea.to_address()
    }
}

impl fmt::Display for Addr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", base58::encode(&self.0))
    }
}

impl cbor_event::se::Serialize for Addr {
    fn serialize<'se, W: Write>(
        &self,
        serializer: &'se mut Serializer<W>,
    ) -> cbor_event::Result<&'se mut Serializer<W>> {
        // Addr is already serialized
        serializer.write_raw_bytes(&self.0)
    }
}
impl cbor_event::de::Deserialize for Addr {
    fn deserialize<R: BufRead>(reader: &mut Deserializer<R>) -> cbor_event::Result<Self> {
        let ea: ExtendedAddr = cbor_event::de::Deserialize::deserialize(reader)?;
        Ok(ea.to_address())
    }
}

const EXTENDED_ADDR_LEN: usize = 28;

/// A valid cardano address deconstructed
#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct ExtendedAddr {
    pub addr: [u8; EXTENDED_ADDR_LEN],
    pub attributes: Attributes,
    pub addr_type: AddrType,
}
impl ExtendedAddr {
    pub fn new(xpub: &XPub, attrs: Attributes) -> Self {
        ExtendedAddr {
            addr: hash_spending_data(AddrType::ATPubKey, xpub, &attrs),
            attributes: attrs,
            addr_type: AddrType::ATPubKey,
        }
    }

    // bootstrap era + no hdpayload address
    pub fn new_simple(xpub: &XPub, protocol_magic: Option<u32>) -> Self {
        ExtendedAddr::new(xpub, Attributes::new_bootstrap_era(None, protocol_magic))
    }
Examples found in repository?
src/address.rs (lines 305-308)
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    pub fn icarus_from_key(key: &Bip32PublicKey, protocol_magic: u32) -> ByronAddress {
        let mut out = [0u8; 64];
        out.clone_from_slice(&key.as_bytes());

        // need to ensure we use None for mainnet since Byron-era addresses omitted the network id
        let filtered_protocol_magic = if protocol_magic == NetworkInfo::mainnet().protocol_magic() {
            None
        } else {
            Some(protocol_magic)
        };
        ByronAddress(ExtendedAddr::new_simple(
            &XPub::from_bytes(out),
            filtered_protocol_magic,
        ))
    }
Examples found in repository?
src/legacy_address/address.rs (line 204)
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
    pub fn identical_with_pubkey(&self, xpub: &XPub) -> AddressMatchXPub {
        let ea = self.deconstruct();
        let newea = ExtendedAddr::new(xpub, ea.attributes);
        if self == &newea.to_address() {
            AddressMatchXPub::Yes
        } else {
            AddressMatchXPub::No
        }
    }

    /// mostly helper of the previous function, so not to have to expose the xpub construction
    pub fn identical_with_pubkey_raw(&self, xpub: &[u8]) -> AddressMatchXPub {
        match XPub::from_slice(xpub) {
            Ok(xpub) => self.identical_with_pubkey(&xpub),
            _ => AddressMatchXPub::No,
        }
    }
}

impl AsRef<[u8]> for Addr {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl TryFrom<&[u8]> for Addr {
    type Error = cbor_event::Error;

    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        let mut v = Vec::new();
        // TODO we only want validation of slice here, but we don't have api to do that yet.
        {
            let mut raw = Deserializer::from(std::io::Cursor::new(&slice));
            let _: ExtendedAddr = cbor_event::de::Deserialize::deserialize(&mut raw)?;
        }
        v.extend_from_slice(slice);
        Ok(Addr(v))
    }
}

impl ::std::str::FromStr for Addr {
    type Err = ParseExtendedAddrError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = base58::decode(s).map_err(ParseExtendedAddrError::Base58Error)?;
        Self::try_from(&bytes[..]).map_err(ParseExtendedAddrError::EncodingError)
    }
}

impl From<ExtendedAddr> for Addr {
    fn from(ea: ExtendedAddr) -> Self {
        ea.to_address()
    }
}

impl fmt::Display for Addr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", base58::encode(&self.0))
    }
}

impl cbor_event::se::Serialize for Addr {
    fn serialize<'se, W: Write>(
        &self,
        serializer: &'se mut Serializer<W>,
    ) -> cbor_event::Result<&'se mut Serializer<W>> {
        // Addr is already serialized
        serializer.write_raw_bytes(&self.0)
    }
}
impl cbor_event::de::Deserialize for Addr {
    fn deserialize<R: BufRead>(reader: &mut Deserializer<R>) -> cbor_event::Result<Self> {
        let ea: ExtendedAddr = cbor_event::de::Deserialize::deserialize(reader)?;
        Ok(ea.to_address())
    }
}

const EXTENDED_ADDR_LEN: usize = 28;

/// A valid cardano address deconstructed
#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct ExtendedAddr {
    pub addr: [u8; EXTENDED_ADDR_LEN],
    pub attributes: Attributes,
    pub addr_type: AddrType,
}
impl ExtendedAddr {
    pub fn new(xpub: &XPub, attrs: Attributes) -> Self {
        ExtendedAddr {
            addr: hash_spending_data(AddrType::ATPubKey, xpub, &attrs),
            attributes: attrs,
            addr_type: AddrType::ATPubKey,
        }
    }

    // bootstrap era + no hdpayload address
    pub fn new_simple(xpub: &XPub, protocol_magic: Option<u32>) -> Self {
        ExtendedAddr::new(xpub, Attributes::new_bootstrap_era(None, protocol_magic))
    }

    pub fn to_address(&self) -> Addr {
        Addr(cbor!(self).unwrap()) // unwrap should never fail from strongly typed extended addr to addr
    }
}
#[derive(Debug)]
pub enum ParseExtendedAddrError {
    EncodingError(cbor_event::Error),
    Base58Error(base58::Error),
}

impl fmt::Display for ParseExtendedAddrError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ParseExtendedAddrError::*;
        match self {
            EncodingError(_error) => f.write_str("encoding error"),
            Base58Error(_error) => f.write_str("base58 error"),
        }
    }
}

impl std::error::Error for ParseExtendedAddrError {
    fn source<'a>(&'a self) -> Option<&'a (dyn std::error::Error + 'static)> {
        use ParseExtendedAddrError::*;
        match self {
            EncodingError(ref error) => Some(error),
            Base58Error(ref error) => Some(error),
        }
    }
}

impl ::std::str::FromStr for ExtendedAddr {
    type Err = ParseExtendedAddrError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = base58::decode(s).map_err(ParseExtendedAddrError::Base58Error)?;

        Self::try_from(&bytes[..]).map_err(ParseExtendedAddrError::EncodingError)
    }
}
impl TryFrom<&[u8]> for ExtendedAddr {
    type Error = cbor_event::Error;

    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        let mut raw = Deserializer::from(std::io::Cursor::new(slice));
        cbor_event::de::Deserialize::deserialize(&mut raw)
    }
}
impl cbor_event::se::Serialize for ExtendedAddr {
    fn serialize<'se, W: Write>(
        &self,
        serializer: &'se mut Serializer<W>,
    ) -> cbor_event::Result<&'se mut Serializer<W>> {
        let addr_bytes = cbor_event::Value::Bytes(self.addr.to_vec());
        cbor::util::encode_with_crc32_(
            &(&addr_bytes, &self.attributes, &self.addr_type),
            serializer,
        )?;
        Ok(serializer)
    }
}
impl cbor_event::de::Deserialize for ExtendedAddr {
    fn deserialize<R: BufRead>(reader: &mut Deserializer<R>) -> cbor_event::Result<Self> {
        let bytes = cbor::util::raw_with_crc32(reader)?;
        let mut raw = Deserializer::from(std::io::Cursor::new(bytes));
        raw.tuple(3, "ExtendedAddr")?;
        let addr_bytes = raw.bytes()?;
        let addr = addr_bytes.as_slice().try_into().map_err(|_| {
            cbor_event::Error::WrongLen(
                addr_bytes.len() as u64,
                cbor_event::Len::Len(EXTENDED_ADDR_LEN as u64),
                "invalid extended address length",
            )
        })?;
        let attributes = cbor_event::de::Deserialize::deserialize(&mut raw)?;
        let addr_type = cbor_event::de::Deserialize::deserialize(&mut raw)?;
        Ok(ExtendedAddr {
            addr,
            addr_type,
            attributes,
        })
    }
}
impl fmt::Display for ExtendedAddr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_address())
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
method to implement to deserialise an object from the given Deserializer.
Formats the value using the given formatter. Read more
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.