How to build an automated will on the blockchain

Adrian Booth
9 min readNov 12, 2022

I recently got into crypto. Yeah I know I’m late, but when some of the stupidest people you know keep pushing you to buy their worthless shitcoins for the past 2 years in order to partake in their Multi Level Marketing scams then it’s a bit difficult to take the whole thing seriously.

Hearing braindead crypto bros boast about how they’ll be able to afford a lambo once Bitcoin reaches $200,000 was enough to make me want to chop my ears off. The stupidity of the scene, as well as the ostentatious offensiveness of the people within it, was enough to put me off it for some time.

So much of the crypto scene is a fraud and a ponzi, as evidenced by the Enron sized bankruptcy of the second largest crypto exchange this week, FTX. Ran by “billionaire next door” Sam Bankman-Fried (Sam Bankrun-Fraud), the exchange essentially took peoples money and pissed it all away on reckless gambles that went sour. Books and business school lessons will be written about this for years, given the sheer scale of the fraud involved and the amount of money vaporised. Bankrun-fraud’s personal net worth crashed from around $16bn to a few hundred million in a single day. But apparently people trusted him because he drove a Corolla, wore shorts and had a big friendly ‘fro of hair.

Recently, with the bear market in crypto pushing out the fraudsters, the fleecers and the charlatans I decided to take a closer look at this industry I’d been intentionally avoiding, as well as the technology behind it. With all the snake oil salesmen and crypto cultists of the past year going quiet recently, it was time to approach this from a less defensive and skeptical position than I did before.

I started learning Solidity, which is essentially a programming language for the Ethereum blockchain. This language allows you to program applications that directly interact with the blockchain, much like Ruby allows you to build applications that allow you to interact with SQL databases. Solidity is a programming language native to the Ethereum ecosystem that allows applications to be built on the blockchain and allow for the idea of “programmable money”.

I got the idea for the program by scouring the internet for example applications that others had built and came across this as an idea. All of the code is mine as I didn’t read anyone else’s; my intention was to learn more about Solidity by building something out so it wouldn’t have helped me a lot if I just copied other peoples code.

The idea for the application is simple: I would like to pass my digital assets onto a beneficiary in the event of my death and I want this to be automated. In this case, I have some Ethereum and would like to bestow these assets to a beneficiary of my choosing in the event of my passing.

Some questions to be dealt with before this is built:

  • How does the blockchain know I’m dead?
  • If the beneficiary can claim the funds, what’s stopping them from claiming the funds anytime? How would we write code that stops them from being malicious?
  • What’s to stop other malicious actors from stealing these assets? They are stored on the internet, after all. How would they be protected?

The code for all of this can be found on my Github here. In summary there are 3 parts to this:

  • A smart contract
  • A frontend to interact with the smart contract via a browser
  • An event listener

Let’s start with the smart contract.

Smart contracts are just pieces of code written in Solidity and stored and run on the Ethereum blockchain. They allow for money to be transferred in an automated way according to rules and logic embedded in the software. In the traditional industry of estate planning, you would typically have funds placed in an escrow account which can then be dispersed to your beneficiaries in the event of your death. The beneficiaries would have to contact the staff at the estate planning firm and provide proof of death to start the process of asset transferal.

What excites me about blockchain development is how the concept of programmable money allows for a completely new type of application, as well as the potential to introduce radical efficiencies across multiple industries. I’m far from a blockchain evangelist at this point, and I still have my doubts, but I see potential in this stuff despite all the slimy conmen that have given the industry a bad name. Smart contracts have often been likened to vending machines; they do away with middlemen and allow customers to directly purchase an item without having to go through another human who will likely take a cut of the transaction.

Let’s start with the first question I posed above; how does the blockchain know I’m dead?

Well, it doesn’t. The blockchain is a deterministic system that doesn’t really have knowledge of the outside world, let alone the heart pulses of a living human being. But this is where the magic of Solidity comes into play and how events can help us to determine whether someone is dead or alive with a high degree of certainty.

The code for the smart contract is so simple that it can fit into a single screenshot (see below).

If you’re not a programmer then don’t worry, i’ll explain this in laymen's terms.

Firstly, a constructor function on line 18 gets called when the contract is deployed to the blockchain. It initiates the contract with two addresses; the address of the estate owner, and the address of the beneficiary. These are digital addresses online where the users hold their funds, and storing these in the contract directly ensures that no other actors (hackers) can interact with this contract.

The addFunds() function then allows the estate owner to add funds to the smart contract. In this way the smart contract acts as an intelligent escrow account that requires no human intervention.

The claimInheritance() function is where things get interesting. You’ll notice that there’s a payable reference in the function declaration which essentially means “this function can move money between accounts”. This is the function the beneficiary will have to call when the estate owner dies and they want to initiate the transfer of funds.

You’ll see a simple conditional statement that says that “if enough time has passed since we last checked the pulse of the estate owner and if the estate owner is not alive, then pay out the funds to the beneficiary

An important point here; we’re working with the passing of time to determine whether the estate owner is dead. This is where the concept of blockchain / Solidity events come into play, and without them we wouldn’t be able to build this application.

Events are a way for the blockchain to communicate with the outside world that something has happened. So in our case we want to communicate that the estate owner might be dead; again, the blockchain can never know for sure whether someone is dead. It can only assume with a high degree of certainty given a lack of interaction or response from the user over a long period of time. We do this by allowing the beneficiary to call the claimInheritance() function which then sets off a CheckPulse event. We’ll talk more about this later on, where we’ll be able to see that the estate owner can get notified of this event and then perform an action (sending a HeartBeat event) which communicates to the blockchain that they are very much alive.

If the estate owner has not sent a heart beat past a certain threshold, then we can assume that they are in fact dead. I’ve used 30 seconds for the purposes of developing this so I can test things out quicker, but in general you’d want a more sensible threshold like 30–45 days.

The frontend code for this is very minimal, and just includes a few buttons and a form to call those functions I described above. I’ve used the ethers.js library in Javascript to make the blockchain function calls from the browser. I won’t go into too much detail here as it’s not the most important, but this is the code and the frontend screen is below which can be called via the browser. You’d need some kind of crypto wallet in your browser for this to work. I’ve used Metamask and i’m using fake ether on a testing network for this. Metamask is essentially a browser extension which tells you how much crypto currency you own, and it’ll fetch this information from your public address on the blockchain. Once you have a Metamask account connected, you can begin interacting with the estate planning UI to start making smart contract calls. Those button clicks will call those functions in our smart contract, like the claimInheritance() function.

The next interesting part is the event listener. We need some type of application that can listen out for events that get emitted from the blockchain and then perform actions accordingly. In our case, we want to listen out for pulse checks (when the beneficiary tries to claim the inheritance it sends out a pulse check to confirm if the estate owner is dead or alive).

For this we need to set up a node that opens up a web socket to the blockchain. A web socket will allow us to keep a continuous connection open to listen out to events and as a result allow any events emitted from the blockchain to reach our code as soon as they happen.

We then set up a listener on line 34 using the web3.js library and using the onData() callback, we can check for any CheckPulse event emitted (line 45).

This is useful because now we can say “When a check pulse event is emitted from the smart contract / blockchain, send an email to the estate owner to check if they’re alive and request they send a heartbeat”. You can see me doing this on line 47 using the nodemailer package.

The estate owner will then receive an email / notification reminding them to send a heartbeat (so calling that heatBeat() function in our Solidity code), which then sets the checkPulseTime back to 0 and estateOwnerAlive boolean to true.

This means that when the beneficiary goes to claim their inheritance, they will not be allowed and the payable call which transfers the balance of the contract will not be run

We’ve seen here how easy it is to automate this stuff and how the blockchain can be used to programatically transfer digital assets from different online crypto wallets in the event of the owners passing.

This is a very simple contract and i’m sure there’s tons of security bugs in this that I haven’t considered (I’m still a noob). I just wanted to write a very simple MVP to show how a blockchain developer would go about building something like this, and the kinds of technological choices involved.

Happy hacking !

--

--