Escrow with Taproot
Library Imports
import * as btc from '@scure/btc-signer'
import { hex } from '@scure/base'
Simple Escrow
Code
const alicePublicKey = hex.decode('1212121212121212121212121212121212121212121212121212121212121212')
const bobPublicKey = hex.decode('2323232323232323232323232323232323232323232323232323232323232323')
const escrowAgentPublicKey = hex.decode('3434343434343434343434343434343434343434343434343434343434343434')
const keySets = [
{ schnorrPub: alicePublicKey },
{ schnorrPub: bobPublicKey },
{ schnorrPub: escrowAgentPublicKey },
]
const leafScriptInfo = btc.p2tr_ns(2, keySets.map(k => k.schnorrPub))
const rootScriptInfo = btc.p2tr(undefined, leafScriptInfo)
const leafScripts = leafScriptInfo.map(s => hex.encode(s.script))
const rootScript = hex.encode(rootScriptInfo.script)
const address = rootScriptInfo.address
Console
> leafScripts.map(s => console.log(s))
201212121212121212121212121212121212121212121212121212121212121212ad202323232323232323232323232323232323232323232323232323232323232323ac
201212121212121212121212121212121212121212121212121212121212121212ad203434343434343434343434343434343434343434343434343434343434343434ac
202323232323232323232323232323232323232323232323232323232323232323ad203434343434343434343434343434343434343434343434343434343434343434ac
> console.log(rootScript)
5120bb20321c60c3f147176cb13848acb1ba3582985f9edf790c521a46c6d2606222
> console.log(address)
bc1phvsry8rqc0c5w9mvkyuy3t93hg6c9xzlnm0hjrzjrfrvd5nqvg3q2p0qn4
Escrow with 2/3 Escrow Committee
Code
const alicePublicKey = hex.decode('1212121212121212121212121212121212121212121212121212121212121212')
const bobPublicKey = hex.decode('2323232323232323232323232323232323232323232323232323232323232323')
const escrowAgent1PublicKey = hex.decode('3434343434343434343434343434343434343434343434343434343434343434')
const escrowAgent2PublicKey = hex.decode('4545454545454545454545454545454545454545454545454545454545454545')
const escrowAgent3PublicKey = hex.decode('5656565656565656565656565656565656565656565656565656565656565656')
const keySets = [
{ schnorrPub: alicePublicKey },
{ schnorrPub: bobPublicKey },
{ schnorrPub: escrowAgent1PublicKey },
{ schnorrPub: escrowAgent2PublicKey },
{ schnorrPub: escrowAgent3PublicKey },
]
const leafScriptInfo = [
# Condition 1: Alice and Bob agree without escrow
btc.p2tr_ns(2, [keySets[0].schnorrPub, keySets[1].schnorrPub])[0],
# Condition 2: Alice wins, escrow committee authorizes unanimously
btc.p2tr_ns(4, [keySets[0].schnorrPub, keySets[2].schnorrPub], keySets[3].schnorrPub], keySets[4].schnorrPub])[0],
# Condition 3: Bob wins, escrow committee authorizes unanimously
btc.p2tr_ns(4, [keySets[1].schnorrPub, keySets[2].schnorrPub], keySets[3].schnorrPub], keySets[4].schnorrPub])[0],
# Conditions 4-6: Alice wins, escrow committe authorizes by 2/3 majority
btc.p2tr_ns(3, [keySets[0].schnorrPub, keySets[2].schnorrPub], keySets[3].schnorrPub])[0],
btc.p2tr_ns(3, [keySets[0].schnorrPub, keySets[2].schnorrPub], keySets[4].schnorrPub])[0],
btc.p2tr_ns(3, [keySets[0].schnorrPub, keySets[3].schnorrPub], keySets[4].schnorrPub])[0],
# Conditions 7-9: Bob wins, escrow committe authorizes by 2/3 majority
btc.p2tr_ns(3, [keySets[1].schnorrPub, keySets[2].schnorrPub], keySets[3].schnorrPub])[0],
btc.p2tr_ns(3, [keySets[1].schnorrPub, keySets[2].schnorrPub], keySets[4].schnorrPub])[0],
btc.p2tr_ns(3, [keySets[1].schnorrPub, keySets[3].schnorrPub], keySets[4].schnorrPub])[0],
]
const rootScriptInfo = btc.p2tr(undefined, leafScriptInfo)
const leafScripts = leafScriptInfo.map(s => hex.encode(s.script))
const rootScript = hex.encode(rootScriptInfo.script)
const address = rootScriptInfo.address
Console
> console.log(rootScript)
5120bb20321c60c3f147176cb13848acb1ba3582985f9edf790c521a46c6d2606222
> console.log(address)
bc1phvsry8rqc0c5w9mvkyuy3t93hg6c9xzlnm0hjrzjrfrvd5nqvg3q2p0qn4
Last updated on March 18, 2023