update AddressInfo struct

This commit is contained in:
eunoia_1729 2022-03-10 06:22:02 +05:30
parent bac15bb207
commit 2698fc0219
No known key found for this signature in database
GPG Key ID: 9250C363C00A1E70

View File

@ -140,6 +140,8 @@ pub struct AddressInfo {
pub index: u32, pub index: u32,
/// Address /// Address
pub address: Address, pub address: Address,
/// Type of keychain
pub keychain: KeychainKind,
} }
impl Deref for AddressInfo { impl Deref for AddressInfo {
@ -246,6 +248,7 @@ where
.map(|address| AddressInfo { .map(|address| AddressInfo {
address, address,
index: incremented_index, index: incremented_index,
keychain,
}) })
.map_err(|_| Error::ScriptDoesntHaveAddressForm) .map_err(|_| Error::ScriptDoesntHaveAddressForm)
} }
@ -276,6 +279,7 @@ where
.map(|address| AddressInfo { .map(|address| AddressInfo {
address, address,
index: current_index, index: current_index,
keychain,
}) })
.map_err(|_| Error::ScriptDoesntHaveAddressForm) .map_err(|_| Error::ScriptDoesntHaveAddressForm)
} }
@ -286,7 +290,11 @@ where
self.get_descriptor_for_keychain(keychain) self.get_descriptor_for_keychain(keychain)
.as_derived(index, &self.secp) .as_derived(index, &self.secp)
.address(self.network) .address(self.network)
.map(|address| AddressInfo { index, address }) .map(|address| AddressInfo {
index,
address,
keychain,
})
.map_err(|_| Error::ScriptDoesntHaveAddressForm) .map_err(|_| Error::ScriptDoesntHaveAddressForm)
} }
@ -298,7 +306,11 @@ where
self.get_descriptor_for_keychain(keychain) self.get_descriptor_for_keychain(keychain)
.as_derived(index, &self.secp) .as_derived(index, &self.secp)
.address(self.network) .address(self.network)
.map(|address| AddressInfo { index, address }) .map(|address| AddressInfo {
index,
address,
keychain,
})
.map_err(|_| Error::ScriptDoesntHaveAddressForm) .map_err(|_| Error::ScriptDoesntHaveAddressForm)
} }
@ -3924,6 +3936,7 @@ pub(crate) mod test {
AddressInfo { AddressInfo {
index: 0, index: 0,
address: Address::from_str("tb1q6yn66vajcctph75pvylgkksgpp6nq04ppwct9a").unwrap(), address: Address::from_str("tb1q6yn66vajcctph75pvylgkksgpp6nq04ppwct9a").unwrap(),
keychain: KeychainKind::External,
} }
); );
@ -3932,7 +3945,8 @@ pub(crate) mod test {
wallet.get_address(New).unwrap(), wallet.get_address(New).unwrap(),
AddressInfo { AddressInfo {
index: 1, index: 1,
address: Address::from_str("tb1q4er7kxx6sssz3q7qp7zsqsdx4erceahhax77d7").unwrap() address: Address::from_str("tb1q4er7kxx6sssz3q7qp7zsqsdx4erceahhax77d7").unwrap(),
keychain: KeychainKind::External,
} }
); );
@ -3941,7 +3955,8 @@ pub(crate) mod test {
wallet.get_address(Peek(25)).unwrap(), wallet.get_address(Peek(25)).unwrap(),
AddressInfo { AddressInfo {
index: 25, index: 25,
address: Address::from_str("tb1qsp7qu0knx3sl6536dzs0703u2w2ag6ppl9d0c2").unwrap() address: Address::from_str("tb1qsp7qu0knx3sl6536dzs0703u2w2ag6ppl9d0c2").unwrap(),
keychain: KeychainKind::External,
} }
); );
@ -3950,7 +3965,8 @@ pub(crate) mod test {
wallet.get_address(New).unwrap(), wallet.get_address(New).unwrap(),
AddressInfo { AddressInfo {
index: 2, index: 2,
address: Address::from_str("tb1qzntf2mqex4ehwkjlfdyy3ewdlk08qkvkvrz7x2").unwrap() address: Address::from_str("tb1qzntf2mqex4ehwkjlfdyy3ewdlk08qkvkvrz7x2").unwrap(),
keychain: KeychainKind::External,
} }
); );
@ -3959,7 +3975,8 @@ pub(crate) mod test {
wallet.get_address(Reset(1)).unwrap(), wallet.get_address(Reset(1)).unwrap(),
AddressInfo { AddressInfo {
index: 1, index: 1,
address: Address::from_str("tb1q4er7kxx6sssz3q7qp7zsqsdx4erceahhax77d7").unwrap() address: Address::from_str("tb1q4er7kxx6sssz3q7qp7zsqsdx4erceahhax77d7").unwrap(),
keychain: KeychainKind::External,
} }
); );
@ -3968,7 +3985,8 @@ pub(crate) mod test {
wallet.get_address(New).unwrap(), wallet.get_address(New).unwrap(),
AddressInfo { AddressInfo {
index: 2, index: 2,
address: Address::from_str("tb1qzntf2mqex4ehwkjlfdyy3ewdlk08qkvkvrz7x2").unwrap() address: Address::from_str("tb1qzntf2mqex4ehwkjlfdyy3ewdlk08qkvkvrz7x2").unwrap(),
keychain: KeychainKind::External,
} }
); );
} }
@ -3997,15 +4015,21 @@ pub(crate) mod test {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
wallet.get_address(AddressIndex::New).unwrap().address, wallet.get_address(AddressIndex::New).unwrap(),
Address::from_str("bcrt1qkmvk2nadgplmd57ztld8nf8v2yxkzmdvwtjf8s").unwrap() AddressInfo {
index: 0,
address: Address::from_str("bcrt1qkmvk2nadgplmd57ztld8nf8v2yxkzmdvwtjf8s").unwrap(),
keychain: KeychainKind::External,
}
); );
assert_eq!( assert_eq!(
wallet wallet.get_internal_address(AddressIndex::New).unwrap(),
.get_internal_address(AddressIndex::New) AddressInfo {
.unwrap() index: 0,
.address, address: Address::from_str("bcrt1qtrwtz00wxl69e5xex7amy4xzlxkaefg3gfdkxa").unwrap(),
Address::from_str("bcrt1qtrwtz00wxl69e5xex7amy4xzlxkaefg3gfdkxa").unwrap() keychain: KeychainKind::Internal,
}
); );
let wallet = Wallet::new( let wallet = Wallet::new(
@ -4017,11 +4041,12 @@ pub(crate) mod test {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
wallet wallet.get_internal_address(AddressIndex::New).unwrap(),
.get_internal_address(AddressIndex::New) AddressInfo {
.unwrap() index: 0,
.address, address: Address::from_str("bcrt1qkmvk2nadgplmd57ztld8nf8v2yxkzmdvwtjf8s").unwrap(),
Address::from_str("bcrt1qkmvk2nadgplmd57ztld8nf8v2yxkzmdvwtjf8s").unwrap(), keychain: KeychainKind::Internal,
},
"when there's no internal descriptor it should just use external" "when there's no internal descriptor it should just use external"
); );
} }