1 package domain
2 3 import (
4 "fmt"
5 6 "git.smesh.lol/tinywallet/pkg/script"
7 )
8 9 // DeriveHTLC builds a swap's contract from its terms.
10 //
11 // It is the local equivalent of asking a node for a descriptor's script and address: the two
12 // public keys, the SHA-256 of the preimage and the locktime are the whole contract, so nothing
13 // about it needs a node, a wallet unlock or a network round trip. The chain is named only to
14 // pick the address prefix, which is how the same terms become a bcrt1 or a tb1 address.
15 func (d *Domain) DeriveHTLC(
16 name string, claimer, refunder []byte, secretHash [32]byte, locktime uint32,
17 ) (string, []byte, [32]byte, error) {
18 var none [32]byte
19 20 link, err := d.link(name)
21 22 if err != nil {
23 return "", nil, none, err
24 }
25 26 witness, err := script.HTLC(claimer, refunder, secretHash, locktime)
27 28 if err != nil {
29 return "", nil, none, fmt.Errorf("domain: %q: %w", name, err)
30 }
31 32 address, err := script.P2WSHAddress(link.Profile.HRP, witness)
33 34 if err != nil {
35 return "", nil, none, fmt.Errorf("domain: %q: %w", name, err)
36 }
37 38 return address, witness, script.P2WSHProgram(witness), nil
39 }
40