github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/blindtree/values.go (about)

     1  package blindtree
     2  
     3  import (
     4  	"github.com/keybase/client/go/protocol/keybase1"
     5  	"github.com/keybase/client/go/sig3"
     6  	"github.com/keybase/go-codec/codec"
     7  )
     8  
     9  // BlindMerkleValue simulates a union type to store values of different types in
    10  // the Blind Merkle Tree. To add a new type, grab a new BlindMerkleValueType
    11  // constant and update the CodecEncodeSelf and CodecDecodeSelf functions as
    12  // appropriate.
    13  type BlindMerkleValue struct {
    14  	ValueType  BlindMerkleValueType
    15  	InnerValue interface{}
    16  }
    17  
    18  // Note: values up to 127 are preferred as they are encoded in a single byte
    19  type BlindMerkleValueType uint8
    20  
    21  const (
    22  	ValueTypeEmpty  BlindMerkleValueType = 0
    23  	ValueTypeTeamV1 BlindMerkleValueType = 1
    24  
    25  	ValueTypeStringForTesting BlindMerkleValueType = 127
    26  )
    27  
    28  var _ codec.Selfer = &BlindMerkleValue{}
    29  
    30  func (t *BlindMerkleValue) CodecEncodeSelf(e *codec.Encoder) {
    31  	switch t.ValueType {
    32  	case ValueTypeTeamV1, ValueTypeStringForTesting, ValueTypeEmpty:
    33  		// pass
    34  	default:
    35  		panic("Unknown merkle value type")
    36  	}
    37  	e.MustEncode(t.ValueType)
    38  	e.MustEncode(t.InnerValue)
    39  }
    40  
    41  func (t *BlindMerkleValue) CodecDecodeSelf(d *codec.Decoder) {
    42  	d.MustDecode(&t.ValueType)
    43  	switch t.ValueType {
    44  	case ValueTypeEmpty:
    45  		// Nothing to do here
    46  	case ValueTypeTeamV1:
    47  		var v TeamV1Value
    48  		d.MustDecode(&v)
    49  		t.InnerValue = v
    50  	case ValueTypeStringForTesting:
    51  		var s string
    52  		d.MustDecode(&s)
    53  		t.InnerValue = s
    54  	default:
    55  		panic("Unrecognized Value Type")
    56  	}
    57  }
    58  
    59  type TeamV1Value struct {
    60  	_struct struct{} `codec:",toarray"` //nolint
    61  	Tails   map[keybase1.SeqType]sig3.Tail
    62  }
    63  
    64  func BlindMerkleValueStringForTesting(s string) BlindMerkleValue {
    65  	return BlindMerkleValue{ValueType: ValueTypeStringForTesting, InnerValue: s}
    66  }
    67  
    68  func BlindMerkleValueTeamV1(v TeamV1Value) BlindMerkleValue {
    69  	return BlindMerkleValue{ValueType: ValueTypeTeamV1, InnerValue: v}
    70  }
    71  
    72  func BlindMerkleValueEmpty() BlindMerkleValue {
    73  	return BlindMerkleValue{ValueType: ValueTypeEmpty, InnerValue: nil}
    74  }