Struct cardano_serialization_lib::address::BaseAddress
source · pub struct BaseAddress { /* private fields */ }
Implementations§
source§impl BaseAddress
impl BaseAddress
sourcepub fn new(
network: u8,
payment: &StakeCredential,
stake: &StakeCredential
) -> Self
pub fn new(
network: u8,
payment: &StakeCredential,
stake: &StakeCredential
) -> Self
Examples found in repository?
More examples
src/address.rs (lines 477-481)
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
fn from_bytes_impl(data: &[u8]) -> Result<Address, DeserializeError> {
use std::convert::TryInto;
// header has 4 bits addr type discrim then 4 bits network discrim.
// Copied from shelley.cddl:
//
// shelley payment addresses:
// bit 7: 0
// bit 6: base/other
// bit 5: pointer/enterprise [for base: stake cred is keyhash/scripthash]
// bit 4: payment cred is keyhash/scripthash
// bits 3-0: network id
//
// reward addresses:
// bits 7-5: 111
// bit 4: credential is keyhash/scripthash
// bits 3-0: network id
//
// byron addresses:
// bits 7-4: 1000
(|| -> Result<Self, DeserializeError> {
let header = data[0];
let network = header & 0x0F;
const HASH_LEN: usize = Ed25519KeyHash::BYTE_COUNT;
// should be static assert but it's maybe not worth importing a whole external crate for it now
assert_eq!(ScriptHash::BYTE_COUNT, HASH_LEN);
// checks the /bit/ bit of the header for key vs scripthash then reads the credential starting at byte position /pos/
let read_addr_cred = |bit: u8, pos: usize| {
let hash_bytes: [u8; HASH_LEN] = data[pos..pos + HASH_LEN].try_into().unwrap();
let x = if header & (1 << bit) == 0 {
StakeCredential::from_keyhash(&Ed25519KeyHash::from(hash_bytes))
} else {
StakeCredential::from_scripthash(&ScriptHash::from(hash_bytes))
};
x
};
let addr = match (header & 0xF0) >> 4 {
// base
0b0000 | 0b0001 | 0b0010 | 0b0011 => {
const BASE_ADDR_SIZE: usize = 1 + HASH_LEN * 2;
if data.len() < BASE_ADDR_SIZE {
return Err(cbor_event::Error::NotEnough(data.len(), BASE_ADDR_SIZE).into());
}
if data.len() > BASE_ADDR_SIZE {
return Err(cbor_event::Error::TrailingData.into());
}
AddrType::Base(BaseAddress::new(
network,
&read_addr_cred(4, 1),
&read_addr_cred(5, 1 + HASH_LEN),
))
}
// pointer
0b0100 | 0b0101 => {
// header + keyhash + 3 natural numbers (min 1 byte each)
const PTR_ADDR_MIN_SIZE: usize = 1 + HASH_LEN + 1 + 1 + 1;
if data.len() < PTR_ADDR_MIN_SIZE {
// possibly more, but depends on how many bytes the natural numbers are for the pointer
return Err(
cbor_event::Error::NotEnough(data.len(), PTR_ADDR_MIN_SIZE).into()
);
}
let mut byte_index = 1;
let payment_cred = read_addr_cred(4, 1);
byte_index += HASH_LEN;
let (slot, slot_bytes) =
variable_nat_decode(&data[byte_index..]).ok_or(DeserializeError::new(
"Address.Pointer.slot",
DeserializeFailure::VariableLenNatDecodeFailed,
))?;
byte_index += slot_bytes;
let (tx_index, tx_bytes) =
variable_nat_decode(&data[byte_index..]).ok_or(DeserializeError::new(
"Address.Pointer.tx_index",
DeserializeFailure::VariableLenNatDecodeFailed,
))?;
byte_index += tx_bytes;
let (cert_index, cert_bytes) =
variable_nat_decode(&data[byte_index..]).ok_or(DeserializeError::new(
"Address.Pointer.cert_index",
DeserializeFailure::VariableLenNatDecodeFailed,
))?;
byte_index += cert_bytes;
if byte_index < data.len() {
return Err(cbor_event::Error::TrailingData.into());
}
AddrType::Ptr(PointerAddress::new(
network,
&payment_cred,
&Pointer::new_pointer(
&to_bignum(slot),
&to_bignum(tx_index),
&to_bignum(cert_index),
),
))
}
// enterprise
0b0110 | 0b0111 => {
const ENTERPRISE_ADDR_SIZE: usize = 1 + HASH_LEN;
if data.len() < ENTERPRISE_ADDR_SIZE {
return Err(
cbor_event::Error::NotEnough(data.len(), ENTERPRISE_ADDR_SIZE).into(),
);
}
if data.len() > ENTERPRISE_ADDR_SIZE {
return Err(cbor_event::Error::TrailingData.into());
}
AddrType::Enterprise(EnterpriseAddress::new(network, &read_addr_cred(4, 1)))
}
// reward
0b1110 | 0b1111 => {
const REWARD_ADDR_SIZE: usize = 1 + HASH_LEN;
if data.len() < REWARD_ADDR_SIZE {
return Err(
cbor_event::Error::NotEnough(data.len(), REWARD_ADDR_SIZE).into()
);
}
if data.len() > REWARD_ADDR_SIZE {
return Err(cbor_event::Error::TrailingData.into());
}
AddrType::Reward(RewardAddress::new(network, &read_addr_cred(4, 1)))
}
// byron
0b1000 => {
// note: 0b1000 was chosen because all existing Byron addresses actually start with 0b1000
// Therefore you can re-use Byron addresses as-is
match ByronAddress::from_bytes(data.to_vec()) {
Ok(addr) => AddrType::Byron(addr),
Err(e) => {
return Err(cbor_event::Error::CustomError(
e.as_string().unwrap_or_default(),
)
.into())
}
}
}
_ => return Err(DeserializeFailure::BadAddressType(header).into()),
};
Ok(Address(addr))
})()
.map_err(|e| e.annotate("Address"))
}
sourcepub fn payment_cred(&self) -> StakeCredential
pub fn payment_cred(&self) -> StakeCredential
Examples found in repository?
src/tx_builder/tx_inputs_builder.rs (line 390)
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
pub fn add_input(&mut self, address: &Address, input: &TransactionInput, amount: &Value) {
match &BaseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(hash) => return self.add_key_input(hash, input, amount),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(hash) => return self.add_script_input(hash, input, amount),
None => (),
}
}
None => (),
}
match &EnterpriseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(hash) => return self.add_key_input(hash, input, amount),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(hash) => return self.add_script_input(hash, input, amount),
None => (),
}
}
None => (),
}
match &PointerAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(hash) => return self.add_key_input(hash, input, amount),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(hash) => return self.add_script_input(hash, input, amount),
None => (),
}
}
None => (),
}
match &ByronAddress::from_address(address) {
Some(addr) => {
return self.add_bootstrap_input(addr, input, amount);
}
None => (),
}
}
More examples
src/tx_builder/batch_tools/witnesses_calculator.rs (line 36)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub(super) fn add_address(&mut self, address: &Address) -> Result<(), JsError> {
if self.adresses.contains(address) {
return Ok(());
}
self.adresses.insert(address.clone());
match &BaseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(_) => self.add_vkey(),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(_) => return Err(JsError::from_str("Script input is not supported for send all")),
None => ()
}
}
None => ()
}
match &EnterpriseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(_) => self.add_vkey(),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(_) => return Err(JsError::from_str("Script input is not supported for send all")),
None => ()
}
}
None => (),
}
match &PointerAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(_) => self.add_vkey(),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(_) => return Err(JsError::from_str("Script input is not supported for send all")),
None => ()
}
}
None => (),
}
match &ByronAddress::from_address(address) {
Some(addr) => self.add_boostrap(addr),
None => (),
}
Ok(())
}
pub fn stake_cred(&self) -> StakeCredential
sourcepub fn to_address(&self) -> Address
pub fn to_address(&self) -> Address
sourcepub fn from_address(addr: &Address) -> Option<BaseAddress>
pub fn from_address(addr: &Address) -> Option<BaseAddress>
Examples found in repository?
src/tx_builder/tx_inputs_builder.rs (line 388)
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
pub fn add_input(&mut self, address: &Address, input: &TransactionInput, amount: &Value) {
match &BaseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(hash) => return self.add_key_input(hash, input, amount),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(hash) => return self.add_script_input(hash, input, amount),
None => (),
}
}
None => (),
}
match &EnterpriseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(hash) => return self.add_key_input(hash, input, amount),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(hash) => return self.add_script_input(hash, input, amount),
None => (),
}
}
None => (),
}
match &PointerAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(hash) => return self.add_key_input(hash, input, amount),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(hash) => return self.add_script_input(hash, input, amount),
None => (),
}
}
None => (),
}
match &ByronAddress::from_address(address) {
Some(addr) => {
return self.add_bootstrap_input(addr, input, amount);
}
None => (),
}
}
More examples
src/tx_builder/batch_tools/witnesses_calculator.rs (line 34)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub(super) fn add_address(&mut self, address: &Address) -> Result<(), JsError> {
if self.adresses.contains(address) {
return Ok(());
}
self.adresses.insert(address.clone());
match &BaseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(_) => self.add_vkey(),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(_) => return Err(JsError::from_str("Script input is not supported for send all")),
None => ()
}
}
None => ()
}
match &EnterpriseAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(_) => self.add_vkey(),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(_) => return Err(JsError::from_str("Script input is not supported for send all")),
None => ()
}
}
None => (),
}
match &PointerAddress::from_address(address) {
Some(addr) => {
match &addr.payment_cred().to_keyhash() {
Some(_) => self.add_vkey(),
None => (),
}
match &addr.payment_cred().to_scripthash() {
Some(_) => return Err(JsError::from_str("Script input is not supported for send all")),
None => ()
}
}
None => (),
}
match &ByronAddress::from_address(address) {
Some(addr) => self.add_boostrap(addr),
None => (),
}
Ok(())
}
Trait Implementations§
source§impl Clone for BaseAddress
impl Clone for BaseAddress
source§fn clone(&self) -> BaseAddress
fn clone(&self) -> BaseAddress
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for BaseAddress
impl Debug for BaseAddress
source§impl Ord for BaseAddress
impl Ord for BaseAddress
source§fn cmp(&self, other: &BaseAddress) -> Ordering
fn cmp(&self, other: &BaseAddress) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq<BaseAddress> for BaseAddress
impl PartialEq<BaseAddress> for BaseAddress
source§fn eq(&self, other: &BaseAddress) -> bool
fn eq(&self, other: &BaseAddress) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<BaseAddress> for BaseAddress
impl PartialOrd<BaseAddress> for BaseAddress
source§fn partial_cmp(&self, other: &BaseAddress) -> Option<Ordering>
fn partial_cmp(&self, other: &BaseAddress) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more