Skip to content

Commit

Permalink
Add signet support
Browse files Browse the repository at this point in the history
  • Loading branch information
polespinasa committed May 1, 2024
1 parent 300bb6e commit 19eac28
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
4 changes: 3 additions & 1 deletion server/grouphug-server/Config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[electrum]
# mainnet_server_endpoint = "ssl://electrum.blockstream.info:50002"
#MAINNET BLOCKSTREAM ELECTRUM ENDPOINT = "ssl://electrum.blockstream.info:50002"
#TESTNET BLOCKSTREAM ELECTRUM ENDPOINT = "ssl://electrum.blockstream.info:60002"
endpoint = "ssl://electrum.blockstream.info:60002"
certificate_validation = true

[group]
max_time = 300
Expand Down
1 change: 1 addition & 0 deletions server/grouphug-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Config {
#[derive(Deserialize)]
pub struct Electrum {
pub endpoint: String,
pub certificate_validation: bool,
}

#[derive(Deserialize)]
Expand Down
3 changes: 3 additions & 0 deletions server/grouphug-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ fn handle_client(mut stream: TcpStream) {
else if &crate::CONFIG.network.name == "mainnet" {
stream.write(b"MAINNET\n").unwrap();
}
else if &crate::CONFIG.network.name == "signet" {
stream.write(b"SIGNET\n").unwrap();
}

// 100KB size for large transactions
let mut buffer = [0; 100*1024];
Expand Down
9 changes: 4 additions & 5 deletions server/grouphug-server/src/server/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bdk::bitcoin::{
consensus::encode::deserialize,
consensus::encode::serialize_hex
};
use bdk::electrum_client::{Client, ElectrumApi};
use bdk::electrum_client::{Client, ConfigBuilder, ElectrumApi};
use bdk::blockchain::{ElectrumBlockchain, GetTx};


Expand Down Expand Up @@ -83,9 +83,8 @@ impl Group {
// Finalize the transaction and send it to the network

// Connect to Electrum node
let client = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();


let config = ConfigBuilder::new().validate_domain(crate::CONFIG.electrum.certificate_validation).build();
let client = Client::from_config(&crate::CONFIG.electrum.endpoint, config.clone()).unwrap();
let blockchain = ElectrumBlockchain::from(client);

// Check that the transactions included in the group have not been already spent
Expand Down Expand Up @@ -140,7 +139,7 @@ impl Group {

// broadcast the transaction
// There's a issue with client 1 here... TODO FIX
let client2 = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();
let client2 = Client::from_config(&crate::CONFIG.electrum.endpoint, config.clone()).unwrap();
let txid = client2.transaction_broadcast_raw(&tx_bytes);

match txid {
Expand Down
24 changes: 13 additions & 11 deletions server/grouphug-server/src/utils/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bdk::bitcoin::{
blockdata::locktime::absolute::{Height, Time}};

use bdk::blockchain::{ElectrumBlockchain, GetTx};
use bdk::electrum_client::{Client, ElectrumApi};
use bdk::electrum_client::{Client, ConfigBuilder, ElectrumApi};
use hex::decode as hex_decode;


Expand All @@ -16,18 +16,20 @@ pub fn which_network(tx: &Transaction) -> bool {
// Take previous UTXO
let tx_id = tx.input[0].previous_output.txid;

// Test mainnet
let client_mainnet = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();
let blockchain_mainnet = ElectrumBlockchain::from(client_mainnet);
let config = ConfigBuilder::new().validate_domain(crate::CONFIG.electrum.certificate_validation).build();
let client = Client::from_config(&crate::CONFIG.electrum.endpoint, config.clone()).unwrap();
let blockchain = ElectrumBlockchain::from(client);


let tx_result_mainnet = blockchain_mainnet.get_tx(&tx_id);
match tx_result_mainnet {
let tx_result = blockchain.get_tx(&tx_id);
match tx_result {
Ok(Some(_tx)) => {
return true
},
Ok(None) => (),
Err(_) => (),
Err(e) => {
println!("Error: {:?}", e);
},
}
return false;
}
Expand All @@ -37,8 +39,8 @@ pub fn get_previous_utxo_value(utxo: OutPoint) -> f32 {
// If no UTXO is recieved back, the value returned is 0.

// Connect to Electrum node
let client = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();

let config = ConfigBuilder::new().validate_domain(crate::CONFIG.electrum.certificate_validation).build();
let client = Client::from_config(&crate::CONFIG.electrum.endpoint, config.clone()).unwrap();
let blockchain = ElectrumBlockchain::from(client);

let tx_result = blockchain.get_tx(&utxo.txid);
Expand All @@ -64,8 +66,8 @@ pub fn previous_utxo_spent(tx: &Transaction) -> bool {
// Validates that the UTXOs pointed to by the transaction inputs have not been spent.

// Connect to Electrum node
let client = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();

let config = ConfigBuilder::new().validate_domain(crate::CONFIG.electrum.certificate_validation).build();
let client = Client::from_config(&crate::CONFIG.electrum.endpoint, config.clone()).unwrap();
let blockchain = ElectrumBlockchain::from(client);

for i in 0..tx.input.len() {
Expand Down

0 comments on commit 19eac28

Please sign in to comment.