Create account

replied 1d
memo-msg/src/main.rs
replied 1d
fn execute_impl(args: Vec<&str>) -> Result<Vec<u8>> {
let out = Command::new("bitcoin-cli").args(args).output()?.stdout;
Ok(out)
}
replied 1d
Ok(serde_json::from_slice(&out)?)
}

fn execute_str(args: Vec<&str>) -> Result<String> {
let out = execute_impl(args)?;
Ok(String::from_utf8(out)?)
}
replied 1d
println!("{}", tx.trim());
}
Ok(())
}

fn execute<T: DeserializeOwned>(args: Vec<&str>) -> Result<T> {
let out = execute_impl(args)?;
replied 1d
let signraw: SignRaw = execute(vec!["signrawtransactionwithkey", &raw, &keys])?;
if signraw.complete {
let tx = execute_str(vec!["sendrawtransaction", &signraw.hex])?;
replied 1d
lock_time: PackedLockTime::ZERO,
input,
output,
};
let raw = hex::encode(tx.serialize());
let keys = format!(r#"["{}"]"#, KEY.memo_key);
replied 1d
script_pubkey: Script::new_p2pkh(&KEY.pubkeyhash),
token: None,
};
output.push(txout);
let tx = Transaction {
version: 2,
replied 1d
if amount > fee + 546 {
amount -= fee;
break;
}
}
let txout = TxOut {
value: amount,
replied 1d
sequence: Sequence::MAX,
witness: Witness::new(),
};
input.push(txin);
amount += u.uamount;
fee += 148;
replied 1d
let outpoint = OutPoint { txid, vout };
let txin = TxIn {
previous_output: outpoint,
script_sig: Script::new(),
replied 1d
unspents.sort_unstable_by_key(|u| u.uamount);
for u in unspents {
let txid = Txid::from_hex(&u.txid)?;
let vout = u.vout;
replied 1d
.into_iter()
.map(|u| Unspent {
uamount: (u.amount * 100000000.0) as u64,
..u
})
.collect();
replied 1d
r#"{"minimumAmount": 0.00000547, "includeTokens": false}"#,
])?;
let mut input = vec![];
let mut output = vec![op_return];
let mut unspents: Vec<_> = unspents
replied 1d
let unspents: Vec<Unspent> = execute(vec![
"listunspent",
"1",
"99999999",
&addrs,
"false",
replied 1d
let op_return = TxOut {
value: 0,
script_pubkey: op_return,
token: None,
};
let mut amount = 0;
let addrs = format!(r#"["{}"]"#, KEY.address);
replied 1d
hex: String,
complete: bool,
}

fn run() -> Result<()> {
let op_return = memo::build_op_return()?;
let mut fee = op_return.as_bytes().len() as u64 + 44 + 9;
replied 1d
#[derive(Deserialize)]
struct Unspent {
txid: String,
vout: u32,
amount: f64,
#[serde(default)]
uamount: u64,
}

#[derive(Deserialize)]
struct SignRaw {
replied 1d
hashes::hex::FromHex, psbt::serialize::Serialize,
};
use serde::{Deserialize, de::DeserializeOwned};

use crate::key::KEY;

fn main() {
run().expect("Failed")
}
replied 1d
mod key;
mod memo;

use std::process::Command;

use anyhow::Result;
use bitcoincash::{
OutPoint, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Txid, Witness,