dfinity / examples

Example applications, microservices, and code samples for the Internet Computer
https://dfinity.org
Apache License 2.0
515 stars 330 forks source link

feat(schnorr,bitcoin): CRP-2533 add taproot transactions and improvements #932

Closed altkdf closed 4 days ago

altkdf commented 3 weeks ago

This PR adds pay-to-taproot (P2TR) transactions to the basic_bitcoin example (see the modified README.md for more details):

Note that those addresses are mutually exclusive, i.e. a P2TR raw key address cannot be used for alternatively spending with scripts and vice versa. This is not a technical limitation but a limitation of the current API. The API will likely be extended to support alternative paths if there will be a reasonable use case.

This PR also implements small improvements like more precise fee computation based on transaction weight instead of serialized transaction, better code structure to accommodate P2TR, simplifications in the code where the bitcoin crate implements whatever was implemented in basic_bitcoin, updated bitcoin crate dependency, etc.

Notice that the motoko example is not modified in this PR but will be in another PR. The reason for the delay is the non-negligible implementation effort due to the complexity of P2TR.

altkdf commented 3 weeks ago

To test this canister, I deployed it on om77v-qqaaa-aaaap-ahmrq-cai with the Schnorr API implemented by the mock canister 6fwhw-fyaaa-aaaap-qb7ua-cai.

Bitcoin Testnet Addresses

P2pkh mot21Ef7HNDpDJa4CBzt48WpEX7AxNyaqx

key spend tb1pkkrwg6e9s5zf3jmftu224rc5ppax26g5yzdg03rhmqw84359xgpsv5mn2y

script spend tb1pnm743sjkw9tq3zf9uyetgqkrx7tauthmxnsl5dtyrwnyz9r7lu8qdtcnnc

Transactions

p2tr, script -> key:

(3->2 spend) Blockstream Block Explorer

p2tr, key -> p2pkh:

(2->2 spend) Blockstream Block Explorer

Blockstream Block Explorer

(2->2 spend) Blockstream Block Explorer

p2pkh -> pt2r script:

(2->2 spend) Blockstream Block Explorer

Errors

There was exactly one “exceeded instruction limit“ error in p2tr key path spending, but I’m not sure what this was and why, currently. Otherwise no errors.