github.com/decred/dcrlnd@v0.7.6/labels/labels.go (about) 1 // Package labels contains labels used to label transactions broadcast by lnd. 2 // These labels are used across packages, so they are declared in a separate 3 // package to avoid dependency issues. 4 // 5 // Labels for transactions broadcast by lnd have two set fields followed by an 6 // optional set labelled data values, all separated by colons. 7 // - Label version: an integer that indicates the version lnd used 8 // - Label type: the type of transaction we are labelling 9 // - {field name}-{value}: a named field followed by its value, these items are 10 // optional, and there may be more than field present. 11 // 12 // For version 0 we have the following optional data fields defined: 13 // - shortchanid: the short channel ID that a transaction is associated with, 14 // with its value set to the uint64 short channel id. 15 package labels 16 17 import ( 18 "fmt" 19 20 "github.com/decred/dcrlnd/lnwire" 21 ) 22 23 // External labels a transaction as user initiated via the api. This 24 // label is only used when a custom user provided label is not given. 25 const External = "external" 26 27 // TxLabelLimit is the length limit we impose on transaction labels. 28 const TxLabelLimit = 500 29 30 // ValidateAPI returns the generic api label if the label provided is empty. 31 // This allows us to label all transactions published by the api, even if 32 // no label is provided. If a label is provided, it is validated against 33 // the known restrictions. 34 func ValidateAPI(label string) (string, error) { 35 if len(label) > TxLabelLimit { 36 return "", fmt.Errorf("label length: %v exceeds "+ 37 "limit of %v", len(label), TxLabelLimit) 38 } 39 40 // If no label was provided by the user, add the generic user 41 // send label. 42 if len(label) == 0 { 43 return External, nil 44 } 45 46 return label, nil 47 } 48 49 // LabelVersion versions our labels so they can be easily update to contain 50 // new data while still easily string matched. 51 type LabelVersion uint8 52 53 // LabelVersionZero is the label version for labels that contain label type and 54 // channel ID (where available). 55 const LabelVersionZero LabelVersion = iota 56 57 // LabelType indicates the type of label we are creating. It is a string rather 58 // than an int for easy string matching and human-readability. 59 type LabelType string 60 61 const ( 62 // LabelTypeChannelOpen is used to label channel opens. 63 LabelTypeChannelOpen LabelType = "openchannel" 64 65 // LabelTypeChannelClose is used to label channel closes. 66 LabelTypeChannelClose LabelType = "closechannel" 67 68 // LabelTypeJusticeTransaction is used to label justice transactions. 69 LabelTypeJusticeTransaction LabelType = "justicetx" 70 71 // LabelTypeSweepTransaction is used to label sweeps. 72 LabelTypeSweepTransaction LabelType = "sweep" 73 ) 74 75 // LabelField is used to tag a value within a label. 76 type LabelField string 77 78 const ( 79 // ShortChanID is used to tag short channel id values in our labels. 80 ShortChanID LabelField = "shortchanid" 81 ) 82 83 // MakeLabel creates a label with the provided type and short channel id. If 84 // our short channel ID is not known, we simply return version:label_type. If 85 // we do have a short channel ID set, the label will also contain its value: 86 // shortchanid-{int64 chan ID}. 87 func MakeLabel(labelType LabelType, channelID *lnwire.ShortChannelID) string { 88 if channelID == nil { 89 return fmt.Sprintf("%v:%v", LabelVersionZero, labelType) 90 } 91 92 return fmt.Sprintf("%v:%v:%v-%v", LabelVersionZero, labelType, 93 ShortChanID, channelID.ToUint64()) 94 }