Ethereum: How to know when a payment has been received by bitcoind?

Ethereum: A Guide to Verifying Bitcoin Receipts Programmatically

As you’re building a website that allows users to send Bitcoins to a new single-use address, you’ll need to implement a way to verify when a payment has been received. In this article, we’ll explore how to accomplish this using Ethereum’s Smart Contracts and the Web3.js library.

What is a Bitcoin Receipt?

In Bitcoin, a receipt (or transaction) is a message sent from a miner or an individual user to the blockchain network confirming that they have successfully confirmed their transaction. When a user sends Bitcoins to a new address, the recipient receives this receipt as part of the transaction confirmation process.

Verify Receipts Programmatically

To verify when a payment has been received programmatically, you’ll need to use Ethereum’s smart contracts and Web3.js library. Here’s a high-level overview of the steps:

  • Create a new contract: Create a new Solidity contract on the Ethereum blockchain that will handle Bitcoin transactions. This contract will contain logic for verifying receipts.

  • Define the receipt event: Define an event that is emitted when a payment has been received, such as “PaymentReceived”.

  • Implement the verification logic: In the verifyReceipt function, check if the transaction that was sent to the new address matches the expected signature of a receipt message.

  • Update the blockchain state: If the receipt is valid, update the blockchain state with the new balance and any additional information.

Example Code

Here’s an example of how you might implement this using Solidity, Web3.js, and Ethereum’s ERC-20 token standard:

pragma solidity ^0.8.0;

import "

import "

contract ReceiptVerify {

// Define the receipt event

event PaymentReceived(uint256 recipient, uint256 amount);

// Define a function to verify receipts

function verifyReceipt(

address payable recipient,

uint256 amount,

string memory signature

) public returns (bool) {

// Check if the transaction matches the expected signature

require(bytes(signature).length == 42, "Invalid signature");

// Get the transaction data

TransactionData storage txData = getTransactionData(recipient, amount);

// Check if the transaction is valid and has a valid signature

require(txData.transactionHash != address(0), "Invalid transaction");

require(txData.signature != "", "Signature is empty");

return true;

}

// Define the function to get transaction data

function getTransactionData(

address payable recipient,

uint256 amount

) internal view returns (TransactionData storage) {

Transaction tx = txs[recipient][amount];

return TxData storage txData;

}

}

// Define the receipt message structure

struct ReceiptMessage {

bytes32 signature;

string message;

}

// Define the transaction data structure

structTransactionData {

addressed container;

uint256 amount;

}

// Define the transaction struct

struct TxData {

sender payable address;

uint256 amount;

string signature;

}

Explanation

In this example, we’ve defined a ReceiptVerifier contract with two functions: verifyReceipt and getTransactionData. The verifyReceipt function checks if the transaction matches the expected signature of a receipt message. It uses the Web3.js library to get the transaction data from the blockchain.