Skip to content

Commit 7737c82

Browse files
committed
Add elip_liquidex module
1 parent 2ca6c7b commit 7737c82

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/pset/elip_liquidex.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//!
2+
//! An implementation of ELIP0XXX as defined in
3+
//! <https://github.com/ElementsProject/ELIPs/blob/main/elip-0XXX.mediawiki>
4+
//!
5+
//! ELIP0XXX defines how to encode the extra data for LiquiDEX in a PSET.
6+
//!
7+
8+
use crate::pset::{
9+
confidential::AssetBlindingFactor,
10+
encode,
11+
raw::ProprietaryKey,
12+
serialize::{Deserialize, Serialize},
13+
Input, Output,
14+
};
15+
16+
/// Input Asset Blinding Factor keytype as defined in ELIP0XXX
17+
pub const PSBT_ELEMENTS_LIQUIDEX_IN_ABF: u8 = 0x00u8;
18+
19+
/// Output Asset Blinding Factor keytype as defined in ELIP0XXX
20+
pub const PSBT_ELEMENTS_LIQUIDEX_OUT_ABF: u8 = 0x00u8;
21+
22+
/// Prefix for PSET LiquiDEX extension as defined in ELIP0XXX
23+
pub const PSET_LIQUIDEX_PREFIX: &[u8] = b"pset_liquidex";
24+
25+
fn prop_key(keytype: u8) -> ProprietaryKey {
26+
ProprietaryKey {
27+
prefix: PSET_LIQUIDEX_PREFIX.to_vec(),
28+
subtype: keytype,
29+
key: vec![],
30+
}
31+
}
32+
33+
/// ELIP0XXX LiquiDEX extensions
34+
impl Input {
35+
/// Set Asset Blinding Factor
36+
pub fn set_abf(&mut self, abf: AssetBlindingFactor) {
37+
let key = prop_key(PSBT_ELEMENTS_LIQUIDEX_IN_ABF);
38+
self.proprietary.insert(key, abf.serialize());
39+
}
40+
41+
/// Get Asset Blinding Factor
42+
pub fn get_abf(&self) -> Option<Result<AssetBlindingFactor, encode::Error>> {
43+
let key = prop_key(PSBT_ELEMENTS_LIQUIDEX_IN_ABF);
44+
self.proprietary
45+
.get(&key)
46+
.map(|data| AssetBlindingFactor::deserialize(data))
47+
}
48+
}
49+
50+
/// ELIP0XXX LiquiDEX extensions
51+
impl Output {
52+
/// Set Asset Blinding Factor
53+
pub fn set_abf(&mut self, abf: AssetBlindingFactor) {
54+
let key = prop_key(PSBT_ELEMENTS_LIQUIDEX_OUT_ABF);
55+
self.proprietary.insert(key, abf.serialize());
56+
}
57+
58+
/// Get Asset Blinding Factor
59+
pub fn get_abf(&self) -> Option<Result<AssetBlindingFactor, encode::Error>> {
60+
let key = prop_key(PSBT_ELEMENTS_LIQUIDEX_OUT_ABF);
61+
self.proprietary
62+
.get(&key)
63+
.map(|data| AssetBlindingFactor::deserialize(data))
64+
}
65+
}
66+
67+
#[cfg(test)]
68+
mod test {
69+
use super::*;
70+
use crate::encode::{serialize_hex, Encodable};
71+
use crate::hex::{FromHex, ToHex};
72+
73+
// b'\xfc\rpset_liquidex'
74+
const ELIP0XXX_IDENTIFIER: &str = "fc0d707365745f6c69717569646578";
75+
76+
#[test]
77+
fn prop_key_serialize() {
78+
let key = prop_key(PSBT_ELEMENTS_LIQUIDEX_IN_ABF);
79+
let mut vec = vec![];
80+
key.consensus_encode(&mut vec).unwrap();
81+
82+
assert_eq!(
83+
vec.to_hex(),
84+
format!("0d{}00", PSET_LIQUIDEX_PREFIX.to_hex())
85+
);
86+
87+
assert!(vec.to_hex().starts_with(&ELIP0XXX_IDENTIFIER[2..])); // cut proprietary prefix "fc"
88+
}
89+
90+
#[test]
91+
fn set_get_abf() {
92+
// An ABF that's different if serialized in reverse or not
93+
let abf_hex = "3311111111111111111111111111111111111111111111111111111111111111";
94+
let abf_bytes = Vec::<u8>::from_hex(abf_hex).unwrap();
95+
let abf = AssetBlindingFactor::from_slice(&abf_bytes).unwrap();
96+
97+
let mut input = Input::default();
98+
assert!(input.get_abf().is_none());
99+
input.set_abf(abf);
100+
assert_eq!(input.get_abf().unwrap().unwrap(), abf);
101+
let input_hex = serialize_hex(&input);
102+
assert!(input_hex.contains(ELIP0XXX_IDENTIFIER));
103+
assert!(input_hex.contains(abf_hex));
104+
105+
let mut output = Output::default();
106+
assert!(output.get_abf().is_none());
107+
output.set_abf(abf);
108+
assert_eq!(output.get_abf().unwrap().unwrap(), abf);
109+
let output_hex = serialize_hex(&output);
110+
assert!(output_hex.contains(ELIP0XXX_IDENTIFIER));
111+
assert!(output_hex.contains(abf_hex));
112+
}
113+
}

src/pset/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod map;
3030
pub mod raw;
3131
pub mod serialize;
3232
pub mod elip100;
33+
pub mod elip_liquidex;
3334

3435
#[cfg(feature = "base64")]
3536
mod str;

0 commit comments

Comments
 (0)