Skip to content

Commit

Permalink
move spk encoding to txscript module
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed Nov 9, 2024
1 parent 3eae88a commit 8422331
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
11 changes: 0 additions & 11 deletions consensus/core/src/tx/script_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ pub struct ScriptPublicKey {
pub(super) script: ScriptVec, // Kept private to preserve read-only semantics
}

impl ScriptPublicKey {
pub fn to_bytes(&self) -> Vec<u8> {
let version = self.version.to_be_bytes();
let script = self.script();
let mut v = Vec::with_capacity(version.len() + script.len());
v.extend_from_slice(&version);
v.extend_from_slice(script);
v
}
}

impl std::fmt::Debug for ScriptPublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ScriptPublicKey").field("version", &self.version).field("script", &self.script.to_hex()).finish()
Expand Down
15 changes: 15 additions & 0 deletions crypto/txscript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,21 @@ impl<'a, T: VerifiableTransaction, Reused: SigHashReusedValues> TxScriptEngine<'
}
}

trait SpkEncoding {
fn to_bytes(&self) -> Vec<u8>;
}

impl SpkEncoding for ScriptPublicKey {
fn to_bytes(&self) -> Vec<u8> {
let version = self.version.to_be_bytes();
let script = self.script();
let mut v = Vec::with_capacity(version.len() + script.len());
v.extend_from_slice(&version);
v.extend_from_slice(script);
v
}
}

#[cfg(test)]
mod tests {
use std::iter::once;
Expand Down
2 changes: 1 addition & 1 deletion crypto/txscript/src/opcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod macros;

use crate::data_stack::{DataStack, Kip10I64, OpcodeData};
use crate::{
ScriptSource, TxScriptEngine, TxScriptError, LOCK_TIME_THRESHOLD, MAX_TX_IN_SEQUENCE_NUM, NO_COST_OPCODE,
ScriptSource, SpkEncoding, TxScriptEngine, TxScriptError, LOCK_TIME_THRESHOLD, MAX_TX_IN_SEQUENCE_NUM, NO_COST_OPCODE,
SEQUENCE_LOCK_TIME_DISABLED, SEQUENCE_LOCK_TIME_MASK,
};
use blake2b_simd::Params;
Expand Down

0 comments on commit 8422331

Please sign in to comment.