How LocalCryptos' Non-Custodial Bitcoin Escrow Works - LocalCryptos Blog
How LocalCryptos' Non-Custodial Bitcoin Escrow Works - LocalCryptos Blog
ह द , हंद 中文
LocalCryptos has launched — a new peer-to-peer crypto marketplace.
This article assumes you understand Bitcoin to a technical degree. We’ll try to
break down simple concepts, but if you don’t know what the term “UTXO” means,
it might be dif cult to follow.
This setup is non-custodial from start to nish. LocalCryptos’ servers only store
encrypted blobs which it can’t differentiate from gibberish. Our servers don’t
generate any transactions described here—it’s all your browser’s JavaScript.
Now we’ve built the same thing for Bitcoin. Except that Bitcoin doesn’t have smart
contracts—it has Scripts. Scripts are like smart contracts in some ways, but very
different in others.
Bitcoin “addresses” are a strange concept to unpack. In a way, they’re not a part of
the protocol—they’re not mentioned in Satoshi Nakamoto’s white paper.
“
1MoNS93aYgeuCcojcYbAJpKxqDN9UuJa9C
There are different types of Bitcoin addresses. A Bitcoin address, when decoded,
contains two parts: a version identi er, and a payload. The version eld is at the
beginning, and it tells your Bitcoin wallet the type of address it’s dealing with. The
usage of the payload depends on the version.
Address standards describe how to create puzzles, and how to create solutions to
unlock them. The puzzle solution allows the recipient to use an output as an input
to another transaction.
In a P2PKH address, the payload following the version is a hash of the recipient’s
public key. The hash function used is “hash160”, which translates to
RIPEMD160(SHA256(thing)) .
When Alice wants to send a payment to Bob, she creates a puzzle named a
ScriptPubKey that only Bob can solve. The puzzle becomes an output of her
Bitcoin transaction.
For Bob to spend this output, he needs to use it as an input in another Bitcoin
transaction. To do that, he needs to craft a valid ScriptSig corresponding to the
ScriptPubKey.
<Signature> <PubKey>
OP_Huh?
“
An output’s ScriptPubKey is a piece of code. It’s in a restricted stack-based
programming language unique to Bitcoin, named “Script”.
A ScriptSig is also a piece of code, but by Bitcoin’s consensus rules it can only
contain “push data”. In simple words, it can’t do anything except to add arbitrary
data to the stack.
In this P2PKH example, Bob’s ScriptSig is adding two things to the stack:
PubKey— Bob’s own public key. Alice was only given a hash of Bob’s public
key, not the real deal.
Before a miner can include this transaction in a block, they will con rm that
ScriptSig is correct for the ScriptPubKey.
The miner adds ScriptPubKey to the end of ScriptSig, and executes the code. After
the last line of code is ran, the stack must end with the top item being true (non-
zero). This is all it takes to verify a ScriptSig.
Remember when I said a moment ago that the spender’s ScriptSig can’t include
non-data code? Forget that for a moment—P2SH changes everything.
Instead of writing spending conditions inside ScriptPubKey, that code goes inside
a RedeemScript. Yes, that’s a new term—in loose non-technical words, it refers to
the “real” puzzle for the output. When you send coins to a P2SH address, the
ScriptPubKey contains a hash of the spending coniditons code. It looks like:
Miners will rst verify that the hash of RedeemScript matches the hash in
ScriptPubKey. If it does, it will unpack the RedeemScript, put Signature above it,
and execute.
The difference between a P2WSH and a P2SH is that in a P2WSH, the old
RedeemScript goes inside a new “Witness Script” eld.
If you haven’t learnt about SegWit, please pretend LocalCryptos’ escrow uses
standard P2SH. It will make as much sense. Using Segregated Witness improves
the escrow’s ef ciency and cost, and nothing more.
Our mechanism is especially useful for in-person exchanges. Users are able to
release Bitcoin from escrow without access to the internet. It will be possible to
trigger a release by sending an SMS with a unique code, or by showing a QR to
the buyer. More on that later.
These ephemeral keys come in three categories, two of which are relevant to BTC
escrows.
Encrypted copies of your private keys are also uploaded to allow you to log in from
another device.
The same process occurs for our other web wallets, including Ethereum.
This allows others to open trades with you and fund escrows, even while you’re
of ine. Users fetch one of your addresses from us, and check the signature against
your public key. Doing so helps avoid the risk of a complex man-in-the-middle
attack.
Escrow keys are 32-byte secret codes which you can reveal later. You generate a
hash of the secret code (using hash160) and sign the hash. Then, you upload the
hashed code, signature, and an encrypted secret code to LocalCryptos.
Funding escrow
To put BTC in escrow, the seller creates a Bitcoin transaction containing two
outputs. One output is for the amount being escrowed, and the other is
LocalCryptos’ refundable fee.
Before doing so, the seller needs to fetch some details from LocalCryptos:
1. Hashed escrow key codes from the buyer, arbitrator, and seller (their own).
2. A signature from the buyer they can use to authenticate the hashed escrow
key code.
4. A signature from the buyer to verify the public key belongs to the buyer.
5. The arbitrator’s hashed public key, so they can send a small fee.
The seller will con rm each of the buyer’s signatures are valid before proceeding.
An incorrect signature means that a hacker has attempted to tamper with the
escrow.
Escrow output
The escrow output, which carries the amount for the buyer, is a P2SH-P2WSH
address for the following Script:
OP_DUP OP_1 OP_EQUAL
OP_IF
# Release by seller
OP_DROP
<BuyerPubKeyHash>
<ReleaseCodeFromSellerHash>
OP_ELSE
OP_DUP OP_2 OP_EQUAL
OP_IF
# Release by arbitrator
OP_DROP
<BuyerPubKeyHash>
<ReleaseCodeFromArbitratorHash>
OP_ELSE
OP_DUP OP_3 OP_EQUAL
OP_IF
# Return by buyer
OP_DROP
<SellerPubKeyHash>
<ReturnCodeFromBuyerHash>
OP_ELSE
OP_4 OP_EQUALVERIFY
# Return by arbitrator
<SellerPubKeyHash>
<ReturnCodeFromArbitratorHash>
OP_ENDIF
OP_ENDIF
OP_ENDIF
OP_ROT
OP_HASH160
OP_EQUALVERIFY
OP_OVER
OP_HASH160
OP_EQUALVERIFY
OP_CHECKSIG
Fee output
The fee output carries an amount of Bitcoin approximately 1% of the trade’s size.
LocalCryptos will claim the fee if the trade is successful. If there’s a cancellation,
the seller can unlock the output to claim a full refund.
The fee output is a P2SH-P2WSH address for the folowing Script:
The buyer already has one of the two inputs needed to unlock the escrow
transaction: their own public key. The only missing piece is the seller’s secret
“releaseBySeller” code.
Likewise, the seller is only one input away from unlocking the escrow. If they get
their hands on the buyer’s secret “returnByBuyer” code, they can recall the
amount in escrow.
This is the crux of the non-custodial escrow system. The hashed script includes
the hash of each secret code, but not the real thing.
PubKey — The receiver’s public key, matching the hashed version in the
code.
The “action” byte will tell the script which hashes to check. In a standard trade, a
buyer will use a “release by seller” code ( 0x01 ). If the rst item is 0x01 , the
script will expect the buyer’s public key and the seller’s release code.
There are four escrow actions, representing all the scenarios of an escrow:
With a return escrow code, the above signature is also compatible with fee output.
The seller can spend the fee output in the same way they spend a recalled escrow
UTXO.
The receiving party can choose to spend these coins immediately, or they can
wait. In the user-interface, an escrow UTXO will appear in the web wallet next to
your regular addresses.
After spending the coins, the escrow’s completion is permanently etched into the
blockchain.
We can add a method for a seller to reveal their code without an internet
connection. The code is too long to write on paper, but it’s a perfect length to store
in your phone or a QR code.
2. Show a QR code to the buyer. The buyer doesn’t need the internet to verify
the code; they only need a piece of software that can calculate a hash. This
method will enable in-person exchanges when neither has a stable internet
connection. It will be useful in Venezuela, where national power outages are a
common occurrence.
We’re working on the rst option today. The second option will become available
when we release the LocalCryptos mobile app.
The cost of a Bitcoin escrow depends on the network’s congestion. For small
trades under $10, we recommend choosing another crypto.
USE LOCALCRYPTOS