matrix-capnproto-fed

What if Matrix was written using Cap'n'proto and a RPC federation API?
git clone git://archive.git.mtrnord.blog/MTRNord/matrix-capnproto-fed.git
Log | Files | Refs | README | LICENSE

utils.go (1868B)


      1 package rpcserver
      2 
      3 import (
      4 	"crypto/ed25519"
      5 
      6 	capnp "capnproto.org/go/capnp/v3"
      7 	"github.com/MTRNord/matrix_protobuf_fed/proto/federation/v1/types"
      8 )
      9 
     10 // The go parser currently doesnt support custom annotations so we keep track of it manually
     11 const (
     12 	GetVersion_MethodID       = 0xab1eb3e81f3344d1
     13 	GetKeys_MethodID          = 0x932dd11596dad50e
     14 	SendTransactions_MethodID = 0xec6b6ce167005c84
     15 	Backfill_MethodID         = 0x970e8e8dfe8f0ced
     16 )
     17 
     18 // A KeyID is the ID of a ed25519 key used to sign Protobuf.
     19 // The key IDs have a format of "ed25519:[0-9A-Za-z]+"
     20 // If we switch to using a different signing algorithm then we will change the
     21 // prefix used.
     22 type KeyID string
     23 
     24 // This signs a capnproto byte sequeence in canonical form (has to be canonicalized by the caller) and adds the signature to the signatures list.
     25 // This is the capnproto equivalent to https://spec.matrix.org/v1.9/appendices/#signing-json
     26 // This is not expected to yield the same signature as the json counterpart as the capnproto serialization is different.
     27 func SignCapnproto(signingName string, keyID KeyID, privateKey ed25519.PrivateKey, message []byte, signatures *types.Signature_List) error {
     28 	signature := ed25519.Sign(privateKey, message)
     29 
     30 	// Go doesnt support generic types from go-capnp yet https://github.com/capnproto/go-capnp/issues/27
     31 	list_size := signatures.Len()
     32 	map_entry := signatures.At(list_size - 1)
     33 	map_entry.SetServer(signingName)
     34 	signatures_map, err := map_entry.NewSignatures()
     35 	if err != nil {
     36 		return err
     37 	}
     38 	signatures_map_wrapper := FromMap(&signatures_map, 1)
     39 	key, err := capnp.NewText(signatures_map_wrapper.Segment(), string(keyID))
     40 	if err != nil {
     41 		return err
     42 	}
     43 
     44 	data, err := capnp.NewData(signatures_map_wrapper.Segment(), signature)
     45 	if err != nil {
     46 		return err
     47 	}
     48 	signatures_map_wrapper.AddEntry(key.ToPtr(), data.ToPtr())
     49 
     50 	return nil
     51 }