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 support for Taproot and BRC20 #3233

Merged
merged 289 commits into from
Jun 21, 2023
Merged

Add support for Taproot and BRC20 #3233

merged 289 commits into from
Jun 21, 2023

Conversation

lamafab
Copy link
Contributor

@lamafab lamafab commented Jun 16, 2023

LATEST UPDATE: See #3233 (comment)


Opening this and closing #3213. We're almost there!

  • Add support for P2WPKH (Segwit) send and receive functionality
    • Tested in regtest and mainnet environment.
    • Add unit test for replicated transfer.
  • Add support for P2TR key-path (Taproot), for simple balance transfers, send and receive functionality
    • Tested in regtest and mainnet environment
    • Add unit test for replicated transfer.
  • Add support for P2TR script-path for complex spending conditions.
  • Add Ordinals Inscription structure (which uses P2TR script-path)
    • Add BRC20 deploy mechanism.
      • Tested on mainnet
    • Add BRC20 mint mechanism.
      • Tested on mainnet
    • Add BRC20 transfer mechanism.
      • Tested on mainnet
  • Add FFI wrappers for C++
    • tw_legacy_address_string
    • tw_segwit_address_string
    • tw_taproot_address_string
    • tw_build_p2pkh_script
    • tw_build_p2wpkh_script
    • tw_build_p2tr_key_path_script
    • tw_build_brc20_inscribe_transfer
    • tw_taproot_build_and_sign_transaction
  • Test FFI wrappers

Also, I'm really not a fan of how the TransactionPlan in the Protobuf structure works and behaves (it combines two types of functionalities in one), but for now we just keep and extend it. @Milerius probably can give me some advice on better C++ design regarding the FFIs and how the BRC20 transfer information should be extended in the Protobuf structure. Additionally, I want to take a couple of hours for cleanup and code quality improvements, especially those mentioned by @satoshiotomakan

Besides that, for the workflow of BRC20 transfers, especially on the iOS/Android side, I created this chart and added a bunch of comments, including open question when selecting inputs/outputs:

image

This looks complicated, but the first two steps (Deploy and Mint) can be ignored, we can maybe add those to the front-end later if we want to. For the transfer itself, it takes three (!!) Bitcoin on-chain transfers to make one BRC20 transfer:

  • Commit transaction on-chain.
  • Reveal transaction on-chain (in the witness script).
    • Making "available" tokens "transferable".
  • Make the actual transfer by sending the marked satoshis via a normal Bitcoin transaction such as P2WPKH (Segwit), according to Ordinal Theory.

I do wonder whether step two can be skipped and hence combining step one and three. I just followed the behavior of the Unistat wallet. Might be worth trying out. (the answer is "no")

@lamafab
Copy link
Contributor Author

lamafab commented Jun 19, 2023

Additionally, I want to take a couple of hours for cleanup and code quality improvements

I think we're good to go.

Copy link
Collaborator

@satoshiotomakan satoshiotomakan left a comment

Choose a reason for hiding this comment

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

A few suggestions before merge 🙂

src/Bitcoin/Signer.cpp Show resolved Hide resolved
src/Bitcoin/TransactionOutput.cpp Outdated Show resolved Hide resolved
rust/tw_bitcoin/src/ffi/address.rs Outdated Show resolved Hide resolved
rust/tw_bitcoin/src/ffi/address.rs Outdated Show resolved Hide resolved
rust/tw_bitcoin/src/ffi/address.rs Outdated Show resolved Hide resolved
rust/tw_bitcoin/src/ffi/mod.rs Show resolved Hide resolved
rust/tw_bitcoin/src/ffi/scripts.rs Show resolved Hide resolved
rust/tw_bitcoin/src/output/p2tr_key_path.rs Show resolved Hide resolved
rust/tw_bitcoin/src/transaction.rs Show resolved Hide resolved
rust/wallet_core_rs/cbindgen.toml Outdated Show resolved Hide resolved
Milerius
Milerius previously approved these changes Jun 21, 2023
Milerius
Milerius previously approved these changes Jun 21, 2023
Copy link
Collaborator

@satoshiotomakan satoshiotomakan left a comment

Choose a reason for hiding this comment

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

Awesome 🔥

@Milerius Milerius merged commit a622268 into master Jun 21, 2023
10 checks passed
@Milerius Milerius deleted the brc20-transfers-and-taproot branch June 21, 2023 14:36
@lamafab
Copy link
Contributor Author

lamafab commented Jun 21, 2023

🎉

@lamafab
Copy link
Contributor Author

lamafab commented Jun 21, 2023

For context, how it works:

image

Explanation: The process involves two transactions: a Commit transaction and a Reveal transaction. Both must be created and submitted simultaneously. The Commit transaction requires one or more standard inputs and outputs for the BRC20 transfer inscription and standard change/return. The Reveal transaction requires the BRC20 transfer inscription as input and outputs tagged satoshis.

To create the BRC20 transfer inscription, use the function TWBitcoinScriptBuildBRC20InscribeTransfer in TWBitcoinScript.h. This function handles all aspects of the inscription mechanism, including deriving the appropriate Taproot address.

Once the commit and reveal inscriptions have been processed, the resulting output of tagged satoshis can be transferred to the final recipient. It's important that the tagged satoshis are used as the first input and then spent as the first output. This transaction can be a normal transaction such as P2PKH or P2WPKH.

For more information, see the reference SignBRC20Transfer* implementations in tests/chains/Bitcoin/TWBitcoinSigningTests.cpp

Milerius added a commit that referenced this pull request Jul 17, 2023
* [r2r]: Add ThetaFuel EVM compatible chain

* [r2r]: Bump wc-kotlin version to 3.1.38 (#3218)

* [ERC-4337] Fix user operation hash mistake (#3228)

* [ERC-4337] Fix user operation hash mistake

* Update TestBarz.kt

---------

Co-authored-by: hewigovens <360470+hewigovens@users.noreply.github.com>

* [r2r] Update starknet-ff crate (#3230)

* [Sync]: Apply changes from DeFi wallet-core (#3225)

* [Barz] Format signature API changes (#3241)

* [Barz] Public API refactoring (#3212)

* Getting rid of attestationObject in Barz public API

* remove duplicates

* Update Barz.cpp

* Update Barz.cpp

* Update BarzTests.swift

* update tests

* Update WebAuthnTests.cpp

* Update TestBarz.kt

* Update TestWebAuthn.kt

* update android tests

* Update TestBarz.kt

* Update TestBarz.kt

* Update TestBarz.kt

* fix nullability

* tests update

* Add support for Taproot and BRC20 (#3233)

* [Barz] Add TokenReceiverFacet support (#3240)

* [CFX]: Add support for Conflux eSpace (#3259)

* [License]: update to apache 2.0 (#3256)

* [Barz] Execute batch (#3257)

* [Kotlin] Fixed GH Action (#3263)

* feat(evm/thorchain): use depositWithExpiry for regular transfer (#3266)

* feat(swift): update to 3.2.1 (#3269)

* Add iOS and android tests to check if new methods available (#3267)

* [WASM] Fixed CoinType.deriveAddressFromPublicKeyAndDerivation (#3271)

* [Polkadot]: Refactor Polkadot to support any Substrate chain (#3261)

* [Acala]: Add Acala and AcalaEVM chains (#3274)

* [Kotlin] Added UInt value to enums (#3276)

Co-authored-by: Sztergbaum Roman <rmscastle@gmail.com>

* fix(bitcoin): Fix `TWAnySignerPlan` (#3278)

* [README]: Update README.md (#3280)

Removed broken IFWallet link
Updated supported blockchain count according to registry.md
Corrected some typos and grammatical mistakes

* [Chain/EVM]: add opbnb testnet (#3291)

* [Barz] Upgrade to the new Barz contract constructor (#3288)

* [Polkadot]: Fix `Staking::Bond` and `Staking::BondAndNominate` (#3293)

* [Webauthn] Add some helpers to extract P256 values (#3292)

* [Cardano] Support utxo with legacy(byron) address (#3284)

* [Cardano]: Fix signing in JS (#3299)

---------

Co-authored-by: Ruslan Serebriakov <rsrbk@users.noreply.github.com>
Co-authored-by: hewigovens <360470+hewigovens@users.noreply.github.com>
Co-authored-by: Fabio Lama <42901763+lamafab@users.noreply.github.com>
Co-authored-by: Sztergbaum Roman <rmscastle@gmail.com>
Co-authored-by: Maxim Pestryakov <17238054+MaximPestryakov@users.noreply.github.com>
Co-authored-by: Sabuhi Fatalizada <93775724+fsabuhi@users.noreply.github.com>
Co-authored-by: Sergey Balashov <Balashov.152@gmail.com>
satoshiotomakan added a commit that referenced this pull request Jul 17, 2023
* [r2r]: Add ThetaFuel EVM compatible chain

* [r2r]: Bump wc-kotlin version to 3.1.38 (#3218)

* [ERC-4337] Fix user operation hash mistake (#3228)

* [ERC-4337] Fix user operation hash mistake

* Update TestBarz.kt

---------

Co-authored-by: hewigovens <360470+hewigovens@users.noreply.github.com>

* [r2r] Update starknet-ff crate (#3230)

* [Sync]: Apply changes from DeFi wallet-core (#3225)

* [Barz] Format signature API changes (#3241)

* [Barz] Public API refactoring (#3212)

* Getting rid of attestationObject in Barz public API

* remove duplicates

* Update Barz.cpp

* Update Barz.cpp

* Update BarzTests.swift

* update tests

* Update WebAuthnTests.cpp

* Update TestBarz.kt

* Update TestWebAuthn.kt

* update android tests

* Update TestBarz.kt

* Update TestBarz.kt

* Update TestBarz.kt

* fix nullability

* tests update

* Add support for Taproot and BRC20 (#3233)

* [Barz] Add TokenReceiverFacet support (#3240)

* [CFX]: Add support for Conflux eSpace (#3259)

* [License]: update to apache 2.0 (#3256)

* [Barz] Execute batch (#3257)

* [Kotlin] Fixed GH Action (#3263)

* feat(evm/thorchain): use depositWithExpiry for regular transfer (#3266)

* feat(swift): update to 3.2.1 (#3269)

* Add iOS and android tests to check if new methods available (#3267)

* [WASM] Fixed CoinType.deriveAddressFromPublicKeyAndDerivation (#3271)

* [Polkadot]: Refactor Polkadot to support any Substrate chain (#3261)

* [Acala]: Add Acala and AcalaEVM chains (#3274)

* [Kotlin] Added UInt value to enums (#3276)

Co-authored-by: Sztergbaum Roman <rmscastle@gmail.com>

* fix(bitcoin): Fix `TWAnySignerPlan` (#3278)

* [README]: Update README.md (#3280)

Removed broken IFWallet link
Updated supported blockchain count according to registry.md
Corrected some typos and grammatical mistakes

* [Chain/EVM]: add opbnb testnet (#3291)

* [Barz] Upgrade to the new Barz contract constructor (#3288)

* [Polkadot]: Fix `Staking::Bond` and `Staking::BondAndNominate` (#3293)

* [Webauthn] Add some helpers to extract P256 values (#3292)

* [Cardano] Support utxo with legacy(byron) address (#3284)

* [Cardano]: Fix signing in JS (#3299)

---------

Co-authored-by: Ruslan Serebriakov <rsrbk@users.noreply.github.com>
Co-authored-by: hewigovens <360470+hewigovens@users.noreply.github.com>
Co-authored-by: Fabio Lama <42901763+lamafab@users.noreply.github.com>
Co-authored-by: Sztergbaum Roman <rmscastle@gmail.com>
Co-authored-by: Maxim Pestryakov <17238054+MaximPestryakov@users.noreply.github.com>
Co-authored-by: Sabuhi Fatalizada <93775724+fsabuhi@users.noreply.github.com>
Co-authored-by: Sergey Balashov <Balashov.152@gmail.com>
@trustwallet trustwallet deleted a comment from tuanggolt Sep 5, 2023
satoshiotomakan added a commit that referenced this pull request Oct 6, 2023
* [r2r]: Add ThetaFuel EVM compatible chain

* [r2r]: Bump wc-kotlin version to 3.1.38 (#3218)

* [ERC-4337] Fix user operation hash mistake (#3228)

* [ERC-4337] Fix user operation hash mistake

* Update TestBarz.kt

---------

Co-authored-by: hewigovens <360470+hewigovens@users.noreply.github.com>

* [r2r] Update starknet-ff crate (#3230)

* [Sync]: Apply changes from DeFi wallet-core (#3225)

* [Barz] Format signature API changes (#3241)

* [Barz] Public API refactoring (#3212)

* Getting rid of attestationObject in Barz public API

* remove duplicates

* Update Barz.cpp

* Update Barz.cpp

* Update BarzTests.swift

* update tests

* Update WebAuthnTests.cpp

* Update TestBarz.kt

* Update TestWebAuthn.kt

* update android tests

* Update TestBarz.kt

* Update TestBarz.kt

* Update TestBarz.kt

* fix nullability

* tests update

* Add support for Taproot and BRC20 (#3233)

* [Barz] Add TokenReceiverFacet support (#3240)

* [CFX]: Add support for Conflux eSpace (#3259)

* [License]: update to apache 2.0 (#3256)

* [Barz] Execute batch (#3257)

* [Kotlin] Fixed GH Action (#3263)

* feat(evm/thorchain): use depositWithExpiry for regular transfer (#3266)

* feat(swift): update to 3.2.1 (#3269)

* Add iOS and android tests to check if new methods available (#3267)

* [WASM] Fixed CoinType.deriveAddressFromPublicKeyAndDerivation (#3271)

* [Polkadot]: Refactor Polkadot to support any Substrate chain (#3261)

* [Acala]: Add Acala and AcalaEVM chains (#3274)

* [Kotlin] Added UInt value to enums (#3276)

Co-authored-by: Sztergbaum Roman <rmscastle@gmail.com>

* fix(bitcoin): Fix `TWAnySignerPlan` (#3278)

* [README]: Update README.md (#3280)

Removed broken IFWallet link
Updated supported blockchain count according to registry.md
Corrected some typos and grammatical mistakes

* [Chain/EVM]: add opbnb testnet (#3291)

* [Barz] Upgrade to the new Barz contract constructor (#3288)

* [Polkadot]: Fix `Staking::Bond` and `Staking::BondAndNominate` (#3293)

* [Webauthn] Add some helpers to extract P256 values (#3292)

* [Cardano] Support utxo with legacy(byron) address (#3284)

* [Cardano]: Fix signing in JS (#3299)

---------

Co-authored-by: Ruslan Serebriakov <rsrbk@users.noreply.github.com>
Co-authored-by: hewigovens <360470+hewigovens@users.noreply.github.com>
Co-authored-by: Fabio Lama <42901763+lamafab@users.noreply.github.com>
Co-authored-by: Sztergbaum Roman <rmscastle@gmail.com>
Co-authored-by: Maxim Pestryakov <17238054+MaximPestryakov@users.noreply.github.com>
Co-authored-by: Sabuhi Fatalizada <93775724+fsabuhi@users.noreply.github.com>
Co-authored-by: Sergey Balashov <Balashov.152@gmail.com>
@vccomnet
Copy link

Hi lamafab,
Thank you for implementing BRC20 and BRC NFT, and thank you for your great work. I am learning to use wallet core and want to develop a BRC20 tool software. I have looked at the sample code in the tests section, including: TEST (Bitcoin Signing, SignBRC20TransferCommit), TEST (Bitcoin Signing, SignBRC20TransferReveal), and TEST (Bitcoin Signing, SignBRC20TransferInsertion), but I cannot see what their respective functions are, which is deploy, which is mint, and which is transfer, The example does not have a method to set op. Please help explain, thank you

Regards
James

@lamafab
Copy link
Contributor Author

lamafab commented Nov 22, 2023

(see discussion #3567)

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.

None yet

4 participants