Skip to content

Commit 7f00e25

Browse files
committed
refactor(stack): helper function to encode pkh as bytes
1 parent 4b16e47 commit 7f00e25

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

stack/src/operators.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ impl MyValue {
7575
_ => Err(ScriptError::UnexpectedArgument),
7676
}
7777
}
78+
79+
pub fn from_pkh(pkh: &PublicKeyHash) -> Self {
80+
let pkh_bytes = pkh.as_ref();
81+
82+
MyValue::Bytes(pkh_bytes.to_vec())
83+
}
84+
85+
pub fn to_pkh(&self) -> Result<PublicKeyHash, ScriptError> {
86+
match self {
87+
MyValue::Bytes(bytes) => {
88+
let pkh: PublicKeyHash = PublicKeyHash::from_bytes(bytes)
89+
.map_err(|_e| ScriptError::InvalidPublicKeyHash)?;
90+
91+
Ok(pkh)
92+
}
93+
_ => Err(ScriptError::UnexpectedArgument),
94+
}
95+
}
7896
}
7997

8098
fn equal_operator(stack: &mut Stack<MyValue>) -> Result<(), ScriptError> {
@@ -199,16 +217,8 @@ fn check_multi_sig(
199217

200218
let mut pkhs = vec![];
201219
for bytes_pkh in bytes_pkhs {
202-
match bytes_pkh {
203-
MyValue::Bytes(bytes) => {
204-
let pkh: PublicKeyHash = PublicKeyHash::from_bytes(&bytes)
205-
.map_err(|_e| ScriptError::InvalidPublicKeyHash)?;
206-
pkhs.push(pkh);
207-
}
208-
_ => {
209-
return Err(ScriptError::UnexpectedArgument);
210-
}
211-
}
220+
let pkh = bytes_pkh.to_pkh()?;
221+
pkhs.push(pkh);
212222
}
213223

214224
for sign_pkh in signed_pkhs {

0 commit comments

Comments
 (0)