Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Examples #81

Merged
merged 3 commits into from
Apr 17, 2024
Merged

Add Examples #81

merged 3 commits into from
Apr 17, 2024

Conversation

Sosthene00
Copy link
Contributor

Create a examples dir to help people figure out how to use the lib.

Copy link

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK c594ddd

Thanks! :)

@cygnet3
Copy link
Owner

cygnet3 commented Mar 19, 2024

Receiving part looks good. Maybe we can also show some examples with hardcoded values?
For example, this worked for me:

cargo run --example find_output "sock wait angry photo endless ready dune mechanic ketchup crowd inhale cat" "020000000001012c8a11d71cf530e68fda5e408dbbafb021e7451568e7bbfea286fc76ac0cd5680000000000fdffffff018012000000000000225120ccbebdd796de2333747d5fa88109abcb463d92548a7dbc68e9cf68bd7c1d206202463043021f0a8c1628e5c50402016627ec1789a862d344cbb49d7e34f99551b5020ce73d02205190ed8c8eda534b719bb0117309cea1f5f3a6b0fc76693a996d140f75ab4e0d012103529dbe46ef50b96fdb89925631156ea31296cef4ba4b2e0b4ba4799ba4499e6240dc0200" "001471c944097ff1f456df4efc8723a508af1d5c7c7f"

@cygnet3
Copy link
Owner

cygnet3 commented Mar 19, 2024

Maybe we can also print the descriptor with the checksum? To import it with bitcoin core you need the checksum iirc. This is how I do it:

    let rawtr = format!("rawtr({})", privkey.to_wif());
    let checksum = bdk::descriptor::checksum::calc_checksum(&rawtr).unwrap();

    println!("descriptor: {}#{}", rawtr, checksum);

though this also adds bdk as a dependency.

@cygnet3
Copy link
Owner

cygnet3 commented Mar 19, 2024

As for sending, a very simple example would be:

    // address to send to
    let sp_address = "tsp1q...".to_string();

    // get the xprv from a given seed phrase
    let mnemonic = Mnemonic::from_str("...").unwrap();
    let root_xprv = Xpriv::new_master(Network::Signet, &mnemonic.to_seed("")).unwrap();

    // get single key from some derivation path, this is the only input we use
    let derivation_path: DerivationPath = DerivationPath::from_str("m/84'/1'/0'/0/0").unwrap();
    let sender_key = root_xprv
        .derive_priv(&secp, &derivation_path)
        .unwrap()
        .to_priv()
        .inner;

    // get the prevout for this private key, from a utxo that 'belongs' to this private key
    let txid = "...";
    let vout = 0;

    let input_keys = [(sender_key, false)];
    let outpoints_data = [(txid.to_owned(), vout)];
    let partial_secret = calculate_partial_secret(&input_keys, &outpoints_data).unwrap();
    let mut output = generate_recipient_pubkeys(vec![sp_address], partial_secret).unwrap();
    let output = output.remove(&sp_address).unwrap()[0];

    let assumetweaked = TweakedPublicKey::dangerous_assume_tweaked(output);
    let p2tr_address = Address::p2tr_tweaked(assumetweaked, Network::Signet);
    println!("p2tr address: {:?}", p2tr_address);

but then get these argument from command line args.
Of course, that might be too simple/not very accurate for wallet developers.

@cygnet3 cygnet3 merged commit 1b5bc85 into cygnet3:master Apr 17, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants