Engineering P2WSH Hashlocks and Timelocks in Go
Recorded evidence and engineering implications
The Go Script function requires a block-height expiry below 500000000. Its claim branch checks a 32-byte preimage, SHA-256 commitment and claim signature; the refund branch checks CLTV and a refund signature. The P2WSH address commits to the script hash, not directly to the secret hash. The fixture uses synthetic transactions and deliberately public keys; never fund those outputs. A passing script fixture does not establish transaction finality or network acceptance. The mock token credited 100 units once and rejected replay. A future SDK should separate script construction, signing, broadcast and confirmation.
Evidence
1package htlc
2
3import (
4 "crypto/sha256"
5 "fmt"
6
7 "github.com/btcsuite/btcd/btcutil"
8 "github.com/btcsuite/btcd/chaincfg"
9 "github.com/btcsuite/btcd/txscript"
10)
11
12// Script uses a block-height refund lock and compressed public keys.
13func Script(hash [32]byte, claimKey, refundKey []byte, expiry int64) ([]byte, error) {
14 if expiry <= 0 || expiry >= 500000000 {
15 return nil, fmt.Errorf("expiry must be a block height")
16 }
17 return txscript.NewScriptBuilder().
18 AddOp(txscript.OP_IF).
19 AddOp(txscript.OP_SIZE).AddInt64(32).AddOp(txscript.OP_EQUALVERIFY).
20 AddOp(txscript.OP_SHA256).AddData(hash[:]).AddOp(txscript.OP_EQUALVERIFY).
21 AddData(claimKey).
22 AddOp(txscript.OP_ELSE).
23 AddInt64(expiry).AddOp(txscript.OP_CHECKLOCKTIMEVERIFY).AddOp(txscript.OP_DROP).
24 AddData(refundKey).
25 AddOp(txscript.OP_ENDIF).AddOp(txscript.OP_CHECKSIG).Script()
26}
27
28func Testnet3Address(script []byte) (*btcutil.AddressWitnessScriptHash, error) {
29 hash := sha256.Sum256(script)
30 return btcutil.NewAddressWitnessScriptHash(hash[:], &chaincfg.TestNet3Params)
31}Scope
This analysis uses archived laboratory observations; no transactions were independently broadcast or verified against live nodes during editorial review.
Primary reference: https://thamer.org/api
