bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.62k stars 2.09k forks source link

Escrow with Timeout JS Script Sample #1581

Closed ghuang123 closed 4 years ago

ghuang123 commented 4 years ago

It would be beneficial to have JS code to implement the following logic:

An escrow that times out automatically 30 days after being funded can be established in the following way. Alice, Bob and Escrow create a 2-of-3 address with the following redeemscript.

IF
    2 <Alice's pubkey> <Bob's pubkey> <Escrow's pubkey> 3 CHECKMULTISIG
ELSE
    "30d" CHECKSEQUENCEVERIFY DROP
    <Alice's pubkey> CHECKSIG
ENDIF

At any time funds can be spent using signatures from any two of Alice, Bob or the Escrow.

After 30 days Alice can sign alone.

The clock does not start ticking until the payment to the escrow address confirms.

junderw commented 4 years ago

We have examples in the integration tests

https://github.com/bitcoinjs/bitcoinjs-lib/blob/9e2a8fe45ab98a63ef59cd75a9c499511d50576c/test/integration/csv.spec.ts#L46-L68

junderw commented 4 years ago

Please let me know if you have questions about the tests.