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

     1  package sig3
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
     7  )
     8  
     9  func (p PerTeamKey) Export(q keybase1.Seqno) keybase1.PerTeamKeyAndCheck {
    10  	return keybase1.PerTeamKeyAndCheck{
    11  		Ptk: keybase1.PerTeamKey{
    12  			Gen:    p.Generation,
    13  			Seqno:  q,
    14  			SigKID: p.SigningKID.ToKID(),
    15  			EncKID: p.EncryptionKID.ToKID(),
    16  		},
    17  		Check: p.SeedCheck,
    18  	}
    19  }
    20  
    21  func (l *LinkID) Export() keybase1.LinkID {
    22  	if l == nil {
    23  		return keybase1.LinkID("")
    24  	}
    25  	return keybase1.LinkID(hex.EncodeToString(l[:]))
    26  }
    27  
    28  func (u UID) Export() keybase1.UID {
    29  	return keybase1.UID(hex.EncodeToString(u[:]))
    30  }
    31  
    32  func (r RotateKey) Export() (ret *keybase1.HiddenTeamChainLink, err error) {
    33  	m := make(map[keybase1.PTKType]keybase1.PerTeamKeyAndCheck)
    34  	readerKey := r.ReaderKey()
    35  	if readerKey == nil {
    36  		return nil, fmt.Errorf("cannot export RotateKey, since no known reeader key")
    37  	}
    38  
    39  	m[keybase1.PTKType_READER] = readerKey.Export(r.Seqno())
    40  	return &keybase1.HiddenTeamChainLink{
    41  		MerkleRoot:  r.Base.inner.MerkleRoot.Export(),
    42  		ParentChain: r.Base.inner.ParentChain.Export(),
    43  		Signer:      r.Base.inner.Signer.Export(),
    44  		Ptk:         m,
    45  	}, nil
    46  }
    47  
    48  func (m MerkleRoot) Export() keybase1.MerkleRootV2 {
    49  	return keybase1.MerkleRootV2{
    50  		Seqno:    m.Seqno,
    51  		HashMeta: m.Hash,
    52  	}
    53  }
    54  
    55  func (t Tail) Export() keybase1.LinkTriple {
    56  	return keybase1.LinkTriple{
    57  		Seqno:   t.Seqno,
    58  		SeqType: t.ChainType,
    59  		LinkID:  t.Hash.Export(),
    60  	}
    61  }
    62  
    63  func (s Signer) Export() keybase1.Signer {
    64  	return keybase1.Signer{
    65  		E: s.EldestSeqno,
    66  		K: s.KID.ToKID(),
    67  		U: s.UID.Export(),
    68  	}
    69  }
    70  
    71  func ExportToPrevLinkTriple(g Generic) keybase1.LinkTriple {
    72  	return keybase1.LinkTriple{
    73  		Seqno:   g.Seqno() - 1,
    74  		SeqType: g.Outer().ChainType,
    75  		LinkID:  g.Prev().Export(),
    76  	}
    77  }
    78  
    79  func ImportLinkID(l keybase1.LinkID) (*LinkID, error) {
    80  	if len(l) != 64 {
    81  		return nil, fmt.Errorf("failed to import linkID; wrong length: %d", len(l))
    82  	}
    83  	tmp, err := hex.DecodeString(string(l))
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	var ret LinkID
    88  	copy(ret[:], tmp)
    89  	return &ret, nil
    90  }
    91  
    92  func ImportTail(l keybase1.LinkTriple) (*Tail, error) {
    93  	hash, err := ImportLinkID(l.LinkID)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return &Tail{
    99  		Seqno:     l.Seqno,
   100  		ChainType: l.SeqType,
   101  		Hash:      *hash,
   102  	}, nil
   103  }
   104  
   105  func ImportUID(u keybase1.UID) (ret UID) {
   106  	tmp := u.ToBytes()
   107  	copy(ret[:], tmp)
   108  	return ret
   109  }
   110  
   111  func ImportKID(k keybase1.KID) (ret KID) {
   112  	return k.ToBinaryKID()
   113  }
   114  
   115  func ImportTeamID(t keybase1.TeamID) (*TeamID, error) {
   116  	tmp, err := hex.DecodeString(string(t))
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	var ret TeamID
   121  	copy(ret[:], tmp)
   122  	return &ret, nil
   123  }