Skip to content

Instantly share code, notes, and snippets.

View RCasatta's full-sized avatar
🦎

Riccardo Casatta RCasatta

🦎
  • Italia
View GitHub Profile
use bitcoin::secp256k1::Secp256k1;
use electrum_client::ElectrumApi;
use electrum_client::Client;
use bitcoin::util::bip32;
use bitcoin::util::bip32::DerivationPath;
use bitcoin::Address;
use bitcoin::Network;
use std::str::FromStr;
fn main() {
@RCasatta
RCasatta / cpufreq.sh
Created April 9, 2022 13:33
Set the max frequency of the CPU
#!/bin/bash
# verify your available scaling frequency with:
# `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies`
# and adapt this script
unset FREQ
case $1 in
@RCasatta
RCasatta / taproot_playground.rs
Created October 27, 2021 12:52
Some test code trying out taproot
#[cfg(test)]
mod tests {
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::schnorr::{KeyPair, PublicKey};
use bitcoin::{Script, Address, Network, Transaction, TxIn, OutPoint, TxOut};
use bitcoin::blockdata::{script, opcodes};
use bitcoin::util::taproot::{TaprootSpendInfo, LeafVersion};
use bitcoin::util::address::WitnessVersion;
use bitcoin::util::sighash::{SigHashCache, ScriptPath, SigHashType};
use bitcoin::util::sighash::Prevouts;
@RCasatta
RCasatta / taproot.rs
Created October 19, 2021 10:55
taproot tests, trying to derive same taproot address from core and from rust
#[test]
fn taproot_tests() {
let secp = Secp256k1::new();
let secret = Vec::from_hex("6c068c2094c3f0986741cddbaff96399e338b4120671c6640216d3e474487a19").unwrap();
let pair = KeyPair::from_seckey_slice(&secp, &secret).unwrap();
let public = schnorr::PublicKey::from_keypair(&secp, &pair);
assert_eq!(public.to_hex(),"72ad3fb702bf1a2111b09c2bf1e72f4f52d4ceb2acdcfcd0c63abf70a59c36d6");
let second_secret = Vec::from_hex("1cbb4d11a771697c1d86d0bbbcf0344b1e8b70d0cc794c3d9d6eff613390ba5e").unwrap();
let second_pair = KeyPair::from_seckey_slice(&secp, &second_secret).unwrap();
@RCasatta
RCasatta / gist:32a579630b04259d580ae6596c715411
Created December 14, 2020 14:50
test create tx with send all
cargo run --example repl --features cli-utils,esplora,electrum -- --wallet single_change_deep --descriptor "wpkh(tpubD6NzVbkrYhZ4YmSHJMXPEvd6dnPgH55EwGBs2AJHyiBgnT7zJzQ1ywHDxxZveoApLeBeSFcjysjQ5PebSg4gsdrVdRxCLAgHK8jKUiydMrg/*)" sync --max_addresses 200
ADDRESS=$(cargo run --example repl --features cli-utils,esplora,electrum -- --wallet single_change_deep --descriptor "wpkh(tpubD6NzVbkrYhZ4YmSHJMXPEvd6dnPgH55EwGBs2AJHyiBgnT7zJzQ1ywHDxxZveoApLeBeSFcjysjQ5PebSg4gsdrVdRxCLAgHK8jKUiydMrg/*)" get_new_address | jq -r .address)
PSBT=$(cargo run --example repl --features cli-utils,esplora,electrum -- --wallet single_change_deep --descriptor "wpkh(tpubD6NzVbkrYhZ4YmSHJMXPEvd6dnPgH55EwGBs2AJHyiBgnT7zJzQ1ywHDxxZveoApLeBeSFcjysjQ5PebSg4gsdrVdRxCLAgHK8jKUiydMrg/*)" create_tx --to $ADDRESS:0 -a | jq -r .psbt)
bitcoin-cli decodepsbt $PSBT
OTHER_ADDRESS=$(cargo run --example repl --features cli-utils,esplora,electrum -- --wallet single_many_utxos --descriptor "wpkh(tpubD6NzVbkrYhZ4X2yy78HWrr1M9NT8dKeWfzNiQqDdMqqa9UmmGztG
@RCasatta
RCasatta / gist:31addef5e610666953144f51ad72b227
Created August 12, 2020 14:00
gdk-electrum exposed SPV
The following is a proposal to expose SPV validation.
gdk-electrum has SPV validation for:
- bitcoin: headers chain is downloaded verified and saved in a flat file, tx proof is downloaded for every wallet tx, and checked against the headers.
- liquid: no need to download the chain, tx proof is downloaded, the header of the block containing the tx is downloaded, the proof is checked against the Merkle root in the header, script, and challenge of the header are verified
This functionality must be exposed (so we can SPV validate on ios and remove bitcoinj on android), for liquid is quite easy since it is not required to download the headers chain which is done in a thread in gdk-electrum (this may change with dynafed). Running a thread without a session is problematic (because you don't know if you need to close it), for this reason, I was thinking an interface like the following:
```
use electrum_client::{Client, GetHistoryRes};
use bitcoin::util::bip32::{ChildNumber, ExtendedPubKey};
use bitcoin::secp256k1::{Secp256k1, All};
use bitcoin::{Address, Network, Script, Transaction, BlockHeader};
use bitcoin::consensus::{serialize, deserialize};
use std::str::FromStr;
use std::time::Instant;
use std::collections::{HashSet, HashMap};
use sled;
FEES_COLLECTED=$(lightning-cli getinfo | jq '.msatoshi_fees_collected / 1000')
AVERAGE_FORWARDED=$(lightning-cli listforwards | jq '.forwards[] | select (.status == "settled") | .in_msatoshi' | jq -s 'add / length / 1000')
PAYMENTS_FORWARDED=$(lightning-cli listforwards | jq '.forwards[] | select (.status == "settled") | .in_msatoshi' | jq -s 'length')
echo "Payments forwarded: $PAYMENTS_FORWARDED"
echo "Average payment forwarded: $AVERAGE_FORWARDED satoshi"
echo "Fees collected: $FEES_COLLECTED satoshi"
@RCasatta
RCasatta / fresh_invoice.py
Last active September 14, 2019 06:32
plugin for c-lightning
#!/usr/bin/env python3
from lightning import Plugin
import uuid
from paramiko import SSHClient
from scp import SCPClient
import io
plugin = Plugin()
@plugin.init()
➜ rust-libwally cat Cargo.toml
[package]
name = "rust-libwally"
version = "0.1.0"
authors = ["Riccardo Casatta <riccardo@casatta.it>"]
build = "build.rs"
[dependencies]
libc = "0.2"