BC9: Apps that talk to the chain

Lesson 2 of 5 · reading 15–25 minutes; practice and project time is on the module page

Keys and signers in apps

🎯You'll be able to

Keep signing keys out of code and repositories, and choose who signs: the user's wallet or a server.

🏠The everyday picture

A shop's safe key isn't taped to the till. It's kept by the manager, and only the manager opens the safe.

📘What it really is

📝A worked example

Input: the starter's app/lib.js has a key in a constant.

Steps:

  1. Remove the constant. loadWallet(env, provider) reads env.SAKHI_PRIVATE_KEY.
  2. If it's missing, throw "No key found. Set SAKHI_PRIVATE_KEY in your .env file", without printing any key.
  3. Add .env to .gitignore, and keep .env.example with an empty value.

Expected output: no key in the project, and a helpful message when it's not set.

🌍Where it's used

📝Case scenario

A developer pushed a script with a private key to a public repository "for a minute". Automated bots that scan new commits took the account's funds within seconds. Removing the commit didn't help, as the key had already been copied. A key read from .env wouldn't have been committed.

🖱️Try it

Search the starter's app/ folder for anything that looks like a key, with your editor's search for a 64-character hex pattern. Success: you find DEMO_KEY and can say why it must go.

⚠️Common mistakes

🛠️For your project

Project 3's test 2 checks the key comes from the environment and that no file in app/, docs/ or scripts/ holds one.

✅Quick check

Who should sign transactions on a website?

Show the answer

The user's own wallet, so the site doesn't see their key.

What should you assume about a key that was ever committed to a repository?

Show the answer

That it's exposed, and replace it.

← PreviousConnecting to the right network
ProLearners Assist