package domain import ( "fmt" "git.smesh.lol/tinywallet/pkg/script" ) // DeriveHTLC builds a swap's contract from its terms. // // It is the local equivalent of asking a node for a descriptor's script and address: the two // public keys, the SHA-256 of the preimage and the locktime are the whole contract, so nothing // about it needs a node, a wallet unlock or a network round trip. The chain is named only to // pick the address prefix, which is how the same terms become a bcrt1 or a tb1 address. func (d *Domain) DeriveHTLC( name string, claimer, refunder []byte, secretHash [32]byte, locktime uint32, ) (string, []byte, [32]byte, error) { var none [32]byte link, err := d.link(name) if err != nil { return "", nil, none, err } witness, err := script.HTLC(claimer, refunder, secretHash, locktime) if err != nil { return "", nil, none, fmt.Errorf("domain: %q: %w", name, err) } address, err := script.P2WSHAddress(link.Profile.HRP, witness) if err != nil { return "", nil, none, fmt.Errorf("domain: %q: %w", name, err) } return address, witness, script.P2WSHProgram(witness), nil }