package psbt import ( "crypto/sha256" "errors" "fmt" ) // ErrNotContract is a witness script that is not a swap contract of this layout. var ErrNotContract = errors.New("psbt: the witness script is not a swap contract") // HTLC is a contract's terms, as its witness script states them. type HTLC struct { // Claimer and Refunder are the two compressed public keys, in script order. Claimer []byte Refunder []byte // SecretHash is the SHA-256 the claim branch compares the preimage against. SecretHash [32]byte // Locktime is the refund branch's absolute locktime. Locktime uint32 } // ParseHTLC reads the terms out of a witness script this wallet compiles. // // It checks the whole shape rather than searching for public keys: a script that merely // contains two of them is not this contract, and signing something else is how a wallet loses // a coin. The layout is the one pkg/script pins to the node's own compilation. func ParseHTLC(witness []byte) (HTLC, error) { var terms HTLC const pushKey = 0x21 // a 33-byte push head := []byte{0x63, pushKey} // OP_IF middle := []byte{0xad, 0x82, 0x01, 0x20, 0x88, 0xa8, 0x20} // CHECKSIGVERIFY SIZE 1 32 EQUALVERIFY SHA256 mark := []byte{0x87, 0x67} // EQUAL ELSE after := []byte{0xb1, 0x69, pushKey} // CLTV VERIFY tail := []byte{0xac, 0x68} // CHECKSIG ENDIF if len(witness) < len(head)+33+len(middle)+32+len(mark)+1+len(after)+33+len(tail) { return terms, ErrNotContract } at := 0 next := func(want []byte) bool { if at+len(want) > len(witness) || string(witness[at:at+len(want)]) != string(want) { return false } at += len(want) return true } if !next(head) { return terms, ErrNotContract } terms.Claimer = append([]byte(nil), witness[at:at+33]...) at += 33 if !next(middle) { return terms, ErrNotContract } copy(terms.SecretHash[:], witness[at:at+32]) at += 32 if !next(mark) { return terms, ErrNotContract } // A script number is signed, so a locktime whose top byte has its high bit set carries a // zero pad: up to five bytes for a 32-bit value. if at >= len(witness) || witness[at] < 1 || witness[at] > 5 { return terms, ErrNotContract } length := int(witness[at]) at++ if at+length > len(witness) { return terms, ErrNotContract } raw := witness[at : at+length] if length == 5 { if raw[4] != 0x00 || raw[3]&0x80 == 0 { return terms, ErrNotContract } raw = raw[:4] } var lock [4]byte copy(lock[:], raw) terms.Locktime = binaryLittleUint32(lock[:]) at += length if !next(after) { return terms, ErrNotContract } terms.Refunder = append([]byte(nil), witness[at:at+33]...) at += 33 if at+len(tail) != len(witness) || !next(tail) { return terms, ErrNotContract } return terms, nil } // binaryLittleUint32 reads a little-endian 32-bit number from four bytes. func binaryLittleUint32(raw []byte) uint32 { return uint32(raw[0]) | uint32(raw[1])<<8 | uint32(raw[2])<<16 | uint32(raw[3])<<24 } // inputFinalWitness is BIP174's final scriptWitness record. const inputFinalWitness byte = 0x08 // p2wsh is the output script a witness script pays to. func p2wsh(witness []byte) []byte { sum := sha256.Sum256(witness) out := make([]byte, 0, 34) out = append(out, 0x00, 0x20) return append(out, sum[:]...) } // opensContract returns the preimage that opens secretHash, or nil. func opensContract(preimages []Preimage, secretHash [32]byte) []byte { for _, preimage := range preimages { if preimage.SecretHash == secretHash && bytesEqual(sha256Of(preimage.Preimage), secretHash[:]) { return preimage.Preimage } } return nil } // witnessRecord serialises a witness stack the way BIP174 carries a final scriptWitness: a // count, then each item length-prefixed. func witnessRecord(stack [][]byte) ([]byte, error) { if len(stack) == 0 { return nil, fmt.Errorf("psbt: an empty witness") } out := compactSize(len(stack)) for _, item := range stack { if len(item) > 0xffff { return nil, fmt.Errorf("psbt: a %d-byte witness item", len(item)) } out = append(out, compactSize(len(item))...) out = append(out, item...) } return out, nil } // bytesEqual compares two byte strings. func bytesEqual(a, b []byte) bool { if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true }