bitcoin-sv / spv-wallet

Open-source non-custodial hosted wallet for the BSV Blockchain (UTXOs, xPubs, Paymail & More!)
https://docs.bsvblockchain.org/network-topology/applications/spv-wallet
MIT License
46 stars 14 forks source link

Validate BEEFs Using Block-Header-Service #629

Closed nikhil478 closed 2 months ago

nikhil478 commented 3 months ago

Proposed Solution Integrate the block-header-service to validate BEEFs when transactions are received from Paymail. This can be implemented by referencing the go-paymail examples.

Steps to Implement

Add integration code to validate BEEFs using the block-header-service. Update relevant documentation to reflect the new validation process. (Optional) Provide example code snippets or tests to demonstrate the validation process.

Additional Context If there is a specific reason why the current implementation does not validate BEEFs using the block-header-service, please provide details. I am in the process of creating a customized version of this project and want to ensure proper BEEF validation.

Questions

Am I missing where the current implementation is verifying BEEFs? Are there any challenges or reasons for not using the block-header-service for BEEF validation?

References : https://github.com/bitcoin-sv/go-paymail/blob/main/server/p2p_receive_transaction.go

nikhil478 commented 3 months ago

@rohenaz @Klar @proapi @mrz1836 i would really appreciate any help ! thanks

dorzepowski commented 3 months ago

To use go-paymail one need to implement PaymailServiceProvider interface https://github.com/bitcoin-sv/go-paymail/blob/main/server/interface.go#L53

We're leaving to the adopter to choose any implementation and any service that could provide this functionality. We have already implementation of this interface in spv-wallet which uses Block Headers Service (BHS) for SPV. Although the implementation has several layers, at the end it is calling BHS in this service: https://github.com/bitcoin-sv/spv-wallet/blob/main/engine/chainstate/merkle_root_provider.go#L59

Is it sufficient for you?

nikhil478 commented 3 months ago

// VerifyMerkleRoots will verify the merkle roots by checking them in external header service - Block Headers Service func (p PaymailDefaultServiceProvider) VerifyMerkleRoots( ctx context.Context, merkleRoots []spv.MerkleRootConfirmationRequestItem, ) (err error) { if metrics, enabled := p.client.Metrics(); enabled { end := metrics.TrackVerifyMerkleRoots() defer func() { success := err == nil end(success) }() }

request := make([]chainstate.MerkleRootConfirmationRequestItem, 0, len(merkleRoots))
for _, m := range merkleRoots {
    request = append(request, chainstate.MerkleRootConfirmationRequestItem{
        MerkleRoot:  m.MerkleRoot,
        BlockHeight: m.BlockHeight,
    })
}
**err = p.client.Chainstate().VerifyMerkleRoots(ctx, request)**
return

} here it is gotcha thanks @dorzepowski

nikhil478 commented 3 months ago

but i didnt find that it was getting used while receiving beef payment through paymail it should be called there what u think @dorzepowski

dorzepowski commented 3 months ago

@nikhil478 The function spv.ExectueSimplifiedPaymentVerification is called upon receiving BEEF through BEEF paymail capability. And inside it there is a call to verifyMerkleRoots which if you follow the implementation of it, you will find that it will call the provider.VerifyMerkleRoots which is a call to an interface implementation which need to be provided by adopter of the go-paymail. We're leaving the choice how to implement this integration and more specifically what service and how it should be used to verify that merkleroot was minded at given block height. In case of spv-wallet, it is implementing this interface and then the implementation is calling https://github.com/bitcoin-sv/spv-wallet/blob/main/engine/chainstate/merkle_root_provider.go#L59 (which I show you earlier).

Hope this answered your question and doubts.

nikhil478 commented 2 months ago

gotcha thanks :)