github.com/decred/dcrlnd@v0.7.6/watchtower/blob/derivation.go (about) 1 package blob 2 3 import ( 4 "encoding/hex" 5 6 "github.com/decred/dcrd/chaincfg/chainhash" 7 ) 8 9 // BreachHintSize is the length of the identifier used to detect remote 10 // commitment broadcasts. 11 const BreachHintSize = 16 12 13 // BreachHint is the first 16-bytes of chainhash(txid), which is used to 14 // identify the breach transaction. 15 type BreachHint [BreachHintSize]byte 16 17 // NewBreachHintFromHash creates a breach hint from a transaction ID. 18 func NewBreachHintFromHash(hash *chainhash.Hash) BreachHint { 19 h := chainhash.HashB(hash[:]) 20 var hint BreachHint 21 copy(hint[:], h) 22 return hint 23 } 24 25 // String returns a hex encoding of the breach hint. 26 func (h BreachHint) String() string { 27 return hex.EncodeToString(h[:]) 28 } 29 30 // BreachKey is computed as SHA256(txid || txid), which produces the key for 31 // decrypting a client's encrypted blobs. 32 type BreachKey [KeySize]byte 33 34 // NewBreachKeyFromHash creates a breach key from a transaction ID. 35 func NewBreachKeyFromHash(hash *chainhash.Hash) BreachKey { 36 var h [64]byte 37 copy(h[:], hash[:]) 38 copy(h[32:], hash[:]) 39 40 var key BreachKey 41 copy(key[:], chainhash.HashB(h[:])) 42 return key 43 } 44 45 // String returns a hex encoding of the breach key. 46 func (k BreachKey) String() string { 47 return hex.EncodeToString(k[:]) 48 } 49 50 // NewBreachHintAndKeyFromHash derives a BreachHint and BreachKey from a given 51 // txid in a single pass. The hint and key are computed as: 52 // 53 // hint = chainhash(txid) 54 // key = chainhash(txid || txid) 55 func NewBreachHintAndKeyFromHash(hash *chainhash.Hash) (BreachHint, BreachKey) { 56 // The chainhash pkg does not currently export a New()/Sum() variant, so 57 // use the default format for calculating it. 58 return NewBreachHintFromHash(hash), NewBreachKeyFromHash(hash) 59 }