neo3/neo_builder/
utils.rs

1use serde_json::Value;
2
3use crate::{
4	builder::{
5		ScriptBuilder, Signer, TransactionAttribute, TransactionSendToken, TransactionSigner,
6	},
7	crypto::Secp256r1PublicKey,
8};
9use neo3::prelude::*;
10// pub type ScriptHash = H160;
11
12/// Converts a list of public keys to a script hash using a given threshold.
13///
14/// # Arguments
15///
16/// * `public_keys` - A mutable slice of `Secp256r1PublicKey` instances.
17/// * `threshold` - The minimum number of signatures required to validate the transaction.
18///
19/// # Returns
20///
21/// A `ScriptHash` instance representing the script hash of the MultiSig script.
22pub fn public_keys_to_scripthash(
23	public_keys: &mut [Secp256r1PublicKey],
24	threshold: usize,
25) -> ScriptHash {
26	let script = ScriptBuilder::build_multi_sig_script(public_keys, threshold as u8).unwrap();
27	// Self::from_script(&script)
28	ScriptHash::from_slice(&script)
29}
30
31/// Converts a public key to a script hash.
32///
33/// # Arguments
34///
35/// * `public_key` - A `Secp256r1PublicKey` instance.
36///
37/// # Returns
38///
39/// A `ScriptHash` instance representing the script hash of the verification script.
40pub fn pubkey_to_scripthash(public_key: &Secp256r1PublicKey) -> ScriptHash {
41	let script = ScriptBuilder::build_verification_script(public_key);
42	ScriptHash::from_script(&script)
43}
44
45pub trait VecValueExtension {
46	fn to_value(&self) -> Value;
47}
48
49impl ValueExtension for TransactionAttribute {
50	fn to_value(&self) -> Value {
51		Value::String(self.to_json())
52	}
53}
54
55impl ValueExtension for TransactionSendToken {
56	fn to_value(&self) -> Value {
57		Value::String(serde_json::to_string(self).unwrap())
58	}
59}
60
61impl VecValueExtension for Vec<TransactionSendToken> {
62	fn to_value(&self) -> Value {
63		self.iter().map(|x| x.to_value()).collect()
64	}
65}
66
67impl VecValueExtension for Vec<TransactionAttribute> {
68	fn to_value(&self) -> Value {
69		self.iter().map(|x| x.to_value()).collect()
70	}
71}
72impl ValueExtension for Signer {
73	fn to_value(&self) -> Value {
74		Value::String(serde_json::to_string(self).unwrap())
75	}
76}
77
78impl VecValueExtension for Vec<Signer> {
79	fn to_value(&self) -> Value {
80		self.iter().map(|x| x.to_value()).collect()
81	}
82}
83
84impl ValueExtension for TransactionSigner {
85	fn to_value(&self) -> Value {
86		Value::String(serde_json::to_string(self).unwrap())
87	}
88}
89
90impl VecValueExtension for Vec<TransactionSigner> {
91	fn to_value(&self) -> Value {
92		self.iter().map(|x| x.to_value()).collect()
93	}
94}