Blockbeat News

Connect a website to your smart contract

Blockchain learning series 10 of 12

By Chris DW, Editor

Published

Run a local interface that hashes files, submits a transaction and reads the receipt.

Gold and blue diagram of a browser and transaction receipt above three panels: Browser, Local signer, Receipt. Full text follows.
Gold and blue mark the Blockchain learning series. Browser: Hashes a selected file without uploading its contents. Local signer: Uses a disposable unlocked practice account. Receipt: Reports confirmed execution separately from submission.
Open the full-size infographic
Read the infographic as text

Connect a website to your smart contract

Run a local interface that hashes files, submits a transaction and reads the receipt.

Browser
Hashes a selected file without uploading its contents.
Local signer
Uses a disposable unlocked practice account.
Receipt
Reports confirmed execution separately from submission.

Learning goal: Run a local interface that hashes files, submits a transaction and reads the receipt.

Blockchain learning series 10 of 12. Educational diagrams; not a production architecture.

A working contract needs an interface that reports what happened accurately. A button marked Register must not announce success merely because it received a transaction hash. It must also avoid uploading a file when the application only needs its fingerprint.

Complete the contract tests first. The supplied website is a local learning application, not a production wallet integration. It uses disposable unlocked accounts provided by the simulation. You need no browser wallet or real funds.

Run the page you can inspect

Download the lab from the setup guide, install its locked dependencies and run npm start. Open http://127.0.0.1:4173. The page displays chain 1337, its deployed contract address, an account selector and a file input.

Choose a small harmless text file. The browser calculates its fingerprint locally. Press Read to confirm that this account has no entry, then Register. The page reports submission while waiting and reports successful execution only after a receipt. Read again to retrieve the saved block number.

Change the practice account and repeat the read. You should see no record for the new account until it registers separately. Restarting the local server clears the simulated chain, so reload the page afterwards to obtain the new instance's configuration.

Follow the file's bytes

The hashing operation reads the selected file into memory and calculates SHA-256. Its output becomes a hexadecimal string acceptable to the contract. The original file is not sent in an RPC call. The supplied page limits files to 10 MB to keep this memory-based exercise manageable.

Code · javascript

const digest = await crypto.subtle.digest(
  'SHA-256', await file.arrayBuffer()
);
const fingerprint = '0x' + [...new Uint8Array(digest)]
  .map(byte => byte.toString(16).padStart(2, '0'))
  .join('');

The browser digest API requires a secure context; loopback development addresses are treated specially by modern browsers. If hashing fails, use the supplied local address and a current browser. Do not solve the error by uploading a private document to an unknown hashing website.

Connect using an ABI and the right address

The ABI describes the contract's functions, arguments, return values and events. The address identifies the deployment, while the network supplies its context. Having one of these without the others is insufficient. Solidity's ABI specification defines the encoding used by the tools.

The lab's server compiles the contract and supplies the address and ABI to the browser. The interface checks chain 1337 and checks that code exists at the configured address. These checks catch common setup errors. They do not establish production authenticity: malicious code could also exist at an address.

The application uses ethers version 6. Its getting-started documentation explains providers, signers and contract instances. In our local example the provider reads the chain, while a selected unlocked practice account signs through the simulator. Never copy that arrangement into a publicly reachable service.

Report the actual interface state

Report the actual interface state
Interface stateUser-facing meaningNext action
No recordSuccessful read returned zeroAllow registration
SubmittedTransaction identifier receivedWait for receipt
Successful executionReceipt confirms executionRead stored result
Connection failureOutcome unknown or unavailableReconnect and query before resubmitting

Handle time and errors explicitly

A useful interface distinguishes no record, existing record, submitting, waiting for a receipt and execution failure. It disables conflicting actions while a submission is in progress. The lab also stops the file or account changing during that action, so that the displayed result refers to the inputs actually submitted.

If a duplicate already exists, the interface reads and displays it. The contract still enforces its duplicate rule because browser checks can be bypassed or become stale. The user interface improves the experience; it does not replace the authoritative rule.

To inspect the requests, open the browser's developer tools and its Network panel. You should see the file fingerprint in calls, not the original file contents. Look at the source in app.js alongside the contract. Trace which line calculates the fingerprint, which sends a state change and which waits for execution.

A funded wallet is a separate next step

A production interface needs an appropriate wallet connection, explicit network handling, signing-request checks, account-change handling and a realistic confirmation policy. It must let users reject a request and recover from a disconnected wallet. This lesson does not claim to supply that complete integration.

Try closing the local server and using Read. The page should report failure rather than inventing an empty record. Restore the server, reload and try again. That small outage exercise is preparation for the infrastructure failures in the next guide.

Sources

Source note: We have made reasonable efforts to provide sources for material claims. Some third-party sources could not be accessed at publication.

Latest original articles